Project

General

Profile

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

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

    
94
    @Inject
95
	private MDirtyable dirty;
96

    
97
    private AbstractBulkEditorInput input;
98

    
99
    private ConversationHolder conversation;
100

    
101
    @Inject
102
    private IEventBroker eventBroker;
103

    
104
    @Inject
105
    IEclipseContext context;
106

    
107
    @Inject
108
    private MPart thisPart;
109

    
110
    private BulkEditorQuery lastQuery = null;
111

    
112
    private BulkEditorComposite bulkEditorComposite;
113

    
114
    private Set<AbstractPostOperation> operations = new HashSet<>();
115

    
116
    @Inject
117
    public BulkEditor() {
118
	}
119

    
120
    public void init(AbstractBulkEditorInput<?> input){
121
	    this.input = input;
122
	    this.conversation = input.getConversation();
123
	    thisPart.setLabel(input.getEditorName());
124

    
125
	    bulkEditorComposite.init(input);
126
	}
127

    
128
	@PostConstruct
129
	public void createPartControl(Composite parent, IEclipseContext context) {
130
	    bulkEditorComposite = new BulkEditorComposite(this, parent, SWT.NONE);
131
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
132
	}
133

    
134
	@Override
135
	@Persist
136
	public void save(IProgressMonitor monitor) {
137
	    save(monitor, true);
138
	}
139

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

    
150
                status = entry.execute(new NullProgressMonitor(), uiInfoAdapter);
151
            } catch (ExecutionException e) {
152
                MessagingUtils.operationDialog(AbstractUtility.class, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
153
            }
154

    
155
            IPostOperationEnabled postOperationEnabled = entry
156
                    .getPostOperationEnabled();
157
            if (postOperationEnabled != null) {
158
                postOperationEnabled.onComplete();
159
            }
160

    
161
            //AbstractUtility.executeOperation(entry,sync);
162
        }
163

    
164
        operations.clear();
165
        if (!input.getCdmEntitySession().isActive()){
166
            input.getCdmEntitySession().bind();
167
        }
168
        input.saveModel(resetMerge);
169

    
170
//       IStructuredSelection selection = getSelection();
171

    
172
        dirty.setDirty(false);
173
        input.dispose();
174
        input.bind();
175
        conversation.commit(true);
176

    
177
        if (lastQuery != null){
178
            bulkEditorComposite.performSearch(lastQuery, selection);
179
        }
180

    
181
    }
182

    
183

    
184
	private IStructuredSelection getLastSelection() {
185
		return new StructuredSelection(this.bulkEditorComposite.getLastSelectedObject());
186
	}
187

    
188
	@Override
189
    @Focus
190
	public void setFocus() {
191
        //make sure to bind again if maybe in another view the conversation was unbound
192
        if(conversation!=null && !conversation.isBound()){
193
            conversation.bind();
194
        }
195
        if(input!=null && input.getCdmEntitySession()!= null) {
196
            input.getCdmEntitySession().bind();
197
        }
198

    
199
	    //make sure to bind again if maybe in another view the conversation was unbound
200
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
201
	}
202

    
203
	@PreDestroy
204
	public void dispose() {
205
	    if(conversation!=null){
206
	        conversation.unregisterForDataStoreChanges(this);
207
	        conversation.close();
208
	    }
209
	    if(input!=null){
210
	        input.dispose();
211
	    }
212
	    dirty.setDirty(false);
213
	    //save table settings
214
	    if(bulkEditorComposite.getNatTableState()!=null){
215
            try (FileOutputStream tableStateStream =
216
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
217
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
218
            } catch (IOException ioe) {
219
                ioe.printStackTrace();
220
            }
221
        }
222
	}
223

    
224
    @Optional
225
    @Inject
226
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
227
        if(selection!=null){
228
            setSelection(selection);
229
        }
230
    }
231

    
232
    public void refresh(){
233
        bulkEditorComposite.refresh();
234
    }
235

    
236
    public IStructuredSelection getSelection(){
237
        return bulkEditorComposite.getSelection();
238
    }
239

    
240
    public IStructuredSelection getCellSelection(){
241
        return bulkEditorComposite.getCellSelection();
242
    }
243

    
244
    @Override
245
    public void setSelection(IStructuredSelection selection){
246
        bulkEditorComposite.setSelection(selection);
247
    }
248

    
249
    public void setDirty(boolean isDirty){
250
        dirty.setDirty(isDirty);
251
    }
252

    
253
    public void setDirty(){
254
        setDirty(true);
255
    }
256

    
257
    @Override
258
    public boolean isDirty() {
259
        return dirty.isDirty();
260
    }
261

    
262
    public AbstractBulkEditorInput getEditorInput() {
263
        return input;
264
    }
265

    
266
    public void copyDataToClipboard() {
267
        bulkEditorComposite.copyDataToClipboard();
268
    }
269

    
270
    @Override
271
    public void update(CdmDataChangeMap arg0) {
272
    }
273

    
274
    @Override
275
    public boolean canAttachMedia() {
276
        return true;
277
    }
278

    
279
    @Override
280
    public void changed(Object element) {
281
        if(element instanceof DerivedUnitFacade){
282
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
283
            if(derivedUnit!=null){
284
                getEditorInput().addSaveCandidate(derivedUnit);
285
            }
286
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
287
            if(fieldUnit!=null){
288
                getEditorInput().addSaveCandidate(fieldUnit);
289
            }
290
        }
291
        else if (element instanceof CdmBase) {
292
            if (element instanceof DescriptionBase){
293
                if (element instanceof TaxonNameDescription){
294
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
295
                    if (getEditorInput() instanceof TaxonEditorInput){
296
                        IStructuredSelection sel = getSelection();
297
                        Object firstElement = sel.getFirstElement();
298
                        if (firstElement instanceof TaxonBase){
299
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
300
                        }
301
                    }else{
302
                        getEditorInput().addSaveCandidate(changedName);
303
                    }
304

    
305
                    input.replaceInModel(changedName);
306
                }else if (element instanceof TaxonDescription){
307
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
308
                    getEditorInput().addSaveCandidate(changedTaxon);
309
                    input.replaceInModel(changedTaxon);
310
                }
311
            }else if (element instanceof DescriptionElementBase){
312
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
313
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
314
                    //check whether the bulk editor is taxon or name bulk editor
315
                    if (getEditorInput() instanceof TaxonEditorInput){
316
                        IStructuredSelection sel = getSelection();
317
                        Object firstElement = sel.getFirstElement();
318
                        if (firstElement instanceof TaxonBase){
319
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
320
                        }
321
                    }else{
322
                        getEditorInput().addSaveCandidate(changedName);
323
                    }
324
                    input.replaceInModel(changedName);
325
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
326
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
327
                    getEditorInput().addSaveCandidate(changedTaxon);
328
                    input.replaceInModel(changedTaxon);
329
                }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
330
                    SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
331
                    getEditorInput().addSaveCandidate(changedSpecimen);
332
                    input.replaceInModel(changedSpecimen);
333
                }
334
            }else if (element instanceof Media && input instanceof MediaEditorInput){
335
            	getEditorInput().addSaveCandidate((Media)element);
336
                input.replaceInModel((Media)element);
337
            }else if (element instanceof Media ) {
338
                IStructuredSelection sel = getSelection();
339
                Object firstElement = sel.getFirstElement();
340
                if (firstElement instanceof TaxonBase){
341
                    TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
342
                    getEditorInput().addSaveCandidate(changedTaxon);
343
                    input.replaceInModel(changedTaxon);
344
                }else if (firstElement instanceof SpecimenOrObservationBase){
345
                    SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
346
                    getEditorInput().addSaveCandidate(changedSpecimen);
347
                    input.replaceInModel(changedSpecimen);
348
                }
349
            }else if(element instanceof Group){
350
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
351
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
352
                 getEditorInput().addSaveCandidate((Group)element);
353
                 input.replaceInModel((CdmBase) element);
354
            }else if(element instanceof TaxonName && input instanceof TaxonEditorInput){
355
                IStructuredSelection sel = this.getSelection();
356
                TaxonBase taxonBase = null;
357
                if (sel.getFirstElement() instanceof TaxonBase){
358
                    taxonBase = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonBase.class);
359
                }
360

    
361
                getEditorInput().addSaveCandidate(taxonBase);
362
                input.replaceInModel( taxonBase);
363
            }else if(element instanceof TypeDesignationBase ){
364
                IStructuredSelection sel = this.getSelection();
365
                TaxonName name = null;
366
                TaxonBase taxon = null;
367
                if (sel.getFirstElement() instanceof TaxonName){
368
                    name = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonName.class);
369
                }else if (sel.getFirstElement() instanceof TaxonBase) {
370
                	taxon = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonBase.class);
371
                }
372
                if (name != null) {
373
                	getEditorInput().addSaveCandidate(name);
374
                	input.replaceInModel( name);
375
                }else {
376
                	getEditorInput().addSaveCandidate(taxon);
377
                	input.replaceInModel(taxon);
378
                }
379

    
380
            }
381
            else{
382
                getEditorInput().addSaveCandidate((CdmBase)element);
383
                input.replaceInModel((CdmBase) element);
384
            }
385
        }
386
        dirty.setDirty(true);
387
        setSelection(new StructuredSelection(element));
388
    }
389

    
390
    @Override
391
    public void forceDirty() {
392
        dirty.setDirty(true);
393
    }
394

    
395
    @Override
396
    public boolean postOperation(Object objectAffectedByOperation) {
397
        return false;
398
    }
399

    
400
    @Override
401
    public boolean onComplete() {
402
        return false;
403
    }
404

    
405
    @Override
406
    public ConversationHolder getConversationHolder() {
407
        return conversation;
408
    }
409

    
410
    public BulkEditorQuery getLastQuery() {
411
       return lastQuery;
412
    }
413

    
414
    public void setLastQuery(BulkEditorQuery lastQuery) {
415
       this.lastQuery = lastQuery;
416

    
417
    }
418

    
419
    @Override
420
    public Taxon getTaxon() {
421
        IStructuredSelection selection = getSelection();
422
        if(selection.size()==1) {
423
            Object object = selection.iterator().next();
424
            if(object instanceof Taxon){
425
                return (Taxon) object;
426
            }
427
        }
428
        return null;
429
    }
430

    
431
    @Override
432
    public void update() {
433
        input.performSearch(lastQuery, getSelection());
434
    }
435

    
436
    @Override
437
    public void addOperation(AbstractPostOperation operation) {
438
        operations.add(operation);
439
    }
440

    
441
    @Inject
442
    @Optional
443
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
444
        if (getEditorInput() instanceof TaxonEditorInput){
445
            for (CdmBase cdmBase: cdmBases){
446
                if (getEditorInput().getModel().contains(cdmBase)){
447
                    input.performSearch(lastQuery, getSelection());
448
                    break;
449
                }
450
            }
451
        }
452
    }
453

    
454
    @Inject
455
    @Optional
456
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
457
        if (input instanceof GroupEditorInput){
458
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
459
        }
460
    }
461

    
462
    @Override
463
    public TaxonNode getTaxonNode() {
464
        return null;
465
    }
466

    
467
}
(1-1/9)