Project

General

Profile

Download (9.57 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.File;
12
import java.io.FileOutputStream;
13
import java.io.IOException;
14
import java.util.Arrays;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.HashMap;
18
import java.util.HashSet;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.UUID;
22

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

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

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

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

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

    
95
    private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
96

    
97
    private DescriptiveDataSet descriptiveDataSet;
98

    
99
    private Collection<UpdateResult> updateResults;
100

    
101
    private ConversationHolder conversation;
102

    
103
    private ICdmEntitySession cdmEntitySession;
104

    
105
    @Inject
106
    private ESelectionService selService;
107

    
108
    @Inject
109
    private MDirtyable dirty;
110

    
111
    @Inject
112
    private MPart thisPart;
113

    
114
    private CharacterMatrix matrix;
115

    
116
    private StackLayout stackLayout;
117

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

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

    
152
    public IStructuredSelection getSelection(){
153
        return matrix.getSelection();
154
    }
155

    
156
    public void setDirty() {
157
        this.dirty.setDirty(true);
158
    }
159

    
160
    public DescriptiveDataSet getDescriptiveDataSet() {
161
        return descriptiveDataSet;
162
    }
163

    
164
    private File getStatePropertiesFile() {
165
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
166
    }
167

    
168
    public NatTable getNatTable() {
169
        return matrix.getNatTable();
170
    }
171

    
172
    public ESelectionService getSelectionService() {
173
        return selService;
174
    }
175

    
176
    public CharacterMatrix getMatrix() {
177
        return matrix;
178
    }
179

    
180
    @Persist
181
    @Override
182
    public void save(IProgressMonitor monitor) {
183
        //save descriptions
184
        matrix.getDescriptions().stream()
185
        .filter(o->o instanceof RowWrapperDTO)
186
        .forEach(wrapper->save((RowWrapperDTO)wrapper));
187
        //save data set
188
        CdmStore.getService(IDescriptiveDataSetService.class).merge(descriptiveDataSet);
189

    
190
//        //save update results (taxon with aggregated description)
191
//        if(updateResults!=null){
192
//            updateResults.forEach(result->
193
//            CdmStore.getService(result.getCdmEntity()).merge(result.getCdmEntity()));
194
//        }
195

    
196

    
197
        conversation.commit();
198
        updateResults = null;
199
        dirty.setDirty(false);
200
    }
201

    
202
    private void save(RowWrapperDTO wrapper){
203
        CdmStore.getService(IDescriptionService.class).merge(wrapper.getDescription());
204
    }
205

    
206
    @Focus
207
    public void setFocus(){
208
        if(conversation!=null){
209
            conversation.bind();
210
        }
211
        if(cdmEntitySession != null) {
212
            cdmEntitySession.bind();
213
        }
214
    }
215

    
216
    @PreDestroy
217
    public void dispose(){
218
        if (conversation != null) {
219
            conversation.close();
220
            conversation = null;
221
        }
222
        if(cdmEntitySession != null) {
223
            cdmEntitySession.dispose();
224
            cdmEntitySession = null;
225
        }
226
        dirty.setDirty(false);
227
        if(matrix.getNatTableState()!=null){
228
            try (FileOutputStream tableStateStream =
229
                    new FileOutputStream(getStatePropertiesFile())) {
230
                matrix.getNatTableState().store(tableStateStream, null);
231
            } catch (IOException ioe) {
232
                ioe.printStackTrace();
233
            }
234
        }
235
    }
236

    
237
    @Override
238
    public void update(CdmDataChangeMap arg0) {
239
    }
240

    
241
    @Override
242
    public ConversationHolder getConversationHolder() {
243
        return conversation;
244
    }
245

    
246
    @Override
247
    public void changed(Object element) {
248
        setDirty();
249
        matrix.getNatTable().refresh();
250
    }
251

    
252
    @Override
253
    public void forceDirty() {
254
        setDirty();
255
    }
256

    
257
    @Override
258
    public ICdmEntitySession getCdmEntitySession() {
259
        return cdmEntitySession;
260
    }
261

    
262
    @Override
263
    public Collection<DescriptiveDataSet> getRootEntities() {
264
        return Collections.singleton(this.descriptiveDataSet);
265
    }
266

    
267
    @Override
268
    public Map<Object, List<String>> getPropertyPathsMap() {
269
        Map<Object, List<String>> propertyMap = new HashMap<>();
270
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
271
        return propertyMap;
272
    }
273

    
274
    public void addUpdateResult(UpdateResult result){
275
        if(updateResults==null){
276
            updateResults = new HashSet<>();
277
        }
278
        updateResults.add(result);
279
    }
280

    
281
    public void loadingDone() {
282
        stackLayout.topControl = matrix;
283
        matrix.getParent().layout();
284
    }
285

    
286
}
(11-11/19)