Project

General

Profile

Download (6.18 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.events.SelectionEvent;
29
import org.eclipse.swt.events.SelectionListener;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Shell;
33

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

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

    
59
    private WorkingSetComposite composite;
60

    
61
    private ConversationHolder conversation;
62

    
63
    private WorkingSet workingSet;
64

    
65
    @Inject
66
    private MDirtyable dirty;
67

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

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

    
83
        composite.getBtnOpenWorkingSet().addSelectionListener(new SelectionListener() {
84
            @Override
85
            public void widgetSelected(SelectionEvent e) {
86
                WorkingSetEditor.this.workingSet = WorkingSetSelectionDialog.select(shell, null);
87
                init();
88
            }
89

    
90
            @Override
91
            public void widgetDefaultSelected(SelectionEvent e) {
92
            }
93
        });
94
    }
95

    
96
    public void init() {
97
        if(workingSet.getTitleCache()!=null){
98
            composite.getTxt_label().setText(workingSet.getTitleCache());
99
        }
100
        if(workingSet.getDescriptiveSystem()!=null){
101
            composite.setCharacters(workingSet.getDescriptiveSystem());
102
        }
103
        Rank maxRank = workingSet.getMaxRank();
104
        if(maxRank!=null){
105
            composite.setRankMax(maxRank);
106
        }
107
        Rank minRank = workingSet.getMinRank();
108
        if(minRank!=null){
109
            composite.setRankMin(minRank);
110
        }
111
        Set<NamedArea> geoFilter = workingSet.getGeoFilter();
112
        if(geoFilter!=null && !geoFilter.isEmpty()){
113
            composite.setArea(geoFilter.iterator().next());
114
        }
115
    }
116

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

    
152
        workingSet.setMaxRank(rankMax);
153
        workingSet.setMinRank(rankMin);
154
        workingSet.setDescriptiveSystem(characters);
155
        workingSet.addTaxonSubtree(taxonNode);
156
        workingSet.setGeoFilter(areas);
157

    
158
        conversation.commit();
159
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
160

    
161
        dirty.setDirty(false);
162
    }
163

    
164
    @Focus
165
    public void setFocus(){
166
        if(conversation!=null){
167
            conversation.bind();
168
        }
169
    }
170

    
171
    @PreDestroy
172
    public void dispose(){
173
        if(conversation!=null){
174
            conversation.close();
175
            conversation = null;
176
        }
177
    }
178

    
179
}
(2-2/2)