Merge branch 'hotfix/5.18.2'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / handler / ConvertPerson2TeamHandlerE4.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.bulkeditor.e4.handler;
10
11 import java.util.Iterator;
12
13 import javax.inject.Named;
14
15 import org.eclipse.core.runtime.NullProgressMonitor;
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.core.di.annotations.Execute;
18 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20 import org.eclipse.e4.ui.services.IServiceConstants;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24
25 import eu.etaxonomy.cdm.api.service.IAgentService;
26 import eu.etaxonomy.cdm.api.service.UpdateResult;
27 import eu.etaxonomy.cdm.model.agent.Person;
28 import eu.etaxonomy.cdm.model.agent.Team;
29 import eu.etaxonomy.cdm.strategy.merge.MergeException;
30 import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
31 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * @author k.luther
37 * @date 12.05.2015
38 */
39 public class ConvertPerson2TeamHandlerE4 {
40
41
42 @Execute
43 public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
44
45 BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
46 if (editor.isDirty()){
47 boolean proceed = MessageDialog.openQuestion(null,
48 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
49 if (proceed) {
50 editor.save(new NullProgressMonitor());
51 } else {
52 return;
53 }
54 }
55 IStructuredSelection selection = editor.getSelection();
56 Iterator<?> iterator = selection.iterator();
57 for(selection.iterator();iterator.hasNext();){
58
59 Person person = (Person) iterator.next();
60 Team team =null;
61 UpdateResult result = null;
62 try {
63 result = CdmStore.getService(IAgentService.class).convertPerson2Team(person.getUuid());
64 } catch (IllegalArgumentException e) {
65 MessagingUtils.errorDialog("Can not convert Person into a Team", null, e.getLocalizedMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID,e, true);
66 } catch (MergeException e) {
67 MessagingUtils.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
68 }
69 if (result != null &&result.isOk()){
70 team = (Team)result.getCdmEntity();
71 editor.getEditorInput().getModel().remove(person);
72 editor.getEditorInput().getModel().add(team);
73 }
74 }
75 }
76
77 @CanExecute
78 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
79 MHandledMenuItem menuItem){
80 boolean canExecute = false;
81 BulkEditorE4 bulkEditorE4 = (BulkEditorE4)activePart.getObject();
82 StructuredSelection selection = (StructuredSelection)bulkEditorE4.getSelection();
83 canExecute = !selection.isEmpty() && bulkEditorE4.getEditorInput().isConvertingEnabled();
84 Iterator<?> iterator = selection.iterator();
85 for(selection.iterator();iterator.hasNext();){
86 if(!(iterator.next() instanceof Person)){
87 canExecute = false;
88 break;
89 }
90 }
91 menuItem.setVisible(canExecute);
92 return canExecute;
93 }
94
95 }