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