Project

General

Profile

Download (6.61 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

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

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

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

    
51
/**
52
 * @author pplitzner
53
 * @since Nov 21, 2017
54
 *
55
 */
56
public class WorkingSetEditor implements IE4SavablePart{
57

    
58
    private WorkingSetComposite composite;
59

    
60
    private ConversationHolder conversation;
61

    
62
    private WorkingSet workingSet;
63

    
64
    @Inject
65
    private MDirtyable dirty;
66

    
67
    @PostConstruct
68
    public void create(Composite parent, IEclipseContext context, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
69
        if(CdmStore.isActive() && conversation==null){
70
            conversation = CdmStore.createConversation();
71
        }
72
        else{
73
            return;
74
        }
75
        CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
76
        ContextInjectionFactory.inject(cdmFormFactory, context);
77

    
78

    
79
        composite = new WorkingSetComposite(parent, SWT.NONE);
80

    
81
        //TODO remove with correct dirty handling
82
        dirty.setDirty(true);
83

    
84
        //add listeners
85
        composite.getTxt_label().addModifyListener(event->dirty.setDirty(true));
86
        composite.getRankMin().addSelectionChangedListener(event->dirty.setDirty(true));
87
        composite.getRankMax().addSelectionChangedListener(event->dirty.setDirty(true));
88
        composite.getBtnOpenWorkingSet().addListener(SWT.MouseDown, event->{
89
            workingSet = WorkingSetSelectionDialog.select(shell, null);
90
            if(workingSet!=null){
91
                init();
92
            }
93
        });
94
        composite.getTextAreaText().addListener(SWT.MouseDown, event->{
95
            NamedArea area = NamedAreaSelectionDialog.select(shell, null, null);
96
            if(area!=null){
97
                composite.setArea(area);
98
            }
99
            dirty.setDirty(true);
100
        });
101
    }
102

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

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

    
160
        workingSet.setMaxRank(rankMax);
161
        workingSet.setMinRank(rankMin);
162
        workingSet.setDescriptiveSystem(characters);
163
        workingSet.addTaxonSubtree(taxonNode);
164
        workingSet.setGeoFilter(areas);
165

    
166
        conversation.commit();
167
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
168

    
169
        dirty.setDirty(false);
170
    }
171

    
172
    @Focus
173
    public void setFocus(){
174
        if(conversation!=null){
175
            conversation.bind();
176
        }
177
    }
178

    
179
    @PreDestroy
180
    public void dispose(){
181
        if(conversation!=null){
182
            conversation.close();
183
            conversation = null;
184
        }
185
    }
186

    
187
}
(2-2/2)