Project

General

Profile

Download (14.5 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.List;
14

    
15
import javax.annotation.PostConstruct;
16
import javax.annotation.PreDestroy;
17
import javax.inject.Inject;
18

    
19
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
21
import org.eclipse.e4.core.contexts.IEclipseContext;
22
import org.eclipse.e4.core.di.annotations.Optional;
23
import org.eclipse.e4.core.services.events.IEventBroker;
24
import org.eclipse.e4.ui.di.Focus;
25
import org.eclipse.e4.ui.di.Persist;
26
import org.eclipse.e4.ui.di.UIEventTopic;
27
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
28
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.jface.viewers.StructuredSelection;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.widgets.Composite;
33

    
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
36
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
37
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
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.IBulkEditor;
60
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
61
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
62
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
63
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
64
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
65
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
66
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
67
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
68
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
69
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
70
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
71

    
72
/**
73
 * @author pplitzner
74
 * @since Sep 8, 2017
75
 */
76
public class BulkEditor implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
77
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
78
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor, IBulkEditor {
79

    
80
    @Inject
81
	private MDirtyable dirty;
82

    
83
    private AbstractBulkEditorInput input;
84

    
85
    private ConversationHolder conversation;
86

    
87
    @Inject
88
    private IEventBroker eventBroker;
89

    
90
    @Inject
91
    IEclipseContext context;
92

    
93
    @Inject
94
    private MPart thisPart;
95

    
96
    private BulkEditorQuery lastQuery = null;
97

    
98
    private BulkEditorComposite bulkEditorComposite;
99

    
100
    @Inject
101
    public BulkEditor() {
102
	}
103

    
104
    public void init(AbstractBulkEditorInput<?> input){
105
	    this.input = input;
106
	    this.conversation = input.getConversation();
107
	    thisPart.setLabel(input.getEditorName());
108

    
109
	    bulkEditorComposite.init(input);
110
	}
111

    
112
	@PostConstruct
113
	public void createPartControl(Composite parent, IEclipseContext context) {
114
	    bulkEditorComposite = new BulkEditorComposite(this, parent, SWT.NONE);
115
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
116
	}
117

    
118
	@Override
119
	@Persist
120
	public void save(IProgressMonitor monitor) {
121
	    save(monitor, true);
122
	}
123

    
124
    public void save(IProgressMonitor monitor, boolean resetMerge) {
125
        if (!input.getCdmEntitySession().isActive()){
126
            input.getCdmEntitySession().bind();
127
        }
128
        input.saveModel(resetMerge);
129

    
130
        IStructuredSelection selection = getSelection();
131

    
132
        dirty.setDirty(false);
133
        input.dispose();
134
        input.bind();
135
        conversation.commit(true);
136

    
137
        if (lastQuery != null){
138
            bulkEditorComposite.performSearch(lastQuery, selection);
139
        }
140
    }
141

    
142

    
143
	@Focus
144
	public void setFocus() {
145
        //make sure to bind again if maybe in another view the conversation was unbound
146
        if(conversation!=null && !conversation.isBound()){
147
            conversation.bind();
148
        }
149
        if(input!=null && input.getCdmEntitySession()!= null) {
150
            input.getCdmEntitySession().bind();
151
        }
152

    
153
	    //make sure to bind again if maybe in another view the conversation was unbound
154
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
155
	}
156

    
157
	@PreDestroy
158
	public void dispose() {
159
	    if(conversation!=null){
160
	        conversation.unregisterForDataStoreChanges(this);
161
	        conversation.close();
162
	    }
163
	    if(input!=null){
164
	        input.dispose();
165
	    }
166
	    dirty.setDirty(false);
167
	    //save table settings
168
	    if(bulkEditorComposite.getNatTableState()!=null){
169
            try (FileOutputStream tableStateStream =
170
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
171
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
172
            } catch (IOException ioe) {
173
                ioe.printStackTrace();
174
            }
175
        }
176
	}
177

    
178
    @Optional
179
    @Inject
180
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
181
        if(selection!=null){
182
            setSelection(selection);
183
        }
184
    }
185

    
186
    public void refresh(){
187
        bulkEditorComposite.refresh();
188
    }
189

    
190
    public IStructuredSelection getSelection(){
191
        return bulkEditorComposite.getSelection();
192
    }
193

    
194
    public IStructuredSelection getCellSelection(){
195
        return bulkEditorComposite.getCellSelection();
196
    }
197

    
198
    public void setSelection(IStructuredSelection selection){
199
        bulkEditorComposite.setSelection(selection);
200
    }
201

    
202
    public void setDirty(boolean isDirty){
203
        dirty.setDirty(isDirty);
204
    }
205

    
206
    public void setDirty(){
207
        setDirty(true);
208
    }
209

    
210
    @Override
211
    public boolean isDirty() {
212
        return dirty.isDirty();
213
    }
214

    
215
    public AbstractBulkEditorInput getEditorInput() {
216
        return input;
217
    }
218

    
219
    public void copyDataToClipboard() {
220
        bulkEditorComposite.copyDataToClipboard();
221
    }
222

    
223
    @Override
224
    public void update(CdmDataChangeMap arg0) {
225
    }
226

    
227
    @Override
228
    public boolean canAttachMedia() {
229
        return true;
230
    }
231

    
232
    @Override
233
    public void changed(Object element) {
234
        if(element instanceof DerivedUnitFacade){
235
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
236
            if(derivedUnit!=null){
237
                getEditorInput().addSaveCandidate(derivedUnit);
238
            }
239
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
240
            if(fieldUnit!=null){
241
                getEditorInput().addSaveCandidate(fieldUnit);
242
            }
243
        }
244
        else if (element instanceof CdmBase) {
245
            if (element instanceof DescriptionBase){
246
                if (element instanceof TaxonNameDescription){
247
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
248
                    if (getEditorInput() instanceof TaxonEditorInput){
249
                        IStructuredSelection sel = getSelection();
250
                        Object firstElement = sel.getFirstElement();
251
                        if (firstElement instanceof TaxonBase){
252
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
253
                        }
254
                    }else{
255
                        getEditorInput().addSaveCandidate(changedName);
256
                    }
257

    
258
                    input.replaceInModel(changedName);
259
                }else if (element instanceof TaxonDescription){
260
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
261
                    getEditorInput().addSaveCandidate(changedTaxon);
262
                    input.replaceInModel(changedTaxon);
263
                }
264
            }else if (element instanceof DescriptionElementBase){
265
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
266
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
267
                    //check whether the bulk editor is taxon or name bulk editor
268
                    if (getEditorInput() instanceof TaxonEditorInput){
269
                        IStructuredSelection sel = getSelection();
270
                        Object firstElement = sel.getFirstElement();
271
                        if (firstElement instanceof TaxonBase){
272
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
273
                        }
274
                    }else{
275
                        getEditorInput().addSaveCandidate(changedName);
276
                    }
277
                    input.replaceInModel(changedName);
278
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
279
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
280
                    getEditorInput().addSaveCandidate(changedTaxon);
281
                    input.replaceInModel(changedTaxon);
282
                }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
283
                    SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
284
                    getEditorInput().addSaveCandidate(changedSpecimen);
285
                    input.replaceInModel(changedSpecimen);
286
                }
287
            }else if (element instanceof Media){
288
                IStructuredSelection sel = getSelection();
289
                Object firstElement = sel.getFirstElement();
290
                if (firstElement instanceof TaxonBase){
291
                    TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
292
                    getEditorInput().addSaveCandidate(changedTaxon);
293
                    input.replaceInModel(changedTaxon);
294
                }else if (firstElement instanceof SpecimenOrObservationBase){
295
                    SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
296
                    getEditorInput().addSaveCandidate(changedSpecimen);
297
                    input.replaceInModel(changedSpecimen);
298
                } else if (firstElement instanceof Media){
299
                    getEditorInput().addSaveCandidate((Media)element);
300
                    input.replaceInModel((Media)element);
301
                }
302
            }else if(element instanceof Group){
303
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
304
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
305
                 getEditorInput().addSaveCandidate((Group)element);
306
                 input.replaceInModel((CdmBase) element);
307
            }else if(element instanceof TaxonName && input instanceof TaxonEditorInput){
308
                IStructuredSelection sel = this.getSelection();
309
                TaxonBase taxonBase = null;
310
                if (sel.getFirstElement() instanceof TaxonBase){
311
                    taxonBase = HibernateProxyHelper.deproxy(sel.getFirstElement(), TaxonBase.class);
312
                }
313

    
314
                getEditorInput().addSaveCandidate(taxonBase);
315
                input.replaceInModel( taxonBase);
316
            }else{
317
                getEditorInput().addSaveCandidate((CdmBase)element);
318
                input.replaceInModel((CdmBase) element);
319
            }
320
        }
321
        dirty.setDirty(true);
322
        setSelection(new StructuredSelection(element));
323
    }
324

    
325
    @Override
326
    public void forceDirty() {
327
        dirty.setDirty(true);
328
    }
329

    
330
    @Override
331
    public boolean postOperation(Object objectAffectedByOperation) {
332
        return false;
333
    }
334

    
335
    @Override
336
    public boolean onComplete() {
337
        return false;
338
    }
339

    
340
    @Override
341
    public ConversationHolder getConversationHolder() {
342
        return conversation;
343
    }
344

    
345
    public BulkEditorQuery getLastQuery() {
346
       return lastQuery;
347
    }
348

    
349
    public void setLastQuery(BulkEditorQuery lastQuery) {
350
       this.lastQuery = lastQuery;
351

    
352
    }
353

    
354
    @Override
355
    public Taxon getTaxon() {
356
        IStructuredSelection selection = getSelection();
357
        if(selection.size()==1) {
358
            Object object = selection.iterator().next();
359
            if(object instanceof Taxon){
360
                return (Taxon) object;
361
            }
362
        }
363
        return null;
364
    }
365

    
366
    @Override
367
    public void update() {
368
        input.performSearch(lastQuery, getSelection());
369
    }
370

    
371
    @Override
372
    public void addOperation(AbstractPostOperation operation) {
373
        // operations not yet used for bulk editor
374
    }
375

    
376
    @Inject
377
    @Optional
378
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
379
        if (getEditorInput() instanceof TaxonEditorInput){
380
            for (CdmBase cdmBase: cdmBases){
381
                if (getEditorInput().getModel().contains(cdmBase)){
382
                    input.performSearch(lastQuery, getSelection());
383
                    break;
384
                }
385
            }
386
        }
387
    }
388

    
389
    @Inject
390
    @Optional
391
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
392
        if (input instanceof GroupEditorInput){
393
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
394
        }
395
    }
396

    
397
    @Override
398
    public TaxonNode getTaxonNode() {
399
        return null;
400
    }
401

    
402
}
(1-1/9)