fix #7237: description editor: when status is empty and no description exist, do...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / ChecklistEditorInput.java
1 /**
2 * Copyright (C) 2013 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.editor.view.checklist;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.eclipse.ui.IMemento;
17 import org.eclipse.ui.IPersistable;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
21 import eu.etaxonomy.cdm.api.service.ITaxonService;
22 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23 import eu.etaxonomy.cdm.model.taxon.Classification;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
28 import eu.etaxonomy.taxeditor.store.CdmStore;
29
30 /**
31 *
32 * @author a.oppermann
33 * @date 25.04.2014
34 *
35 */
36 public class ChecklistEditorInput extends CdmEntitySessionInput implements IPersistable {
37
38 /**
39 * The selected classification
40 */
41 private TaxonNode taxonNode = null;
42
43 private List<TaxonBase> taxa = new ArrayList();
44
45 private Classification classification = null;
46
47 private final ConversationHolder conversation;
48
49 private List<TaxonBase> taxaToSave = new ArrayList();
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 initSession();
61 this.taxonNode =CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
62 getChildTaxa(taxonNode);
63 classification = taxonNode.getClassification();
64
65 }
66
67
68 /**
69 * @param taxonNode2
70 */
71 private void getChildTaxa(TaxonNode taxonNode2) {
72 taxonNode2.removeNullValueFromChildren();
73 if (taxonNode2.hasChildNodes()){
74 for (TaxonNode node: taxonNode2.getChildNodes()){
75 node = CdmStore.getService(ITaxonNodeService.class).load(node.getUuid(), getTaxonNodePropertyPaths());
76 taxa.add(HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class));
77 getChildTaxa(node);
78 }
79 }
80
81
82 }
83
84 /**
85 * Creates an editor input for the {@link ChecklistView} with the currently selected classification.
86 * @param classificationUuid
87 */
88 public ChecklistEditorInput(Classification classification) {
89 super(true);
90 this.conversation = CdmStore.createConversation();
91 initSession();
92 this.classification = CdmStore.getCurrentApplicationConfiguration().getClassificationService().load(classification.getUuid());
93 getChildTaxa(classification.getRootNode());
94
95 }
96
97
98
99 public List<TaxonBase> getTaxa() {
100 return taxa;
101 }
102
103 public String getName() {
104 if(taxonNode != null && taxonNode.getTaxon()!=null){
105 return taxonNode.getTaxon().getName().getTitleCache();
106 }else{
107 return classification.getTitleCache();
108 }
109 }
110
111
112 /**
113 * @return the taxonNode
114 */
115 public TaxonNode getTaxonNode() {
116 return taxonNode;
117 }
118
119 /**
120 * @return classification
121 */
122 public Classification getClassification(){
123 return classification;
124 }
125
126 // /**
127 // * @return the conversationHolder
128 // */
129 // public ConversationHolder getConversationHolder() {
130 // return conversationHolder;
131 // }
132
133
134
135 // /* (non-Javadoc)
136 // * @see java.lang.Object#hashCode()
137 // */
138 // @Override
139 // public int hashCode() {
140 // final int prime = 31;
141 // int result = 1;
142 // result = prime * result + ((classification == null) ? 0 : classification.hashCode());
143 // result = prime * result + ((taxonNode == null) ? 0 : taxonNode.hashCode());
144 // return result;
145 // }
146
147 /* (non-Javadoc)
148 * @see java.lang.Object#equals(java.lang.Object)
149 */
150 @Override
151 public boolean equals(Object obj) {
152 if (this == obj) {
153 return true;
154 }
155 if (obj == null) {
156 return false;
157 }
158 if (getClass() != obj.getClass()) {
159 return false;
160 }
161 ChecklistEditorInput other = (ChecklistEditorInput) obj;
162 if (classification == null) {
163 if (other.classification != null) {
164 return false;
165 }
166 } else if (!classification.equals(other.classification)) {
167 return false;
168 }
169 if (taxonNode == null) {
170 if (other.taxonNode != null) {
171 return false;
172 }
173 } else if (!taxonNode.equals(other.taxonNode)) {
174 return false;
175 }
176 return true;
177 }
178
179 /* (non-Javadoc)
180 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
181 */
182 @Override
183 public void saveState(IMemento memento) {
184 UUID uuid = classification.getUuid();
185 }
186
187 @Override
188 public void merge() {
189 if (!getCdmEntitySession().isActive()){
190 getCdmEntitySession().bind();
191 }
192 CdmStore.getService(ITaxonService.class).merge(taxaToSave, true);
193 }
194
195 /* (non-Javadoc)
196 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
197 */
198 @Override
199 public List<TaxonNode> getRootEntities() {
200 return Arrays.asList(taxonNode);
201 }
202
203 /**
204 * @return the conversation
205 */
206 public ConversationHolder getConversation() {
207 return conversation;
208 }
209
210 private List<String> getTaxonNodePropertyPaths() {
211 List<String> taxonNodePropertyPaths = new ArrayList<String>();
212 for(String propertyPath : getTaxonBasePropertyPaths()) {
213 taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
214 }
215 return taxonNodePropertyPaths;
216 }
217
218 private List<String> getTaxonBasePropertyPaths() {
219 List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
220 "sec", //$NON-NLS-1$
221 "createdBy", //$NON-NLS-1$
222 "updatedBy", //$NON-NLS-1$
223 "annotations", //$NON-NLS-1$
224 "markers", //$NON-NLS-1$
225 "credits", //$NON-NLS-1$
226 "extensions", //$NON-NLS-1$
227 "rights", //$NON-NLS-1$
228 "sources", //$NON-NLS-1$
229 "descriptions", //$NON-NLS-1$
230 "relationsToThisTaxon", //$NON-NLS-1$
231 "relationsFromThisTaxon", //$NON-NLS-1$
232 "taxonNodes", //$NON-NLS-1$
233 "descriptions.descriptionElements.feature", //$NON-NLS-1$
234 "descriptions.descriptionElements.area", //$NON-NLS-1$
235 "descriptions.descriptionElements.status", //$NON-NLS-1$
236 "descriptions.markers", //$NON-NLS-1$
237 "name.descriptions", //$NON-NLS-1$
238 "name.typeDesignations", //$NON-NLS-1$
239 "name.status", //$NON-NLS-1$
240 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
241 "name.taxonBases.taxonNodes", //$NON-NLS-1$
242 "name.relationsFromThisName", //$NON-NLS-1$
243 "name.relationsToThisName", //$NON-NLS-1$
244 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
245 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
246 "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
247 "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
248 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
249 "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
250 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
251 });
252
253 return taxonBasePropertyPaths;
254 }
255
256
257 public void setTaxa(List<TaxonBase> taxonList) {
258 this.taxa = taxonList;
259
260 }
261
262
263 /**
264 * @param element
265 */
266 public void addTaxonToSave(Taxon element) {
267 this.taxaToSave.add(element);
268
269 }
270
271 // @Override
272 // public void dispose(){
273 // conversation.unbind();
274 // conversation.close();
275 // getCdmEntitySession().dispose();
276 // super.dispose();
277 // }
278
279 }