ref #6932 Fix new element handler and save
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Sep 2017 09:09:43 +0000 (11:09 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Sep 2017 09:10:19 +0000 (11:10 +0200)
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/annotatedlineeditor/e4/handler/NewObjectHandlerE4.java
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/BulkEditorLineDisplay.java
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/AnnotatedTableItem.java
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/BulkEditorE4.java
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/input/AbstractBulkEditorInput.java

index 94639d63a697db1ea0a90044d0c9b283bc6d2397..d585ed609250589cc4795ef7f36f4375f3717b3c 100644 (file)
@@ -15,10 +15,16 @@ import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.dialogs.IInputValidator;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
 
 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
 import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
+import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.GroupCreator;
+import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.UserCreator;
 
 /**
  *
@@ -30,56 +36,46 @@ public class NewObjectHandlerE4 {
 
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
+            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
             MHandledMenuItem menuItem) {
 
         Object key = menuItem.getTransientData().get(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID+".key");
 
         BulkEditorE4 bulkEditor = (BulkEditorE4) activePart.getObject();
 
-
         if (key != null) {
-            IEntityCreator entityCreator = bulkEditor.getEditorInput().getEntityCreator();
             String text = menuItem.getCommand().getCommandName();
-            Object createdEntity = entityCreator.createEntity(text);
-            bulkEditor.getEditorInput().getModel().add(createdEntity);
-            bulkEditor.refresh();
+            //FIXME : This should probably go into some ValidatorFactory
+            IInputValidator nonEmptyInputValidator = null;
+            //FIXME : This is a workaround to not allow empty strings in the
+            //        input dialog for User and Group entities.
+            //        Normally this should be default
+            //        behaviour, so we need to discuss whether this handler
+            //        should be used to handle the creating new entities of
+            //        type other than User and Group.
+            //        Once #4348 is fixed this check can be removed.
+            if(text.equals(UserCreator.USER) || text.equals(GroupCreator.GROUP)) {
+                nonEmptyInputValidator = new IInputValidator() {
+                    @Override
+                    public String isValid(String text) {
+                        if(text == null || text.isEmpty()) {
+                            return "Input cannot be empty";
+                        }
+                        return null;
+                    }
+                };
+            }
+            InputDialog dialog = new InputDialog(shell,
+                    String.format("Create %s", text), String.format("Enter new %s", text), "",
+                    nonEmptyInputValidator);
 
-            //                         if(!(key instanceof SpecimenOrObservationType)) {
-            //
-            //                                 //FIXME : This should probably go into some ValidatorFactory
-            //                                 IInputValidator nonEmptyInputValidator = null;
-            //                                 //FIXME : This is a workaround to not allow empty strings in the
-            //                                 //        input dialog for User and Group entities.
-            //                                 //        Normally this should be default
-            //                                 //        behaviour, so we need to discuss whether this handler
-            //                                 //        should be used to handle the creating new entities of
-            //                                 //        type other than User and Group.
-            //                                 //        Once #4348 is fixed this check can be removed.
-            //                                 if(text.equals(UserCreator.USER) || text.equals(GroupCreator.GROUP)) {
-            //                                         nonEmptyInputValidator = new IInputValidator() {
-            //                                                 @Override
-            //                            public String isValid(String text) {
-            //                                                         if(text == null || text.isEmpty()) {
-            //                                                                 return "Input cannot be empty";
-            //                                                         }
-            //                                                         return null;
-            //                                                 }
-            //                                         };
-            //                                 }
-            //                                 InputDialog dialog = new InputDialog(shell,
-            //                                                 String.format("Create %s", text), String.format("Enter new %s", text), "",
-            //                                                 nonEmptyInputValidator);
-            //
-            //                                 if (dialog.open() != Window.CANCEL) {
-            //                                         ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject(key, dialog.getValue());
-            //                                 }
-            //                         } else {
-            //                                 ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject(key, "Untitled");
-            //                         }
-            //
-            //                 } else {
-            //                         ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject();
-            //                 }
+            if (dialog.open() != Window.CANCEL) {
+                IEntityCreator entityCreator = bulkEditor.getEditorInput().getEntityCreator();
+                Object createdEntity = entityCreator.createEntity(key, dialog.getValue());
+                bulkEditor.getEditorInput().getModel().add(createdEntity);
+                bulkEditor.refresh(true);
+                bulkEditor.setDirty();
+            }
         }
     }
 }
index b48bdc0604c4cf3af0779c8a5a9e3bd67fe8f247..2b1deddedd8d66362a481f0151db2f70c0d86df7 100644 (file)
@@ -22,7 +22,6 @@ import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
-import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.common.Marker;
 import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.reference.Reference;
@@ -64,7 +63,7 @@ public class BulkEditorLineDisplay implements ILineDisplayStrategy {
        /** {@inheritDoc} */
        @Override
     public String getText(Object entity) {
-               return editor.getEditorInput().getText((ICdmBase) entity);
+               return editor.getEditorInput().getText((CdmBase) entity);
        }
 
        /** {@inheritDoc} */
index d714c8fa9eccfd062a71cfd566e9df3b1dbc52d5..31dca273fcd906970871d481b90bcb3f8738e9dc 100644 (file)
@@ -9,14 +9,14 @@
 */
 package eu.etaxonomy.taxeditor.bulkeditor.e4;
 
-import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 
 /**
  * @author pplitzner
  * @date 12.09.2017
  *
  */
-public class AnnotatedTableItem <T extends ICdmBase> {
+public class AnnotatedTableItem <T extends CdmBase> {
 
     private T element;
     private boolean isMergeCandidate = false;
index 2fd6341f1f8ddec240a619f3a9d4b548e20d016b..866b2d3fdf729c0106dd1ee00bfc7ef07d575e37 100644 (file)
@@ -126,10 +126,20 @@ public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnable
 
        @Persist
        public void save() {
-           dirty.setDirty(false);
+           // commit the conversation and start a new transaction immediately
+           conversation.commit(true);
 
-           this.searchBar.updateEditorInput();
+           Object object = viewer.getInput();
+           if(object instanceof List){
+               for (Object item: (List)object) {
+                   if(item instanceof AnnotatedTableItem){
+                       CdmBase cdmBase = ((AnnotatedTableItem) item).getElement();
+                    CdmStore.getService(cdmBase).merge(cdmBase, true);
+                   }
 
+            }
+           }
+        dirty.setDirty(false);
        }
 
        /** {@inheritDoc} */
@@ -156,9 +166,24 @@ public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnable
     }
 
     public void refresh() {
+        refresh(false);
+    }
+
+    public void refresh(boolean resetInput) {
+        if(resetInput){
+            viewer.setInput(getEditorInput().getWrappedModel());
+        }
         viewer.refresh();
     }
 
+    public TableViewer getViewer() {
+        return viewer;
+    }
+
+    public void setDirty(){
+        dirty.setDirty(true);
+    }
+
     public boolean isDirty() {
         return dirty.isDirty();
     }
index 5813dcdd789d9360082a7263bd232fd289104c82..0bd958bab9048ce5e812aec78b1d0bbd15ad0919 100644 (file)
@@ -23,7 +23,6 @@ import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.strategy.merge.IMergable;
@@ -46,12 +45,12 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @version 1.0
  * @param <T>
  */
-public abstract class AbstractBulkEditorInput<T extends ICdmBase> extends CdmEntitySessionInput implements IEditorInput ,
+public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEntitySessionInput implements IEditorInput ,
     IEntityPersistenceService<T> {
 
        private UUID entityUuid;
 
-       private List<T> model;
+       private List<T> model = new ArrayList<>();
 
        private IEntityCreator<T> entityCreator;
        private final ConversationHolder conversation;