View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.businessobject.options;
17  
18  import org.apache.commons.io.DirectoryWalker;
19  import org.apache.commons.io.filefilter.DirectoryFileFilter;
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.module.purap.document.service.OlePurapService;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.batch.BatchFileUtils;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.core.api.config.property.ConfigurationService;
26  import org.kuali.rice.core.api.util.ConcreteKeyValue;
27  import org.kuali.rice.core.api.util.KeyValue;
28  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  public class MarcFileUploadFileDirectoryPathValuesFinder extends KeyValuesBase {
37  
38      protected OlePurapService olePurapService;
39  
40      public OlePurapService getOlePurapService() {
41          if (olePurapService == null) {
42              olePurapService = SpringContext.getBean(OlePurapService.class);
43          }
44          return olePurapService;
45      }
46  
47      @Override
48      public List<KeyValue> getKeyValues() {
49          List<File> rootDirectories = BatchFileUtils.retrieveBatchFileLookupRootDirectories();
50          List<KeyValue> keyValues = new ArrayList<KeyValue>();
51  
52          for (File rootDirectory : rootDirectories) {
53              SubDirectoryWalker walker = new SubDirectoryWalker(keyValues);
54              try {
55                  walker.addKeyValues(rootDirectory);
56              } catch (IOException e) {
57                  throw new RuntimeException("IOException caught.", e);
58              }
59          }
60          return keyValues;
61      }
62  
63  
64      protected class SubDirectoryWalker extends DirectoryWalker {
65          private List<KeyValue> keyValues;
66          private int recursiveDepth;
67          private File rootDirectory;
68  
69          public SubDirectoryWalker(List<KeyValue> keyValues) {
70              super(DirectoryFileFilter.DIRECTORY, -1);
71              this.keyValues = keyValues;
72              this.recursiveDepth = 0;
73          }
74  
75          public void addKeyValues(File startDirectory) throws IOException {
76              rootDirectory = startDirectory;
77              walk(startDirectory, null);
78          }
79  
80          /**
81           * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection)
82           */
83          @Override
84          protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException {
85              String sourcePath = getOlePurapService().getParameter(OLEConstants.PARENT_FOLDER).trim();
86              super.handleDirectoryStart(directory, depth, results);
87              if (directory.getName() != null && !directory.getName().equalsIgnoreCase(sourcePath) && directory.getPath().contains(sourcePath)) {
88                  ConcreteKeyValue entry = new ConcreteKeyValue();
89                  entry.setKey(BatchFileUtils.pathRelativeToRootDirectory(directory.getAbsolutePath()));
90                  StringBuilder indent = new StringBuilder();
91                  indent.append(StringUtils.repeat("-", 4 * recursiveDepth - 8));
92                  indent.append(directory.getName());
93                  entry.setValue(indent.toString());
94                  keyValues.add(entry);
95              }
96              this.recursiveDepth++;
97          }
98  
99  
100         /**
101          * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection)
102          */
103         @Override
104         protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException {
105             super.handleDirectoryEnd(directory, depth, results);
106             this.recursiveDepth--;
107         }
108     }
109 }