Project

General

Profile

Download (9.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.editor.workingSet;
10

    
11
import java.util.Collection;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import javax.annotation.PostConstruct;
19
import javax.annotation.PreDestroy;
20
import javax.inject.Inject;
21
import javax.inject.Named;
22

    
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
25
import org.eclipse.e4.core.contexts.IEclipseContext;
26
import org.eclipse.e4.ui.di.Focus;
27
import org.eclipse.e4.ui.di.Persist;
28
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
29
import org.eclipse.e4.ui.services.IServiceConstants;
30
import org.eclipse.jface.util.LocalSelectionTransfer;
31
import org.eclipse.jface.viewers.Viewer;
32
import org.eclipse.swt.SWT;
33
import org.eclipse.swt.dnd.DND;
34
import org.eclipse.swt.dnd.Transfer;
35
import org.eclipse.swt.events.ModifyEvent;
36
import org.eclipse.swt.events.ModifyListener;
37
import org.eclipse.swt.widgets.Composite;
38
import org.eclipse.swt.widgets.Display;
39
import org.eclipse.swt.widgets.Shell;
40

    
41
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
42
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
43
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
44
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
45
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
46
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47
import eu.etaxonomy.cdm.model.description.FeatureTree;
48
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
49
import eu.etaxonomy.cdm.model.description.WorkingSet;
50
import eu.etaxonomy.cdm.model.location.NamedArea;
51
import eu.etaxonomy.cdm.model.name.Rank;
52
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
53
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
54
import eu.etaxonomy.cdm.model.taxon.Taxon;
55
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
56
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
57
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
58
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
59
import eu.etaxonomy.taxeditor.store.CdmStore;
60
import eu.etaxonomy.taxeditor.ui.dialog.selection.NamedAreaSelectionDialog;
61
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
62
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
63

    
64
/**
65
 * @author pplitzner
66
 * @since Nov 21, 2017
67
 *
68
 */
69
public class WorkingSetEditor implements IE4SavablePart, IConversationEnabled, ICdmEntitySessionEnabled{
70

    
71
    private WorkingSetComposite composite;
72

    
73
    private ConversationHolder conversation;
74

    
75
    private ICdmEntitySession cdmEntitySession;
76

    
77
    private WorkingSet workingSet;
78

    
79
    private final int dndOperations = DND.DROP_MOVE;
80

    
81
    @Inject
82
    private MDirtyable dirty;
83

    
84
    private ModifyListener labelModifyListener;
85

    
86
    @PostConstruct
87
    public void create(Composite parent, IEclipseContext context, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
88
        if (CdmStore.isActive()){
89
            if(conversation == null){
90
                conversation = CdmStore.createConversation();
91
            }
92
            if(cdmEntitySession == null){
93
                cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
94
            }
95
        }
96
        else{
97
            return;
98
        }
99
        CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
100
        ContextInjectionFactory.inject(cdmFormFactory, context);
101

    
102
        composite = new WorkingSetComposite(parent, SWT.NONE);
103

    
104
        labelModifyListener = new ModifyListener() {
105
            @Override
106
            public void modifyText(ModifyEvent e) {
107
                dirty.setDirty(true);
108
            }
109
        };
110
        composite.getRankMin().addSelectionChangedListener(event->dirty.setDirty(true));
111
        composite.getRankMax().addSelectionChangedListener(event->dirty.setDirty(true));
112

    
113
        composite.getTextAreaText().addListener(SWT.MouseDown, event-> {
114
            NamedArea area = NamedAreaSelectionDialog.select(shell, null, null);
115
            if(area!=null){
116
                composite.setArea(area);
117
            }
118
            dirty.setDirty(true);
119
        });
120

    
121
        //add drag'n'drop support
122
        Transfer[] transfers = new Transfer[] {LocalSelectionTransfer.getTransfer()};
123
        composite.getTaxonNodeTree().addDropSupport(dndOperations, transfers, new TaxonNodeDropAdapter(this));
124

    
125
    }
126

    
127
    public void init(UUID workingSetUuid) {
128
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid);
129
        if(workingSet.getTitleCache()!=null){
130
            composite.getTxt_label().setText(workingSet.getTitleCache());
131
        }
132
        if(workingSet.getDescriptiveSystem()!=null){
133
            composite.setCharacters(workingSet.getDescriptiveSystem());
134
        }
135
        Rank maxRank = workingSet.getMaxRank();
136
        if(maxRank!=null){
137
            composite.setRankMax(maxRank);
138
        }
139
        Rank minRank = workingSet.getMinRank();
140
        if(minRank!=null){
141
            composite.setRankMin(minRank);
142
        }
143
        Set<NamedArea> geoFilter = workingSet.getGeoFilter();
144
        if(geoFilter!=null && !geoFilter.isEmpty()){
145
            composite.setArea(geoFilter.iterator().next());
146
        }
147

    
148
        composite.getTxt_label().addModifyListener(labelModifyListener);
149
    }
150

    
151
    /**
152
     * {@inheritDoc}
153
     */
154
    @Persist
155
    @Override
156
    public void save(IProgressMonitor monitor) {
157
        NamedArea area = composite.getArea();
158
        Set<NamedArea> areas = new HashSet<>();
159
        if(area!=null){
160
            areas.add(area);
161
        }
162
        Object input = composite.getTaxonNodeTree().getInput();
163
        if(input!=null){
164
            Collection<ITaxonTreeNode> taxonNodes = (Collection<ITaxonTreeNode>) input;
165
            for (ITaxonTreeNode taxonTreeNode : taxonNodes) {
166
                if(taxonTreeNode instanceof TaxonNode){
167
                    TaxonNode taxonNode = (TaxonNode)taxonTreeNode;
168
                    workingSet.addTaxonSubtree(taxonNode);
169
                    Set<NamedArea> descriptions;
170
                    if(taxonNode!=null && taxonNode.getTaxon()!=null){
171
                        Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon(), Taxon.class);
172
                        Collection<SpecimenOrObservationBase> specimens = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(SpecimenOrObservationBase.class, null, taxon, null, null, null, null, null);
173
                        for (SpecimenOrObservationBase specimen : specimens) {
174
                            Set<SpecimenDescription> specimenDescriptions = specimen.getSpecimenDescriptions();
175
                            for (SpecimenDescription specimenDescription : specimenDescriptions) {
176
                                workingSet.addDescription(specimenDescription);
177
                            }
178
                        }
179
                    }
180
                }
181
            }
182
        }
183
        FeatureTree characters = composite.getCharacters();
184
        DefinedTermBase rankMaxSelection = composite.getRankMax().getSelection();
185
        Rank rankMax = null;
186
        if(rankMaxSelection instanceof Rank){
187
            rankMax = (Rank) rankMaxSelection;
188
        }
189
        DefinedTermBase rankMinSelection = composite.getRankMin().getSelection();
190
        Rank rankMin = null;
191
        if(rankMinSelection instanceof Rank){
192
            rankMin = (Rank) rankMinSelection;
193
        }
194

    
195
        workingSet.setMaxRank(rankMax);
196
        workingSet.setMinRank(rankMin);
197
        workingSet.setDescriptiveSystem(characters);
198
        workingSet.setGeoFilter(areas);
199

    
200
        conversation.commit();
201
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
202

    
203
        dirty.setDirty(false);
204
    }
205

    
206
    /** {@inheritDoc} */
207
    @PreDestroy
208
    public void dispose() {
209
        if (conversation != null) {
210
            conversation.close();
211
            conversation = null;
212
        }
213
        if(cdmEntitySession != null) {
214
            cdmEntitySession.dispose();
215
            cdmEntitySession = null;
216
        }
217
    }
218

    
219
    /** {@inheritDoc} */
220
    @Focus
221
    public void setFocus() {
222
        if (getConversationHolder() != null) {
223
            getConversationHolder().bind();
224
        }
225
        if(cdmEntitySession != null) {
226
            cdmEntitySession.bind();
227
        }
228
    }
229

    
230
    public Viewer getTaxonTreeViewer() {
231
        return composite.getTaxonNodeTree();
232
    }
233

    
234
    public void setDirty() {
235
        dirty.setDirty(true);
236
    }
237

    
238
    /**
239
     * {@inheritDoc}
240
     */
241
    @Override
242
    public void update(CdmDataChangeMap arg0) {
243
    }
244

    
245
    /**
246
     * {@inheritDoc}
247
     */
248
    @Override
249
    public ICdmEntitySession getCdmEntitySession() {
250
        return cdmEntitySession;
251
    }
252

    
253
    /**
254
     * {@inheritDoc}
255
     */
256
    @Override
257
    public Collection<WorkingSet> getRootEntities() {
258
        return null;
259
    }
260

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public Map<Object, List<String>> getPropertyPathsMap() {
266
        return null;
267
    }
268

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
    @Override
273
    public ConversationHolder getConversationHolder() {
274
        return conversation;
275
    }
276

    
277
}
(3-3/4)