001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.sys.businessobject.options;
017
018 import java.io.File;
019 import java.io.FileFilter;
020 import java.io.IOException;
021 import java.util.ArrayList;
022 import java.util.Collection;
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.commons.io.DirectoryWalker;
027 import org.apache.commons.io.filefilter.DirectoryFileFilter;
028 import org.apache.commons.lang.StringUtils;
029 import org.kuali.kfs.sys.KFSConstants;
030 import org.kuali.kfs.sys.batch.BatchFileUtils;
031 import org.kuali.kfs.sys.context.SpringContext;
032 import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
033 import org.kuali.rice.kns.service.KualiConfigurationService;
034 import org.kuali.rice.core.util.KeyLabelPair;
035
036 public class BatchFileDirectoryPathValuesFinder extends KeyValuesBase {
037 public List<KeyLabelPair> getKeyValues() {
038 List<File> rootDirectories = BatchFileUtils.retrieveBatchFileLookupRootDirectories();
039 List<KeyLabelPair> keyValues = new ArrayList<KeyLabelPair>();
040
041 for (File rootDirectory: rootDirectories) {
042 SubDirectoryWalker walker = new SubDirectoryWalker(keyValues);
043 try {
044 walker.addKeyValues(rootDirectory);
045 }
046 catch (IOException e) {
047 throw new RuntimeException("IOException caught.", e);
048 }
049 }
050
051 return keyValues;
052 }
053
054 protected class SubDirectoryWalker extends DirectoryWalker {
055 private List<KeyLabelPair> keyValues;
056 private int recursiveDepth;
057 private File rootDirectory;
058
059 public SubDirectoryWalker(List<KeyLabelPair> keyValues) {
060 super(DirectoryFileFilter.DIRECTORY, -1);
061 this.keyValues = keyValues;
062 this.recursiveDepth = 0;
063 }
064
065 public void addKeyValues(File startDirectory) throws IOException {
066 rootDirectory = startDirectory;
067 walk(startDirectory, null);
068 }
069
070 /**
071 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection)
072 */
073 @Override
074 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException {
075 super.handleDirectoryStart(directory, depth, results);
076 KeyLabelPair entry = new KeyLabelPair();
077 entry.setKey(BatchFileUtils.pathRelativeToRootDirectory(directory.getAbsolutePath()));
078 entry.setLabel(directory.getName());
079 entry.setNumPaddedSpaces(4 * this.recursiveDepth);
080 keyValues.add(entry);
081 this.recursiveDepth++;
082 }
083
084 /**
085 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection)
086 */
087 @Override
088 protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException {
089 super.handleDirectoryEnd(directory, depth, results);
090 this.recursiveDepth--;
091 }
092 }
093 }