001/**
002 * Copyright 2010-2014 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.common.util.execute.impl;
017
018import java.io.File;
019import java.util.List;
020
021import org.kuali.common.util.Assert;
022import org.kuali.common.util.LocationUtils;
023import org.kuali.common.util.execute.Executable;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027public abstract class AbstractCopyLocationsExecutable implements Executable {
028
029        private static final Logger logger = LoggerFactory.getLogger(AbstractCopyLocationsExecutable.class);
030
031        private final String locationListing;
032        private final File directory;
033        private final boolean skip;
034
035        public AbstractCopyLocationsExecutable(String locationListing, File directory) {
036                this(locationListing, directory, false);
037        }
038
039        public AbstractCopyLocationsExecutable(String locationListing, File directory, boolean skip) {
040                Assert.noBlanks(locationListing);
041                Assert.notNull(directory);
042                Assert.exists(locationListing);
043
044                this.locationListing = locationListing;
045                this.directory = directory;
046                this.skip = skip;
047        }
048
049        protected abstract List<File> getFiles(List<String> locations);
050
051        @Override
052        public void execute() {
053                logger.info("Copying [{}] -> [{}]", locationListing, LocationUtils.getCanonicalPath(directory));
054                List<String> locations = LocationUtils.getLocations(locationListing);
055                List<File> files = getFiles(locations);
056                LocationUtils.copyLocationsToFiles(locations, files);
057                logger.info("Copied {} files", locations.size());
058        }
059
060        public String getLocationListing() {
061                return locationListing;
062        }
063
064        public File getDirectory() {
065                return directory;
066        }
067
068        public boolean isSkip() {
069                return skip;
070        }
071}