001/* 002 * Copyright 2009 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 */ 016package org.kuali.ole.sys.businessobject.options; 017 018import java.io.File; 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.List; 023 024import org.apache.commons.io.DirectoryWalker; 025import org.apache.commons.io.filefilter.DirectoryFileFilter; 026import org.apache.commons.lang.StringUtils; 027import org.kuali.ole.sys.batch.BatchFileUtils; 028import org.kuali.rice.core.api.util.ConcreteKeyValue; 029import org.kuali.rice.core.api.util.KeyValue; 030import org.kuali.rice.krad.keyvalues.KeyValuesBase; 031 032public class BatchFileDirectoryPathValuesFinder extends KeyValuesBase { 033 public List<KeyValue> getKeyValues() { 034 List<File> rootDirectories = BatchFileUtils.retrieveBatchFileLookupRootDirectories(); 035 List<KeyValue> keyValues = new ArrayList<KeyValue>(); 036 037 for (File rootDirectory: rootDirectories) { 038 SubDirectoryWalker walker = new SubDirectoryWalker(keyValues); 039 try { 040 walker.addKeyValues(rootDirectory); 041 } 042 catch (IOException e) { 043 throw new RuntimeException("IOException caught.", e); 044 } 045 } 046 047 return keyValues; 048 } 049 050 protected class SubDirectoryWalker extends DirectoryWalker { 051 private List<KeyValue> keyValues; 052 private int recursiveDepth; 053 private File rootDirectory; 054 055 public SubDirectoryWalker(List<KeyValue> keyValues) { 056 super(DirectoryFileFilter.DIRECTORY, -1); 057 this.keyValues = keyValues; 058 this.recursiveDepth = 0; 059 } 060 061 public void addKeyValues(File startDirectory) throws IOException { 062 rootDirectory = startDirectory; 063 walk(startDirectory, null); 064 } 065 066 /** 067 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection) 068 */ 069 @Override 070 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException { 071 super.handleDirectoryStart(directory, depth, results); 072 ConcreteKeyValue entry = new ConcreteKeyValue(); 073 entry.setKey(BatchFileUtils.pathRelativeToRootDirectory(directory.getAbsolutePath())); 074 // use the unicode literal for space....KFSMI-7392 fix 075 entry.setValue( StringUtils.repeat("\u00A0", 4 * this.recursiveDepth) + directory.getName()); 076 keyValues.add(entry); 077 this.recursiveDepth++; 078 } 079 080 /** 081 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection) 082 */ 083 @Override 084 protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException { 085 super.handleDirectoryEnd(directory, depth, results); 086 this.recursiveDepth--; 087 } 088 } 089}