]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/NormalizeDistribution.java
work on file downloads
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / NormalizeDistribution.java
index 967ea770b5537190d968256e026f36611857497f..a01cd7e51f2e8f804cfe2b20e1f18f0a3a348aa5 100644 (file)
 package org.argeo.slc.client.ui.dist.commands;
 
 import javax.jcr.Binary;
+import javax.jcr.Credentials;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
+import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.query.Query;
@@ -31,7 +33,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoMonitor;
 import org.argeo.eclipse.ui.EclipseArgeoMonitor;
-import org.argeo.eclipse.ui.dialogs.SingleValue;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.SlcException;
@@ -41,9 +42,11 @@ import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.jcr.SlcTypes;
 import org.argeo.slc.repo.ArtifactIndexer;
 import org.argeo.slc.repo.JarFileIndexer;
+import org.argeo.slc.repo.RepoConstants;
 import org.argeo.slc.repo.RepoUtils;
 import org.argeo.slc.repo.maven.MavenConventionsUtils;
 import org.argeo.slc.repo.osgi.NormalizeGroup;
+import org.argeo.util.security.Keyring;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
@@ -51,40 +54,80 @@ import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.handlers.HandlerUtil;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
 
-/** Make sure than Maven and OSGi metadata are consistent */
+/** Legacy - Make sure than Maven and OSGi metadata are consistent */
 public class NormalizeDistribution extends AbstractHandler implements SlcNames {
-       public final static String ID = DistPlugin.ID + ".normalizeDistribution";
-       public final static String PARAM_WORKSPACE = "workspace";
-       public final static String DEFAULT_LABEL = "Normalize";
-       public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
-
        private final static Log log = LogFactory
                        .getLog(NormalizeDistribution.class);
 
-       private Repository repository;
-       private String artifactBasePath = "/";
+       public final static String ID = DistPlugin.ID + ".normalizeDistribution";
+       public final static String DEFAULT_LABEL = "Legacy Normalization...";
+       public final static ImageDescriptor DEFAULT_ICON = DistPlugin
+                       .getImageDescriptor("icons/normalize.gif");
+
+       public final static String PARAM_WORKSPACE_NAME = "workspaceName";
+       public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
+
+       private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
 
        private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
        private JarFileIndexer jarFileIndexer = new JarFileIndexer();
 
+       // DEPENDENCY INJECTION
+       private RepositoryFactory repositoryFactory;
+       private Keyring keyring;
+       private Repository nodeRepository;
+
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               String workspace = event.getParameter(PARAM_WORKSPACE);
-               String version = SingleValue.ask("Version",
-                               "Enter Distribution Version");
-               if (version == null)
-                       return null;
 
+               String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
+               String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
+
+               Session nodeSession = null;
                NormalizeJob job;
                try {
-                       job = new NormalizeJob(repository.login(workspace), version);
+
+                       NormalizationDialog dialog = new NormalizationDialog(
+                                       HandlerUtil.getActiveShell(event));
+                       if (dialog.open() != Dialog.OK)
+                               return null;
+
+                       nodeSession = nodeRepository.login();
+                       Node repoNode = nodeSession.getNode(targetRepoPath);
+                       Repository repository = RepoUtils.getRepository(repositoryFactory,
+                                       keyring, repoNode);
+                       Credentials credentials = RepoUtils.getRepositoryCredentials(
+                                       keyring, repoNode);
+
+                       String version = dialog.getVersion();
+                       Boolean overridePoms = dialog.getOverridePoms();
+
+                       job = new NormalizeJob(repository.login(credentials, wkspName),
+                                       version, overridePoms);
+                       job.setUser(true);
+                       job.schedule();
                } catch (RepositoryException e) {
-                       throw new SlcException("Cannot normalize " + workspace, e);
+                       throw new SlcException("Cannot normalize " + wkspName, e);
+               } finally {
+                       JcrUtils.logoutQuietly(nodeSession);
                }
-               job.setUser(true);
-               job.schedule();
                return null;
        }
 
@@ -146,18 +189,17 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
 
        }
 
-       public void setRepository(Repository repository) {
-               this.repository = repository;
-       }
-
        private class NormalizeJob extends Job {
                private Session session;
                private String version;
+               private Boolean overridePoms;
 
-               public NormalizeJob(Session session, String version) {
+               public NormalizeJob(Session session, String version,
+                               Boolean overridePoms) {
                        super("Normalize Distribution");
                        this.session = session;
                        this.version = version;
+                       this.overridePoms = overridePoms;
                }
 
                @Override
@@ -191,10 +233,8 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
                                                + session.getWorkspace().getName(),
                                                (int) groups.getSize());
                                while (groups.hasNext()) {
-                                       NormalizeGroup normalizeGroup = new NormalizeGroup();
-                                       normalizeGroup.setArtifactBasePath(artifactBasePath);
-                                       normalizeGroup.processGroupNode(groups.nextNode(), version,
-                                                       monitor);
+                                       NormalizeGroup.processGroupNode(groups.nextNode(), version,
+                                                       overridePoms, monitor);
                                }
                        } catch (Exception e) {
                                return new Status(IStatus.ERROR, DistPlugin.ID,
@@ -260,4 +300,87 @@ public class NormalizeDistribution extends AbstractHandler implements SlcNames {
                }
 
        }
-}
+
+       public class NormalizationDialog extends TitleAreaDialog {
+               private Text versionT;
+               private String version;
+               private Button overridePomsC;
+               private Boolean overridePoms;
+
+               public NormalizationDialog(Shell parentShell) {
+                       super(parentShell);
+               }
+
+               protected Point getInitialSize() {
+                       return new Point(300, 250);
+               }
+
+               protected Control createDialogArea(Composite parent) {
+                       Composite dialogarea = (Composite) super.createDialogArea(parent);
+                       dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
+                                       true));
+                       Composite composite = new Composite(dialogarea, SWT.NONE);
+                       composite.setLayout(new GridLayout(2, false));
+                       composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
+                                       false));
+                       versionT = createLT(composite, "Version");
+                       overridePomsC = createLC(composite, "Override POMs");
+                       setMessage("Configure normalization", IMessageProvider.NONE);
+
+                       parent.pack();
+                       return composite;
+               }
+
+               @Override
+               protected void okPressed() {
+                       version = versionT.getText();
+                       overridePoms = overridePomsC.getSelection();
+                       super.okPressed();
+               }
+
+               /** Creates label and text. */
+               protected Text createLT(Composite parent, String label) {
+                       new Label(parent, SWT.NONE).setText(label);
+                       Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
+                                       | SWT.NONE);
+                       text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+                       return text;
+               }
+
+               /** Creates label and check. */
+               protected Button createLC(Composite parent, String label) {
+                       new Label(parent, SWT.NONE).setText(label);
+                       Button check = new Button(parent, SWT.CHECK);
+                       check.setSelection(false);
+                       check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+                       return check;
+               }
+
+               protected void configureShell(Shell shell) {
+                       super.configureShell(shell);
+                       shell.setText("Normalize...");
+               }
+
+               public String getVersion() {
+                       return version;
+               }
+
+               public Boolean getOverridePoms() {
+                       return overridePoms;
+               }
+
+       }
+
+       /* DEPENDENCY INJECTION */
+       public void setNodeRepository(Repository nodeRepository) {
+               this.nodeRepository = nodeRepository;
+       }
+
+       public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
+               this.repositoryFactory = repositoryFactory;
+       }
+
+       public void setKeyring(Keyring keyring) {
+               this.keyring = keyring;
+       }
+}
\ No newline at end of file