4c66f553695ef34415a415a22f61f09e6c8c7537
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / DerivateViewEditorInput.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.derivate;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IPersistableElement;
25
26 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
27 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
29 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
30 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
31 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
36
37 /**
38 * Editor input for the {@link DerivateView} which holds the currently selected derivate for which
39 * the derivate hierarchy should be shown in the DerivateView.<br>
40 * It also holds a {@link SpecimenOrObservationBase} which is the root of the hierarchy. (both may be the same object)
41 * @author pplitzner
42 * @date 25.11.2013
43 *
44 */
45 public class DerivateViewEditorInput extends CdmEntitySessionInput implements IEditorInput {
46
47 /**
48 * The selected derivate {@link UUID}s
49 */
50 private Set<SpecimenOrObservationBase<?>> derivateEntities;
51 /**
52 * List of the {@link UUID}s of the root elements of the hierarchy (may be the same objects as the derivates)
53 */
54 private Set<SpecimenOrObservationBase<?>> rootEntities;
55 private Set<UUID> rootUUIDs;
56
57 private final ConversationHolder conversationHolder;
58
59 private static final List<String> SPECIMEN_INIT_STRATEGY = Arrays.asList(new String[] {
60 "descriptions",
61 "annotations",
62 "markers",
63 "credits",
64 "extensions",
65 "rights",
66 "sources",
67 "derivationEvents.derivatives.annotations",
68 "derivationEvents.derivatives.markers",
69 "derivationEvents.derivatives.credits",
70 "derivationEvents.derivatives.extensions",
71 "derivationEvents.derivatives.rights",
72 "derivationEvents.derivatives.sources"
73 });
74
75 /**
76 * Creates an editor input for the {@link DerivateView} with the currently selected derivates and the
77 * corresponding {@link FieldUnit}s (both may be the same object).
78 * @param derivateUuids the {@link UUID}s of the derivates for which the derivate hierarchy should be shown
79 * @param rootUUIDs the root of the hierarchy
80 */
81 public DerivateViewEditorInput(Set<UUID> derivateUuids) {
82 super(false);
83 rootUUIDs = derivateUuids;
84 //FIXME:Remoting temporary hack for making the sessions work
85 //This should ideally be changed to initializing the
86 //super class with a collection of (id) objects which can
87 //then be used for the hashCode, equals methods
88 initSession();
89 this.conversationHolder = CdmStore.createConversation();
90 updateRootEntities(derivateUuids);
91
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
96 */
97 @Override
98 public Object getAdapter(Class adapter) {
99 // TODO Auto-generated method stub
100 return null;
101 }
102
103 /* (non-Javadoc)
104 * @see org.eclipse.ui.IEditorInput#exists()
105 */
106 @Override
107 public boolean exists() {
108 return false;
109 }
110
111 /* (non-Javadoc)
112 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
113 */
114 @Override
115 public ImageDescriptor getImageDescriptor() {
116 // TODO Auto-generated method stub
117 return null;
118 }
119
120 /* (non-Javadoc)
121 * @see org.eclipse.ui.IEditorInput#getName()
122 */
123 @Override
124 public String getName() {
125 return getEditorName();
126 }
127
128 /* (non-Javadoc)
129 * @see org.eclipse.ui.IEditorInput#getPersistable()
130 */
131 @Override
132 public IPersistableElement getPersistable() {
133 return null;
134 }
135
136 /* (non-Javadoc)
137 * @see org.eclipse.ui.IEditorInput#getToolTipText()
138 */
139 @Override
140 public String getToolTipText() {
141 return getEditorName();
142 }
143
144 private String getEditorName() {
145 String name = null;
146 for( SpecimenOrObservationBase<?> specimen : rootEntities){
147 if(specimen!=null){
148 if(name==null){
149 name = DerivateLabelProvider.getDerivateText(specimen, conversationHolder);
150 }
151 else{
152 name += " + "+DerivateLabelProvider.getDerivateText(specimen, conversationHolder); //$NON-NLS-1$
153 }
154 }
155 }
156 if(name==null){
157 name = "Derivative Editor";
158 }
159 return name;
160 }
161
162 @Override
163 public Set<SpecimenOrObservationBase<?>> getRootEntities() {
164 return rootEntities;
165 }
166
167 public Set<SpecimenOrObservationBase<?>> getDerivateEntities() {
168 return derivateEntities;
169 }
170
171 public void addRootEntity(SpecimenOrObservationBase<?> root){
172 rootEntities.add(root);
173 }
174
175
176 public ConversationHolder getConversationHolder() {
177 return conversationHolder;
178 }
179
180 @Override
181 public int hashCode() {
182 final int prime = 31;
183 int result = 1;
184 result = prime * result + ((rootUUIDs == null) ? 0 : rootUUIDs.hashCode());
185 return result;
186 }
187
188 @Override
189 public boolean equals(Object obj) {
190 if (this == obj) {
191 return true;
192 }
193 if (obj == null) {
194 return false;
195 }
196 if (getClass() != obj.getClass()) {
197 return false;
198 }
199 DerivateViewEditorInput other = (DerivateViewEditorInput) obj;
200 if (rootUUIDs == null) {
201 if (other.rootUUIDs != null) {
202 return false;
203 }
204 } else if (!rootUUIDs.equals(other.rootUUIDs)) {
205 return false;
206 }
207 return true;
208 }
209
210 @Override
211 public void merge() {
212 if(CdmStore.getCurrentSessionManager().isRemoting()) {
213 CdmApplicationState.getCurrentAppConfig().getOccurrenceService().merge(new ArrayList(getRootEntities()), true);
214 }
215
216 }
217
218 @Override
219 public Map<Object, List<String>> getPropertyPathsMap() {
220 List<String> specimenPropertyPaths = Arrays.asList(new String[] {
221 "descriptions",
222 "derivationEvents.derivates",
223 "annotations",
224 "markers",
225 "credits",
226 "extensions",
227 "rights",
228 "sources"
229 });
230 Map<Object, List<String>> specimenPropertyPathMap =
231 new HashMap<Object, List<String>>();
232 specimenPropertyPathMap.put(SpecimenOrObservationBase.class,specimenPropertyPaths);
233 return specimenPropertyPathMap;
234 }
235
236 public void updateRootEntities(Collection<UUID> derivateUuids) {
237 this.derivateEntities = new HashSet<SpecimenOrObservationBase<?>>();
238 this.rootEntities = new HashSet<SpecimenOrObservationBase<?>>();
239 for (UUID uuid : derivateUuids) {
240 SpecimenOrObservationBase<?> derivate = CdmStore.getService(IOccurrenceService.class).load(uuid, SPECIMEN_INIT_STRATEGY);
241 derivateEntities.add(derivate);
242 if(derivate instanceof FieldUnit){
243 rootEntities.add(derivate);
244 }
245 else if(derivate instanceof DerivedUnit){
246 SpecimenOrObservationBase<?> topMostDerivate = EditorUtil.getTopMostDerivate(derivate);
247 if(topMostDerivate!=null){
248 rootEntities.add(topMostDerivate);
249 }
250 }
251 }
252 if(rootEntities.isEmpty()){
253 rootEntities = derivateEntities;
254 }
255 }
256
257 }