View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.scm;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.kuali.common.util.Assert;
23  import org.kuali.common.util.CollectionUtils;
24  import org.kuali.common.util.SyncResult;
25  import org.kuali.common.util.execute.Executable;
26  import org.kuali.common.util.file.DirRequest;
27  import org.kuali.common.util.sync.DefaultSyncService;
28  import org.kuali.common.util.sync.SyncService;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public class UpdateScmExecutable implements Executable {
33  
34  	private static final Logger logger = LoggerFactory.getLogger(UpdateScmExecutable.class);
35  
36  	public static final boolean DEFAULT_SKIP_VALUE = false;
37  	public static final boolean DEFAULT_SKIP_COMMIT_VALUE = true;
38  
39  	boolean skip = DEFAULT_SKIP_VALUE;
40  	// Always skip the commit step, unless they specifically set this to false
41  	boolean skipCommit = DEFAULT_SKIP_COMMIT_VALUE;
42  	ScmService scmService;
43  	SyncService syncService = new DefaultSyncService();
44  	List<DirRequest> requests;
45  	String message = "Automated update";
46  	List<File> commitPaths;
47  
48  	@Override
49  	public void execute() {
50  
51  		if (skip) {
52  			return;
53  		}
54  
55  		Assert.notNull(scmService);
56  		Assert.notNull(syncService);
57  
58  		List<SyncResult> results = syncService.sync(requests);
59  
60  		ScmRequest request = getScmRequest(requests, results, commitPaths);
61  
62  		logger.info("---------- Sync results ----------");
63  		logger.info("Files added - {}", request.getAdds().size());
64  		logger.info("Files updated - {}", request.getUpdates().size());
65  		logger.info("Files deleted - {}", request.getDeletes().size());
66  		logger.info("---------- Sync results ----------");
67  
68  		if (skipCommit) {
69  			logger.info("Skipping SCM commit");
70  		} else {
71  			scmService.add(request.getAdds());
72  			scmService.delete(request.getDeletes());
73  			scmService.commit(request.getCommits(), message);
74  		}
75  	}
76  
77  	protected ScmRequest getScmRequest(List<DirRequest> requests, List<SyncResult> results, List<File> commitPaths) {
78  		List<File> adds = new ArrayList<File>();
79  		List<File> deletes = new ArrayList<File>();
80  		List<File> updates = new ArrayList<File>();
81  
82  		for (SyncResult result : results) {
83  			adds.addAll(result.getAdds());
84  			deletes.addAll(result.getDeletes());
85  			updates.addAll(result.getUpdates());
86  		}
87  
88  		// Add any commit paths they explicitly provided
89  		List<File> commits = new ArrayList<File>(CollectionUtils.toEmptyList(commitPaths));
90  
91  		// Add each target directory as a path to recursively commit
92  		for (DirRequest request : CollectionUtils.toEmptyList(requests)) {
93  			commits.add(request.getTargetDir());
94  		}
95  
96  		ScmRequest scmRequest = new ScmRequest();
97  		scmRequest.setAdds(adds);
98  		scmRequest.setUpdates(updates);
99  		scmRequest.setDeletes(deletes);
100 		scmRequest.setCommits(commits);
101 		return scmRequest;
102 	}
103 
104 	public boolean isSkip() {
105 		return skip;
106 	}
107 
108 	public void setSkip(boolean skip) {
109 		this.skip = skip;
110 	}
111 
112 	public boolean isSkipCommit() {
113 		return skipCommit;
114 	}
115 
116 	public void setSkipCommit(boolean commitChanges) {
117 		this.skipCommit = commitChanges;
118 	}
119 
120 	public ScmService getScmService() {
121 		return scmService;
122 	}
123 
124 	public void setScmService(ScmService scmService) {
125 		this.scmService = scmService;
126 	}
127 
128 	public SyncService getSyncService() {
129 		return syncService;
130 	}
131 
132 	public void setSyncService(SyncService syncService) {
133 		this.syncService = syncService;
134 	}
135 
136 	public List<DirRequest> getRequests() {
137 		return requests;
138 	}
139 
140 	public void setRequests(List<DirRequest> requests) {
141 		this.requests = requests;
142 	}
143 
144 	public String getMessage() {
145 		return message;
146 	}
147 
148 	public void setMessage(String message) {
149 		this.message = message;
150 	}
151 
152 	public List<File> getCommitPaths() {
153 		return commitPaths;
154 	}
155 
156 	public void setCommitPaths(List<File> commitPaths) {
157 		this.commitPaths = commitPaths;
158 	}
159 
160 }