1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.businessobject.options;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.apache.commons.io.DirectoryWalker;
25 import org.apache.commons.io.filefilter.DirectoryFileFilter;
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.ole.sys.batch.BatchFileUtils;
28 import org.kuali.rice.core.api.util.ConcreteKeyValue;
29 import org.kuali.rice.core.api.util.KeyValue;
30 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
31
32 public class BatchFileDirectoryPathValuesFinder extends KeyValuesBase {
33 public List<KeyValue> getKeyValues() {
34 List<File> rootDirectories = BatchFileUtils.retrieveBatchFileLookupRootDirectories();
35 List<KeyValue> keyValues = new ArrayList<KeyValue>();
36
37 for (File rootDirectory: rootDirectories) {
38 SubDirectoryWalker walker = new SubDirectoryWalker(keyValues);
39 try {
40 walker.addKeyValues(rootDirectory);
41 }
42 catch (IOException e) {
43 throw new RuntimeException("IOException caught.", e);
44 }
45 }
46
47 return keyValues;
48 }
49
50 protected class SubDirectoryWalker extends DirectoryWalker {
51 private List<KeyValue> keyValues;
52 private int recursiveDepth;
53 private File rootDirectory;
54
55 public SubDirectoryWalker(List<KeyValue> keyValues) {
56 super(DirectoryFileFilter.DIRECTORY, -1);
57 this.keyValues = keyValues;
58 this.recursiveDepth = 0;
59 }
60
61 public void addKeyValues(File startDirectory) throws IOException {
62 rootDirectory = startDirectory;
63 walk(startDirectory, null);
64 }
65
66
67
68
69 @Override
70 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException {
71 super.handleDirectoryStart(directory, depth, results);
72 ConcreteKeyValue entry = new ConcreteKeyValue();
73 entry.setKey(BatchFileUtils.pathRelativeToRootDirectory(directory.getAbsolutePath()));
74
75 entry.setValue( StringUtils.repeat("\u00A0", 4 * this.recursiveDepth) + directory.getName());
76 keyValues.add(entry);
77 this.recursiveDepth++;
78 }
79
80
81
82
83 @Override
84 protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException {
85 super.handleDirectoryEnd(directory, depth, results);
86 this.recursiveDepth--;
87 }
88 }
89 }