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.io.IOException;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.kuali.common.util.Assert;
024    import org.kuali.common.util.FileSystemUtils;
025    import org.kuali.common.util.SyncRequest;
026    import org.kuali.common.util.SyncResult;
027    import org.kuali.common.util.service.ScmService;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    public class SyncFilesExecutable implements Executable {
032    
033        private static final Logger logger = LoggerFactory.getLogger(SyncFilesExecutable.class);
034    
035        boolean skip;
036        // Don't commit changes unless they specifically set this to true
037        boolean commitChanges;
038        ScmService service;
039        String message = "Automated update";
040        List<SyncRequest> requests;
041        List<File> commitPaths;
042    
043        @Override
044        public void execute() {
045            if (skip) {
046                logger.info("Skipping file sync");
047                return;
048            }
049    
050            Assert.notNull(requests);
051            Assert.notNull(service);
052            Assert.notNull(commitPaths);
053    
054            List<File> adds = new ArrayList<File>();
055            List<File> deletes = new ArrayList<File>();
056    
057            List<SyncResult> results = syncFiles();
058            for (SyncResult result : results) {
059                adds.addAll(result.getAdds());
060                deletes.addAll(result.getDeletes());
061            }
062    
063            logger.info("---------- Sync results ----------");
064            logger.info("Files added - {}", adds.size());
065            logger.info("Files deleted - {}", deletes.size());
066            logger.info("---------- Sync results ----------");
067    
068            if (commitChanges) {
069                service.add(adds);
070                service.delete(deletes);
071                service.commit(commitPaths, message);
072            } else {
073                logger.info("Skipping SCM commit");
074            }
075        }
076    
077        public List<SyncResult> syncFiles() {
078            logger.info("Syncing {} requests", requests.size());
079            try {
080                return FileSystemUtils.syncFiles(requests);
081            } catch (IOException e) {
082                throw new IllegalStateException("Unexpected IO error", e);
083            }
084        }
085    
086        public boolean isSkip() {
087            return skip;
088        }
089    
090        public void setSkip(boolean skip) {
091            this.skip = skip;
092        }
093    
094        public ScmService getService() {
095            return service;
096        }
097    
098        public void setService(ScmService service) {
099            this.service = service;
100        }
101    
102        public String getMessage() {
103            return message;
104        }
105    
106        public void setMessage(String message) {
107            this.message = message;
108        }
109    
110        public boolean isCommitChanges() {
111            return commitChanges;
112        }
113    
114        public void setCommitChanges(boolean commitChanges) {
115            this.commitChanges = commitChanges;
116        }
117    
118        public List<File> getCommitPaths() {
119            return commitPaths;
120        }
121    
122        public void setCommitPaths(List<File> commitPaths) {
123            this.commitPaths = commitPaths;
124        }
125    
126        public List<SyncRequest> getRequests() {
127            return requests;
128        }
129    
130        public void setRequests(List<SyncRequest> requests) {
131            this.requests = requests;
132        }
133    }