View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.argeo.slc.client.ui.dist.wizards;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.NodeIterator;
23  import javax.jcr.RepositoryException;
24  import javax.jcr.Session;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.argeo.cms.ui.workbench.util.PrivilegedJob;
29  import org.argeo.eclipse.ui.EclipseJcrMonitor;
30  import org.argeo.jcr.JcrMonitor;
31  import org.argeo.jcr.JcrUtils;
32  import org.argeo.slc.SlcException;
33  import org.argeo.slc.SlcTypes;
34  import org.argeo.slc.client.ui.dist.DistPlugin;
35  import org.argeo.slc.client.ui.dist.utils.ViewerUtils;
36  import org.argeo.slc.repo.RepoConstants;
37  import org.argeo.slc.repo.RepoService;
38  import org.argeo.slc.repo.RepoUtils;
39  import org.argeo.slc.repo.maven.GenerateBinaries;
40  import org.eclipse.aether.artifact.Artifact;
41  import org.eclipse.core.runtime.IProgressMonitor;
42  import org.eclipse.core.runtime.IStatus;
43  import org.eclipse.core.runtime.Status;
44  import org.eclipse.jface.dialogs.IMessageProvider;
45  import org.eclipse.jface.dialogs.MessageDialog;
46  import org.eclipse.jface.viewers.ColumnLabelProvider;
47  import org.eclipse.jface.viewers.IStructuredContentProvider;
48  import org.eclipse.jface.viewers.TableViewer;
49  import org.eclipse.jface.viewers.TableViewerColumn;
50  import org.eclipse.jface.viewers.Viewer;
51  import org.eclipse.jface.viewers.ViewerComparator;
52  import org.eclipse.jface.wizard.IWizardPage;
53  import org.eclipse.jface.wizard.Wizard;
54  import org.eclipse.jface.wizard.WizardPage;
55  import org.eclipse.swt.SWT;
56  import org.eclipse.swt.layout.GridData;
57  import org.eclipse.swt.layout.GridLayout;
58  import org.eclipse.swt.widgets.Button;
59  import org.eclipse.swt.widgets.Composite;
60  import org.eclipse.swt.widgets.Label;
61  import org.eclipse.swt.widgets.Table;
62  import org.eclipse.swt.widgets.Text;
63  
64  /**
65   * Define parameters to asynchronously generate binaries, sources and sdk pom
66   * artifacts for this group using a {@link GenerateBinaries} runnable
67   */
68  public class GenerateBinariesWizard extends Wizard {
69  	private final static Log log = LogFactory
70  			.getLog(GenerateBinariesWizard.class);
71  
72  	// Business objects
73  	private final RepoService repoService;
74  	private final String repoNodePath;
75  	private String wkspName;
76  	private String groupNodePath;
77  
78  	// The pages
79  	private RecapPage recapPage;
80  
81  	// Controls with parameters
82  	private Text versionTxt;
83  	private Text latestVersionTxt;
84  	private Text highestArtifactVersionTxt;
85  
86  	public GenerateBinariesWizard(RepoService repoService, String repoNodePath,
87  			String wkspName, String groupNodePath) {
88  		super();
89  		this.repoService = repoService;
90  		this.repoNodePath = repoNodePath;
91  		this.wkspName = wkspName;
92  		this.groupNodePath = groupNodePath;
93  	}
94  
95  	@Override
96  	public void dispose() {
97  		super.dispose();
98  	}
99  
100 	@Override
101 	public void addPages() {
102 		try {
103 			recapPage = new RecapPage();
104 			addPage(recapPage);
105 			setWindowTitle("Define Binary Generation Procedure");
106 		} catch (Exception e) {
107 			throw new SlcException("Cannot add page to wizard ", e);
108 		}
109 	}
110 
111 	@Override
112 	public boolean performFinish() {
113 		if (!canFinish())
114 			return false;
115 		try {
116 			String msg = "Your are about to generate binaries, sources and sdk "
117 					+ "pom artifacts for this group, "
118 					+ "do you really want to proceed ?";
119 
120 			boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
121 					.getWorkbench().getDisplay().getActiveShell(),
122 					"Confirm Launch", msg);
123 
124 			if (result) {
125 				GenerateBinaryJob job = new GenerateBinaryJob(repoService,
126 						repoNodePath, wkspName, groupNodePath,
127 						versionTxt.getText());
128 				job.setUser(true);
129 				job.schedule();
130 			}
131 		} catch (Exception e) {
132 			throw new SlcException(
133 					"Unexpected error while launching the fetch", e);
134 		}
135 		return true;
136 	}
137 
138 	// ///////////////////////////////
139 	// ////// THE PAGES
140 	private class RecapPage extends WizardPage {
141 		private static final long serialVersionUID = 904196417910874087L;
142 		private TableViewer recapViewer;
143 
144 		public RecapPage() {
145 			super("Define parameters and launch");
146 			setTitle("Define parameters and launch");
147 		}
148 
149 		@Override
150 		public boolean isPageComplete() {
151 			return isCurrentPage();
152 		}
153 
154 		public IWizardPage getNextPage() {
155 			return null; // always last
156 		}
157 
158 		private void refreshValues() {
159 			Session session = null;
160 			try {
161 				session = repoService.getRemoteSession(repoNodePath, null,
162 						wkspName);
163 				Node groupNode = session.getNode(groupNodePath);
164 				GenerateBinaries gb = GenerateBinaries.preProcessGroupNode(
165 						groupNode, null);
166 
167 				List<Artifact> binaries = new ArrayList<Artifact>();
168 				binaries.addAll(gb.getBinaries());
169 
170 				Artifact highestVersion = gb.getHighestArtifactVersion();
171 				if (highestVersion != null)
172 					highestArtifactVersionTxt.setText(highestVersion
173 							.getBaseVersion());
174 
175 				if (groupNode.hasNode(RepoConstants.BINARIES_ARTIFACT_ID)) {
176 					Node binaryNode = groupNode
177 							.getNode(RepoConstants.BINARIES_ARTIFACT_ID);
178 					Artifact currHighestVersion = null;
179 					for (NodeIterator ni = binaryNode.getNodes(); ni.hasNext();) {
180 						Node currN = ni.nextNode();
181 						if (currN
182 								.isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
183 							Artifact currVersion = RepoUtils.asArtifact(currN);
184 
185 							if (currHighestVersion == null
186 									|| currVersion.getBaseVersion()
187 											.compareTo(
188 													currHighestVersion
189 															.getBaseVersion()) > 0)
190 								currHighestVersion = currVersion;
191 						}
192 					}
193 					if (currHighestVersion != null)
194 						latestVersionTxt.setText(currHighestVersion
195 								.getBaseVersion());
196 				}
197 				recapViewer.setInput(binaries);
198 				recapViewer.refresh();
199 			} catch (RepositoryException re) {
200 				throw new SlcException("Unable to get repositories URIs", re);
201 			} finally {
202 				JcrUtils.logoutQuietly(session);
203 			}
204 		}
205 
206 		public void createControl(Composite parent) {
207 			setMessage("Configure Maven Indexing", IMessageProvider.NONE);
208 
209 			Composite composite = new Composite(parent, SWT.NO_FOCUS);
210 			composite.setLayout(new GridLayout(2, false));
211 
212 			versionTxt = createLT(composite, "Version");
213 			versionTxt
214 					.setToolTipText("Enter a version for the new Modular Distribution");
215 
216 			latestVersionTxt = createLT(composite, "Latest version");
217 			latestVersionTxt.setEditable(false);
218 			latestVersionTxt
219 					.setToolTipText("The actual latest version of this modular distribution");
220 
221 			highestArtifactVersionTxt = createLT(composite,
222 					"Highest version in current category");
223 			highestArtifactVersionTxt.setEditable(false);
224 			highestArtifactVersionTxt
225 					.setToolTipText("The highest version among all version of the below listed modules.");
226 
227 			// Creates the table
228 			Table table = new Table(composite, SWT.H_SCROLL | SWT.V_SCROLL
229 					| SWT.BORDER);
230 			table.setLinesVisible(true);
231 			table.setHeaderVisible(true);
232 			table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2,
233 					1));
234 			recapViewer = new TableViewer(table);
235 
236 			TableViewerColumn column = ViewerUtils.createTableViewerColumn(
237 					recapViewer, "Name", SWT.NONE, 250);
238 			column.setLabelProvider(new ColumnLabelProvider() {
239 				private static final long serialVersionUID = -9145709097621022043L;
240 
241 				@Override
242 				public String getText(Object element) {
243 					return ((Artifact) element).getArtifactId();
244 				}
245 			});
246 
247 			column = ViewerUtils.createTableViewerColumn(recapViewer,
248 					"Version", SWT.NONE, 250);
249 			column.setLabelProvider(new ColumnLabelProvider() {
250 				private static final long serialVersionUID = 5524185741667651628L;
251 
252 				@Override
253 				public String getText(Object element) {
254 					return ((Artifact) element).getBaseVersion();
255 				}
256 			});
257 
258 			recapViewer.setContentProvider(new IStructuredContentProvider() {
259 				private static final long serialVersionUID = -4579434453554442858L;
260 
261 				
262 				List<Artifact> artifacts;
263 
264 				@SuppressWarnings("unchecked")
265 				public void inputChanged(Viewer viewer, Object oldInput,
266 						Object newInput) {
267 					artifacts = (List<Artifact>) newInput;
268 					if (artifacts != null)
269 						recapViewer.refresh();
270 				}
271 
272 				public void dispose() {
273 				}
274 
275 				public Object[] getElements(Object inputElement) {
276 					return artifacts == null ? null : artifacts.toArray();
277 				}
278 			});
279 
280 			// A basic comparator
281 			recapViewer.setComparator(new ViewerComparator());
282 			refreshValues();
283 			setControl(composite);
284 		}
285 	}
286 
287 	/**
288 	 * Define the privileged job that will be run asynchronously generate
289 	 * corresponding artifacts
290 	 */
291 	private class GenerateBinaryJob extends PrivilegedJob {
292 
293 		private final RepoService repoService;
294 		private final String repoNodePath;
295 		private final String wkspName;
296 		private final String groupNodePath;
297 		private final String version;
298 
299 		public GenerateBinaryJob(RepoService repoService, String repoNodePath,
300 				String wkspName, String groupNodePath, String version) {
301 			super("Fetch");
302 			this.version = version;
303 			this.repoService = repoService;
304 			this.repoNodePath = repoNodePath;
305 			this.wkspName = wkspName;
306 			this.groupNodePath = groupNodePath;
307 		}
308 
309 		@Override
310 		protected IStatus doRun(IProgressMonitor progressMonitor) {
311 			Session session = null;
312 			try {
313 				JcrMonitor monitor = new EclipseJcrMonitor(progressMonitor);
314 				session = repoService.getRemoteSession(repoNodePath, null,
315 						wkspName);
316 				Node groupBaseNode = session.getNode(groupNodePath);
317 				GenerateBinaries.processGroupNode(groupBaseNode, version,
318 						monitor);
319 			} catch (Exception e) {
320 				if (log.isDebugEnabled())
321 					e.printStackTrace();
322 				return new Status(IStatus.ERROR, DistPlugin.PLUGIN_ID,
323 						"Cannot normalize group", e);
324 			} finally {
325 				JcrUtils.logoutQuietly(session);
326 			}
327 			return Status.OK_STATUS;
328 		}
329 	}
330 
331 	// ////////////////////////////
332 	// // Helpers
333 	/** Creates label and text. */
334 	protected Text createLT(Composite parent, String label) {
335 		new Label(parent, SWT.NONE).setText(label);
336 		Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
337 				| SWT.NONE);
338 		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
339 		return text;
340 	}
341 
342 	/** Creates label and check. */
343 	protected Button createLC(Composite parent, String label) {
344 		new Label(parent, SWT.NONE).setText(label);
345 		Button check = new Button(parent, SWT.CHECK);
346 		check.setSelection(false);
347 		check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
348 		return check;
349 	}
350 }