ref #7095 Fix WS property path
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / workingSet / matrix / CharacterMatrixPart.java
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.IWorkingSetService;
38 import eu.etaxonomy.cdm.model.description.WorkingSet;
39 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
41 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
43 import eu.etaxonomy.taxeditor.model.MessagingUtils;
44 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
45 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
46 import eu.etaxonomy.taxeditor.store.CdmStore;
47 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
48 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
49
50 /**
51 * Character matrix editor for editing specimen/taxon descriptions in a table
52 * @author pplitzner
53 * @since Nov 26, 2017
54 *
55 */
56 public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
57 ICdmEntitySessionEnabled{
58
59 private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
60 "descriptions", //$NON-NLS-1$
61 "descriptions.describedSpecimenOrObservation", //$NON-NLS-1$
62 "descriptions.describedSpecimenOrObservation.gatheringEvent", //$NON-NLS-1$
63 "descriptions.describedSpecimenOrObservation.gatheringEvent.actor", //$NON-NLS-1$
64 "descriptions.descriptionElements", //$NON-NLS-1$
65 "descriptions.descriptionElements.stateData", //$NON-NLS-1$
66 "descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
67 "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
68 "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
69 "descriptions.descriptionElements.feature", //$NON-NLS-1$
70 "representations", //$NON-NLS-1$
71 "descriptiveSystem", //$NON-NLS-1$
72 "taxonSubtreeFilter", //$NON-NLS-1$
73 "taxonSubtreeFilter.parent", //$NON-NLS-1$
74 "taxonSubtreeFilter.classification", //$NON-NLS-1$
75 "taxonSubtreeFilter.taxon", //$NON-NLS-1$
76 "taxonSubtreeFilter.taxon.name", //$NON-NLS-1$
77 "taxonSubtreeFilter.taxon.name.rank", //$NON-NLS-1$
78 "geoFilter", //$NON-NLS-1$
79 "minRank", //$NON-NLS-1$
80 "maxRank", //$NON-NLS-1$
81 });
82
83 private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
84
85 private WorkingSet workingSet;
86
87 private ConversationHolder conversation;
88
89 private ICdmEntitySession cdmEntitySession;
90
91 @Inject
92 private ESelectionService selService;
93
94 @Inject
95 private MDirtyable dirty;
96
97 @Inject
98 private MPart thisPart;
99
100 private CharacterMatrix matrix;
101
102 @PostConstruct
103 public void create(Composite parent) {
104 if(CdmStore.isActive() && conversation==null){
105 conversation = CdmStore.createConversation();
106 }
107 if(cdmEntitySession == null){
108 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
109 }
110 else{
111 return;
112 }
113 matrix = new CharacterMatrix(parent, this);
114 }
115
116 public void init(UUID workingSetUuid, boolean treeView) {
117 this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
118 if(workingSet!=null){
119 if(workingSet.getDescriptiveSystem()==null){
120 MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
121 return;
122 }
123 matrix.initWorkingSet(workingSet);
124 matrix.createTable(treeView);
125 thisPart.setLabel(workingSet.getLabel());
126 matrix.loadDescriptions(workingSet);
127 }
128 }
129
130 public void setDirty() {
131 this.dirty.setDirty(true);
132 }
133
134 public WorkingSet getWorkingSet() {
135 return workingSet;
136 }
137
138 private File getStatePropertiesFile() {
139 return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
140 }
141
142 public NatTable getNatTable() {
143 return matrix.getNatTable();
144 }
145
146 public ESelectionService getSelectionService() {
147 return selService;
148 }
149
150 @Persist
151 @Override
152 public void save(IProgressMonitor monitor) {
153 CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
154 conversation.commit();
155 dirty.setDirty(false);
156 }
157
158 @Focus
159 public void setFocus(){
160 if(conversation!=null){
161 conversation.bind();
162 }
163 if(cdmEntitySession != null) {
164 cdmEntitySession.bind();
165 }
166 }
167
168 @PreDestroy
169 public void dispose(){
170 if (conversation != null) {
171 conversation.close();
172 conversation = null;
173 }
174 if(cdmEntitySession != null) {
175 cdmEntitySession.dispose();
176 cdmEntitySession = null;
177 }
178 dirty.setDirty(false);
179 if(matrix.getNatTableState()!=null){
180 try (FileOutputStream tableStateStream =
181 new FileOutputStream(getStatePropertiesFile())) {
182 matrix.getNatTableState().store(tableStateStream, null);
183 } catch (IOException ioe) {
184 ioe.printStackTrace();
185 }
186 }
187 }
188
189 @Override
190 public void update(CdmDataChangeMap arg0) {
191 }
192
193 @Override
194 public ConversationHolder getConversationHolder() {
195 return conversation;
196 }
197
198 @Override
199 public void changed(Object element) {
200 setDirty();
201 matrix.getNatTable().refresh();
202 }
203
204 @Override
205 public void forceDirty() {
206 setDirty();
207 }
208
209 @Override
210 public ICdmEntitySession getCdmEntitySession() {
211 return cdmEntitySession;
212 }
213
214 @Override
215 public Collection<WorkingSet> getRootEntities() {
216 return Collections.singleton(this.workingSet);
217 }
218
219 @Override
220 public Map<Object, List<String>> getPropertyPathsMap() {
221 Map<Object, List<String>> propertyMap = new HashMap<>();
222 propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
223 return propertyMap;
224 }
225
226 }