ref #6595 Fix initial selection for synonymy and accepted taxa
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / e4 / TaxonEditorInputE4.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.e4;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
22 import eu.etaxonomy.cdm.api.service.IClassificationService;
23 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
24 import eu.etaxonomy.cdm.api.service.ITaxonService;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.name.TaxonName;
27 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
28 import eu.etaxonomy.cdm.model.taxon.Synonym;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
33 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
36 import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
37 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
38 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
40 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42
43
44 /**
45 *
46 * @author pplitzner
47 * @date Aug 24, 2017
48 *
49 */
50 public class TaxonEditorInputE4 extends CdmEntitySessionInput implements IConversationEnabled {
51
52 private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
53
54 private final ConversationHolder conversation;
55
56 private TaxonNode taxonNode;
57
58 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
59
60 private TaxonBase initiallySelectedTaxonBase;
61
62 private enum CdmType {
63 TAXON_NODE,
64 TAXON_BASE,
65 PARENT_TAXON_NODE
66 }
67
68 private TaxonEditorInputE4(UUID uuid, CdmType type) {
69 super(true);
70 this.conversation = CdmStore.createConversation();
71 switch(type) {
72 case PARENT_TAXON_NODE:
73 initForParentTaxonNode(uuid);
74 break;
75 case TAXON_BASE:
76 initForTaxonBase(uuid);
77 break;
78 case TAXON_NODE:
79 initForTaxonNode(uuid);
80 break;
81 }
82 }
83
84 private void init(TaxonNode taxonNode) {
85 this.taxonNode = taxonNode;
86 }
87
88
89 private void initForTaxonNode(UUID taxonNodeUuid) {
90
91
92 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
93
94 if(taxonNode == null){
95 MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
96 }
97 init(taxonNode);
98 setInitiallySelectedTaxonBase(getTaxon());
99
100 }
101
102 private void initForTaxonBase(UUID taxonBaseUuid) {
103 TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
104 if (taxonBase != null){
105 if(taxonBase.isInstanceOf(Taxon.class)){
106 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
107 setInitiallySelectedTaxonBase(taxon);
108
109 if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
110 // TODO get accepted taxon
111 MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
112
113 Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
114 Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
115 for(TaxonRelationship relation : relations){
116 if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
117 acceptedTaxa.add(relation.getToTaxon());
118 }
119 }
120 setInputForMultipleTaxa(conversation, acceptedTaxa);
121
122 }else{
123 setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
124 }
125 }else if(taxonBase instanceof Synonym){
126 Synonym synonym = (Synonym) taxonBase;
127
128 Set<Taxon> taxa = new HashSet<>();
129 Taxon taxon = synonym.getAcceptedTaxon();
130 if (taxon != null){
131 taxa.add(taxon);
132 }
133 setInputForMultipleTaxa(conversation, taxa);
134 setInitiallySelectedTaxonBase(synonym);
135 }
136 }
137 }
138
139
140 private void initForParentTaxonNode(UUID parentNodeUuid){
141
142
143 TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
144 ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
145
146 Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
147 TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
148
149 // add the new taxon to the editors persistence context
150 UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
151
152 initForTaxonNode(newTaxonNodeUuid);
153 }
154
155 private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
156 if(taxonNodes.size() == 1){
157 TaxonNode taxonNode = taxonNodes.iterator().next();
158 init(taxonNode);
159 }else if(taxonNodes.size() > 1){
160 TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
161 if(taxonNode != null){
162 init(taxonNode);
163 }
164 } else if (taxonNodes.size() == 0) {
165 // this is an undesired state
166 MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
167 }
168 }
169
170 private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
171 if(taxa.size() == 1){
172 Taxon taxon = taxa.iterator().next();
173 Set<TaxonNode> nodes = taxon.getTaxonNodes();
174 setInputForMultipleNodes(conversation, nodes);
175 }else if(taxa.size() > 1){
176 Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
177 for ( Taxon taxon : taxa ){
178 taxonNodes.addAll(taxon.getTaxonNodes());
179 }
180 setInputForMultipleNodes(conversation, taxonNodes);
181 }else if(taxa.size() == 0){
182 // this is an undesired state
183 MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
184 }
185 }
186
187 public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) throws Exception {
188 return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
189
190 }
191
192 public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
193 return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
194 }
195
196
197 public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
198 return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
199 }
200
201 public Object getAdapter(Class adapter) {
202
203 if (adapter == Taxon.class) {
204 return getTaxon();
205 }
206
207 if (adapter == TaxonNode.class) {
208 return taxonNode;
209 }
210
211 return null;
212 }
213
214 /**
215 * {@inheritDoc}
216 *
217 * Overrides equals to ensure that a taxon can only be edited by
218 * one editor at a time.
219 */
220 @Override
221 public boolean equals(Object obj) {
222 if (TaxonEditorInputE4.class.equals(obj.getClass())
223 && getTaxon() != null
224 && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
225 if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
226 setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
227 }
228 return true;
229 }
230 return false;
231 }
232
233 public Taxon getTaxon(){
234 if(taxonNode!=null){
235 Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
236 return taxon;
237 }
238 return null;
239 }
240
241 public TaxonNode getTaxonNode() {
242 return taxonNode;
243 }
244
245 @Override
246 public ConversationHolder getConversationHolder() {
247 return conversation;
248 }
249
250 /** {@inheritDoc} */
251 @Override
252 public void update(CdmDataChangeMap events) {
253 if(dataChangeBehavior == null){
254 dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
255 }
256
257 DataChangeBridge.handleDataChange(events, dataChangeBehavior);
258 }
259
260 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
261 this.initiallySelectedTaxonBase = taxonBase;
262 }
263
264 public TaxonBase getInitiallySelectedTaxonBase() {
265 return initiallySelectedTaxonBase;
266 }
267
268 @Override
269 public List<TaxonNode> getRootEntities() {
270 return Arrays.asList(taxonNode);
271 }
272
273 @Override
274 public void merge() {
275 CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
276
277 }
278
279 @Override
280 public Map<Object, List<String>> getPropertyPathsMap() {
281 return null;
282 }
283
284 private List<String> getTaxonNodePropertyPaths() {
285 List<String> taxonNodePropertyPaths = new ArrayList<String>();
286 for(String propertyPath : getTaxonBasePropertyPaths()) {
287 taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
288 }
289 return taxonNodePropertyPaths;
290 }
291
292 private List<String> getTaxonBasePropertyPaths() {
293 List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
294 "sec", //$NON-NLS-1$
295 "createdBy", //$NON-NLS-1$
296 "updatedBy", //$NON-NLS-1$
297 "annotations", //$NON-NLS-1$
298 "markers", //$NON-NLS-1$
299 "credits", //$NON-NLS-1$
300 "extensions", //$NON-NLS-1$
301 "rights", //$NON-NLS-1$
302 "sources", //$NON-NLS-1$
303 "descriptions", //$NON-NLS-1$
304 "relationsToThisTaxon", //$NON-NLS-1$
305 "relationsFromThisTaxon", //$NON-NLS-1$
306 "taxonNodes", //$NON-NLS-1$
307 "descriptions.descriptionElements.feature", //$NON-NLS-1$
308 "descriptions.descriptionElements.area", //$NON-NLS-1$
309 "descriptions.descriptionElements.status", //$NON-NLS-1$
310 "descriptions.markers", //$NON-NLS-1$
311 "name.descriptions", //$NON-NLS-1$
312 "name.typeDesignations", //$NON-NLS-1$
313 "name.status", //$NON-NLS-1$
314 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
315 "name.taxonBases.taxonNodes", //$NON-NLS-1$
316 "name.relationsFromThisName", //$NON-NLS-1$
317 "name.relationsToThisName", //$NON-NLS-1$
318 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
319 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
320 "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
321 "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
322 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
323 "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
324 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
325 });
326
327 return taxonBasePropertyPaths;
328 }
329
330 }