View Javadoc
1   package org.kuali.ole.workflow;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.io.FilenameUtils;
5   import org.apache.commons.io.IOUtils;
6   
7   import java.io.*;
8   import java.net.URISyntaxException;
9   import java.net.URL;
10  import java.util.Enumeration;
11  import java.util.Iterator;
12  import java.util.StringTokenizer;
13  
14  /**
15   * User: peris
16   * Date: 12/16/12
17   * Time: 8:17 AM
18   */
19  public class WorfklowFileHandler {
20  
21      private String workflowXMLSrcDir;
22      private String workflowXMLDestDir;
23      private File destDir;
24      private String tmpDir;
25  
26      /**
27       * This method is the main method that copies the xml files from the srouce
28       * directory to the destination directory. This takes the relative path from
29       * WEB-INF/classes directory and hence the soruce should be present under the
30       * directory. The destination can be anywhere in the file system and has to be
31       * an absolute path.
32       */
33      public void execute() {
34          File xmlWorkflowSourceDir = null;
35          URL resource = Thread.currentThread().getContextClassLoader().getResource("");
36          try {
37              File file = null;
38              if (resource != null) {
39                  file = new File(resource.toURI());
40                  if (null != file && file.isDirectory()) {
41                      xmlWorkflowSourceDir = getXMLWorkflowSourceDirRelativeToPath(file);
42                      iterateDir(xmlWorkflowSourceDir);
43                  }
44              } else {
45                  Enumeration<URL> resources = null;
46                  resources = ClassLoader.getSystemClassLoader().getResources("");
47                  File sourceDirctory = new File(resources.nextElement().getFile(), getWorkflowXMLSrcDir());
48                  iterateDir(sourceDirctory);
49              }
50          } catch (IOException e) {
51              e.printStackTrace();
52          } catch (URISyntaxException e) {
53              e.printStackTrace();
54          }
55      }
56  
57      /**
58       * @param file - Name of the folder
59       * @return Folder - Since the source is relative to WEB-INF/classes, thie method
60       *         returns the folder that would contain the xml files or sub-folders with the xml files
61       *         after traversing to the directory specified in the workflowXMLSrcDir.
62       */
63      private File getXMLWorkflowSourceDirRelativeToPath(File file) {
64          String dir = getWorkflowXMLSrcDir();
65          StringTokenizer stringTokenizer = new StringTokenizer(dir, "/");
66          while (stringTokenizer.hasMoreTokens()) {
67              String token = stringTokenizer.nextToken();
68              file = getFolder(file, token);
69          }
70  
71          return file;
72      }
73  
74      private File getFolder(File file, String token) {
75          File[] files = file.listFiles();
76          for (int i = 0; i < files.length; i++) {
77              File file1 = files[i];
78              if (file1.getName().equals(token)) {
79                  return file1;
80              }
81          }
82          return null;
83      }
84  
85      /**
86       * @param sourceDirctory
87       * @throws IOException This method iterates through the folder to copy any xml files found in the path.
88       *                     The assumption here would be the xml files are valid workflow xml files.
89       */
90      private void iterateDir(File sourceDirctory) throws IOException {
91          File[] files = sourceDirctory.listFiles();
92          for (int i = 0; i < files.length; i++) {
93              File file = files[i];
94  //            if (FilenameUtils.getExtension(file.getName()).contains("xml")) {
95  //                copyFileToDestDir(file);
96  //            } else if (file.isDirectory()) {
97  //                iterateDir(file);
98  //            }
99              copyFileToDestDir(file);
100         }
101     }
102 
103     private void copyFileToDestDir(File inputFile) throws IOException {
104 //        InputStream fileInputStream = new FileInputStream(inputFile);
105 //        File tempFile = new File(getTempDir(), inputFile.getName());
106 //        IOUtils.copy(fileInputStream, new FileOutputStream(tempFile));
107 //        FileUtils.copyFileToDirectory(tempFile, getDestDir());
108 
109         if (inputFile.isDirectory()) {
110             FileUtils.copyDirectoryToDirectory(inputFile, getDestDir());
111         } else if (inputFile.isFile() && FilenameUtils.getExtension(inputFile.getName()).contains("xml")) {
112             FileUtils.copyFileToDirectory(inputFile, getDestDir());
113         }
114     }
115 
116     public File getDestDir() {
117         if (null == destDir) {
118             destDir = new File(workflowXMLDestDir);
119         }
120         return destDir;
121     }
122 
123     public String getTempDir() {
124         if (null == tmpDir) {
125             tmpDir = System.getProperty("java.io.tmpdir");
126         }
127         return tmpDir;
128     }
129 
130     public String getWorkflowXMLSrcDir() {
131         return workflowXMLSrcDir;
132     }
133 
134     public void setWorkflowXMLSrcDir(String workflowXMLSrcDir) {
135         this.workflowXMLSrcDir = workflowXMLSrcDir;
136     }
137 
138     public String getWorkflowXMLDestDir() {
139         return workflowXMLDestDir;
140     }
141 
142     public void setWorkflowXMLDestDir(String workflowXMLDestDir) {
143         this.workflowXMLDestDir = workflowXMLDestDir;
144     }
145 }