Project

General

Profile

Download (8.18 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.ESelectionService;
32
import org.eclipse.swt.custom.StackLayout;
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.IDescriptionService;
38
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
39
import eu.etaxonomy.cdm.api.service.ITaxonService;
40
import eu.etaxonomy.cdm.api.service.UpdateResult;
41
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
42
import eu.etaxonomy.cdm.model.description.DescriptionBase;
43
import eu.etaxonomy.cdm.model.description.TaxonDescription;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
46
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
47
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
48
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
49
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
50
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
51
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
52
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
53
import eu.etaxonomy.taxeditor.store.CdmStore;
54
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
55

    
56
/**
57
 * @author k.luther
58
 * @since 28.11.2018
59
 *
60
 */
61
public class DistributionEditorPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
62
ICdmEntitySessionEnabled, IPartContentHasSupplementalData, IPartContentHasDetails, IDistributionEditor{
63

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

    
75
    private List<TaxonDistributionDTO> taxonList;
76
    private Collection<UpdateResult> updateResults;
77

    
78
    private Collection<Taxon> rootEntities;
79

    
80
    private ConversationHolder conversation;
81

    
82
    private ICdmEntitySession cdmEntitySession;
83

    
84
    @Inject
85
    private ESelectionService selService;
86

    
87
    @Inject
88
    private MDirtyable dirty;
89

    
90
    @Inject
91
    private MPart thisPart;
92

    
93
    private DistributionEditor editor;
94

    
95
    private StackLayout stackLayout;
96

    
97
    @PostConstruct
98
    public void create(Composite parent, IEclipseContext context) {
99
        if(CdmStore.isActive() && conversation==null){
100
            conversation = CdmStore.createConversation();
101
        }
102
        if(cdmEntitySession == null){
103
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
104
        }
105
        else{
106
            return;
107
        }
108
        stackLayout = new StackLayout();
109
        parent.setLayout(stackLayout);
110
        editor = new DistributionEditor(parent, this);
111

    
112
        ContextInjectionFactory.inject(editor, context);
113
    }
114

    
115
    public void init(UUID parentTaxonUuid) {
116
        this.taxonList = CdmStore.getService(ITaxonNodeService.class).getTaxonDistributionDTOForSubtree(parentTaxonUuid, TAXONNODE_PROPERTY_PATH);
117

    
118
        if(taxonList!=null){
119
            editor.loadDistributions(taxonList);
120
            editor.createTable();
121
            editor.pack();
122
            stackLayout.topControl = editor;
123
            editor.getParent().layout();
124
        }
125
    }
126
    public ESelectionService getSelectionService() {
127
        return selService;
128
    }
129

    
130
    @Focus
131
    public void setFocus(){
132
        if(conversation!=null){
133
            conversation.bind();
134
        }
135
        if(cdmEntitySession != null) {
136
            cdmEntitySession.bind();
137
        }
138
    }
139

    
140

    
141
    public DistributionEditor getEditor() {
142
        return editor;
143
    }
144

    
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    public void update(CdmDataChangeMap changeEvents) {
150
        // TODO Auto-generated method stub
151

    
152
    }
153
    @PreDestroy
154
    public void dispose(){
155
        if (conversation != null) {
156
            conversation.close();
157
            conversation = null;
158
        }
159
        if(cdmEntitySession != null) {
160
            cdmEntitySession.dispose();
161
            cdmEntitySession = null;
162
        }
163
        dirty.setDirty(false);
164

    
165
    }
166

    
167

    
168
    /**
169
     * {@inheritDoc}
170
     */
171
    @Override
172
    public ICdmEntitySession getCdmEntitySession() {
173

    
174
        return cdmEntitySession;
175
    }
176

    
177

    
178
    @Override
179
    public List<DescriptionBase> getRootEntities() {
180
        List<DescriptionBase> rootEntities = new ArrayList();
181
        editor.taxonList.forEach(dto -> rootEntities.addAll(dto.getDescriptionsWrapper().getDescriptions()));
182
        return rootEntities;
183
    }
184

    
185
    /**
186
     * {@inheritDoc}
187
     */
188
    @Override
189
    public Map<Object, List<String>> getPropertyPathsMap() {
190
        // TODO Auto-generated method stub
191
        return null;
192
    }
193

    
194
    /**
195
     * {@inheritDoc}
196
     */
197
    @Override
198
    public void changed(Object element) {
199
        if (element != null){
200
            dirty.setDirty(true);
201
        }
202

    
203
    }
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
    @Override
209
    public void forceDirty() {
210
        // TODO Auto-generated method stub
211

    
212
    }
213

    
214
    /**
215
     * {@inheritDoc}
216
     */
217
    @Override
218
    public ConversationHolder getConversationHolder() {
219

    
220
        return conversation;
221
    }
222

    
223

    
224
    @Persist
225
    @Override
226
    public void save(IProgressMonitor monitor) {
227
        saveDistributions();
228
        dirty.setDirty(false);
229
    }
230

    
231
    protected void saveDistributions() {
232
        Set<TaxonDistributionDTO> coll = new HashSet();
233
        editor.getDescriptionsToSave().forEach(dto->coll.add(dto));
234
        Set<TaxonDistributionDTO> removeList = new HashSet();
235
        for (TaxonDistributionDTO dto: coll){
236
            Taxon taxon = null;
237
            for(TaxonDescription desc: dto.getDescriptionsWrapper().getDescriptions()){
238
                if (desc.getTaxon() == null){
239
                    if (taxon == null){
240
                        taxon = (Taxon)CdmStore.getService(ITaxonService.class).load(dto.getTaxonUuid());
241
                    }
242
                    taxon.addDescription(desc);
243
                }
244
                if (taxon != null){
245
                    MergeResult result = CdmStore.getService(ITaxonService.class).merge(taxon, true);
246
                    boolean removed = dto.getDescriptionsWrapper().getDescriptions().remove(desc);
247
                    System.err.println("removed:"  + removed);
248
                }
249
            }
250
        }
251
        coll.removeAll(removeList);
252
        List<MergeResult<DescriptionBase>> result = CdmStore.getService(IDescriptionService.class).mergeDescriptionElements(coll, true);
253
        for (MergeResult<DescriptionBase> mergeRes: result ){
254
            cdmEntitySession.load(mergeRes, true);
255
            cdmEntitySession.load(editor.getDefaultSource(), true);
256
        }
257
        editor.createTaxonDistributionMap();
258
        conversation.commit();
259
        this.dirty.setDirty(false);
260
        editor.getDescriptionsToSave().clear();
261
    }
262

    
263
    /**
264
     * {@inheritDoc}
265
     */
266
    @Override
267
    public boolean isDirty() {
268
        return this.dirty.isDirty();
269
    }
270

    
271
    public void setDirty() {
272
        this.dirty.setDirty(true);
273
    }
274

    
275

    
276

    
277

    
278
}
(13-13/20)