Merge branch 'release/4.3.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / ChecklistEditorInput.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.checklist;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IMemento;
20 import org.eclipse.ui.IPersistable;
21 import org.eclipse.ui.IPersistableElement;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.model.taxon.Classification;
27 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
32 /**
33 *
34 * @author a.oppermann
35 * @date 25.04.2014
36 *
37 */
38 public class ChecklistEditorInput extends CdmEntitySessionInput implements IEditorInput, IPersistable {
39
40 /**
41 * The selected classification
42 */
43 private TaxonNode taxonNode = null;
44
45 private final List<TaxonBase> taxa = new ArrayList<TaxonBase>();
46
47 private Classification classification = null;
48
49 private final ConversationHolder conversation;
50
51
52 /**
53 * Creates an editor input for the {@link ChecklistView} with the currently selected TaxonNode
54 * and its childNodes.
55 * @param taxonNode
56 */
57 public ChecklistEditorInput(TaxonNode taxonNode){
58 super(false);
59 this.conversation = CdmStore.createConversation();
60 this.taxonNode =CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
61 // getChildTaxa(taxonNode);
62 classification = taxonNode.getClassification();
63 initSession();
64 }
65
66
67 /**
68 * @param taxonNode2
69 */
70 private void getChildTaxa(TaxonNode taxonNode2) {
71 taxonNode2.removeNullValueFromChildren();
72 if (taxonNode2.hasChildNodes()){
73 for (TaxonNode node: taxonNode2.getChildNodes()){
74 taxa.add(node.getTaxon());
75 getChildTaxa(node);
76 }
77 }
78
79
80 }
81
82 /**
83 * Creates an editor input for the {@link ChecklistView} with the currently selected classification.
84 * @param classificationUuid
85 */
86 public ChecklistEditorInput(Classification classification) {
87 super(true);
88 this.conversation = CdmStore.createConversation();
89 this.classification = CdmStore.getCurrentApplicationConfiguration().getClassificationService().load(classification.getUuid());
90
91 }
92
93
94
95 /* (non-Javadoc)
96 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
97 */
98 @Override
99 public Object getAdapter(Class adapter) {
100 // TODO Auto-generated method stub
101 return null;
102 }
103
104 /* (non-Javadoc)
105 * @see org.eclipse.ui.IEditorInput#exists()
106 */
107 @Override
108 public boolean exists() {
109 return false;
110 }
111
112 /* (non-Javadoc)
113 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
114 */
115 @Override
116 public ImageDescriptor getImageDescriptor() {
117 // TODO Auto-generated method stub
118 return null;
119 }
120
121 /* (non-Javadoc)
122 * @see org.eclipse.ui.IEditorInput#getName()
123 */
124 @Override
125 public String getName() {
126 if(taxonNode != null){
127 return taxonNode.getTaxon().getName().getTitleCache();
128 }else{
129 return classification.getTitleCache();
130 }
131 }
132
133 /* (non-Javadoc)
134 * @see org.eclipse.ui.IEditorInput#getPersistable()
135 */
136 @Override
137 public IPersistableElement getPersistable() {
138 return null;
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.ui.IEditorInput#getToolTipText()
143 */
144 @Override
145 public String getToolTipText() {
146 return classification.getTitleCache();
147 }
148
149 /**
150 * @return the taxonNode
151 */
152 public TaxonNode getTaxonNode() {
153 return taxonNode;
154 }
155
156 /**
157 * @return classification
158 */
159 public Classification getClassification(){
160 return classification;
161 }
162
163 // /**
164 // * @return the conversationHolder
165 // */
166 // public ConversationHolder getConversationHolder() {
167 // return conversationHolder;
168 // }
169
170
171
172 /* (non-Javadoc)
173 * @see java.lang.Object#hashCode()
174 */
175 @Override
176 public int hashCode() {
177 final int prime = 31;
178 int result = 1;
179 result = prime * result + ((classification == null) ? 0 : classification.hashCode());
180 result = prime * result + ((taxonNode == null) ? 0 : taxonNode.hashCode());
181 return result;
182 }
183
184 /* (non-Javadoc)
185 * @see java.lang.Object#equals(java.lang.Object)
186 */
187 @Override
188 public boolean equals(Object obj) {
189 if (this == obj) {
190 return true;
191 }
192 if (obj == null) {
193 return false;
194 }
195 if (getClass() != obj.getClass()) {
196 return false;
197 }
198 ChecklistEditorInput other = (ChecklistEditorInput) obj;
199 if (classification == null) {
200 if (other.classification != null) {
201 return false;
202 }
203 } else if (!classification.equals(other.classification)) {
204 return false;
205 }
206 if (taxonNode == null) {
207 if (other.taxonNode != null) {
208 return false;
209 }
210 } else if (!taxonNode.equals(other.taxonNode)) {
211 return false;
212 }
213 return true;
214 }
215
216 /* (non-Javadoc)
217 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
218 */
219 @Override
220 public void saveState(IMemento memento) {
221 UUID uuid = classification.getUuid();
222 }
223
224 @Override
225 public void merge() {
226 for (TaxonBase taxon:taxa){
227 CdmStore.getService(ITaxonService.class).merge(taxon, true);
228 }
229
230 // CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
231 }
232
233 /* (non-Javadoc)
234 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
235 */
236 @Override
237 public List<TaxonNode> getRootEntities() {
238 return Arrays.asList(taxonNode);
239 }
240
241 /**
242 * @return the conversation
243 */
244 public ConversationHolder getConversation() {
245 return conversation;
246 }
247
248 private List<String> getTaxonNodePropertyPaths() {
249 List<String> taxonNodePropertyPaths = new ArrayList<String>();
250 for(String propertyPath : getTaxonBasePropertyPaths()) {
251 taxonNodePropertyPaths.add("taxon." + propertyPath);
252 }
253 return taxonNodePropertyPaths;
254 }
255
256 private List<String> getTaxonBasePropertyPaths() {
257 List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
258 "sec",
259 "createdBy",
260 "updatedBy",
261 "annotations",
262 "markers",
263 "credits",
264 "extensions",
265 "rights",
266 "sources",
267 "descriptions",
268 "relationsToThisTaxon",
269 "relationsFromThisTaxon",
270 "taxonNodes",
271 "descriptions.descriptionElements.feature",
272 "descriptions.descriptionElements.area",
273 "descriptions.descriptionElements.status",
274 "descriptions.markers",
275 "name.descriptions",
276 "name.typeDesignations",
277 "name.status",
278 "name.nomenclaturalReference.inReference",
279 "name.taxonBases.taxonNodes",
280 "name.relationsFromThisName",
281 "name.relationsToThisName",
282 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status",
283 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName",
284 "synonymRelations.synonym.name.status.type",
285 "synonymRelations.synonym.name.relationsToThisName.fromName",
286 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship",
287 "synonymRelations.synonym.name.nomenclaturalReference.authorship",
288 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations"
289 });
290
291 return taxonBasePropertyPaths;
292 }
293
294 // @Override
295 // public void dispose(){
296 // conversation.unbind();
297 // conversation.close();
298 // getCdmEntitySession().dispose();
299 // super.dispose();
300 // }
301
302 }