]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/wizards/GenerateBinariesWizard.java
3d7df051d28419464b671c92dea97abfaca2a72f
[gpl/argeo-slc.git] / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / wizards / GenerateBinariesWizard.java
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.ArgeoMonitor;
29 import org.argeo.eclipse.ui.EclipseArgeoMonitor;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.client.ui.dist.DistPlugin;
33 import org.argeo.slc.client.ui.dist.PrivilegedJob;
34 import org.argeo.slc.client.ui.dist.utils.ViewerUtils;
35 import org.argeo.slc.jcr.SlcTypes;
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.core.runtime.IProgressMonitor;
41 import org.eclipse.core.runtime.IStatus;
42 import org.eclipse.core.runtime.Status;
43 import org.eclipse.jface.dialogs.IMessageProvider;
44 import org.eclipse.jface.dialogs.MessageDialog;
45 import org.eclipse.jface.viewers.ColumnLabelProvider;
46 import org.eclipse.jface.viewers.IStructuredContentProvider;
47 import org.eclipse.jface.viewers.TableViewer;
48 import org.eclipse.jface.viewers.TableViewerColumn;
49 import org.eclipse.jface.viewers.Viewer;
50 import org.eclipse.jface.viewers.ViewerComparator;
51 import org.eclipse.jface.wizard.IWizardPage;
52 import org.eclipse.jface.wizard.Wizard;
53 import org.eclipse.jface.wizard.WizardPage;
54 import org.eclipse.swt.SWT;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.layout.GridLayout;
57 import org.eclipse.swt.widgets.Button;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Label;
60 import org.eclipse.swt.widgets.Table;
61 import org.eclipse.swt.widgets.Text;
62 import org.eclipse.aether.artifact.Artifact;
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
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 @Override
240 public String getText(Object element) {
241 return ((Artifact) element).getArtifactId();
242 }
243 });
244
245 column = ViewerUtils.createTableViewerColumn(recapViewer,
246 "Version", SWT.NONE, 250);
247 column.setLabelProvider(new ColumnLabelProvider() {
248 @Override
249 public String getText(Object element) {
250 return ((Artifact) element).getBaseVersion();
251 }
252 });
253
254 recapViewer.setContentProvider(new IStructuredContentProvider() {
255 List<Artifact> artifacts;
256
257 @SuppressWarnings("unchecked")
258 public void inputChanged(Viewer viewer, Object oldInput,
259 Object newInput) {
260 artifacts = (List<Artifact>) newInput;
261 if (artifacts != null)
262 recapViewer.refresh();
263 }
264
265 public void dispose() {
266 }
267
268 public Object[] getElements(Object inputElement) {
269 return artifacts == null ? null : artifacts.toArray();
270 }
271 });
272
273 // A basic comparator
274 recapViewer.setComparator(new ViewerComparator());
275 refreshValues();
276 setControl(composite);
277 }
278 }
279
280 /**
281 * Define the privileged job that will be run asynchronously generate
282 * corresponding artifacts
283 */
284 private class GenerateBinaryJob extends PrivilegedJob {
285
286 private final RepoService repoService;
287 private final String repoNodePath;
288 private final String wkspName;
289 private final String groupNodePath;
290 private final String version;
291
292 public GenerateBinaryJob(RepoService repoService, String repoNodePath,
293 String wkspName, String groupNodePath, String version) {
294 super("Fetch");
295 this.version = version;
296 this.repoService = repoService;
297 this.repoNodePath = repoNodePath;
298 this.wkspName = wkspName;
299 this.groupNodePath = groupNodePath;
300 }
301
302 @Override
303 protected IStatus doRun(IProgressMonitor progressMonitor) {
304 Session session = null;
305 try {
306 ArgeoMonitor monitor = new EclipseArgeoMonitor(progressMonitor);
307 session = repoService.getRemoteSession(repoNodePath, null,
308 wkspName);
309 Node groupBaseNode = session.getNode(groupNodePath);
310 GenerateBinaries.processGroupNode(groupBaseNode, version,
311 monitor);
312 } catch (Exception e) {
313 if (log.isDebugEnabled())
314 e.printStackTrace();
315 return new Status(IStatus.ERROR, DistPlugin.ID,
316 "Cannot normalize group", e);
317 } finally {
318 JcrUtils.logoutQuietly(session);
319 }
320 return Status.OK_STATUS;
321 }
322 }
323
324 // ////////////////////////////
325 // // Helpers
326 /** Creates label and text. */
327 protected Text createLT(Composite parent, String label) {
328 new Label(parent, SWT.NONE).setText(label);
329 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
330 | SWT.NONE);
331 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
332 return text;
333 }
334
335 /** Creates label and check. */
336 protected Button createLC(Composite parent, String label) {
337 new Label(parent, SWT.NONE).setText(label);
338 Button check = new Button(parent, SWT.CHECK);
339 check.setSelection(false);
340 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
341 return check;
342 }
343 }