Project

General

Profile

Download (7.13 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.UpdateResult;
40
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
41
import eu.etaxonomy.cdm.model.description.DescriptionBase;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
44
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
45
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
46
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
47
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
48
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
49
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
50
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
51
import eu.etaxonomy.taxeditor.store.CdmStore;
52
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
53

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

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

    
73
    private List<TaxonDistributionDTO> taxonList;
74
    private Collection<UpdateResult> updateResults;
75

    
76
    private Collection<Taxon> rootEntities;
77

    
78
    private ConversationHolder conversation;
79

    
80
    private ICdmEntitySession cdmEntitySession;
81

    
82
    @Inject
83
    private ESelectionService selService;
84

    
85
    @Inject
86
    private MDirtyable dirty;
87

    
88
    @Inject
89
    private MPart thisPart;
90

    
91
    private DistributionEditor editor;
92

    
93
    private StackLayout stackLayout;
94

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

    
110
        ContextInjectionFactory.inject(editor, context);
111
    }
112

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

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

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

    
138

    
139
    public DistributionEditor getEditor() {
140
        return editor;
141
    }
142

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

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

    
163
    }
164

    
165

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

    
172
        return cdmEntitySession;
173
    }
174

    
175

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

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

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

    
201
    }
202

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

    
210
    }
211

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

    
218
        return conversation;
219
    }
220

    
221

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

    
229
    protected void saveDistributions() {
230
        Set<TaxonDistributionDTO> coll = new HashSet();
231
        editor.taxonList.forEach(dto->coll.add(dto));
232
        List<MergeResult<DescriptionBase>> result = CdmStore.getService(IDescriptionService.class).mergeDescriptionElements(coll, true);
233
        for (MergeResult<DescriptionBase> mergeRes: result ){
234
            cdmEntitySession.load(mergeRes, true);
235
        }
236
       // editor.createTaxonDistributionMap();
237
        conversation.commit();
238
        this.dirty.setDirty(false);
239
        editor.getDescriptionsToSave().clear();
240
    }
241

    
242
    /**
243
     * {@inheritDoc}
244
     */
245
    @Override
246
    public boolean isDirty() {
247
        return this.dirty.isDirty();
248
    }
249

    
250
    public void setDirty() {
251
        this.dirty.setDirty(true);
252
    }
253

    
254

    
255

    
256

    
257
}
(13-13/19)