Project

General

Profile

Download (16.6 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.taxeditor.bulkeditor.e4;
10

    
11
import java.io.FileOutputStream;
12
import java.io.IOException;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import javax.annotation.PostConstruct;
18
import javax.annotation.PreDestroy;
19
import javax.inject.Inject;
20

    
21
import org.eclipse.core.commands.ExecutionException;
22
import org.eclipse.core.commands.operations.IOperationHistory;
23
import org.eclipse.core.runtime.IAdaptable;
24
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.core.runtime.IStatus;
26
import org.eclipse.core.runtime.NullProgressMonitor;
27
import org.eclipse.core.runtime.Status;
28
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
29
import org.eclipse.e4.core.contexts.IEclipseContext;
30
import org.eclipse.e4.core.di.annotations.Optional;
31
import org.eclipse.e4.core.services.events.IEventBroker;
32
import org.eclipse.e4.ui.di.Focus;
33
import org.eclipse.e4.ui.di.Persist;
34
import org.eclipse.e4.ui.di.UIEventTopic;
35
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
36
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
37
import org.eclipse.jface.viewers.IStructuredSelection;
38
import org.eclipse.jface.viewers.StructuredSelection;
39
import org.eclipse.swt.SWT;
40
import org.eclipse.swt.widgets.Composite;
41
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
42

    
43
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
44
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
45
import eu.etaxonomy.cdm.model.common.CdmBase;
46
import eu.etaxonomy.cdm.model.description.DescriptionBase;
47
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
48
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
49
import eu.etaxonomy.cdm.model.description.TaxonDescription;
50
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
51
import eu.etaxonomy.cdm.model.media.Media;
52
import eu.etaxonomy.cdm.model.name.TaxonName;
53
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
54
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
55
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
56
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
57
import eu.etaxonomy.cdm.model.permission.Group;
58
import eu.etaxonomy.cdm.model.permission.User;
59
import eu.etaxonomy.cdm.model.taxon.Taxon;
60
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
61
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
62
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
63
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
64
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
65
import eu.etaxonomy.taxeditor.bulkeditor.input.GroupEditorInput;
66
import eu.etaxonomy.taxeditor.bulkeditor.input.MediaEditorInput;
67
import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
68
import eu.etaxonomy.taxeditor.editor.IBulkEditor;
69
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
70
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
71
import eu.etaxonomy.taxeditor.model.AbstractUtility;
72
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
73
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
74
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
75
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
76
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
77
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
78
import eu.etaxonomy.taxeditor.model.MessagingUtils;
79
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
80
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
81
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
82
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
83

    
84
/**
85
 * @author pplitzner
86
 * @since Sep 8, 2017
87
 */
88
public class BulkEditor implements IPartContentHasDetails, IPostOperationEnabled,
89
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
90
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor, IBulkEditor {
91

    
92
    @Inject
93
	private MDirtyable dirty;
94

    
95
    private AbstractBulkEditorInput input;
96

    
97
    @Inject
98
    private IEventBroker eventBroker;
99

    
100
    @Inject
101
    IEclipseContext context;
102

    
103
    @Inject
104
    private MPart thisPart;
105

    
106
    private BulkEditorQuery lastQuery = null;
107

    
108
    private BulkEditorComposite bulkEditorComposite;
109

    
110
    private Set<AbstractPostOperation> operations = new HashSet<>();
111

    
112
    @Inject
113
    public BulkEditor() {
114
	}
115

    
116
    public void init(AbstractBulkEditorInput<?> input){
117
	    this.input = input;
118
	    thisPart.setLabel(input.getEditorName());
119

    
120
	    bulkEditorComposite.init(input);
121
	}
122

    
123
	@PostConstruct
124
	public void createPartControl(Composite parent, IEclipseContext context) {
125
	    bulkEditorComposite = new BulkEditorComposite(this, parent, SWT.NONE);
126
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
127
	}
128

    
129
	@Override
130
	@Persist
131
	public void save(IProgressMonitor monitor) {
132
	    save(monitor, true);
133
	}
134

    
135
    public void save(IProgressMonitor monitor, boolean resetMerge) {
136
    	IStructuredSelection selection = getLastSelection();
137
    	for(AbstractPostOperation<?> entry:operations){
138
            IStatus status = Status.CANCEL_STATUS;
139
            final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
140
                    .getUIInfoAdapter(AbstractUtility.getShell());
141
            String operationlabel = entry.getLabel();
142
            try {
143
                entry.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
144

    
145
                status = entry.execute(new NullProgressMonitor(), uiInfoAdapter);
146
            } catch (ExecutionException e) {
147
                MessagingUtils.operationDialog(AbstractUtility.class, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
148
            }
149

    
150
            IPostOperationEnabled postOperationEnabled = entry
151
                    .getPostOperationEnabled();
152
            if (postOperationEnabled != null) {
153
                postOperationEnabled.onComplete();
154
            }
155

    
156
            //AbstractUtility.executeOperation(entry,sync);
157
        }
158

    
159
        operations.clear();
160
        if (!input.getCdmEntitySession().isActive()){
161
            input.getCdmEntitySession().bind();
162
        }
163
        input.saveModel(resetMerge);
164

    
165
        dirty.setDirty(false);
166
        input.dispose();
167
        input.bind();
168

    
169
        if (lastQuery != null){
170
            bulkEditorComposite.performSearch(lastQuery, selection);
171
        }
172

    
173
    }
174

    
175

    
176
	private IStructuredSelection getLastSelection() {
177
		return new StructuredSelection(this.bulkEditorComposite.getLastSelectedObject());
178
	}
179

    
180
	@Override
181
    @Focus
182
	public void setFocus() {
183
        //make sure to bind again if maybe in another view the conversation was unbound
184

    
185
        if(input!=null && input.getCdmEntitySession()!= null) {
186
            input.getCdmEntitySession().bind();
187
        }
188

    
189
	    //make sure to bind again if maybe in another view the conversation was unbound
190
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
191
	}
192

    
193
	@PreDestroy
194
	public void dispose() {
195
	    if(input!=null){
196
	        input.dispose();
197
	    }
198
	    dirty.setDirty(false);
199
	    //save table settings
200
	    if(bulkEditorComposite.getNatTableState()!=null){
201
            try (FileOutputStream tableStateStream =
202
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
203
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
204
            } catch (IOException ioe) {
205
                ioe.printStackTrace();
206
            }
207
        }
208
	}
209

    
210
    @Optional
211
    @Inject
212
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
213
        if(selection!=null){
214
            setSelection(selection);
215
        }
216
    }
217

    
218
    public void refresh(){
219
        bulkEditorComposite.refresh();
220
    }
221

    
222
    public IStructuredSelection getSelection(){
223
        return bulkEditorComposite.getSelection();
224
    }
225

    
226
    public IStructuredSelection getCellSelection(){
227
        return bulkEditorComposite.getCellSelection();
228
    }
229

    
230
    @Override
231
    public void setSelection(IStructuredSelection selection){
232
        bulkEditorComposite.setSelection(selection);
233
    }
234

    
235
    public void setDirty(boolean isDirty){
236
        dirty.setDirty(isDirty);
237
    }
238

    
239
    public void setDirty(){
240
        setDirty(true);
241
    }
242

    
243
    @Override
244
    public boolean isDirty() {
245
        return dirty.isDirty();
246
    }
247

    
248
    public AbstractBulkEditorInput getEditorInput() {
249
        return input;
250
    }
251

    
252
    public void copyDataToClipboard() {
253
        bulkEditorComposite.copyDataToClipboard();
254
    }
255

    
256
    @Override
257
    public boolean canAttachMedia() {
258
        return true;
259
    }
260

    
261
    @Override
262
    public void changed(Object element) {
263
        if(element instanceof DerivedUnitFacade){
264
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
265
            if(derivedUnit!=null){
266
                getEditorInput().addSaveCandidate(derivedUnit);
267
            }
268
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
269
            if(fieldUnit!=null){
270
                getEditorInput().addSaveCandidate(fieldUnit);
271
            }
272
        }
273
        else if (element instanceof CdmBase) {
274
            if (element instanceof DescriptionBase){
275
                if (element instanceof TaxonNameDescription){
276
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
277
                    if (getEditorInput() instanceof TaxonEditorInput){
278
                        IStructuredSelection sel = getSelection();
279
                        Object firstElement = sel.getFirstElement();
280
                        if (firstElement instanceof TaxonBase){
281
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
282
                        }
283
                    }else{
284
                        getEditorInput().addSaveCandidate(changedName);
285
                    }
286

    
287
                    input.replaceInModel(changedName);
288
                }else if (element instanceof TaxonDescription){
289
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
290
                    getEditorInput().addSaveCandidate(changedTaxon);
291
                    input.replaceInModel(changedTaxon);
292
                }
293
            }else if (element instanceof DescriptionElementBase){
294
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
295
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
296
                    //check whether the bulk editor is taxon or name bulk editor
297
                    if (getEditorInput() instanceof TaxonEditorInput){
298
                        IStructuredSelection sel = getSelection();
299
                        Object firstElement = sel.getFirstElement();
300
                        if (firstElement instanceof TaxonBase){
301
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
302
                        }
303
                    }else{
304
                        getEditorInput().addSaveCandidate(changedName);
305
                    }
306
                    input.replaceInModel(changedName);
307
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
308
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
309
                    getEditorInput().addSaveCandidate(changedTaxon);
310
                    input.replaceInModel(changedTaxon);
311
                }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
312
                    SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
313
                    getEditorInput().addSaveCandidate(changedSpecimen);
314
                    input.replaceInModel(changedSpecimen);
315
                }
316
            }else if (element instanceof Media && input instanceof MediaEditorInput){
317
            	getEditorInput().addSaveCandidate((Media)element);
318
                input.replaceInModel((Media)element);
319
            }else if (element instanceof Media ) {
320
                IStructuredSelection sel = getSelection();
321
                Object firstElement = sel.getFirstElement();
322
                if (firstElement instanceof TaxonBase){
323
                    TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
324
                    getEditorInput().addSaveCandidate(changedTaxon);
325
                    input.replaceInModel(changedTaxon);
326
                }else if (firstElement instanceof SpecimenOrObservationBase){
327
                    SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
328
                    getEditorInput().addSaveCandidate(changedSpecimen);
329
                    input.replaceInModel(changedSpecimen);
330
                }
331
            }else if(element instanceof Group){
332
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
333
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
334
                 getEditorInput().addSaveCandidate((Group)element);
335
                 input.replaceInModel((CdmBase) element);
336
            }else if(element instanceof TaxonName && input instanceof TaxonEditorInput){
337
                IStructuredSelection sel = this.getSelection();
338
                TaxonBase taxonBase = null;
339
                if (sel.getFirstElement() instanceof TaxonBase){
340
                    taxonBase = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonBase.class);
341
                }
342

    
343
                getEditorInput().addSaveCandidate(taxonBase);
344
                input.replaceInModel( taxonBase);
345
            }else if(element instanceof TypeDesignationBase ){
346
                IStructuredSelection sel = this.getSelection();
347
                TaxonName name = null;
348
                TaxonBase taxon = null;
349
                if (sel.getFirstElement() instanceof TaxonName){
350
                    name = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonName.class);
351
                }else if (sel.getFirstElement() instanceof TaxonBase) {
352
                	taxon = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonBase.class);
353
                }
354
                if (name != null) {
355
                	getEditorInput().addSaveCandidate(name);
356
                	input.replaceInModel( name);
357
                }else {
358
                	getEditorInput().addSaveCandidate(taxon);
359
                	input.replaceInModel(taxon);
360
                }
361

    
362
            }
363
            else{
364
                getEditorInput().addSaveCandidate((CdmBase)element);
365
                input.replaceInModel((CdmBase) element);
366
            }
367
        }
368
        dirty.setDirty(true);
369
        setSelection(new StructuredSelection(element));
370
    }
371

    
372
    @Override
373
    public void forceDirty() {
374
        dirty.setDirty(true);
375
    }
376

    
377
    @Override
378
    public boolean postOperation(Object objectAffectedByOperation) {
379
        return false;
380
    }
381

    
382
    @Override
383
    public boolean onComplete() {
384
        return false;
385
    }
386

    
387
    
388

    
389
    public BulkEditorQuery getLastQuery() {
390
       return lastQuery;
391
    }
392

    
393
    public void setLastQuery(BulkEditorQuery lastQuery) {
394
       this.lastQuery = lastQuery;
395

    
396
    }
397

    
398
    @Override
399
    public Taxon getTaxon() {
400
        IStructuredSelection selection = getSelection();
401
        if(selection.size()==1) {
402
            Object object = selection.iterator().next();
403
            if(object instanceof Taxon){
404
                return (Taxon) object;
405
            }
406
        }
407
        return null;
408
    }
409

    
410
    @Override
411
    public void update() {
412
        input.performSearch(lastQuery, getSelection());
413
    }
414

    
415
    @Override
416
    public void addOperation(AbstractPostOperation operation) {
417
        operations.add(operation);
418
    }
419

    
420
    @Inject
421
    @Optional
422
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
423
        if (getEditorInput() instanceof TaxonEditorInput){
424
            for (CdmBase cdmBase: cdmBases){
425
                if (getEditorInput().getModel().contains(cdmBase)){
426
                    input.performSearch(lastQuery, getSelection());
427
                    break;
428
                }
429
            }
430
        }
431
    }
432

    
433
    @Inject
434
    @Optional
435
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
436
        if (input instanceof GroupEditorInput){
437
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
438
        }
439
    }
440

    
441
    @Override
442
    public TaxonNode getTaxonNode() {
443
        return null;
444
    }
445

    
446
}
(1-1/9)