View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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           * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection)
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              // use the unicode literal for space....KFSMI-7392 fix
75              entry.setValue( StringUtils.repeat("\u00A0", 4 * this.recursiveDepth) + directory.getName());
76              keyValues.add(entry);
77              this.recursiveDepth++;
78          }
79  
80          /**
81           * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection)
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  }