Project

General

Profile

Download (7.7 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.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.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.ui.di.Focus;
28
import org.eclipse.e4.ui.di.Persist;
29
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
30
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
31
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
32
import org.eclipse.nebula.widgets.nattable.NatTable;
33
import org.eclipse.swt.widgets.Composite;
34

    
35
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
36
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
37
import eu.etaxonomy.cdm.api.service.IDescriptionService;
38
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
39
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
40
import eu.etaxonomy.cdm.model.description.WorkingSet;
41
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
42
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
47
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
48
import eu.etaxonomy.taxeditor.store.CdmStore;
49
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
50
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
51

    
52
/**
53
 * Character matrix editor for editing specimen/taxon descriptions in a table
54
 * @author pplitzner
55
 * @since Nov 26, 2017
56
 *
57
 */
58
public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
59
ICdmEntitySessionEnabled{
60

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

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

    
87
    private WorkingSet workingSet;
88

    
89
    private ConversationHolder conversation;
90

    
91
    private ICdmEntitySession cdmEntitySession;
92

    
93
    @Inject
94
    private ESelectionService selService;
95

    
96
    @Inject
97
    private MDirtyable dirty;
98

    
99
    @Inject
100
    private MPart thisPart;
101

    
102
    private CharacterMatrix matrix;
103

    
104
    @PostConstruct
105
    public void create(Composite parent) {
106
        if(CdmStore.isActive() && conversation==null){
107
            conversation = CdmStore.createConversation();
108
        }
109
        if(cdmEntitySession == null){
110
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
111
        }
112
        else{
113
            return;
114
        }
115
        matrix = new CharacterMatrix(parent, this);
116
    }
117

    
118
    public void init(UUID workingSetUuid, boolean treeView) {
119
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
120
        if(workingSet!=null){
121
            if(workingSet.getDescriptiveSystem()==null){
122
                MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
123
                return;
124
            }
125
            matrix.initWorkingSet(workingSet);
126
            matrix.createTable(treeView);
127
            thisPart.setLabel(workingSet.getLabel());
128
            matrix.loadDescriptions(workingSet);
129
        }
130
    }
131

    
132
    public void setDirty() {
133
        this.dirty.setDirty(true);
134
    }
135

    
136
    public WorkingSet getWorkingSet() {
137
        return workingSet;
138
    }
139

    
140
    private File getStatePropertiesFile() {
141
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
142
    }
143

    
144
    public NatTable getNatTable() {
145
        return matrix.getNatTable();
146
    }
147

    
148
    public ESelectionService getSelectionService() {
149
        return selService;
150
    }
151

    
152
    @Persist
153
    @Override
154
    public void save(IProgressMonitor monitor) {
155
        matrix.getDescriptions().stream()
156
        .filter(o->o instanceof RowWrapperDTO)
157
        .forEach(wrapper->save((RowWrapperDTO)wrapper));
158
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
159
        conversation.commit();
160
        dirty.setDirty(false);
161
    }
162

    
163
    private void save(RowWrapperDTO wrapper){
164
        CdmStore.getService(IDescriptionService.class).merge(wrapper.getSpecimenDescription(), true);
165
    }
166

    
167
    @Focus
168
    public void setFocus(){
169
        if(conversation!=null){
170
            conversation.bind();
171
        }
172
        if(cdmEntitySession != null) {
173
            cdmEntitySession.bind();
174
        }
175
    }
176

    
177
    @PreDestroy
178
    public void dispose(){
179
        if (conversation != null) {
180
            conversation.close();
181
            conversation = null;
182
        }
183
        if(cdmEntitySession != null) {
184
            cdmEntitySession.dispose();
185
            cdmEntitySession = null;
186
        }
187
        dirty.setDirty(false);
188
        if(matrix.getNatTableState()!=null){
189
            try (FileOutputStream tableStateStream =
190
                    new FileOutputStream(getStatePropertiesFile())) {
191
                matrix.getNatTableState().store(tableStateStream, null);
192
            } catch (IOException ioe) {
193
                ioe.printStackTrace();
194
            }
195
        }
196
    }
197

    
198
    @Override
199
    public void update(CdmDataChangeMap arg0) {
200
    }
201

    
202
    @Override
203
    public ConversationHolder getConversationHolder() {
204
        return conversation;
205
    }
206

    
207
    @Override
208
    public void changed(Object element) {
209
        setDirty();
210
        matrix.getNatTable().refresh();
211
    }
212

    
213
    @Override
214
    public void forceDirty() {
215
        setDirty();
216
    }
217

    
218
    @Override
219
    public ICdmEntitySession getCdmEntitySession() {
220
        return cdmEntitySession;
221
    }
222

    
223
    @Override
224
    public Collection<WorkingSet> getRootEntities() {
225
        return Collections.singleton(this.workingSet);
226
    }
227

    
228
    @Override
229
    public Map<Object, List<String>> getPropertyPathsMap() {
230
        Map<Object, List<String>> propertyMap = new HashMap<>();
231
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
232
        return propertyMap;
233
    }
234

    
235
}
(3-3/9)