Project

General

Profile

Download (9.41 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.view.checklist.e4;
10

    
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;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import javax.annotation.PostConstruct;
21
import javax.annotation.PreDestroy;
22
import javax.inject.Inject;
23

    
24
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
26
import org.eclipse.e4.core.contexts.IEclipseContext;
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.EModelService;
32
import org.eclipse.e4.ui.workbench.modeling.EPartService;
33
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
34
import org.eclipse.swt.custom.StackLayout;
35
import org.eclipse.swt.widgets.Composite;
36

    
37
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
38
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
39
import eu.etaxonomy.cdm.api.service.IDescriptionService;
40
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
41
import eu.etaxonomy.cdm.api.service.ITaxonService;
42
import eu.etaxonomy.cdm.api.service.UpdateResult;
43
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
44
import eu.etaxonomy.cdm.model.description.DescriptionBase;
45
import eu.etaxonomy.cdm.model.description.TaxonDescription;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
48
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
49
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
50
import eu.etaxonomy.taxeditor.editor.EditorUtil;
51
import eu.etaxonomy.taxeditor.editor.IDistributionEditorPart;
52
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
56
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
57
import eu.etaxonomy.taxeditor.store.CdmStore;
58
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
59

    
60
/**
61
 * @author k.luther
62
 * @since 28.11.2018
63
 *
64
 */
65
public class DistributionEditorPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
66
ICdmEntitySessionEnabled, IPartContentHasSupplementalData, IPartContentHasDetails, IDistributionEditorPart{
67

    
68
    private static final List<String> TAXONNODE_PROPERTY_PATH = Arrays.asList(new String[] {
69
            "taxon", //$NON-NLS-1$
70
            "taxon.name", //$NON-NLS-1$
71
            "taxon.name.rank",//$NON-NLS-1$
72
            "taxon.descriptions", //$NON-NLS-1$
73
            "taxon.descriptions.descriptionElements", //$NON-NLS-1$
74
            "taxon.descriptions.descriptionElements.feature", //$NON-NLS-1$
75
            "taxon.descriptions.descriptionElements.sources", //$NON-NLS-1$
76
            "taxon.descriptions.descriptionElements.media" //$NON-NLS-1$
77
    });
78

    
79
    private List<TaxonDistributionDTO> taxonList;
80
    private Collection<UpdateResult> updateResults;
81

    
82
    private Collection<Taxon> rootEntities;
83

    
84
    private ConversationHolder conversation;
85

    
86
    private ICdmEntitySession cdmEntitySession;
87

    
88
    @Inject
89
    private ESelectionService selService;
90

    
91
    @Inject
92
    private MDirtyable dirty;
93

    
94
    @Inject
95
    private MPart thisPart;
96

    
97
    private DistributionEditor editor;
98

    
99
    private StackLayout stackLayout;
100

    
101
    @Inject
102
    private EModelService modelService;
103

    
104
    @Inject
105
    private EPartService partService;
106

    
107
    @PostConstruct
108
    public void create(Composite parent, IEclipseContext context) {
109
        if(CdmStore.isActive() && conversation==null){
110
            conversation = CdmStore.createConversation();
111
        }
112
        if(cdmEntitySession == null){
113
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
114
        }
115
        else{
116
            return;
117
        }
118
        stackLayout = new StackLayout();
119
        parent.setLayout(stackLayout);
120
        editor = new DistributionEditor(parent, this);
121

    
122
        ContextInjectionFactory.inject(editor, context);
123
    }
124

    
125
    public void init(UuidAndTitleCache nodeDto) {
126
        List<UUID> uuidList = new ArrayList();
127
        uuidList.add(nodeDto.getUuid());
128
        this.taxonList = CdmStore.getService(ITaxonNodeService.class).getTaxonDistributionDTO(uuidList, TAXONNODE_PROPERTY_PATH, true);
129
       // taxonList.stream().filter(taxonDistribution ->  CdmStore.currentAuthentiationHasPermission(taxonDistribution.getTaxonUuid(), RequiredPermissions.TAXONNODE_EDIT));
130

    
131

    
132
        if(taxonList!=null){
133
            editor.loadDistributions(taxonList);
134
            editor.createTable();
135
            editor.pack();
136
            stackLayout.topControl = editor;
137
            editor.getParent().layout();
138
        }
139
    }
140

    
141
    public void init(List<UuidAndTitleCache> uuidAndTitleCaches) {
142
        List<UUID> nodeUuids = new ArrayList<>();
143
        uuidAndTitleCaches.forEach(element -> nodeUuids.add(element.getUuid()));
144
        this.taxonList = CdmStore.getService(ITaxonNodeService.class).getTaxonDistributionDTO(nodeUuids, TAXONNODE_PROPERTY_PATH, true);
145
       // taxonList.stream().filter(taxonDistribution ->  CdmStore.currentAuthentiationHasPermission(taxonDistribution.getTaxonUuid(), RequiredPermissions.TAXONNODE_EDIT));
146

    
147

    
148
        if(taxonList!=null){
149
            editor.loadDistributions(taxonList);
150
            editor.createTable();
151
            editor.pack();
152
            stackLayout.topControl = editor;
153
            editor.getParent().layout();
154
        }
155
    }
156

    
157
    public ESelectionService getSelectionService() {
158
        return selService;
159
    }
160

    
161
    @Focus
162
    public void setFocus(){
163
        if(conversation!=null){
164
            conversation.bind();
165
        }
166
        if(cdmEntitySession != null) {
167
            cdmEntitySession.bind();
168
        }
169
        EditorUtil.checkAndCloseFactsAndMediaParts(partService);
170
    }
171

    
172
    @Override
173
    public DistributionEditor getEditor() {
174
        return editor;
175
    }
176

    
177
    /**
178
     * {@inheritDoc}
179
     */
180
    @Override
181
    public void update(CdmDataChangeMap changeEvents) {
182
        // TODO Auto-generated method stub
183

    
184
    }
185
    @PreDestroy
186
    public void dispose(){
187
        if (conversation != null) {
188
            conversation.close();
189
            conversation = null;
190
        }
191
        if(cdmEntitySession != null) {
192
            cdmEntitySession.dispose();
193
            cdmEntitySession = null;
194
        }
195
        dirty.setDirty(false);
196

    
197
    }
198

    
199

    
200
    /**
201
     * {@inheritDoc}
202
     */
203
    @Override
204
    public ICdmEntitySession getCdmEntitySession() {
205

    
206
        return cdmEntitySession;
207
    }
208

    
209

    
210
    @Override
211
    public List<DescriptionBase> getRootEntities() {
212
        List<DescriptionBase> rootEntities = new ArrayList();
213
        editor.taxonList.forEach(dto -> rootEntities.addAll(dto.getDescriptionsWrapper().getDescriptions()));
214
        return rootEntities;
215
    }
216

    
217
    /**
218
     * {@inheritDoc}
219
     */
220
    @Override
221
    public Map<Object, List<String>> getPropertyPathsMap() {
222
        // TODO Auto-generated method stub
223
        return null;
224
    }
225

    
226
    /**
227
     * {@inheritDoc}
228
     */
229
    @Override
230
    public void changed(Object element) {
231
        if (element != null){
232
            dirty.setDirty(true);
233
        }
234

    
235
    }
236

    
237
    /**
238
     * {@inheritDoc}
239
     */
240
    @Override
241
    public void forceDirty() {
242
        // TODO Auto-generated method stub
243

    
244
    }
245

    
246
    /**
247
     * {@inheritDoc}
248
     */
249
    @Override
250
    public ConversationHolder getConversationHolder() {
251

    
252
        return conversation;
253
    }
254

    
255

    
256
    @Persist
257
    @Override
258
    public void save(IProgressMonitor monitor) {
259
        saveDistributions();
260
        dirty.setDirty(false);
261
    }
262

    
263
    protected void saveDistributions() {
264
        Set<TaxonDistributionDTO> coll = new HashSet();
265
        editor.getDescriptionsToSave().forEach(dto->coll.add(dto));
266
        Set<TaxonDistributionDTO> removeList = new HashSet();
267
        for (TaxonDistributionDTO dto: coll){
268
            Taxon taxon = null;
269
            for(TaxonDescription desc: dto.getDescriptionsWrapper().getDescriptions()){
270
                if (desc.getTaxon() == null){
271
                    if (taxon == null){
272
                        taxon = (Taxon)CdmStore.getService(ITaxonService.class).load(dto.getTaxonUuid());
273
                    }
274
                    taxon.addDescription(desc);
275
                }
276
                if (taxon != null){
277
                    MergeResult result = CdmStore.getService(ITaxonService.class).merge(taxon, true);
278
                }
279
            }
280
        }
281
        coll.removeAll(removeList);
282
        List<MergeResult<DescriptionBase>> result = CdmStore.getService(IDescriptionService.class).mergeDescriptionElements(coll, true);
283
        for (MergeResult<DescriptionBase> mergeRes: result ){
284
            cdmEntitySession.load(mergeRes, true);
285
            cdmEntitySession.load(editor.getDefaultSource(), true);
286
        }
287
        editor.createTaxonDistributionMap();
288
        conversation.commit();
289
        this.dirty.setDirty(false);
290
        editor.getDescriptionsToSave().clear();
291
    }
292

    
293
    /**
294
     * {@inheritDoc}
295
     */
296
    @Override
297
    public boolean isDirty() {
298
        return this.dirty.isDirty();
299
    }
300

    
301
    public void setDirty() {
302
        this.dirty.setDirty(true);
303
    }
304

    
305

    
306

    
307

    
308
}
(10-10/18)