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
16
17
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
28
29
30
31
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
59
60
61
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
87
88
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
95
96
97
98
99 copyFileToDestDir(file);
100 }
101 }
102
103 private void copyFileToDestDir(File inputFile) throws IOException {
104
105
106
107
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 }