001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.businessobject.options; 017 018import org.apache.commons.io.DirectoryWalker; 019import org.apache.commons.io.filefilter.DirectoryFileFilter; 020import org.apache.commons.lang.StringUtils; 021import org.kuali.ole.module.purap.document.service.OlePurapService; 022import org.kuali.ole.sys.OLEConstants; 023import org.kuali.ole.sys.batch.BatchFileUtils; 024import org.kuali.ole.sys.context.SpringContext; 025import org.kuali.rice.core.api.config.property.ConfigurationService; 026import org.kuali.rice.core.api.util.ConcreteKeyValue; 027import org.kuali.rice.core.api.util.KeyValue; 028import org.kuali.rice.krad.keyvalues.KeyValuesBase; 029 030import java.io.File; 031import java.io.IOException; 032import java.util.ArrayList; 033import java.util.Collection; 034import java.util.List; 035 036public class MarcFileUploadFileDirectoryPathValuesFinder extends KeyValuesBase { 037 038 protected OlePurapService olePurapService; 039 040 public OlePurapService getOlePurapService() { 041 if (olePurapService == null) { 042 olePurapService = SpringContext.getBean(OlePurapService.class); 043 } 044 return olePurapService; 045 } 046 047 @Override 048 public List<KeyValue> getKeyValues() { 049 List<File> rootDirectories = BatchFileUtils.retrieveBatchFileLookupRootDirectories(); 050 List<KeyValue> keyValues = new ArrayList<KeyValue>(); 051 052 for (File rootDirectory : rootDirectories) { 053 SubDirectoryWalker walker = new SubDirectoryWalker(keyValues); 054 try { 055 walker.addKeyValues(rootDirectory); 056 } catch (IOException e) { 057 throw new RuntimeException("IOException caught.", e); 058 } 059 } 060 return keyValues; 061 } 062 063 064 protected class SubDirectoryWalker extends DirectoryWalker { 065 private List<KeyValue> keyValues; 066 private int recursiveDepth; 067 private File rootDirectory; 068 069 public SubDirectoryWalker(List<KeyValue> keyValues) { 070 super(DirectoryFileFilter.DIRECTORY, -1); 071 this.keyValues = keyValues; 072 this.recursiveDepth = 0; 073 } 074 075 public void addKeyValues(File startDirectory) throws IOException { 076 rootDirectory = startDirectory; 077 walk(startDirectory, null); 078 } 079 080 /** 081 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection) 082 */ 083 @Override 084 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException { 085 String sourcePath = getOlePurapService().getParameter(OLEConstants.PARENT_FOLDER).trim(); 086 super.handleDirectoryStart(directory, depth, results); 087 if (directory.getName() != null && !directory.getName().equalsIgnoreCase(sourcePath) && directory.getPath().contains(sourcePath)) { 088 ConcreteKeyValue entry = new ConcreteKeyValue(); 089 entry.setKey(BatchFileUtils.pathRelativeToRootDirectory(directory.getAbsolutePath())); 090 StringBuilder indent = new StringBuilder(); 091 indent.append(StringUtils.repeat("-", 4 * recursiveDepth - 8)); 092 indent.append(directory.getName()); 093 entry.setValue(indent.toString()); 094 keyValues.add(entry); 095 } 096 this.recursiveDepth++; 097 } 098 099 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}