Project

General

Profile

Download (9.24 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.descriptiveDataSet.matrix;
10

    
11
import java.io.FileOutputStream;
12
import java.io.IOException;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.UUID;
21

    
22
import javax.annotation.PostConstruct;
23
import javax.annotation.PreDestroy;
24
import javax.inject.Inject;
25

    
26
import org.eclipse.core.runtime.IProgressMonitor;
27
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
28
import org.eclipse.e4.core.contexts.IEclipseContext;
29
import org.eclipse.e4.ui.di.Focus;
30
import org.eclipse.e4.ui.di.Persist;
31
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
32
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
33
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
34
import org.eclipse.jface.viewers.IStructuredSelection;
35
import org.eclipse.nebula.widgets.nattable.NatTable;
36
import org.eclipse.swt.SWT;
37
import org.eclipse.swt.custom.StackLayout;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Label;
40

    
41
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
42
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
43
import eu.etaxonomy.cdm.api.service.IDescriptionService;
44
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
45
import eu.etaxonomy.cdm.api.service.UpdateResult;
46
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
47
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
48
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
49
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
50
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
51
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
54
import eu.etaxonomy.taxeditor.model.MessagingUtils;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
56
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
57
import eu.etaxonomy.taxeditor.store.CdmStore;
58
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
59

    
60
/**
61
 * Character matrix editor for editing specimen/taxon descriptions in a table
62
 * @author pplitzner
63
 * @since Nov 26, 2017
64
 *
65
 */
66
public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
67
ICdmEntitySessionEnabled, IPartContentHasSupplementalData, IPartContentHasDetails{
68

    
69
    private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
70
            "descriptions", //$NON-NLS-1$
71
            "descriptions.describedSpecimenOrObservation", //$NON-NLS-1$
72
            "descriptions.describedSpecimenOrObservation.gatheringEvent", //$NON-NLS-1$
73
            "descriptions.describedSpecimenOrObservation.gatheringEvent.actor", //$NON-NLS-1$
74
            "descriptions.descriptionElements", //$NON-NLS-1$
75
            "descriptions.descriptionElements.stateData", //$NON-NLS-1$
76
            "descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
77
            "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
78
            "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
79
            "descriptions.descriptionElements.feature", //$NON-NLS-1$
80
            "representations", //$NON-NLS-1$
81
            "descriptiveSystem", //$NON-NLS-1$
82
            "taxonSubtreeFilter", //$NON-NLS-1$
83
            "taxonSubtreeFilter.parent", //$NON-NLS-1$
84
            "taxonSubtreeFilter.classification", //$NON-NLS-1$
85
            "taxonSubtreeFilter.taxon", //$NON-NLS-1$
86
            "taxonSubtreeFilter.taxon.name", //$NON-NLS-1$
87
            "taxonSubtreeFilter.taxon.name.rank", //$NON-NLS-1$
88
            "geoFilter", //$NON-NLS-1$
89
            "minRank", //$NON-NLS-1$
90
            "maxRank", //$NON-NLS-1$
91
    });
92

    
93
    private DescriptiveDataSet descriptiveDataSet;
94

    
95
    private Collection<UpdateResult> updateResults;
96

    
97
    private ConversationHolder conversation;
98

    
99
    private ICdmEntitySession cdmEntitySession;
100

    
101
    @Inject
102
    private ESelectionService selService;
103

    
104
    @Inject
105
    private MDirtyable dirty;
106

    
107
    @Inject
108
    private MPart thisPart;
109

    
110
    private CharacterMatrix matrix;
111

    
112
    private StackLayout stackLayout;
113

    
114
    @PostConstruct
115
    public void create(Composite parent, IEclipseContext context) {
116
        if(CdmStore.isActive() && conversation==null){
117
            conversation = CdmStore.createConversation();
118
        }
119
        if(cdmEntitySession == null){
120
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
121
        }
122
        else{
123
            return;
124
        }
125
        stackLayout = new StackLayout();
126
        parent.setLayout(stackLayout);
127
        matrix = new CharacterMatrix(parent, this);
128
        Label label = new Label(parent, SWT.NONE);
129
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX);
130
        stackLayout.topControl = label;
131
        ContextInjectionFactory.inject(matrix, context);
132
    }
133

    
134
    public void init(UUID descriptiveDataSetUuid, boolean treeView) {
135
        this.descriptiveDataSet = CdmStore.getService(IDescriptiveDataSetService.class).load(descriptiveDataSetUuid, WS_PROPERTY_PATH);
136
        if(descriptiveDataSet!=null){
137
            if(descriptiveDataSet.getDescriptiveSystem()==null){
138
                MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
139
                return;
140
            }
141
            matrix.initDescriptiveDataSet(descriptiveDataSet);
142
            matrix.createTable(treeView, true);
143
            thisPart.setLabel(descriptiveDataSet.getLabel());
144
            matrix.loadDescriptions(descriptiveDataSet);
145
        }
146
    }
147

    
148
    public IStructuredSelection getSelection(){
149
        return matrix.getSelection();
150
    }
151

    
152
    public void setDirty() {
153
        this.dirty.setDirty(true);
154
    }
155

    
156
    public DescriptiveDataSet getDescriptiveDataSet() {
157
        return descriptiveDataSet;
158
    }
159

    
160
    public NatTable getNatTable() {
161
        return matrix.getNatTable();
162
    }
163

    
164
    public ESelectionService getSelectionService() {
165
        return selService;
166
    }
167

    
168
    public CharacterMatrix getMatrix() {
169
        return matrix;
170
    }
171

    
172
    @Persist
173
    @Override
174
    public void save(IProgressMonitor monitor) {
175
        //save descriptions
176
        matrix.getDescriptions().stream()
177
        .filter(o->o instanceof RowWrapperDTO)
178
        .forEach(wrapper->save((RowWrapperDTO)wrapper));
179
        //save data set
180
        CdmStore.getService(IDescriptiveDataSetService.class).merge(descriptiveDataSet);
181

    
182
//        //save update results (taxon with aggregated description)
183
//        if(updateResults!=null){
184
//            updateResults.forEach(result->
185
//            CdmStore.getService(result.getCdmEntity()).merge(result.getCdmEntity()));
186
//        }
187

    
188

    
189
        conversation.commit();
190
        updateResults = null;
191
        dirty.setDirty(false);
192
    }
193

    
194
    private void save(RowWrapperDTO wrapper){
195
        CdmStore.getService(IDescriptionService.class).merge(wrapper.getDescription());
196
    }
197

    
198
    @Focus
199
    public void setFocus(){
200
        if(conversation!=null){
201
            conversation.bind();
202
        }
203
        if(cdmEntitySession != null) {
204
            cdmEntitySession.bind();
205
        }
206
    }
207

    
208
    @PreDestroy
209
    public void dispose(){
210
        if (conversation != null) {
211
            conversation.close();
212
            conversation = null;
213
        }
214
        if(cdmEntitySession != null) {
215
            cdmEntitySession.dispose();
216
            cdmEntitySession = null;
217
        }
218
        dirty.setDirty(false);
219
        if(matrix.getNatTableState()!=null){
220
            try (FileOutputStream tableStateStream =
221
                    new FileOutputStream(matrix.getStatePropertiesFile())) {
222
                matrix.getNatTableState().store(tableStateStream, null);
223
            } catch (IOException ioe) {
224
                ioe.printStackTrace();
225
            }
226
        }
227
    }
228

    
229
    @Override
230
    public void update(CdmDataChangeMap arg0) {
231
    }
232

    
233
    @Override
234
    public ConversationHolder getConversationHolder() {
235
        return conversation;
236
    }
237

    
238
    @Override
239
    public void changed(Object element) {
240
        setDirty();
241
        matrix.getNatTable().refresh();
242
    }
243

    
244
    @Override
245
    public void forceDirty() {
246
        setDirty();
247
    }
248

    
249
    @Override
250
    public ICdmEntitySession getCdmEntitySession() {
251
        return cdmEntitySession;
252
    }
253

    
254
    @Override
255
    public Collection<DescriptiveDataSet> getRootEntities() {
256
        return Collections.singleton(this.descriptiveDataSet);
257
    }
258

    
259
    @Override
260
    public Map<Object, List<String>> getPropertyPathsMap() {
261
        Map<Object, List<String>> propertyMap = new HashMap<>();
262
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
263
        return propertyMap;
264
    }
265

    
266
    public void addUpdateResult(UpdateResult result){
267
        if(updateResults==null){
268
            updateResults = new HashSet<>();
269
        }
270
        updateResults.add(result);
271
    }
272

    
273
    public void loadingDone() {
274
        stackLayout.topControl = matrix;
275
        matrix.getParent().layout();
276
    }
277

    
278
}
(11-11/19)