ref #9359 upgrade TaxEditor to log4j2
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / e4 / handler / ChangeSynonymToAcceptedTaxonHandlerE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.editor.name.e4.handler;
11
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import javax.inject.Named;
18
19 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20 import org.eclipse.e4.core.di.annotations.CanExecute;
21 import org.eclipse.e4.core.di.annotations.Execute;
22 import org.eclipse.e4.ui.di.UISynchronize;
23 import org.eclipse.e4.ui.model.application.MApplication;
24 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26 import org.eclipse.e4.ui.services.IServiceConstants;
27 import org.eclipse.e4.ui.workbench.modeling.EModelService;
28 import org.eclipse.e4.ui.workbench.modeling.EPartService;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31
32 import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
33 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
34 import eu.etaxonomy.cdm.model.reference.Reference;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.Taxon;
37 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
39 import eu.etaxonomy.taxeditor.editor.EditorUtil;
40 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
41 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
43 import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
44 import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
45 import eu.etaxonomy.taxeditor.event.EventUtility;
46 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
47 import eu.etaxonomy.taxeditor.model.AbstractUtility;
48 import eu.etaxonomy.taxeditor.model.MessagingUtils;
49 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51 import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
52 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
53
54 /**
55 * @author pplitzner
56 * @since Aug 28, 2017
57 */
58 public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
59
60 private static final Logger logger = LogManager.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
61
62 private TaxonNameEditorE4 editor;
63 private EPartService partService;
64 private MApplication application;
65 private EModelService modelService;
66
67 @Execute
68 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
69 @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, EPartService partService,
70 EModelService modelService, MApplication application, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
71 UISynchronize sync) {
72
73 this.modelService = modelService;
74 this.application = application;
75 this.partService = partService;
76
77 editor = (TaxonNameEditorE4) activePart.getObject();
78
79 TaxonEditorInputE4 input = editor.getEditorInput();
80
81 // Get synonym from selection
82 if (!(selection.getFirstElement() instanceof Synonym)) {
83 logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
84 return;
85 }
86
87 Synonym synonym = (Synonym) selection.getFirstElement();
88 Reference synSecRef = synonym.getSec();
89
90 // Force user to save taxon - not really necessary though, is it?
91 if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
92 return;
93 }
94
95 // Get taxon
96 Taxon taxon = input.getTaxon();
97
98 TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell,// editor.getConversationHolder(),
99 Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
100
101 if(newParentNode != null){
102 Reference parentSecRef = newParentNode.getTaxon() != null? newParentNode.getTaxon().getSec(): null;
103
104 // TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
105 // apply confirmation dialog
106 HomotypicalGroup group = synonym.getHomotypicGroup();
107 List<Synonym> homotypicSynonyms = taxon.getSynonymsInGroup(synonym.getHomotypicGroup());
108 Set<Reference> secRefs = new HashSet<>();
109 boolean nullExist = false;
110 for (Synonym synInHomtypicGroup: homotypicSynonyms){
111 if (synInHomtypicGroup.getSec() == null && synonym.getSec() != null){
112 nullExist = true;
113 }
114 secRefs.add(synInHomtypicGroup.getSec());
115 }
116
117 // FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
118
119 //compare parentSec and synSec and ask for handling.
120 SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
121 UUID newSecUuid = null;
122 if (((synSecRef != parentSecRef || secRefs.size()>1) && (secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn)))|| secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect)){
123
124 if (secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect)){
125 String message = null;
126 String[] options = null;
127 if (secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect) && synSecRef == parentSecRef){
128 message = Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message_always_select;
129 options = new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select};
130 }else {
131 message = Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message;
132 options = new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, };
133 }
134 int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, message, options);
135
136 // int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message,
137 // new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select});
138 if (result == 2){
139 newSecUuid = parentSecRef != null? parentSecRef.getUuid(): null;
140 }else if (result == 1){
141 Reference sec = ReferenceSelectionDialog.select(shell, null);
142 newSecUuid = sec != null? sec.getUuid(): null;
143 }else if (result == 0){
144 secHandling = SecReferenceHandlingEnum.KeepOrWarn;
145 }else{
146 return ;
147 }
148
149 }else if (parentSecRef != synSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn)){
150 MessagingUtils.warningDialog(Messages.SecundumReference,this, Messages.ChangeSynonymToAcceptedHandler_Different_Secundum_references);
151
152 }
153 }
154 if (newParentNode.getTaxon() != null &&
155 synonym.isPublish() != newParentNode.getTaxon().isPublish()){
156 MessagingUtils.warningDialog("Publish flag",this, Messages.ChangeSynonymToAcceptedHandler_Different_Publish_Flag);
157 }
158
159 ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
160 taxon, newParentNode, synonym,
161 newSecUuid, secHandling,
162 this, editor, editor.getEditorInput()); //$NON-NLS-1$
163
164 AbstractUtility.executeOperation(operation, sync);
165 }
166 }
167
168 @Override
169 public boolean postOperation(Object objectAffectedByOperation) {
170
171 // Redraw existing editor
172 ((IPostOperationEnabled) editor).postOperation(null);
173
174 editor.save(AbstractUtility.getMonitor());
175
176
177 if (objectAffectedByOperation instanceof TaxonNode) {
178 TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
179 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(newNode.getParent()));
180 }
181 return true;
182 }
183
184 @Override
185 public boolean onComplete() {
186 return false;
187 }
188
189 @CanExecute
190 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
191 MHandledMenuItem menuItem){
192 boolean canExecute = false;
193 if(selection.size()==1){
194 Object selectedElement = selection.getFirstElement();
195 canExecute =
196 NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
197 && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
198 && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
199 && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement);
200 menuItem.setVisible(canExecute);
201 }
202 return canExecute;
203 }
204 }