Merge branch 'hotfix/5.18.2'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / BulkEditorE4.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.bulkeditor.e4;
11
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.util.List;
15
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
22 import org.eclipse.e4.core.contexts.IEclipseContext;
23 import org.eclipse.e4.core.di.annotations.Optional;
24 import org.eclipse.e4.core.services.events.IEventBroker;
25 import org.eclipse.e4.ui.di.Focus;
26 import org.eclipse.e4.ui.di.Persist;
27 import org.eclipse.e4.ui.di.UIEventTopic;
28 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
29 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.widgets.Composite;
34
35 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
36 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
37 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
38 import eu.etaxonomy.cdm.model.common.CdmBase;
39 import eu.etaxonomy.cdm.model.description.DescriptionBase;
40 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
42 import eu.etaxonomy.cdm.model.description.TaxonDescription;
43 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
44 import eu.etaxonomy.cdm.model.media.Media;
45 import eu.etaxonomy.cdm.model.name.TaxonName;
46 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
47 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
48 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
49 import eu.etaxonomy.cdm.model.permission.Group;
50 import eu.etaxonomy.cdm.model.permission.User;
51 import eu.etaxonomy.cdm.model.taxon.Taxon;
52 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
53 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
54 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
55 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
56 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
57 import eu.etaxonomy.taxeditor.bulkeditor.input.GroupEditorInput;
58 import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
59 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
60 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
61 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
62 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
63 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
64 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
65 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
66 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
67 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
68 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
69 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
70
71 /**
72 * @author pplitzner
73 * @since Sep 8, 2017
74 */
75 public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
76 IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
77 IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
78
79 @Inject
80 private MDirtyable dirty;
81
82 private AbstractBulkEditorInput input;
83
84 private ConversationHolder conversation;
85
86 @Inject
87 private IEventBroker eventBroker;
88
89 @Inject
90 IEclipseContext context;
91
92 @Inject
93 private MPart thisPart;
94
95 private BulkEditorQuery lastQuery = null;
96
97 private BulkEditorE4Composite bulkEditorComposite;
98
99 @Inject
100 public BulkEditorE4() {
101 }
102
103 public void init(AbstractBulkEditorInput<?> input){
104 this.input = input;
105 this.conversation = input.getConversation();
106 thisPart.setLabel(input.getEditorName());
107
108 bulkEditorComposite.init(input);
109 }
110
111 @PostConstruct
112 public void createPartControl(Composite parent, IEclipseContext context) {
113 bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
114 ContextInjectionFactory.inject(bulkEditorComposite, context);
115 }
116
117 @Override
118 @Persist
119 public void save(IProgressMonitor monitor) {
120 save(monitor, true);
121 }
122
123 public void save(IProgressMonitor monitor, boolean resetMerge) {
124 if (!input.getCdmEntitySession().isActive()){
125 input.getCdmEntitySession().bind();
126 }
127 input.saveModel(resetMerge);
128
129 IStructuredSelection selection = getSelection();
130
131 dirty.setDirty(false);
132 input.dispose();
133 input.bind();
134 conversation.commit(true);
135
136 if (lastQuery != null){
137 bulkEditorComposite.performSearch(lastQuery, selection);
138 }
139 }
140
141
142 @Focus
143 public void setFocus() {
144 //make sure to bind again if maybe in another view the conversation was unbound
145 if(conversation!=null && !conversation.isBound()){
146 conversation.bind();
147 }
148 if(input!=null && input.getCdmEntitySession()!= null) {
149 input.getCdmEntitySession().bind();
150 }
151
152 //make sure to bind again if maybe in another view the conversation was unbound
153 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
154 }
155
156 @PreDestroy
157 public void dispose() {
158 if(conversation!=null){
159 conversation.unregisterForDataStoreChanges(this);
160 conversation.close();
161 }
162 if(input!=null){
163 input.dispose();
164 }
165 dirty.setDirty(false);
166 //save table settings
167 if(bulkEditorComposite.getNatTableState()!=null){
168 try (FileOutputStream tableStateStream =
169 new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
170 bulkEditorComposite.getNatTableState().store(tableStateStream, null);
171 } catch (IOException ioe) {
172 ioe.printStackTrace();
173 }
174 }
175 }
176
177 @Optional
178 @Inject
179 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
180
181
182 if(selection!=null){
183 setSelection(selection);
184 }
185 }
186
187 public void refresh(){
188 bulkEditorComposite.refresh();
189 }
190
191 public IStructuredSelection getSelection(){
192 return bulkEditorComposite.getSelection();
193 }
194
195 public void setSelection(IStructuredSelection selection){
196 bulkEditorComposite.setSelection(selection);
197 }
198
199 public void setDirty(boolean isDirty){
200 dirty.setDirty(isDirty);
201 }
202
203 public void setDirty(){
204 setDirty(true);
205 }
206
207 @Override
208 public boolean isDirty() {
209 return dirty.isDirty();
210 }
211
212 public AbstractBulkEditorInput getEditorInput() {
213 return input;
214 }
215
216 public void copyDataToClipboard() {
217 bulkEditorComposite.copyDataToClipboard();
218 }
219
220 @Override
221 public void update(CdmDataChangeMap arg0) {
222 }
223
224 @Override
225 public boolean canAttachMedia() {
226 return true;
227 }
228
229 @Override
230 public void changed(Object element) {
231 if(element instanceof DerivedUnitFacade){
232 DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
233 if(derivedUnit!=null){
234 getEditorInput().addSaveCandidate(derivedUnit);
235 }
236 FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
237 if(fieldUnit!=null){
238 getEditorInput().addSaveCandidate(fieldUnit);
239 }
240 }
241 else if (element instanceof CdmBase) {
242 if (element instanceof DescriptionBase){
243 if (element instanceof TaxonNameDescription){
244 TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
245 if (getEditorInput() instanceof TaxonEditorInput){
246 IStructuredSelection sel = getSelection();
247 Object firstElement = sel.getFirstElement();
248 if (firstElement instanceof TaxonBase){
249 getEditorInput().addSaveCandidate((TaxonBase)firstElement);
250 }
251 }else{
252 getEditorInput().addSaveCandidate(changedName);
253 }
254
255 input.replaceInModel(changedName);
256 }else if (element instanceof TaxonDescription){
257 Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
258 getEditorInput().addSaveCandidate(changedTaxon);
259 input.replaceInModel(changedTaxon);
260 }
261 }else if (element instanceof DescriptionElementBase){
262 if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
263 TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
264 //check whether the bulk editor is taxon or name bulk editor
265 if (getEditorInput() instanceof TaxonEditorInput){
266 IStructuredSelection sel = getSelection();
267 Object firstElement = sel.getFirstElement();
268 if (firstElement instanceof TaxonBase){
269 getEditorInput().addSaveCandidate((TaxonBase)firstElement);
270 }
271 }else{
272 getEditorInput().addSaveCandidate(changedName);
273 }
274 input.replaceInModel(changedName);
275 }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
276 Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
277 getEditorInput().addSaveCandidate(changedTaxon);
278 input.replaceInModel(changedTaxon);
279 }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
280 SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
281 getEditorInput().addSaveCandidate(changedSpecimen);
282 input.replaceInModel(changedSpecimen);
283 }
284 }else if (element instanceof Media){
285 IStructuredSelection sel = getSelection();
286 Object firstElement = sel.getFirstElement();
287 if (firstElement instanceof TaxonBase){
288 TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
289 getEditorInput().addSaveCandidate(changedTaxon);
290 input.replaceInModel(changedTaxon);
291 }else if (firstElement instanceof SpecimenOrObservationBase){
292 SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
293 getEditorInput().addSaveCandidate(changedSpecimen);
294 input.replaceInModel(changedSpecimen);
295 }
296 }else if(element instanceof Group){
297 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
298 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
299 getEditorInput().addSaveCandidate((Group)element);
300 input.replaceInModel((CdmBase) element);
301 }else{
302 getEditorInput().addSaveCandidate((CdmBase)element);
303 input.replaceInModel((CdmBase) element);
304 }
305 }
306 dirty.setDirty(true);
307 setSelection(new StructuredSelection(element));
308 }
309
310 @Override
311 public void forceDirty() {
312 dirty.setDirty(true);
313 }
314
315 @Override
316 public boolean postOperation(Object objectAffectedByOperation) {
317 return false;
318 }
319
320 @Override
321 public boolean onComplete() {
322 return false;
323 }
324
325 @Override
326 public ConversationHolder getConversationHolder() {
327 return conversation;
328 }
329
330 public BulkEditorQuery getLastQuery() {
331 return lastQuery;
332 }
333
334 public void setLastQuery(BulkEditorQuery lastQuery) {
335 this.lastQuery = lastQuery;
336
337 }
338
339 @Override
340 public Taxon getTaxon() {
341 IStructuredSelection selection = getSelection();
342 if(selection.size()==1) {
343 Object object = selection.iterator().next();
344 if(object instanceof Taxon){
345 return (Taxon) object;
346 }
347 }
348 return null;
349 }
350
351 @Override
352 public void update() {
353 input.performSearch(lastQuery, getSelection());
354 }
355
356 @Override
357 public void addOperation(AbstractPostOperation operation) {
358 // operations not yet used for bulk editor
359 }
360
361 @Inject
362 @Optional
363 private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
364 if (getEditorInput() instanceof TaxonEditorInput){
365 for (CdmBase cdmBase: cdmBases){
366 if (getEditorInput().getModel().contains(cdmBase)){
367 input.performSearch(lastQuery, getSelection());
368 break;
369 }
370 }
371 }
372 }
373
374 @Inject
375 @Optional
376 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
377 if (input instanceof GroupEditorInput){
378 ((GroupEditorInput)input).getSaveUserCandidates().add(user);
379 }
380 }
381
382 @Override
383 public TaxonNode getTaxonNode() {
384 return null;
385 }
386
387 }