// Path: flyJenkins/src/main/java/com/agun/flyJenkins/model/ProductionMeta.java
// public class ProductionMeta {
// 	
// 	private String jobName;
// 	private Date createDate;
// 	private String productionPath;
// 	private String productionPathOfJob;	
// 	private int serviceGroup;
// 	private int buildNumber;
// 	
// 	public String getJobName() {
// 		return jobName;
// 	}
// 	
// 	public void setJobName(String jobName) {
// 		this.jobName = jobName;
// 	}
// 	
// 	public int getBuildNumber() {
// 		return buildNumber;
// 	}
// 	
// 	public void setBuildNumber(int buildNumber) {
// 		this.buildNumber = buildNumber;
// 	}
// 	
// 	public Date getCreateDate() {
// 		return createDate;
// 	}
// 	
// 	public void setCreateDate(Date createDate) {
// 		this.createDate = createDate;
// 	}
// 	
// 	public int getServiceGroup() {
// 		return serviceGroup;
// 	}
// 	
// 	public void setServiceGroup(int serviceGroup) {
// 		this.serviceGroup = serviceGroup;
// 	}
// 	
// 	public String getProductionPath() {
// 		return productionPath;
// 	}
// 	
// 	public void setProductionPath(String productionPath) {
// 		this.productionPath = productionPath;
// 	}
// 
// 	public String getProductionPathOfJob() {
// 		return productionPathOfJob;
// 	}
// 
// 	public void setProductionPathOfJob(String productionPathOfJob) {
// 		this.productionPathOfJob = productionPathOfJob;
// 	}
// }
// 
// Path: flyJenkins/src/main/java/com/agun/flyJenkins/persistence/ProductionSaveable.java
// public class ProductionSaveable implements Saveable{
// 
// 	List<ProductionMeta> productionMetaList = null;
// 	
// 	public void save() throws IOException {
// 		if(productionMetaList != null){
// 			ModelSoting.productionSortDate(productionMetaList);
// 		}
// 		if(BulkChange.contains(this))   return;
//         try {
//             getConfigFile().write(this);
//             SaveableListener.fireOnChange(this, getConfigFile());
//         } catch (IOException e) {
//            	e.printStackTrace();
//         }
// 		
// 	}
// 	protected XmlFile getConfigFile() {
// 		
// 	    return new XmlFile(new File(Jenkins.getInstance().getRootDir(), this.getClass().getSimpleName()+".xml"));
//     }
// 
// 	
// 	/**
// 	 * open file and load data
// 	 */
// 	 public synchronized void load() {
// 		 XmlFile file = getConfigFile();
// 		 if(!file.exists())
// 			 return;
// 		 try {
// 			 file.unmarshal(this);
// 		 } catch (IOException e) {
// 			 e.printStackTrace();
// 		 }
// 	 }
// 	 
// 	public List<ProductionMeta> getProductionMetaList() {
// 		return productionMetaList;
// 	}
// 	
// 	public void setProductionMetaList(List<ProductionMeta> productionMetaList) {
// 		this.productionMetaList = productionMetaList;
// 	}
// }
// 
// Path: flyJenkins/src/main/java/com/agun/flyJenkins/util/FlyJenkinsEnv.java
// public class FlyJenkinsEnv {
// 
// 	public static String getProductionRoot(){
// 		return Jenkins.getInstance().getRootDir().getAbsolutePath() + "/flyJenkins/job";
// 	}
// 	
// 	public static String getLastBuildPath(int serviceId){
// 		return Jenkins.getInstance().getRootDir().getAbsolutePath() + "/flyJenkins/lastBuild/"+serviceId+"/service.zip";
// 	}
// }


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jenkins.model.Jenkins;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import com.agun.flyJenkins.model.ProductionMeta;
import com.agun.flyJenkins.persistence.ProductionSaveable;
import com.agun.flyJenkins.util.FlyJenkinsEnv;

package com.agun.flyJenkins.deploy;





/**
 * this class handle production when build 
 * @author agun
 *
 */
public class DeployProduction {

	public final static String SUCCESS = "SUCCESS";
	public final static String FAIL = "FAILURE";

	/**
	 * check state of building
	 * @param result
	 * @return
	 */
	public static boolean isSuccess(String result){
		return (result == null || "SUCCESS".equals(result) == false) ? false : true;
	}

	/**
	 * handle production when build
	 * @param jobName
	 * @param production
	 * @param build
	 * @return
	 */
	public boolean process(String jobName, String production, int serviceGroup, AbstractBuild<?,?> build){
		ProductionMeta productionMeta = normalizationProductionMeta(jobName, production, serviceGroup, build);
		makeJobDir(productionMeta, build);
		compressToCopy(productionMeta);
		saveProductionMeta(productionMeta);
		return true;
	}
	
	
	private ProductionMeta normalizationProductionMeta(String jobName, String production, int serviceGroup, AbstractBuild<?,?> build){
		
		ProductionMeta productionMeta = new ProductionMeta();
		productionMeta.setJobName(jobName);
		productionMeta.setCreateDate(new Date());
		productionMeta.setBuildNumber(build.getNumber());
		productionMeta.setProductionPath(production);
		productionMeta.setServiceGroup(serviceGroup);
		
		String filename = getName(production);

// Path: flyJenkins/src/main/java/com/agun/flyJenkins/model/ProductionMeta.java
// public class ProductionMeta {
// 	
// 	private String jobName;
// 	private Date createDate;
// 	private String productionPath;
// 	private String productionPathOfJob;	
// 	private int serviceGroup;
// 	private int buildNumber;
// 	
// 	public String getJobName() {
// 		return jobName;
// 	}
// 	
// 	public void setJobName(String jobName) {
// 		this.jobName = jobName;
// 	}
// 	
// 	public int getBuildNumber() {
// 		return buildNumber;
// 	}
// 	
// 	public void setBuildNumber(int buildNumber) {
// 		this.buildNumber = buildNumber;
// 	}
// 	
// 	public Date getCreateDate() {
// 		return createDate;
// 	}
// 	
// 	public void setCreateDate(Date createDate) {
// 		this.createDate = createDate;
// 	}
// 	
// 	public int getServiceGroup() {
// 		return serviceGroup;
// 	}
// 	
// 	public void setServiceGroup(int serviceGroup) {
// 		this.serviceGroup = serviceGroup;
// 	}
// 	
// 	public String getProductionPath() {
// 		return productionPath;
// 	}
// 	
// 	public void setProductionPath(String productionPath) {
// 		this.productionPath = productionPath;
// 	}
// 
// 	public String getProductionPathOfJob() {
// 		return productionPathOfJob;
// 	}
// 
// 	public void setProductionPathOfJob(String productionPathOfJob) {
// 		this.productionPathOfJob = productionPathOfJob;
// 	}
// }
// 
// Path: flyJenkins/src/main/java/com/agun/flyJenkins/persistence/ProductionSaveable.java
// public class ProductionSaveable implements Saveable{
// 
// 	List<ProductionMeta> productionMetaList = null;
// 	
// 	public void save() throws IOException {
// 		if(productionMetaList != null){
// 			ModelSoting.productionSortDate(productionMetaList);
// 		}
// 		if(BulkChange.contains(this))   return;
//         try {
//             getConfigFile().write(this);
//             SaveableListener.fireOnChange(this, getConfigFile());
//         } catch (IOException e) {
//            	e.printStackTrace();
//         }
// 		
// 	}
// 	protected XmlFile getConfigFile() {
// 		
// 	    return new XmlFile(new File(Jenkins.getInstance().getRootDir(), this.getClass().getSimpleName()+".xml"));
//     }
// 
// 	
// 	/**
// 	 * open file and load data
// 	 */
// 	 public synchronized void load() {
// 		 XmlFile file = getConfigFile();
// 		 if(!file.exists())
// 			 return;
// 		 try {
// 			 file.unmarshal(this);
// 		 } catch (IOException e) {
// 			 e.printStackTrace();
// 		 }
// 	 }
// 	 
// 	public List<ProductionMeta> getProductionMetaList() {
// 		return productionMetaList;
// 	}
// 	
// 	public void setProductionMetaList(List<ProductionMeta> productionMetaList) {
// 		this.productionMetaList = productionMetaList;
// 	}
// }
// 
// Path: flyJenkins/src/main/java/com/agun/flyJenkins/util/FlyJenkinsEnv.java
// public class FlyJenkinsEnv {
// 
// 	public static String getProductionRoot(){
// 		return Jenkins.getInstance().getRootDir().getAbsolutePath() + "/flyJenkins/job";
// 	}
// 	
// 	public static String getLastBuildPath(int serviceId){
// 		return Jenkins.getInstance().getRootDir().getAbsolutePath() + "/flyJenkins/lastBuild/"+serviceId+"/service.zip";
// 	}
// }


// Path: flyJenkins/src/main/java/com/agun/flyJenkins/deploy/DeployProduction.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jenkins.model.Jenkins;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import com.agun.flyJenkins.model.ProductionMeta;
import com.agun.flyJenkins.persistence.ProductionSaveable;
import com.agun.flyJenkins.util.FlyJenkinsEnv;

package com.agun.flyJenkins.deploy;





/**
 * this class handle production when build 
 * @author agun
 *
 */
public class DeployProduction {

	public final static String SUCCESS = "SUCCESS";
	public final static String FAIL = "FAILURE";

	/**
	 * check state of building
	 * @param result
	 * @return
	 */
	public static boolean isSuccess(String result){
		return (result == null || "SUCCESS".equals(result) == false) ? false : true;
	}

	/**
	 * handle production when build
	 * @param jobName
	 * @param production
	 * @param build
	 * @return
	 */
	public boolean process(String jobName, String production, int serviceGroup, AbstractBuild<?,?> build){
		ProductionMeta productionMeta = normalizationProductionMeta(jobName, production, serviceGroup, build);
		makeJobDir(productionMeta, build);
		compressToCopy(productionMeta);
		saveProductionMeta(productionMeta);
		return true;
	}
	
	
	private ProductionMeta normalizationProductionMeta(String jobName, String production, int serviceGroup, AbstractBuild<?,?> build){
		
		ProductionMeta productionMeta = new ProductionMeta();
		productionMeta.setJobName(jobName);
		productionMeta.setCreateDate(new Date());
		productionMeta.setBuildNumber(build.getNumber());
		productionMeta.setProductionPath(production);
		productionMeta.setServiceGroup(serviceGroup);
		
		String filename = getName(production);

		String productionPathOfJob = FlyJenkinsEnv.getProductionRoot() + "/"+ jobName + "/" + build.getNumber() + "/" + filename;
