2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import javax
.annotation
.PostConstruct
;
21 import javax
.annotation
.PreDestroy
;
22 import javax
.inject
.Inject
;
24 import eu
.etaxonomy
.cdm
.compare
.taxon
.TaxonNodeSortMode
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.e4
.core
.contexts
.ContextInjectionFactory
;
27 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
28 import org
.eclipse
.e4
.ui
.di
.Focus
;
29 import org
.eclipse
.e4
.ui
.di
.Persist
;
30 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
31 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
32 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EModelService
;
33 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
34 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
35 import org
.eclipse
.swt
.custom
.StackLayout
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
40 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
41 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
43 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
44 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TaxonDistributionDTO
;
45 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
46 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
49 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
50 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
51 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
52 import eu
.etaxonomy
.taxeditor
.editor
.IDistributionEditorPart
;
53 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
56 import eu
.etaxonomy
.taxeditor
.preference
.NavigatorOrderEnum
;
57 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
58 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
59 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
60 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
61 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
67 public class DistributionEditorPart
68 implements IE4SavablePart
, IConversationEnabled
, IDirtyMarkable
,
69 ICdmEntitySessionEnabled
<DescriptionBase
>, IPartContentHasSupplementalData
,
70 IPartContentHasDetails
, IDistributionEditorPart
{
72 private static final List
<String
> TAXONNODE_PROPERTY_PATH
= Arrays
.asList(new String
[] {
73 "taxon", //$NON-NLS-1$
74 "taxon.name", //$NON-NLS-1$
75 "taxon.name.rank",//$NON-NLS-1$
76 "taxon.descriptions", //$NON-NLS-1$
77 "taxon.descriptions.descriptionElements", //$NON-NLS-1$
78 "taxon.descriptions.descriptionElements.feature", //$NON-NLS-1$
79 "taxon.descriptions.descriptionElements.sources", //$NON-NLS-1$
80 "taxon.descriptions.descriptionElements.media" //$NON-NLS-1$
83 private List
<TaxonDistributionDTO
> taxonList
;
84 private Collection
<UpdateResult
> updateResults
;
86 private Collection
<Taxon
> rootEntities
;
88 private ConversationHolder conversation
;
90 private ICdmEntitySession cdmEntitySession
;
93 private ESelectionService selService
;
96 private MDirtyable dirty
;
99 private MPart thisPart
;
101 private DistributionEditor editor
;
103 private StackLayout stackLayout
;
106 private EModelService modelService
;
109 private EPartService partService
;
112 public void create(Composite parent
, IEclipseContext context
) {
113 if(CdmStore
.isActive() && conversation
==null){
114 conversation
= CdmStore
.createConversation();
116 if(cdmEntitySession
== null){
117 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
122 stackLayout
= new StackLayout();
123 parent
.setLayout(stackLayout
);
124 editor
= new DistributionEditor(parent
, this);
126 ContextInjectionFactory
.inject(editor
, context
);
129 public void init(UuidAndTitleCache nodeDto
) {
130 List
<UUID
> uuidList
= new ArrayList
<>();
131 uuidList
.add(nodeDto
.getUuid());
132 this.taxonList
= CdmStore
.getService(ITaxonNodeService
.class).getTaxonDistributionDTO(uuidList
, TAXONNODE_PROPERTY_PATH
, true);
133 // taxonList.stream().filter(taxonDistribution -> CdmStore.currentAuthentiationHasPermission(taxonDistribution.getTaxonUuid(), RequiredPermissions.TAXONNODE_EDIT));
137 editor
.loadDistributions(taxonList
);
138 editor
.createTable();
140 stackLayout
.topControl
= editor
;
141 editor
.getParent().layout();
145 public void init(List
<UuidAndTitleCache
> uuidAndTitleCaches
) {
146 List
<UUID
> nodeUuids
= new ArrayList
<>();
147 uuidAndTitleCaches
.forEach(element
-> nodeUuids
.add(element
.getUuid()));
149 TaxonNodeSortMode sortMode
= TaxonNodeSortMode
.RankAndAlphabeticalOrder
;
150 NavigatorOrderEnum orderValue
= NavigatorOrderEnum
.RankAndNameOrder
;
152 orderValue
= PreferencesUtil
.getSortNodes();
153 }catch(IllegalArgumentException e
){
157 if (orderValue
.equals(NavigatorOrderEnum
.NaturalOrder
)){
158 sortMode
= TaxonNodeSortMode
.NaturalOrder
;
159 } else if (orderValue
.equals(NavigatorOrderEnum
.AlphabeticalOrder
)){
160 sortMode
= TaxonNodeSortMode
.AlphabeticalOrder
;
162 sortMode
= TaxonNodeSortMode
.RankAndAlphabeticalOrder
;
164 this.taxonList
= CdmStore
.getService(ITaxonNodeService
.class).getTaxonDistributionDTO(nodeUuids
, TAXONNODE_PROPERTY_PATH
,null, true, sortMode
);
165 // taxonList.stream().filter(taxonDistribution -> CdmStore.currentAuthentiationHasPermission(taxonDistribution.getTaxonUuid(), RequiredPermissions.TAXONNODE_EDIT));
168 editor
.loadDistributions(taxonList
);
169 editor
.createTable();
171 stackLayout
.topControl
= editor
;
172 editor
.getParent().layout();
176 public ESelectionService
getSelectionService() {
181 public void setFocus(){
182 if(conversation
!=null){
185 if(cdmEntitySession
!= null) {
186 cdmEntitySession
.bind();
188 EditorUtil
.checkAndCloseFactsAndMediaParts(partService
);
192 public DistributionEditor
getEditor() {
197 public void update(CdmDataChangeMap changeEvents
) {
198 // TODO Auto-generated method stub
202 public void dispose(){
203 if (conversation
!= null) {
204 conversation
.close();
207 if(cdmEntitySession
!= null) {
208 cdmEntitySession
.dispose();
209 cdmEntitySession
= null;
211 dirty
.setDirty(false);
215 public ICdmEntitySession
getCdmEntitySession() {
216 return cdmEntitySession
;
220 public List
<DescriptionBase
> getRootEntities() {
221 List
<DescriptionBase
> rootEntities
= new ArrayList
<>();
222 editor
.taxonList
.forEach(dto
-> rootEntities
.addAll(dto
.getDescriptionsWrapper().getDescriptions()));
227 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
228 // TODO Auto-generated method stub
233 public void changed(Object element
) {
234 if (element
!= null){
235 dirty
.setDirty(true);
240 public void forceDirty() {
241 // TODO Auto-generated method stub
245 public ConversationHolder
getConversationHolder() {
251 public void save(IProgressMonitor monitor
) {
253 dirty
.setDirty(false);
256 protected void saveDistributions() {
257 Set
<TaxonDistributionDTO
> coll
= new HashSet
<>();
258 editor
.getDescriptionsToSave().forEach(dto
->coll
.add(dto
));
259 Set
<TaxonDistributionDTO
> removeList
= new HashSet
<>();
260 for (TaxonDistributionDTO dto
: coll
){
262 for(TaxonDescription desc
: dto
.getDescriptionsWrapper().getDescriptions()){
263 if (desc
.getTaxon() == null){
265 taxon
= (Taxon
)CdmStore
.getService(ITaxonService
.class).load(dto
.getTaxonUuid());
267 taxon
.addDescription(desc
);
270 MergeResult result
= CdmStore
.getService(ITaxonService
.class).merge(taxon
, true);
274 coll
.removeAll(removeList
);
275 List
<MergeResult
<DescriptionBase
>> result
= CdmStore
.getService(IDescriptionService
.class).mergeDescriptionElements(coll
, true);
276 for (MergeResult
<DescriptionBase
> mergeRes
: result
){
277 cdmEntitySession
.load(mergeRes
, true);
278 cdmEntitySession
.load(editor
.getDefaultSource(), true);
280 editor
.createTaxonDistributionMap();
281 conversation
.commit();
282 this.dirty
.setDirty(false);
283 editor
.getDescriptionsToSave().clear();
287 public boolean isDirty() {
288 return this.dirty
.isDirty();
291 public void setDirty() {
292 this.dirty
.setDirty(true);