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;
017
018import org.kuali.common.util.Assert;
019import org.kuali.common.util.CollectionUtils;
020import org.kuali.common.util.FormatUtils;
021import org.kuali.common.util.scm.ScmRequest;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * @deprecated
027 */
028@Deprecated
029public class ScmExecutable implements Executable {
030
031        private static final Logger logger = LoggerFactory.getLogger(ScmExecutable.class);
032
033        boolean skip;
034        boolean logConfiguration;
035        org.kuali.common.util.service.ScmService service;
036        ScmRequest request;
037
038        @Override
039        public void execute() {
040
041                // Show our current configuration
042                if (logConfiguration) {
043                        log(this);
044                }
045
046                // They have explicitly asked that execution be skipped
047                if (skip) {
048                        logger.info("Skipping execution");
049                        return;
050                }
051
052                // The SCM request is not allowed to be null here
053                Assert.notNull(request);
054
055                // There are no files to add/delete/commit, no point in going further.
056                if (isEmpty(request)) {
057                        logger.info("Skipping execution.  Nothing to do!");
058                        return;
059                }
060
061                // Make sure we are configured correctly
062                validateConfiguration(service, request);
063
064                // Add files as needed
065                if (!CollectionUtils.isEmpty(request.getAdds())) {
066                        service.add(request.getAdds());
067                }
068
069                // Delete files as needed
070                if (!CollectionUtils.isEmpty(request.getDeletes())) {
071                        service.delete(request.getDeletes());
072                }
073
074                // Commit files as needed
075                if (!CollectionUtils.isEmpty(request.getCommits())) {
076                        service.commit(request.getCommits(), request.getCommitMessage());
077                }
078        }
079
080        protected void validateConfiguration(org.kuali.common.util.service.ScmService service, ScmRequest request) {
081                // Make sure we are configured correctly
082                Assert.notNull(service, "service is null");
083                if (!CollectionUtils.isEmpty(request.getCommits())) {
084                        Assert.hasText(request.getCommitMessage(), "commitMessage has no text");
085                }
086        }
087
088        protected void log(ScmExecutable exec) {
089                ScmRequest request = org.kuali.common.util.ScmUtils.cloneOrNew(exec.getRequest());
090                String adds = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getAdds()).size());
091                String deletes = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getDeletes()).size());
092                String commits = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getCommits()).size());
093
094                logger.info(" -- SCM --");
095                logger.info("Adds: {}", adds);
096                logger.info("Deletes: {}", deletes);
097                logger.info("Commits: {}", commits);
098                logger.info("Skip: {}", skip);
099        }
100
101        public boolean isEmpty(ScmRequest request) {
102                if (!CollectionUtils.isEmpty(request.getAdds())) {
103                        return false;
104                }
105                if (!CollectionUtils.isEmpty(request.getDeletes())) {
106                        return false;
107                }
108                if (!CollectionUtils.isEmpty(request.getCommits())) {
109                        return false;
110                }
111                return true;
112        }
113
114        public org.kuali.common.util.service.ScmService getService() {
115                return service;
116        }
117
118        public void setService(org.kuali.common.util.service.ScmService service) {
119                this.service = service;
120        }
121
122        public boolean isSkip() {
123                return skip;
124        }
125
126        public void setSkip(boolean skip) {
127                this.skip = skip;
128        }
129
130        public ScmRequest getRequest() {
131                return request;
132        }
133
134        public void setRequest(ScmRequest request) {
135                this.request = request;
136        }
137
138        public boolean isLogConfiguration() {
139                return logConfiguration;
140        }
141
142        public void setLogConfiguration(boolean logConfiguration) {
143                this.logConfiguration = logConfiguration;
144        }
145
146}