Fix problem for characters with same name
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / BulkEditorE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.bulkeditor.e4;
11
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.util.List;
15
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
22 import org.eclipse.e4.core.contexts.IEclipseContext;
23 import org.eclipse.e4.core.di.annotations.Optional;
24 import org.eclipse.e4.core.services.events.IEventBroker;
25 import org.eclipse.e4.ui.di.Focus;
26 import org.eclipse.e4.ui.di.Persist;
27 import org.eclipse.e4.ui.di.UIEventTopic;
28 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
29 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.swt.SWT;
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.facade.DerivedUnitFacade;
38 import eu.etaxonomy.cdm.model.common.CdmBase;
39 import eu.etaxonomy.cdm.model.common.Group;
40 import eu.etaxonomy.cdm.model.common.User;
41 import eu.etaxonomy.cdm.model.description.DescriptionBase;
42 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43 import eu.etaxonomy.cdm.model.description.TaxonDescription;
44 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
45 import eu.etaxonomy.cdm.model.name.TaxonName;
46 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
47 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
48 import eu.etaxonomy.cdm.model.taxon.Taxon;
49 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
50 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
51 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
52 import eu.etaxonomy.taxeditor.bulkeditor.input.GroupEditorInput;
53 import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
54 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
55 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
56 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
57 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
58 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
59 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
60 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
61 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
62 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
63 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
64 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
65
66 /**
67 *
68 * @author pplitzner
69 * @since Sep 8, 2017
70 *
71 */
72 public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
73 IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
74 IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
75
76 @Inject
77 private MDirtyable dirty;
78
79 private AbstractBulkEditorInput input;
80
81 private ConversationHolder conversation;
82
83 @Inject
84 private IEventBroker eventBroker;
85
86 @Inject
87 IEclipseContext context;
88
89 @Inject
90 private MPart thisPart;
91
92 private BulkEditorQuery lastQuery = null;
93
94 private BulkEditorE4Composite bulkEditorComposite;
95
96 @Inject
97 public BulkEditorE4() {
98 }
99
100 @SuppressWarnings("unused")
101 public void init(AbstractBulkEditorInput<?> input){
102 this.input = input;
103 this.conversation = input.getConversation();
104 thisPart.setLabel(input.getEditorName());
105
106 bulkEditorComposite.init(input);
107 }
108
109 /** {@inheritDoc} */
110 @PostConstruct
111 public void createPartControl(Composite parent, IEclipseContext context) {
112 bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
113 ContextInjectionFactory.inject(bulkEditorComposite, context);
114 }
115
116 @Override
117 @Persist
118 public void save(IProgressMonitor monitor) {
119 save(monitor, true);
120 }
121
122 public void save(IProgressMonitor monitor, boolean resetMerge) {
123 if (!input.getCdmEntitySession().isActive()){
124 input.getCdmEntitySession().bind();
125 }
126 input.saveModel(resetMerge);
127
128 IStructuredSelection selection = getSelection();
129
130 dirty.setDirty(false);
131 input.dispose();
132 input.bind();
133 conversation.commit(true);
134
135 if (lastQuery != null){
136 bulkEditorComposite.performSearch(lastQuery, selection);
137 }
138 }
139
140
141 @Focus
142 public void setFocus() {
143 //make sure to bind again if maybe in another view the conversation was unbound
144 if(conversation!=null && !conversation.isBound()){
145 conversation.bind();
146 }
147 if(input!=null && input.getCdmEntitySession()!= null) {
148 input.getCdmEntitySession().bind();
149 }
150
151 //make sure to bind again if maybe in another view the conversation was unbound
152 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
153 }
154
155 @PreDestroy
156 public void dispose() {
157 if(conversation!=null){
158 conversation.unregisterForDataStoreChanges(this);
159 conversation.close();
160 }
161 if(input!=null){
162 input.dispose();
163 }
164 dirty.setDirty(false);
165 //save table settings
166 if(bulkEditorComposite.getNatTableState()!=null){
167 try (FileOutputStream tableStateStream =
168 new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
169 bulkEditorComposite.getNatTableState().store(tableStateStream, null);
170 } catch (IOException ioe) {
171 ioe.printStackTrace();
172 }
173 }
174 }
175
176 @Optional
177 @Inject
178 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
179 if(selection!=null){
180 setSelection(selection);
181 }
182 }
183
184 public void refresh(){
185 bulkEditorComposite.refresh();
186 }
187
188 public IStructuredSelection getSelection(){
189 return bulkEditorComposite.getSelection();
190 }
191
192 public void setSelection(IStructuredSelection selection){
193 bulkEditorComposite.setSelection(selection);
194 }
195
196 public void setDirty(boolean isDirty){
197 dirty.setDirty(isDirty);
198 }
199
200 public void setDirty(){
201 setDirty(true);
202 }
203
204 @Override
205 public boolean isDirty() {
206 return dirty.isDirty();
207 }
208
209 public AbstractBulkEditorInput getEditorInput() {
210 return input;
211 }
212
213 public void copyDataToClipboard() {
214 bulkEditorComposite.copyDataToClipboard();
215 }
216
217 @Override
218 public void update(CdmDataChangeMap arg0) {
219 }
220
221 @Override
222 public boolean canAttachMedia() {
223 return true;
224 }
225
226 @Override
227 public void changed(Object element) {
228 if(element instanceof DerivedUnitFacade){
229 DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
230 if(derivedUnit!=null){
231 getEditorInput().addSaveCandidate(derivedUnit);
232 }
233 FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
234 if(fieldUnit!=null){
235 getEditorInput().addSaveCandidate(fieldUnit);
236 }
237 }
238 else if (element instanceof CdmBase) {
239 if (element instanceof DescriptionBase){
240 if (element instanceof TaxonNameDescription){
241 TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
242 getEditorInput().addSaveCandidate(changedName);
243 input.replaceInModel(changedName);
244 }else if (element instanceof TaxonDescription){
245 Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
246 getEditorInput().addSaveCandidate(changedTaxon);
247 input.replaceInModel(changedTaxon);
248 }
249 }else if (element instanceof DescriptionElementBase){
250 if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
251 TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
252 getEditorInput().addSaveCandidate(changedName);
253 input.replaceInModel(changedName);
254 }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
255 Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
256 getEditorInput().addSaveCandidate(changedTaxon);
257 input.replaceInModel(changedTaxon);
258 }
259 }else if(element instanceof Group){
260 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
261 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
262 getEditorInput().addSaveCandidate((Group)element);
263 input.replaceInModel((CdmBase) element);
264 }else{
265 getEditorInput().addSaveCandidate((CdmBase)element);
266 input.replaceInModel((CdmBase) element);
267 }
268 }
269 dirty.setDirty(true);
270 setSelection(new StructuredSelection(element));
271 }
272
273 @Override
274 public void forceDirty() {
275 dirty.setDirty(true);
276 }
277
278 @Override
279 public boolean postOperation(Object objectAffectedByOperation) {
280 return false;
281 }
282
283 @Override
284 public boolean onComplete() {
285 return false;
286 }
287
288 @Override
289 public ConversationHolder getConversationHolder() {
290 return conversation;
291 }
292
293 public BulkEditorQuery getLastQuery() {
294 return lastQuery;
295 }
296
297 public void setLastQuery(BulkEditorQuery lastQuery) {
298 this.lastQuery = lastQuery;
299
300 }
301
302 @Override
303 public Taxon getTaxon() {
304 IStructuredSelection selection = getSelection();
305 if(selection.size()==1) {
306 Object object = selection.iterator().next();
307 if(object instanceof Taxon){
308 return (Taxon) object;
309 }
310 }
311 return null;
312 }
313
314 /**
315 * {@inheritDoc}
316 */
317 @Override
318 public void update() {
319 input.performSearch(lastQuery, getSelection());
320 }
321
322 @Override
323 public void addOperation(AbstractPostOperation operation) {
324 // operations not yet used for bulk editor
325 }
326
327 @Inject
328 @Optional
329 private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
330 if (getEditorInput() instanceof TaxonEditorInput){
331 for (CdmBase cdmBase: cdmBases){
332 if (getEditorInput().getModel().contains(cdmBase)){
333 input.performSearch(lastQuery, getSelection());
334 break;
335 }
336 }
337 }
338 }
339
340 @Inject
341 @Optional
342 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
343 if (input instanceof GroupEditorInput){
344 ((GroupEditorInput)input).getSaveUserCandidates().add(user);
345 }
346 }
347
348 }