001    /**
002     * Copyright 2010-2013 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.common.util.execute;
017    
018    import java.io.File;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.kuali.common.util.CollectionUtils;
023    import org.kuali.common.util.ComparisonResults;
024    import org.kuali.common.util.LocationUtils;
025    import org.kuali.common.util.PropertyUtils;
026    import org.kuali.common.util.SimpleScanner;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    import org.springframework.util.Assert;
030    
031    /**
032     * This executable compares the files found by the given pattern in two different locations and populates a properties
033     * object with information about the comparison
034     * 
035     * @author andrewlubbers
036     */
037    public class FileListComparisonExecutable implements Executable {
038    
039            private static final Logger logger = LoggerFactory.getLogger(FileListComparisonExecutable.class);
040    
041            private String newFilesBaseDir;
042            private String filePattern;
043            private String originalFilesBaseDir;
044            private List<String> propertyNames;
045            private Properties mavenProperties;
046    
047            @Override
048            public void execute() {
049                    Assert.notNull(mavenProperties);
050                    Assert.notNull(propertyNames);
051                    Assert.notNull(newFilesBaseDir);
052                    Assert.notNull(originalFilesBaseDir);
053                    Assert.notNull(filePattern);
054    
055                    logger.info("Starting File List Comparison");
056                    logger.info("Original files dir: " + originalFilesBaseDir);
057                    logger.info("New files dir: " + LocationUtils.getCanonicalPath(new File(newFilesBaseDir)));
058                    logger.info("File pattern: " + filePattern);
059                    logger.info("Property names: " + propertyNames);
060    
061                    List<String> filePatterns = CollectionUtils.getTrimmedListFromCSV(filePattern);
062                    String[] includesArray = filePatterns.toArray(new String[filePatterns.size()]);
063    
064                    SimpleScanner scanner = new SimpleScanner();
065                    scanner.setBasedir(newFilesBaseDir);
066                    scanner.setIncludes(includesArray);
067    
068                    List<String> newLocations = LocationUtils.getAbsolutePaths(scanner.getFiles());
069                    List<String> newFileNames = LocationUtils.getFilenames(newLocations);
070    
071                    scanner = new SimpleScanner();
072                    scanner.setBasedir(originalFilesBaseDir);
073                    scanner.setIncludes(includesArray);
074    
075                    List<String> originalLocations = LocationUtils.getAbsolutePaths(scanner.getFiles());
076                    List<String> originalFileNames = LocationUtils.getFilenames(originalLocations);
077    
078                    ComparisonResults comparison = LocationUtils.getLocationListComparison(newFileNames, originalFileNames);
079    
080                    logger.info("Added {} files.", comparison.getAdded().size());
081                    logger.info("Updated {} existing files.", comparison.getSame().size());
082                    logger.info("Deleted {} files.", comparison.getDeleted().size());
083    
084                    PropertyUtils.addListComparisonProperties(mavenProperties, comparison, propertyNames);
085            }
086    
087            public String getFilePattern() {
088                    return filePattern;
089            }
090    
091            public void setFilePattern(String filePattern) {
092                    this.filePattern = filePattern;
093            }
094    
095            public Properties getMavenProperties() {
096                    return mavenProperties;
097            }
098    
099            public void setMavenProperties(Properties mavenProperties) {
100                    this.mavenProperties = mavenProperties;
101            }
102    
103            public String getNewFilesBaseDir() {
104                    return newFilesBaseDir;
105            }
106    
107            public void setNewFilesBaseDir(String newFilesBaseDir) {
108                    this.newFilesBaseDir = newFilesBaseDir;
109            }
110    
111            public String getOriginalFilesBaseDir() {
112                    return originalFilesBaseDir;
113            }
114    
115            public void setOriginalFilesBaseDir(String originalFilesBaseDir) {
116                    this.originalFilesBaseDir = originalFilesBaseDir;
117            }
118    
119            public List<String> getPropertyNames() {
120                    return propertyNames;
121            }
122    
123            public void setPropertyNames(List<String> propertyNames) {
124                    this.propertyNames = propertyNames;
125            }
126    }