]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessBuilderPage.java
Don't switch to log
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / editors / ProcessBuilderPage.java
index 8b85260557247e94b03ea71251c0111151f15549..28c2f69c611a6bd485256a70f055f1759a5cc287 100644 (file)
@@ -3,18 +3,19 @@ package org.argeo.slc.client.ui.editors;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.UUID;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.observation.Event;
-import javax.jcr.observation.EventIterator;
 import javax.jcr.observation.EventListener;
 import javax.jcr.observation.ObservationManager;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
 
 import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.jcr.AsyncUiEventListener;
@@ -60,15 +61,14 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.forms.AbstractFormPart;
 import org.eclipse.ui.forms.IManagedForm;
 import org.eclipse.ui.forms.editor.FormPage;
 import org.eclipse.ui.forms.widgets.FormToolkit;
 import org.eclipse.ui.forms.widgets.ScrolledForm;
 
-public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
+/** Definition of the process. */
+public class ProcessBuilderPage extends FormPage implements SlcNames {
        public final static String ID = "processBuilderPage";
        // private final static Log log =
        // LogFactory.getLog(ProcessBuilderPage.class);
@@ -110,8 +110,8 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                        getManagedForm().addPart(formPart);
 
                        // observation
-                       statusObserver = new AsyncUiEventListener() {
-                               protected void onEventInUiThread(EventIterator events) {
+                       statusObserver = new AsyncUiEventListener(form.getDisplay()) {
+                               protected void onEventInUiThread(List<Event> events) {
                                        statusChanged();
                                }
                        };
@@ -121,6 +121,9 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                                        Event.PROPERTY_CHANGED, processNode.getPath(), true, null,
                                        null, false);
 
+                       // make sure all controls are in line with status
+                       statusChanged();
+
                        // add initial flows
                        addInitialFlows();
 
@@ -142,7 +145,9 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                run.addSelectionListener(new SelectionListener() {
                        public void widgetSelected(SelectionEvent e) {
                                if (isFinished(getProcessStatus())) {
-                                       relaunch();
+                                       ((ProcessEditor) getEditor()).relaunch();
+                               } else if (isRunning(getProcessStatus())) {
+                                       ((ProcessEditor) getEditor()).kill();
                                } else {
                                        ((ProcessEditor) getEditor()).process();
                                }
@@ -187,8 +192,6 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                statusComposite.setLayout(new FillLayout());
                statusLabel = tk.createLabel(statusComposite, getProcessStatus());
 
-               // make sure all controls are in line with status
-               statusChanged();
        }
 
        protected void createBuilder(Composite parent) {
@@ -275,58 +278,27 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
        /*
         * CONTROLLERS
         */
-       /** Opens a new editor with a copy of this process */
-       protected void relaunch() {
-               try {
-                       Node duplicatedNode = duplicateProcess();
-                       IWorkbenchPage activePage = PlatformUI.getWorkbench()
-                                       .getActiveWorkbenchWindow().getActivePage();
-                       activePage.openEditor(
-                                       new ProcessEditorInput(duplicatedNode.getPath()),
-                                       ProcessEditor.ID);
-                       getEditor().close(false);
-               } catch (Exception e1) {
-                       throw new SlcException("Cannot relaunch " + processNode, e1);
-               }
-       }
-
-       /** Duplicates the process */
-       protected Node duplicateProcess() {
-               try {
-                       Session session = processNode.getSession();
-                       String uuid = UUID.randomUUID().toString();
-                       String destPath = SlcJcrUtils.createExecutionProcessPath(uuid);
-                       Node newNode = JcrUtils.mkdirs(session, destPath, SLC_PROCESS);
-                       JcrUtils.copy(processNode, newNode);
-                       // session.getWorkspace().copy(processNode.getPath(), destPath);
-                       // Node newNode = session.getNode(destPath);
-                       // make sure that we kept the mixins
-                       // newNode.addMixin(NodeType.MIX_CREATED);
-                       // newNode.addMixin(NodeType.MIX_LAST_MODIFIED);
-                       newNode.setProperty(SLC_UUID, uuid);
-                       newNode.setProperty(SLC_STATUS, ExecutionProcess.INITIALIZED);
-                       session.save();
-                       return newNode;
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot duplicate process", e);
-               }
-       }
-
        /** Reflects a status change */
        protected void statusChanged() {
                String status = getProcessStatus();
                statusLabel.setText(status);
                Boolean isEditable = isEditable(status);
-               run.setEnabled(isEditable);
+               run.setEnabled(status.equals(ExecutionProcess.RUNNING) || isEditable);
                remove.setEnabled(isEditable);
                clear.setEnabled(isEditable);
                // flowsViewer.getTree().setEnabled(isEditable);
-               if (status.equals(ExecutionProcess.COMPLETED)
-                               || status.equals(ExecutionProcess.ERROR)) {
+               if (status.equals(ExecutionProcess.RUNNING)) {
+                       run.setEnabled(true);
+                       run.setImage(SlcImages.KILL);
+                       run.setToolTipText("Kill");
+               } else if (isFinished(status)) {
                        run.setEnabled(true);
                        run.setImage(SlcImages.RELAUNCH);
                        run.setToolTipText("Relaunch");
                }
+
+               if (flowsViewer != null)
+                       flowsViewer.refresh();
        }
 
        /** Adds initial flows from the editor input if any */
@@ -343,32 +315,46 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
         * @param path
         *            the path of the flow
         */
-       public void addFlow(String path) {
+       protected void addFlow(String path) {
                try {
                        Node flowNode = processNode.getSession().getNode(path);
                        Node realizedFlowNode = processNode.getNode(SLC_FLOW).addNode(
                                        SLC_FLOW);
-                       realizedFlowNode.addMixin(SLC_REALIZED_FLOW);
+                       realizedFlowNode.addMixin(SlcTypes.SLC_REALIZED_FLOW);
                        Node address = realizedFlowNode.addNode(SLC_ADDRESS,
                                        NodeType.NT_ADDRESS);
                        address.setProperty(Property.JCR_PATH, path);
 
                        // copy spec attributes
                        Node specAttrsBase;
-                       if (flowNode.hasProperty(SLC_SPEC))
-                               specAttrsBase = flowNode.getProperty(SLC_SPEC).getNode();
-                       else
+                       if (flowNode.hasProperty(SLC_SPEC)) {
+                               Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
+                                               .getNode();
+                               specAttrsBase = executionSpecNode;
+                               String executionSpecName = executionSpecNode.getProperty(
+                                               SLC_NAME).getString();
+                               realizedFlowNode.setProperty(SLC_SPEC, executionSpecName);
+                       } else
                                specAttrsBase = flowNode;
 
                        specAttrs: for (NodeIterator nit = specAttrsBase.getNodes(); nit
                                        .hasNext();) {
                                Node specAttrNode = nit.nextNode();
+                               String attrName = specAttrNode.getName();
                                if (!specAttrNode
                                                .isNodeType(SlcTypes.SLC_EXECUTION_SPEC_ATTRIBUTE))
                                        continue specAttrs;
                                Node realizedAttrNode = realizedFlowNode.addNode(specAttrNode
                                                .getName());
                                JcrUtils.copy(specAttrNode, realizedAttrNode);
+
+                               // ovveride with flow value
+                               if (flowNode.hasNode(attrName)) {
+                                       // assuming this is a primitive
+                                       realizedAttrNode.setProperty(SLC_VALUE,
+                                                       flowNode.getNode(attrName).getProperty(SLC_VALUE)
+                                                                       .getValue());
+                               }
                        }
 
                        flowsViewer.refresh();
@@ -429,14 +415,19 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
        }
 
        /** Optimization so that we don't call the node each time */
-       protected Boolean isEditable(String status) {
+       protected static Boolean isEditable(String status) {
                return status.equals(ExecutionProcess.NEW)
                                || status.equals(ExecutionProcess.INITIALIZED);
        }
 
-       protected Boolean isFinished(String status) {
+       protected static Boolean isFinished(String status) {
                return status.equals(ExecutionProcess.COMPLETED)
-                               || status.equals(ExecutionProcess.ERROR);
+                               || status.equals(ExecutionProcess.ERROR)
+                               || status.equals(ExecutionProcess.KILLED);
+       }
+
+       protected static Boolean isRunning(String status) {
+               return status.equals(ExecutionProcess.RUNNING);
        }
 
        /*
@@ -460,6 +451,8 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                                // TODO optimize based on data type?
                                Object value = PrimitiveUtils.convert(type, specAttrNode
                                                .getProperty(SLC_VALUE).getString());
+                               // log.debug(specAttrNode + ", type=" + type + ", value=" +
+                               // value);
                                return value;
                        }
                        return null;
@@ -516,7 +509,7 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                public String getText(Object element) {
                        Node node = (Node) element;
                        try {
-                               if (node.isNodeType(SLC_REALIZED_FLOW)) {
+                               if (node.isNodeType(SlcTypes.SLC_REALIZED_FLOW)) {
                                        if (node.hasNode(SLC_ADDRESS)) {
                                                String path = node.getNode(SLC_ADDRESS)
                                                                .getProperty(Property.JCR_PATH).getString();
@@ -534,7 +527,19 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                public Image getImage(Object element) {
                        Node node = (Node) element;
                        try {
-                               if (node.isNodeType(SLC_REALIZED_FLOW)) {
+                               if (node.isNodeType(SlcTypes.SLC_REALIZED_FLOW)) {
+                                       if (node.hasProperty(SLC_STATUS)) {
+                                               String status = node.getProperty(SLC_STATUS)
+                                                               .getString();
+                                               // TODO: factorize with process view ?
+                                               if (status.equals(ExecutionProcess.RUNNING))
+                                                       return SlcImages.PROCESS_RUNNING;
+                                               else if (status.equals(ExecutionProcess.ERROR)
+                                                               || status.equals(ExecutionProcess.KILLED))
+                                                       return SlcImages.PROCESS_ERROR;
+                                               else if (status.equals(ExecutionProcess.COMPLETED))
+                                                       return SlcImages.PROCESS_COMPLETED;
+                                       }
                                        return SlcImages.FLOW;
                                }
                        } catch (RepositoryException e) {
@@ -558,6 +563,7 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                }
        }
 
+       /** Manages drop event. */
        class FlowsDropListener extends ViewerDropAdapter {
 
                public FlowsDropListener(Viewer viewer) {
@@ -567,8 +573,31 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                @Override
                public boolean performDrop(Object data) {
                        String path = data.toString();
-                       addFlow(path);
-                       return true;
+                       try {
+                               // either a node or a whole directory was dragged
+                               QueryManager qm = processNode.getSession().getWorkspace()
+                                               .getQueryManager();
+                               String statement = "SELECT * FROM ["
+                                               + SlcTypes.SLC_EXECUTION_FLOW
+                                               + "] WHERE ISDESCENDANTNODE(['" + path
+                                               + "']) OR ISSAMENODE(['" + path + "'])";
+                               // log.debug(statement);
+                               Query query = qm.createQuery(statement, Query.JCR_SQL2);
+
+                               // order paths
+                               SortedSet<String> paths = new TreeSet<String>();
+                               for (NodeIterator nit = query.execute().getNodes(); nit
+                                               .hasNext();) {
+                                       paths.add(nit.nextNode().getPath());
+                               }
+
+                               for (String p : paths) {
+                                       addFlow(p);
+                               }
+                               return true;
+                       } catch (RepositoryException e) {
+                               throw new SlcException("Cannot query flows under " + path, e);
+                       }
                }
 
                @Override
@@ -611,7 +640,7 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                }
        }
 
-       static class ValuesEditingSupport extends EditingSupport {
+       class ValuesEditingSupport extends EditingSupport {
                private final TableViewer tableViewer;
 
                public ValuesEditingSupport(ColumnViewer viewer) {
@@ -638,7 +667,9 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                                Node specAttrNode = (Node) element;
                                return !(specAttrNode.getProperty(SLC_IS_IMMUTABLE)
                                                .getBoolean() || specAttrNode.getProperty(
-                                               SLC_IS_CONSTANT).getBoolean());
+                                               SLC_IS_CONSTANT).getBoolean())
+                                               && specAttrNode
+                                                               .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE);
                        } catch (RepositoryException e) {
                                throw new SlcException("Cannot check canEdit", e);
                        }
@@ -646,7 +677,18 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
 
                @Override
                protected Object getValue(Object element) {
-                       return getAttributeSpecValue((Node) element);
+                       Node specAttrNode = (Node) element;
+                       try {
+                               Object value = getAttributeSpecValue(specAttrNode);
+                               if (value == null)
+                                       throw new SlcException("Unsupported attribute " + element);
+                               if (specAttrNode
+                                               .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE))
+                                       return value.toString();
+                               return value;
+                       } catch (RepositoryException e) {
+                               throw new SlcException("Cannot get value for " + element, e);
+                       }
                }
 
                @Override
@@ -659,6 +701,8 @@ public class ProcessBuilderPage extends FormPage implements SlcNames, SlcTypes {
                                                        .getString();
                                        SlcJcrUtils.setPrimitiveAsProperty(specAttrNode, SLC_VALUE,
                                                        type, value);
+                                       valuesViewer.refresh();
+                                       formPart.markDirty();
                                }
                        } catch (RepositoryException e) {
                                throw new SlcException("Cannot get celle editor", e);