Project

General

Profile

Download (9.39 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.MessagingUtils;
54
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
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{
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 static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
94

    
95
    private DescriptiveDataSet descriptiveDataSet;
96

    
97
    private Collection<UpdateResult> updateResults;
98

    
99
    private ConversationHolder conversation;
100

    
101
    private ICdmEntitySession cdmEntitySession;
102

    
103
    @Inject
104
    private ESelectionService selService;
105

    
106
    @Inject
107
    private MDirtyable dirty;
108

    
109
    @Inject
110
    private MPart thisPart;
111

    
112
    private CharacterMatrix matrix;
113

    
114
    private StackLayout stackLayout;
115

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

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

    
150
    public IStructuredSelection getSelection(){
151
        return matrix.getSelection();
152
    }
153

    
154
    public void setDirty() {
155
        this.dirty.setDirty(true);
156
    }
157

    
158
    public DescriptiveDataSet getDescriptiveDataSet() {
159
        return descriptiveDataSet;
160
    }
161

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

    
166
    public NatTable getNatTable() {
167
        return matrix.getNatTable();
168
    }
169

    
170
    public ESelectionService getSelectionService() {
171
        return selService;
172
    }
173

    
174
    public CharacterMatrix getMatrix() {
175
        return matrix;
176
    }
177

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

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

    
194

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

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

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

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

    
235
    @Override
236
    public void update(CdmDataChangeMap arg0) {
237
    }
238

    
239
    @Override
240
    public ConversationHolder getConversationHolder() {
241
        return conversation;
242
    }
243

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

    
250
    @Override
251
    public void forceDirty() {
252
        setDirty();
253
    }
254

    
255
    @Override
256
    public ICdmEntitySession getCdmEntitySession() {
257
        return cdmEntitySession;
258
    }
259

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

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

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

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

    
284
}
(11-11/19)