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