ProcessServiceImpl.java
上传用户:ahit0551
上传日期:2009-04-15
资源大小:2345k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. package name.xio.xiorkflow.domain.logic;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import name.xio.util.PathUtil;
  8. import name.xio.util.SingleIOFilenameFilter;
  9. import name.xio.xiorkflow.domain.Process;
  10. import name.xio.xiorkflow.domain.ProcessResult;
  11. import name.xio.xml.SimpleXMLWorkShop;
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import org.jdom.Document;
  15. import org.jdom.JDOMException;
  16. public class ProcessServiceImpl implements ProcessService {
  17.     public ProcessServiceImpl() {
  18.         this.setWebAppRootKey(ProcessServiceImpl.WEB_APP_ROOT_KEY);
  19.         this.setBaseDirName(ProcessServiceImpl.BASE_DIR_NAME);
  20.         this.setDefaultFileName(ProcessServiceImpl.DEFAULT_FILE_NAME);
  21.         this.setFileType(ProcessServiceImpl.FILE_TYPE);
  22.     }
  23.     public List listProcess() {
  24.         File baseDir = new File(this.getWebAppRoot(), this.getBaseDirName());
  25.         File[] files = baseDir.listFiles(new SingleIOFilenameFilter(this
  26.                 .getFileType()));
  27.         List list = new ArrayList();
  28.         for (int i = 0; i < files.length; i++) {
  29.             File file = files[i];
  30.             list.add(file);
  31.         }
  32.         return list;
  33.     }
  34.     public ProcessResult getProcess(String name) {
  35.         ProcessResult result = new ProcessResult();
  36.         File baseDir = new File(this.getWebAppRoot(), this.getBaseDirName());
  37.         File file = new File(baseDir, name + PathUtil.SEPARATOR_FORMAT
  38.                 + this.getFileType());
  39.         if (!file.exists()) {
  40.             result.setStatus(ProcessService.FILE_NOT_FOUND);
  41.             return result;
  42.         }
  43.         Process process = new Process();
  44.         process.setName(name);
  45.         try {
  46.             process.setDoc(SimpleXMLWorkShop.file2Doc(file));
  47.         } catch (IOException e) {
  48.             result.setStatus(ProcessService.IO_ERROR);
  49.             result.setMes(e.getMessage());
  50.             process = null;
  51.             log.warn("io error on getprocess:" + e.getMessage());
  52.         } catch (JDOMException e) {
  53.             result.setStatus(ProcessService.XML_PARSER_ERROR);
  54.             result.setMes(e.getMessage());
  55.             process = null;
  56.             log.warn("jdom error on getprocess:" + e.getMessage());
  57.         }
  58.         result.setProcess(process);
  59.         return result;
  60.     }
  61.     public ProcessResult addProcess(Process process) {
  62.         ProcessResult result = new ProcessResult();
  63.         File baseDir = new File(this.getWebAppRoot(), this.getBaseDirName());
  64.         File file = new File(baseDir, process.getName()
  65.                 + PathUtil.SEPARATOR_FORMAT + this.getFileType());
  66.         if (file.exists()) {
  67.             result.setStatus(ProcessService.FILE_EXIST);
  68.             return result;
  69.         }
  70.         Document doc = process.getDoc();
  71.         try {
  72.             SimpleXMLWorkShop.outputXML(doc, file);
  73.         } catch (FileNotFoundException e) {
  74.             result.setStatus(ProcessService.FILE_NOT_FOUND);
  75.             result.setMes(e.getMessage());
  76.             log.warn("file not found on addprocess:" + e.getMessage());
  77.             return result;
  78.         } catch (IOException e) {
  79.             result.setStatus(ProcessService.IO_ERROR);
  80.             result.setMes(e.getMessage());
  81.             log.warn("io error on addprocess:" + e.getMessage());
  82.             return result;
  83.         }
  84.         result.setStatus(ProcessService.SUCCESS);
  85.         return result;
  86.     }
  87.     public ProcessResult deleteProcess(Process process) {
  88.         ProcessResult result = new ProcessResult();
  89.         File baseDir = new File(this.getWebAppRoot(), this.getBaseDirName());
  90.         File file = new File(baseDir, process.getName()
  91.                 + PathUtil.SEPARATOR_FORMAT + this.getFileType());
  92.         if (!file.exists()) {
  93.             result.setStatus(ProcessService.FILE_NOT_FOUND);
  94.             return result;
  95.         }
  96.         if (file.delete()) {
  97.             result.setStatus(ProcessService.SUCCESS);
  98.         }
  99.         else {
  100.             result.setStatus(ProcessService.FAIL);
  101.         }
  102.         return result;
  103.     }
  104.     public ProcessResult updateProcess(Process process) {
  105.         ProcessResult result = new ProcessResult();
  106.         File baseDir = new File(this.getWebAppRoot(), this.getBaseDirName());
  107.         File file = new File(baseDir, process.getName()
  108.                 + PathUtil.SEPARATOR_FORMAT + this.getFileType());
  109.         if (!file.exists()) {
  110.             result.setStatus(ProcessService.FILE_NOT_FOUND);
  111.             return result;
  112.         }
  113.         Document doc = process.getDoc();
  114.         try {
  115.             SimpleXMLWorkShop.outputXML(doc, file);
  116.         } catch (FileNotFoundException e) {
  117.             result.setStatus(ProcessService.FILE_NOT_FOUND);
  118.             result.setMes(e.getMessage());
  119.             log.warn("file not found on updateprocess:" + e.getMessage());
  120.             return result;
  121.         } catch (IOException e) {
  122.             result.setStatus(ProcessService.IO_ERROR);
  123.             result.setMes(e.getMessage());
  124.             log.warn("io error on updateprocess:" + e.getMessage());
  125.             return result;
  126.         }
  127.         result.setStatus(ProcessService.SUCCESS);
  128.         return result;
  129.     }
  130.     /**
  131.      * @return Returns the baseDirName.
  132.      */
  133.     public String getBaseDirName() {
  134.         return baseDirName;
  135.     }
  136.     /**
  137.      * @param baseDirName The baseDirName to set.
  138.      */
  139.     public void setBaseDirName(String baseDirName) {
  140.         this.baseDirName = baseDirName;
  141.     }
  142.     /**
  143.      * @return Returns the defaultFileName.
  144.      */
  145.     public String getDefaultFileName() {
  146.         return this.defaultFileName;
  147.     }
  148.     /**
  149.      * @param defaultFileName The defaultFileName to set.
  150.      */
  151.     public void setDefaultFileName(String defaultFileName) {
  152.         this.defaultFileName = defaultFileName;
  153.     }
  154.     /**
  155.      * @return Returns the fileType.
  156.      */
  157.     public String getFileType() {
  158.         return this.fileType;
  159.     }
  160.     /**
  161.      * @param fileType The fileType to set.
  162.      */
  163.     public void setFileType(String fileType) {
  164.         this.fileType = fileType;
  165.     }
  166.     /**
  167.      * @return Returns the webAppRoot.
  168.      */
  169.     public String getWebAppRoot() {
  170.         return this.webAppRoot;
  171.     }
  172.     /**
  173.      * @param webAppRoot The webAppRoot to set.
  174.      */
  175.     public void setWebAppRoot(String webAppRoot) {
  176.         this.webAppRoot = webAppRoot;
  177.     }
  178.     /**
  179.      * @return Returns the webAppRootKey.
  180.      */
  181.     public String getWebAppRootKey() {
  182.         return this.webAppRootKey;
  183.     }
  184.     /**
  185.      * @param webAppRootKey The webAppRootKey to set.
  186.      */
  187.     public void setWebAppRootKey(String webAppRootKey) {
  188.         this.webAppRootKey = webAppRootKey;
  189.         this.setWebAppRoot(System.getProperty(this.getWebAppRootKey()));
  190.     }
  191.     private Log log = LogFactory.getLog(ProcessServiceImpl.class);
  192.     public final static String WEB_APP_ROOT_KEY = "xiorkflow.root";
  193.     public final static String BASE_DIR_NAME = "processes";
  194.     public final static String DEFAULT_FILE_NAME = "default";
  195.     public final static String FILE_TYPE = "xml";
  196.     //
  197.     private String webAppRootKey;
  198.     private String baseDirName;
  199.     private String defaultFileName;
  200.     private String fileType;
  201.     //
  202.     private String webAppRoot;
  203. }