Project

General

Profile

Download (6.66 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.Set;
14
import java.util.UUID;
15

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

    
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
23
import org.eclipse.e4.core.contexts.IEclipseContext;
24
import org.eclipse.e4.ui.di.Focus;
25
import org.eclipse.e4.ui.di.Persist;
26
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
27
import org.eclipse.e4.ui.services.IServiceConstants;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.events.ModifyEvent;
30
import org.eclipse.swt.events.ModifyListener;
31
import org.eclipse.swt.widgets.Composite;
32
import org.eclipse.swt.widgets.Display;
33
import org.eclipse.swt.widgets.Listener;
34
import org.eclipse.swt.widgets.Shell;
35

    
36
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
37
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
38
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
39
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
40
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
41
import eu.etaxonomy.cdm.model.description.FeatureTree;
42
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
43
import eu.etaxonomy.cdm.model.description.WorkingSet;
44
import eu.etaxonomy.cdm.model.location.NamedArea;
45
import eu.etaxonomy.cdm.model.name.Rank;
46
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
47
import eu.etaxonomy.cdm.model.taxon.Taxon;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50
import eu.etaxonomy.taxeditor.ui.dialog.selection.NamedAreaSelectionDialog;
51
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
52
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
53

    
54
/**
55
 * @author pplitzner
56
 * @since Nov 21, 2017
57
 *
58
 */
59
public class WorkingSetEditor implements IE4SavablePart{
60

    
61
    private WorkingSetComposite composite;
62

    
63
    private ConversationHolder conversation;
64

    
65
    private WorkingSet workingSet;
66

    
67
    @Inject
68
    private MDirtyable dirty;
69

    
70
    private Listener nameAreaSelectionListener;
71

    
72
    private ModifyListener labelModifyListener;
73

    
74
    @PostConstruct
75
    public void create(Composite parent, IEclipseContext context, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
76
        if(CdmStore.isActive() && conversation==null){
77
            conversation = CdmStore.createConversation();
78
        }
79
        else{
80
            return;
81
        }
82
        CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
83
        ContextInjectionFactory.inject(cdmFormFactory, context);
84

    
85
        composite = new WorkingSetComposite(parent, SWT.NONE);
86

    
87
        labelModifyListener = new ModifyListener() {
88
            @Override
89
            public void modifyText(ModifyEvent e) {
90
                dirty.setDirty(true);
91
            }
92
        };
93
        composite.getRankMin().addSelectionChangedListener(event->dirty.setDirty(true));
94
        composite.getRankMax().addSelectionChangedListener(event->dirty.setDirty(true));
95

    
96
        composite.getTextAreaText().addListener(SWT.MouseDown, event-> {
97
            NamedArea area = NamedAreaSelectionDialog.select(shell, null, null);
98
            if(area!=null){
99
                composite.setArea(area);
100
            }
101
            dirty.setDirty(true);
102
        });
103
    }
104

    
105
    public void init(UUID workingSetUuid) {
106
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid);
107
        if(workingSet.getTitleCache()!=null){
108
            composite.getTxt_label().setText(workingSet.getTitleCache());
109
        }
110
        if(workingSet.getDescriptiveSystem()!=null){
111
            composite.setCharacters(workingSet.getDescriptiveSystem());
112
        }
113
        Rank maxRank = workingSet.getMaxRank();
114
        if(maxRank!=null){
115
            composite.setRankMax(maxRank);
116
        }
117
        Rank minRank = workingSet.getMinRank();
118
        if(minRank!=null){
119
            composite.setRankMin(minRank);
120
        }
121
        Set<NamedArea> geoFilter = workingSet.getGeoFilter();
122
        if(geoFilter!=null && !geoFilter.isEmpty()){
123
            composite.setArea(geoFilter.iterator().next());
124
        }
125

    
126
        composite.getTxt_label().addModifyListener(labelModifyListener);
127
    }
128

    
129
    /**
130
     * {@inheritDoc}
131
     */
132
    @Persist
133
    @Override
134
    public void save(IProgressMonitor monitor) {
135
        NamedArea area = composite.getArea();
136
        Set<NamedArea> areas = new HashSet<>();
137
        if(area!=null){
138
            areas.add(area);
139
        }
140
        TaxonNode taxonNode = composite.getTaxonNode();
141
        Set<NamedArea> descriptions;
142
        if(taxonNode!=null && taxonNode.getTaxon()!=null){
143
            Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon(), Taxon.class);
144
            Collection<SpecimenOrObservationBase> specimens = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(SpecimenOrObservationBase.class, null, taxon, null, null, null, null, null);
145
            for (SpecimenOrObservationBase specimen : specimens) {
146
                Set<SpecimenDescription> specimenDescriptions = specimen.getSpecimenDescriptions();
147
                for (SpecimenDescription specimenDescription : specimenDescriptions) {
148
                    workingSet.addDescription(specimenDescription);
149
                }
150
            }
151
        }
152
        FeatureTree characters = composite.getCharacters();
153
        DefinedTermBase rankMaxSelection = composite.getRankMax().getSelection();
154
        Rank rankMax = null;
155
        if(rankMaxSelection instanceof Rank){
156
            rankMax = (Rank) rankMaxSelection;
157
        }
158
        DefinedTermBase rankMinSelection = composite.getRankMin().getSelection();
159
        Rank rankMin = null;
160
        if(rankMinSelection instanceof Rank){
161
            rankMin = (Rank) rankMinSelection;
162
        }
163

    
164
        workingSet.setMaxRank(rankMax);
165
        workingSet.setMinRank(rankMin);
166
        workingSet.setDescriptiveSystem(characters);
167
        workingSet.addTaxonSubtree(taxonNode);
168
        workingSet.setGeoFilter(areas);
169

    
170
        conversation.commit();
171
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
172

    
173
        dirty.setDirty(false);
174
    }
175

    
176
    @Focus
177
    public void setFocus(){
178
        if(conversation!=null){
179
            conversation.bind();
180
        }
181
    }
182

    
183
    @PreDestroy
184
    public void dispose(){
185
        if(conversation!=null){
186
            conversation.close();
187
            conversation = null;
188
        }
189
    }
190

    
191
}
(2-2/3)