import java.io.File;
import java.io.FileOutputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * save or load a job
 * @author 413
 *
 */
public class ServiceJob 
{

	//XML elements
	static Element saveroot = new Element("Job");
	static Element loadroot = new Element("Job");
	static Element EPDFSource = new Element("PDFSource");
	static Element EPDFStamp = new Element("PDFStamp");
	static Element EPDFStampType = new Element("PDFStampType");
	static Element EStartPage = new Element("StartPage");
	static Element ELastPage = new Element("LastPage");
	static Element EParameter1 = new Element("Parameter1");
	static Element EParameter2 = new Element("Parameter2");
	static Element EParameter3 = new Element("Parameter3");
	static Element EParameter4 = new Element("Parameter4");
	static Element EParameter5 = new Element("Parameter5");
	static Element EResultFile = new Element("ResultFile");
	
	static org.jdom.Document saveDoc = new Document(saveroot);
	static org.jdom.Document loadDoc = new Document(loadroot);
	
	/**
	 * constructor
	 */
	public static void init()
	{
		saveroot.addContent(EPDFSource);
		saveroot.addContent(EPDFStamp);
		saveroot.addContent(EPDFStampType);
		saveroot.addContent(EStartPage);
		saveroot.addContent(ELastPage);
		saveroot.addContent(EParameter1);
		saveroot.addContent(EParameter2);
		saveroot.addContent(EParameter3);
		saveroot.addContent(EParameter4);
		saveroot.addContent(EParameter5);
		saveroot.addContent(EResultFile);
	}

	/**
	 * save the job into a job.xml file
	 * @param job the job to save
	 */
	public void save(Job job)
	{
		   try
		   {
			  EPDFSource.setText(job.getPDFSource().getPath());
			  EPDFStamp.setText(job.getPDFStamp().getPath());
			  EPDFStampType.setText(job.getPDFStampType().getPath());
			  EStartPage.setText(job.getStartPage()+"");
			  if(job.getLastPage()!=null)
			  ELastPage.setText(job.getLastPage()+"");
			  EParameter1.setText(job.getParameter1());
			  EParameter2.setText(job.getParameter2());
			  EParameter3.setText(job.getParameter3());
			  EParameter4.setText(job.getParameter4());
			  EParameter5.setText(job.getParameter5());
			  EResultFile.setText(job.getResultFile().getPath());
			  //save data
		      XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
		      FileOutputStream out = new FileOutputStream(job.getjobFile());
		      sortie.output(saveDoc, out);
		      out.close();
		   }
		   catch (java.io.IOException e){System.out.println("an error occurred while saving the file "+job.getPDFSource().getPath());}
	}

	/**
	 * load the job from job.xml file
	 * @param source
	 * @return the job
	 */
	public Job load(File source)
	{
		Job ret = new Job();
		ret.setResult(new Result());
		SAXBuilder sxb = new SAXBuilder();
		try
		{
			org.jdom.Document loadDoc = sxb.build(source);
			Element rootload = loadDoc.getRootElement();
			ret.setPDFSource(new File(rootload.getChild("PDFSource").getText()));
			ret.setPDFStamp(new File(rootload.getChild("PDFStamp").getText()));
			ret.setPDFStampType(new File(rootload.getChild("PDFStampType").getText()));
			ret.setStartPage(Integer.parseInt(rootload.getChild("StartPage").getText()));
			if(!rootload.getChild("LastPage").getText().isEmpty())
			{
				ret.setLastPage(Integer.parseInt(rootload.getChild("LastPage").getText()));
			}
			ret.setParameter1(rootload.getChild("Parameter1").getText());
			ret.setParameter2(rootload.getChild("Parameter2").getText());
			ret.setParameter3(rootload.getChild("Parameter3").getText());
			ret.setParameter4(rootload.getChild("Parameter4").getText());
			ret.setParameter5(rootload.getChild("Parameter5").getText());
			ret.setResultFile(new File(rootload.getChild("ResultFile").getText()));
		}
		catch(Exception e)
		{
			System.out.println("an error occurred while loading the file "+source.getPath());
		}
		return ret;

	}
}
