ref #6774 session handling and minor refactoring
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureTreeEditor.java
index a5d141580dd734926592e5155c232576dd213ff5..206c69e65dceb781f00d4b3557b0f8146ab3e173 100644 (file)
@@ -9,9 +9,15 @@
 
 package eu.etaxonomy.taxeditor.featuretree.e4;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Named;
 
@@ -37,11 +43,13 @@ import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
 import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.FeatureNode;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
 
@@ -51,11 +59,13 @@ import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
  * @date 06.06.2017
  *
  */
-public class FeatureTreeEditor implements
+public class FeatureTreeEditor implements ICdmEntitySessionEnabled,
                ModifyListener, ISelectionChangedListener {
 
     private ConversationHolder conversation;
 
+    private ICdmEntitySession cdmEntitySession;
+
     @Inject
     private ESelectionService selService;
 
@@ -64,55 +74,56 @@ public class FeatureTreeEditor implements
 
     private Shell shell;
 
-    private FeatureTree featureTree;
-
     private FeatureTreeEditorComposite composite;
 
     @Inject
-    public FeatureTreeEditor(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
-        this.shell = shell;
-        if(conversation==null){
-            conversation = CdmStore.createConversation();
-        }
+    public FeatureTreeEditor() {
     }
 
        /** {@inheritDoc} */
-       @PostConstruct
-       public void createControl(Composite parent, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
-           composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
-               composite.init(new FeatureNodeDragListener(composite.getViewer()), new FeatureNodeDropAdapter(this, composite.getViewer()), this, new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
-                if(tree!=null){
-                    setSelectedTree(tree);
-                }
+    @PostConstruct
+    public void createControl(Composite parent) {
+        if (CdmStore.isActive()){
+            if(conversation == null){
+                conversation = CdmStore.createConversation();
             }
-        }, new AddButtonListener(), new RemoveSelectionListener());
-       }
+            if(cdmEntitySession!=null){
+                cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
+            }
+        }
+        else{
+            return;
+        }
+        composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
+        composite.init(new FeatureNodeDragListener(composite.getViewer()),
+                new FeatureNodeDropAdapter(this, composite.getViewer()), this, new SelectionAdapter() {
+                    @Override
+                    public void widgetSelected(SelectionEvent e) {
+                        FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
+                        if (tree != null) {
+                            composite.setSelectedTree(tree, FeatureTreeEditor.this);
+                        }
+                    }
+                }, new AddButtonListener(), new RemoveSelectionListener(), new FeatureTreeExportListener(shell, composite));
+        composite.getText_title().setEnabled(false);
+    }
 
        public void setDirty(boolean isDirty){
            this.dirty.setDirty(isDirty);
        }
 
-    public void setSelectedTree(FeatureTree featureTree) {
-               this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
-               this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
-               composite.getViewer().setInput(featureTree);
-
-               composite.getText_title().removeModifyListener(this);
-               composite.getText_title().setText(featureTree.getTitleCache());
-               composite.getText_title().addModifyListener(this);
+       public boolean isDirty(){
+           return dirty.isDirty();
        }
 
     public FeatureTree getSelectedFeatureTree(){
-        return this.featureTree;
+        return composite.getFeatureTree();
     }
 
        /** {@inheritDoc} */
        @Override
        public void modifyText(ModifyEvent e) {
-               featureTree.setTitleCache(composite.getText_title().getText(), true);
+           composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
                setDirty(true);
        }
 
@@ -122,58 +133,97 @@ public class FeatureTreeEditor implements
                IStructuredSelection selection = (IStructuredSelection) event
                                .getSelection();
 
-               composite.getButton_add().setEnabled(selection.size() <= 1);
-               composite.getButton_remove().setEnabled(selection.size() > 0);
+               composite.getBtnAdd().setEnabled(selection.size() <= 1);
+               composite.getBtnRemove().setEnabled(selection.size() > 0);
                //propagate selection
-               IStructuredSelection isel = (IStructuredSelection) event.getSelection();
-               selService.setSelection((isel.size() == 1 ? isel.getFirstElement() : isel.toArray()));
+               selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
        }
 
        @Focus
-       public void focus(){
-           composite.getViewer().getControl().setFocus();
+       public void focus(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell){
+           this.shell = shell;
+           if(composite!=null){
+               composite.getViewer().getControl().setFocus();
+           }
         if(conversation!=null && !conversation.isBound()){
             conversation.bind();
         }
+        if(cdmEntitySession != null) {
+            cdmEntitySession.bind();
+        }
        }
 
        @Persist
        public void save(){
-           CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
-           setDirty(false);
+        if (!conversation.isBound()) {
+            conversation.bind();
+        }
+
+        // commit the conversation and start a new transaction immediately
+        conversation.commit(true);
+
+        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
+
+        this.setDirty(false);
        }
 
-       /**
-     * @param additionalFeatures
-     */
-    public void addFeatures(Collection<Feature> additionalFeatures) {
-        IStructuredSelection selection = (IStructuredSelection) composite.getViewer().getSelection();
-        FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection.getFirstElement()
-                : ((FeatureTree) composite.getViewer().getInput()).getRoot());
-        for (Feature feature : additionalFeatures) {
-            FeatureNode child = FeatureNode.NewInstance(feature);
-            //TODO session handling
-//            CdmStore.getService(IFeatureNodeService.class).merge(child, true);
-
-            parent.addChild(child);
+       @PreDestroy
+       public void dispose(){
+        if(conversation!=null){
+            conversation.close();
         }
-        //TODO session handling
-//        setDirty(true);
-        composite.getViewer().refresh();
+        if(cdmEntitySession != null) {
+            cdmEntitySession.dispose();
+        }
+       }
+
+    @Override
+    public ICdmEntitySession getCdmEntitySession() {
+        return cdmEntitySession;
+    }
+
+    @Override
+    public Map<Object, List<String>> getPropertyPathsMap() {
+        List<String> propertyPaths = Arrays.asList(new String[] {
+                "children", //$NON-NLS-1$
+                "feature", //$NON-NLS-1$
+                "featureTree", //$NON-NLS-1$
+        });
+        Map<Object, List<String>> propertyPathMap =
+                new HashMap<Object, List<String>>();
+        propertyPathMap.put(FeatureNode.class,propertyPaths);
+        return propertyPathMap;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public List<FeatureTree> getRootEntities() {
+        List<FeatureTree> root = new ArrayList<>();
+        root.add(composite.getFeatureTree());
+        return root;
     }
 
        private class AddButtonListener extends SelectionAdapter {
                @Override
                public void widgetSelected(SelectionEvent e) {
                        AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
-                                       featureTree);
+                               composite.getFeatureTree());
                        WizardDialog dialog = new WizardDialog(shell, wizard);
 
-            if (dialog.open() == IStatus.OK) {
+                       if (dialog.open() == IStatus.OK) {
+                FeatureNode parent = ((FeatureTree) composite.getViewer().getInput()).getRoot();
                 Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
-                addFeatures(additionalFeatures);
-            }
-        }
+                for (Feature feature : additionalFeatures) {
+                                   CdmStore.getService(IFeatureNodeService.class).addChildFeaturNode(parent, feature);
+                               }
+                               setDirty(true);
+                               composite.getViewer().refresh();
+                               composite.getViewer().expandToLevel(parent, 1);
+                       }
+               }
+
        }
 
        private class RemoveSelectionListener extends SelectionAdapter {
@@ -184,9 +234,6 @@ public class FeatureTreeEditor implements
 
                        for (Object selectedObject : selection.toArray()) {
                                FeatureNode featureNode = (FeatureNode) selectedObject;
-                               FeatureNode parent = featureNode.getParent();
-                               parent.removeChild(featureNode);
-
                                CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
 
                        }
@@ -194,4 +241,5 @@ public class FeatureTreeEditor implements
                        composite.getViewer().refresh();
                }
        }
+
 }