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.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Properties;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
import java.util.stream.Collectors;
|
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.core.services.events.IEventBroker;
|
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.DeleteResult;
|
45
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
46
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
47
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
48
|
import eu.etaxonomy.cdm.api.service.config.RemoveDescriptionsFromDescriptiveDataSetConfigurator;
|
49
|
import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
|
50
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
51
|
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
|
52
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
53
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
54
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
55
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
56
|
import eu.etaxonomy.cdm.model.term.TermType;
|
57
|
import eu.etaxonomy.cdm.persistence.dto.DescriptiveDataSetBaseDto;
|
58
|
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
|
59
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
60
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
61
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
62
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
63
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
64
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
65
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
66
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
67
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
68
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
69
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
70
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
71
|
|
72
|
/**
|
73
|
* Character matrix editor for editing specimen/taxon descriptions in a table
|
74
|
* @author pplitzner
|
75
|
* @since Nov 26, 2017
|
76
|
*/
|
77
|
public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
|
78
|
ICdmEntitySessionEnabled<DescriptiveDataSet>, IPartContentHasSupplementalData,
|
79
|
IPartContentHasDetails {
|
80
|
|
81
|
private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
|
82
|
"descriptions", //$NON-NLS-1$
|
83
|
"descriptions.describedSpecimenOrObservation", //$NON-NLS-1$
|
84
|
"descriptions.describedSpecimenOrObservation.gatheringEvent", //$NON-NLS-1$
|
85
|
"descriptions.describedSpecimenOrObservation.gatheringEvent.actor", //$NON-NLS-1$
|
86
|
"descriptions.descriptionElements", //$NON-NLS-1$
|
87
|
"descriptions.descriptionElements.stateData", //$NON-NLS-1$
|
88
|
"descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
|
89
|
"descriptions.descriptionElements.inDescription", //$NON-NLS-1$
|
90
|
"descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
|
91
|
"descriptions.descriptionElements.feature", //$NON-NLS-1$
|
92
|
"representations", //$NON-NLS-1$
|
93
|
"descriptiveSystem", //$NON-NLS-1$
|
94
|
"taxonSubtreeFilter", //$NON-NLS-1$
|
95
|
"taxonSubtreeFilter.parent", //$NON-NLS-1$
|
96
|
"taxonSubtreeFilter.classification", //$NON-NLS-1$
|
97
|
"taxonSubtreeFilter.taxon", //$NON-NLS-1$
|
98
|
"taxonSubtreeFilter.taxon.name", //$NON-NLS-1$
|
99
|
"taxonSubtreeFilter.taxon.name.rank", //$NON-NLS-1$
|
100
|
"geoFilter", //$NON-NLS-1$
|
101
|
"minRank", //$NON-NLS-1$
|
102
|
"maxRank", //$NON-NLS-1$
|
103
|
});
|
104
|
|
105
|
private DescriptiveDataSetBaseDto descriptiveDataSet;
|
106
|
|
107
|
private ConversationHolder conversation;
|
108
|
|
109
|
private ICdmEntitySession cdmEntitySession;
|
110
|
|
111
|
@Inject
|
112
|
private ESelectionService selService;
|
113
|
|
114
|
@Inject
|
115
|
private MDirtyable dirty;
|
116
|
|
117
|
@Inject
|
118
|
private MPart thisPart;
|
119
|
|
120
|
@Inject
|
121
|
private IEventBroker eventBroker;
|
122
|
|
123
|
private CharacterMatrix matrix;
|
124
|
|
125
|
private StackLayout stackLayout;
|
126
|
|
127
|
private Label label;
|
128
|
|
129
|
@PostConstruct
|
130
|
public void create(Composite parent, IEclipseContext context) {
|
131
|
if(CdmStore.isActive() && conversation==null){
|
132
|
conversation = CdmStore.createConversation();
|
133
|
}
|
134
|
if(cdmEntitySession == null){
|
135
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
136
|
cdmEntitySession.bind();
|
137
|
}
|
138
|
else{
|
139
|
return;
|
140
|
}
|
141
|
stackLayout = new StackLayout();
|
142
|
parent.setLayout(stackLayout);
|
143
|
matrix = new CharacterMatrix(parent, this);
|
144
|
label = new Label(parent, SWT.NONE);
|
145
|
label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX);
|
146
|
stackLayout.topControl = label;
|
147
|
ContextInjectionFactory.inject(matrix, context);
|
148
|
}
|
149
|
|
150
|
public void init(UUID descriptiveDataSetUuid, boolean treeView) {
|
151
|
// this.descriptiveDataSet = CdmStore.getService(IDescriptiveDataSetService.class).load(descriptiveDataSetUuid, WS_PROPERTY_PATH);
|
152
|
this.descriptiveDataSet = CdmStore.getService(IDescriptiveDataSetService.class).getDescriptiveDataSetDtoByUuid(descriptiveDataSetUuid);
|
153
|
if(descriptiveDataSet!=null){
|
154
|
if(descriptiveDataSet.getDescriptiveSystem()==null
|
155
|
|| descriptiveDataSet.getDescriptiveSystem().getTermType()==null
|
156
|
|| !descriptiveDataSet.getDescriptiveSystem().getTermType().equals(TermType.Character)){
|
157
|
MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
|
158
|
loadingFailed();
|
159
|
return;
|
160
|
}
|
161
|
try {
|
162
|
matrix.initDescriptiveDataSet();
|
163
|
} catch (IllegalArgumentException e) {
|
164
|
MessagingUtils.errorDialog("Error during initilization", this, "Could not load matrix", TaxeditorEditorPlugin.PLUGIN_ID, e, false);
|
165
|
loadingFailed();
|
166
|
return;
|
167
|
}
|
168
|
matrix.createTable(treeView, true, true);
|
169
|
thisPart.setLabel(descriptiveDataSet.getTitleCache());
|
170
|
matrix.loadDescriptions( true, true);
|
171
|
|
172
|
}
|
173
|
}
|
174
|
|
175
|
public IStructuredSelection getSelection(){
|
176
|
return matrix.getSelection();
|
177
|
}
|
178
|
|
179
|
public void setDirty() {
|
180
|
this.dirty.setDirty(true);
|
181
|
}
|
182
|
|
183
|
public DescriptiveDataSetBaseDto getDescriptiveDataSet() {
|
184
|
return descriptiveDataSet;
|
185
|
}
|
186
|
|
187
|
public void setDescriptiveDataSet(DescriptiveDataSetBaseDto dataSet) {
|
188
|
this.descriptiveDataSet = dataSet;
|
189
|
}
|
190
|
|
191
|
public NatTable getNatTable() {
|
192
|
return matrix.getNatTable();
|
193
|
}
|
194
|
|
195
|
public ESelectionService getSelectionService() {
|
196
|
return selService;
|
197
|
}
|
198
|
|
199
|
public CharacterMatrix getMatrix() {
|
200
|
return matrix;
|
201
|
}
|
202
|
|
203
|
@Persist
|
204
|
@Override
|
205
|
public void save(IProgressMonitor monitor) {
|
206
|
|
207
|
HashMap<UUID, DescriptionBase<?>> descriptionResults = matrix.addSpecimensToDescriptiveDataSet();
|
208
|
//newly added descriptions should not be saved again.
|
209
|
// descriptions.removeAll(matrix.getRowsToSave().values());
|
210
|
// save edited descriptions
|
211
|
List<DescriptionBaseDto> descriptionsToMerge = matrix.getRowsToMerge().values().stream()
|
212
|
.map(row->row.getDescription())
|
213
|
.collect(Collectors.toList());
|
214
|
|
215
|
if (matrix.getDescriptionsToDelete() != null && !matrix.getDescriptionsToDelete().isEmpty()){
|
216
|
DeleteResult result = new DeleteResult();
|
217
|
for (RemoveDescriptionsFromDescriptiveDataSetConfigurator config: matrix.getDescriptionsToDelete().keySet()){
|
218
|
result.includeResult(CdmStore.getService(IDescriptiveDataSetService.class).removeDescriptions(matrix.getDescriptionsToDelete().get(config), matrix.getDescriptiveDataSet().getUuid(), config));
|
219
|
}
|
220
|
matrix.getDescriptionsToDelete().clear();
|
221
|
|
222
|
}
|
223
|
|
224
|
List<MergeResult<DescriptionBase>> resultsFromSave = CdmStore.getService(IDescriptionService.class).merge(new ArrayList<>(matrix.getDescriptionsToSave()), true);
|
225
|
for (MergeResult<DescriptionBase> result: resultsFromSave){
|
226
|
Set<DescriptionBase> newEntities = result.getNewEntities();
|
227
|
for(DescriptionBase<?> newEntity: newEntities){
|
228
|
descriptionResults.put(newEntity.getUuid(), newEntity);
|
229
|
}
|
230
|
descriptionResults.put(result.getMergedEntity().getUuid(), result.getMergedEntity());
|
231
|
}
|
232
|
matrix.getDescriptionsToSave().clear();
|
233
|
UpdateResult results = CdmStore.getService(IDescriptionService.class).mergeDescriptions(descriptionsToMerge, matrix.getDescriptiveDataSet().getUuid());
|
234
|
matrix.getRowsToMerge().clear();
|
235
|
// conversation.commit();
|
236
|
|
237
|
for (CdmBase result: results.getUpdatedObjects()){
|
238
|
if (result instanceof DescriptionBase){
|
239
|
descriptionResults.put(result.getUuid(), (DescriptionBase<?>)result);
|
240
|
}
|
241
|
}
|
242
|
List<Object> updateRows = matrix.getDescriptions().stream().filter(row->descriptionResults.keySet().contains(((RowWrapperDTO<?>)row).getDescription().getDescriptionUuid())).collect(Collectors.toList());
|
243
|
for (Object updateRow: updateRows){
|
244
|
if (updateRow instanceof SpecimenRowWrapperDTO){
|
245
|
SpecimenRowWrapperDTO dto = (SpecimenRowWrapperDTO)updateRow;
|
246
|
DescriptionBaseDto desc = DescriptionBaseDto.fromDescription(descriptionResults.get(((SpecimenRowWrapperDTO) updateRow).getDescription().getDescriptionUuid()));
|
247
|
dto.setDescription(desc);
|
248
|
}
|
249
|
}
|
250
|
// matrix.loadDescriptions(getDescriptiveDataSet().getUuid(), false);
|
251
|
eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
|
252
|
dirty.setDirty(false);
|
253
|
}
|
254
|
|
255
|
@Override
|
256
|
public boolean isDirty() {
|
257
|
return dirty.isDirty();
|
258
|
}
|
259
|
|
260
|
@Focus
|
261
|
public void setFocus(){
|
262
|
if(conversation!=null){
|
263
|
conversation.bind();
|
264
|
}
|
265
|
if(cdmEntitySession != null) {
|
266
|
cdmEntitySession.bind();
|
267
|
}
|
268
|
}
|
269
|
|
270
|
@PreDestroy
|
271
|
public void dispose(){
|
272
|
if (conversation != null) {
|
273
|
conversation.close();
|
274
|
conversation = null;
|
275
|
}
|
276
|
if(cdmEntitySession != null) {
|
277
|
cdmEntitySession.dispose();
|
278
|
cdmEntitySession = null;
|
279
|
}
|
280
|
descriptiveDataSet = null;
|
281
|
this.matrix.dispose();
|
282
|
dirty.setDirty(false);
|
283
|
}
|
284
|
|
285
|
@Override
|
286
|
public void update(CdmDataChangeMap arg0) {
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public ConversationHolder getConversationHolder() {
|
291
|
return conversation;
|
292
|
}
|
293
|
|
294
|
@Override
|
295
|
public void changed(Object element) {
|
296
|
setDirty();
|
297
|
matrix.getNatTable().refresh();
|
298
|
if (element instanceof DescriptionBase){
|
299
|
if (!matrix.getRowsToMerge().containsKey(((DescriptionBase<?>)element).getUuid())){
|
300
|
matrix.addDescriptionToSave((DescriptionBase<?>)element);
|
301
|
}
|
302
|
}
|
303
|
}
|
304
|
|
305
|
@Override
|
306
|
public void forceDirty() {
|
307
|
setDirty();
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
public ICdmEntitySession getCdmEntitySession() {
|
312
|
return cdmEntitySession;
|
313
|
}
|
314
|
|
315
|
@Override
|
316
|
public Collection<DescriptiveDataSet> getRootEntities() {
|
317
|
//no dataset entity only dto
|
318
|
return null;
|
319
|
}
|
320
|
|
321
|
@Override
|
322
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
323
|
Map<Object, List<String>> propertyMap = new HashMap<>();
|
324
|
propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
|
325
|
return propertyMap;
|
326
|
}
|
327
|
|
328
|
private void loadingFailed() {
|
329
|
label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX_FAILED);
|
330
|
}
|
331
|
|
332
|
void loadingDone() {
|
333
|
stackLayout.topControl = matrix;
|
334
|
matrix.getParent().layout();
|
335
|
}
|
336
|
}
|