ref #5329: refactor convertPerson2Team handler
authorKatja Luther <k.luther@bgbm.org>
Wed, 28 Jun 2017 08:19:30 +0000 (10:19 +0200)
committerKatja Luther <k.luther@bgbm.org>
Wed, 28 Jun 2017 08:21:28 +0000 (10:21 +0200)
eu.etaxonomy.taxeditor.bulkeditor/plugin.xml
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/ConvertPerson2TeamRemotingHandler.java [new file with mode: 0755]
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/operation/ConvertPerson2TeamOperation.java [new file with mode: 0755]

index 7c11f80a923880d73983e0f560f88a3b6c00f555..7b80e3b8e8f31572959a4d602c516a672416c5db 100644 (file)
             commandId="eu.etaxonomy.taxeditor.bulkeditor.convertTeam2Person">
       </handler>
       <handler
-            class="eu.etaxonomy.taxeditor.bulkeditor.handler.ConvertPerson2TeamHandler"
+            class="eu.etaxonomy.taxeditor.bulkeditor.handler.ConvertPerson2TeamRemotingHandler"
             commandId="eu.etaxonomy.taxeditor.bulkeditor.convertPerson2Team">
+             <activeWhen>
+            <reference
+                  definitionId="isRemoting">
+            </reference>
+         </activeWhen>
       </handler>
       <handler
             class="eu.etaxonomy.taxeditor.bulkeditor.handler.defaultHandler.OpenBulkEditorForIdentifiableEntity"
diff --git a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/ConvertPerson2TeamRemotingHandler.java b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/ConvertPerson2TeamRemotingHandler.java
new file mode 100755 (executable)
index 0000000..fa4081c
--- /dev/null
@@ -0,0 +1,122 @@
+/**
+* Copyright (C) 2017 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.bulkeditor.handler;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.core.commands.operations.AbstractOperation;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.taxeditor.bulkeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.bulkeditor.operation.ConvertPerson2TeamOperation;
+import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
+
+/**
+ * @author k.luther
+ * @date 10.05.2017
+ *
+ */
+public class ConvertPerson2TeamRemotingHandler extends RemotingCdmHandler {
+    IEditorPart editor;
+
+    /**
+     * @param label
+     */
+    public ConvertPerson2TeamRemotingHandler() {
+        super(Messages.ConvertPerson2TeamHandler_lable);
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IStatus allowOperations(ExecutionEvent event) {
+        ISelection selection = HandlerUtil.getCurrentSelection(event);
+
+        editor = HandlerUtil.getActiveEditor(event);
+
+        IEditorInput input = editor.getEditorInput();
+        if (editor.isDirty()){
+            boolean proceed = MessageDialog.openQuestion(null,
+                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
+            if (proceed) {
+                editor.doSave(null);
+            } else {
+                return Status.CANCEL_STATUS;
+            }
+        }
+        Iterator<IStructuredSelection> it = ((IStructuredSelection) selection).iterator();
+        while( it.hasNext()){
+            Object object =it.next();
+            if (!(object instanceof Person)){
+                MessageDialog.openInformation(null, "Operation not applicable.", "This operation is only applicable for persons");
+                return Status.CANCEL_STATUS;
+            }
+        }
+        return Status.OK_STATUS;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public AbstractOperation prepareOperation(ExecutionEvent event) {
+        IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
+
+        try {
+            String label = event.getCommand().getName();
+            Iterator<IStructuredSelection> it = selection.iterator();
+            ConvertPerson2TeamOperation operation;
+            List<Person> persons =  new ArrayList<Person>();
+            while( it.hasNext()){
+                Object object =it.next();
+                if (object instanceof Person){
+                    Person person = HibernateProxyHelper.deproxy(object, Person.class);
+                    persons.add(person);
+                }
+            }
+            return new ConvertPerson2TeamOperation(label, persons, true, editor);
+
+
+        } catch (NotDefinedException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+
+
+
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onComplete() {
+        // TODO Auto-generated method stub
+
+    }
+
+
+
+}
diff --git a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/operation/ConvertPerson2TeamOperation.java b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/operation/ConvertPerson2TeamOperation.java
new file mode 100755 (executable)
index 0000000..bfcf4c6
--- /dev/null
@@ -0,0 +1,109 @@
+/**
+* Copyright (C) 2017 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.bulkeditor.operation;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.ui.IEditorPart;
+
+import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
+import eu.etaxonomy.cdm.api.service.IAgentService;
+import eu.etaxonomy.cdm.api.service.UpdateResult;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.strategy.merge.MergeException;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
+import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.operation.RemotingCdmUpdateOperation;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+/**
+ * @author k.luther
+ * @date 10.05.2017
+ *
+ */
+public class ConvertPerson2TeamOperation extends RemotingCdmUpdateOperation {
+    List<Person> persons;
+    List<Team> teams;
+    IEditorPart editor;
+
+    /**
+     * @param label
+     * @param editor
+     * @param undoContext
+     * @param element
+     */
+    public ConvertPerson2TeamOperation(String label, List<Person> persons, boolean async, IEditorPart editor) {
+        super(label, Action.Update, persons, async);
+        this.persons = persons;
+        this.editor = editor;
+        this.teams = new ArrayList<Team>();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+        UpdateResult result = new UpdateResult();
+        try {
+            for (Person person: persons){
+                UpdateResult resultTemp = CdmStore.getService(IAgentService.class).convertPerson2Team(person.getUuid());
+                 result.includeResult(resultTemp);
+                 result.addUpdatedObject(resultTemp.getCdmEntity());
+
+            }
+            for (CdmBase team:result.getUpdatedObjects()){
+
+                teams.add((Team)team);
+            }
+        } catch (IllegalArgumentException e) {
+            MessagingUtils.errorDialog("Can not convert Person into a Team", null, e.getLocalizedMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID,e, true);
+        } catch (MergeException e) {
+            MessagingUtils.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
+        }
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected IStatus onComplete(boolean success) {
+
+        ((BulkEditor) editor).getSearchBar().updateEditorInput();
+        return Status.OK_STATUS;
+    }
+
+
+
+}