Project

General

Profile

Download (7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2013 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;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.eclipse.ui.IMemento;
17
import org.eclipse.ui.IPersistable;
18

    
19
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
21
import eu.etaxonomy.cdm.api.service.ITaxonService;
22
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23
import eu.etaxonomy.cdm.model.taxon.Classification;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29

    
30
/**
31
 *
32
 * @author a.oppermann
33
 * @date 25.04.2014
34
 *
35
 */
36
public class ChecklistEditorInput extends CdmEntitySessionInput implements IPersistable {
37

    
38
    /**
39
     * The selected classification
40
     */
41
    private TaxonNode taxonNode = null;
42

    
43
    private List<TaxonBase> taxa = new ArrayList();
44

    
45
    private Classification classification = null;
46

    
47
    private final ConversationHolder conversation;
48

    
49
    private List<TaxonBase> taxaToSave = new ArrayList();
50

    
51

    
52
    /**
53
     * Creates an editor input for the {@link ChecklistView} with the currently selected TaxonNode
54
     * and its childNodes.
55
     * @param taxonNode
56
     */
57
    public ChecklistEditorInput(TaxonNode taxonNode){
58
        super(false);
59
        this.conversation = CdmStore.createConversation();
60
        initSession();
61
        this.taxonNode =CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
62
        getChildTaxa(taxonNode);
63
        classification = taxonNode.getClassification();
64

    
65
    }
66

    
67

    
68
    /**
69
     * @param taxonNode2
70
     */
71
    private void getChildTaxa(TaxonNode taxonNode2) {
72
       taxonNode2.removeNullValueFromChildren();
73
       if (taxonNode2.hasChildNodes()){
74
           for (TaxonNode node: taxonNode2.getChildNodes()){
75
        	   node = CdmStore.getService(ITaxonNodeService.class).load(node.getUuid(), getTaxonNodePropertyPaths());
76
               taxa.add(HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class));
77
               getChildTaxa(node);
78
           }
79
       }
80

    
81

    
82
    }
83

    
84
    /**
85
     * Creates an editor input for the {@link ChecklistView} with the currently selected classification.
86
     * @param classificationUuid
87
     */
88
    public ChecklistEditorInput(Classification classification) {
89
        super(true);
90
        this.conversation = CdmStore.createConversation();
91
        initSession();
92
        this.classification = CdmStore.getCurrentApplicationConfiguration().getClassificationService().load(classification.getUuid());
93
        getChildTaxa(classification.getRootNode());
94

    
95
    }
96

    
97

    
98

    
99
    public List<TaxonBase> getTaxa() {
100
		return taxa;
101
	}
102

    
103
    public String getName() {
104
        if(taxonNode != null && taxonNode.getTaxon()!=null){
105
            return taxonNode.getTaxon().getName().getTitleCache();
106
        }else{
107
            return classification.getTitleCache();
108
        }
109
    }
110

    
111

    
112
    /**
113
     * @return the taxonNode
114
     */
115
    public TaxonNode getTaxonNode() {
116
        return taxonNode;
117
    }
118

    
119
    /**
120
     * @return classification
121
     */
122
    public Classification getClassification(){
123
    	return classification;
124
    }
125

    
126
//    /**
127
//     * @return the conversationHolder
128
//     */
129
//    public ConversationHolder getConversationHolder() {
130
//        return conversationHolder;
131
//    }
132

    
133

    
134

    
135
//    /* (non-Javadoc)
136
//     * @see java.lang.Object#hashCode()
137
//     */
138
//    @Override
139
//    public int hashCode() {
140
//        final int prime = 31;
141
//        int result = 1;
142
//        result = prime * result + ((classification == null) ? 0 : classification.hashCode());
143
//        result = prime * result + ((taxonNode == null) ? 0 : taxonNode.hashCode());
144
//        return result;
145
//    }
146

    
147
    /* (non-Javadoc)
148
     * @see java.lang.Object#equals(java.lang.Object)
149
     */
150
    @Override
151
    public boolean equals(Object obj) {
152
        if (this == obj) {
153
            return true;
154
        }
155
        if (obj == null) {
156
            return false;
157
        }
158
        if (getClass() != obj.getClass()) {
159
            return false;
160
        }
161
        ChecklistEditorInput other = (ChecklistEditorInput) obj;
162
        if (classification == null) {
163
            if (other.classification != null) {
164
                return false;
165
            }
166
        } else if (!classification.equals(other.classification)) {
167
            return false;
168
        }
169
        if (taxonNode == null) {
170
            if (other.taxonNode != null) {
171
                return false;
172
            }
173
        } else if (!taxonNode.equals(other.taxonNode)) {
174
            return false;
175
        }
176
        return true;
177
    }
178

    
179
    /* (non-Javadoc)
180
     * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
181
     */
182
    @Override
183
    public void saveState(IMemento memento) {
184
        UUID uuid = classification.getUuid();
185
    }
186

    
187
    @Override
188
    public void merge() {
189
        if (!getCdmEntitySession().isActive()){
190
            getCdmEntitySession().bind();
191
        }
192
        CdmStore.getService(ITaxonService.class).merge(taxaToSave, true);
193
    }
194

    
195
    /* (non-Javadoc)
196
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
197
     */
198
    @Override
199
    public List<TaxonNode> getRootEntities() {
200
        return Arrays.asList(taxonNode);
201
    }
202

    
203
    /**
204
     * @return the conversation
205
     */
206
    public ConversationHolder getConversation() {
207
        return conversation;
208
    }
209

    
210
    private List<String> getTaxonNodePropertyPaths() {
211
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
212
        for(String propertyPath : getTaxonBasePropertyPaths()) {
213
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
214
        }
215
        return taxonNodePropertyPaths;
216
    }
217

    
218
    private List<String> getTaxonBasePropertyPaths() {
219
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
220
                "sec", //$NON-NLS-1$
221
                "rights", //$NON-NLS-1$
222
                "sources", //$NON-NLS-1$
223
                "descriptions", //$NON-NLS-1$
224
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
225
                "descriptions.descriptionElements.area", //$NON-NLS-1$
226
                "descriptions.descriptionElements.status", //$NON-NLS-1$
227
                "descriptions.markers", //$NON-NLS-1$
228

    
229
        });
230

    
231
        return taxonBasePropertyPaths;
232
    }
233

    
234

    
235
	public void setTaxa(List<TaxonBase> taxonList) {
236
		this.taxa = taxonList;
237

    
238
	}
239

    
240

    
241
    /**
242
     * @param element
243
     */
244
    public void addTaxonToSave(Taxon element) {
245
        this.taxaToSave.add(element);
246

    
247
    }
248

    
249
//    @Override
250
//    public void dispose(){
251
//    	conversation.unbind();
252
//    	conversation.close();
253
//    	getCdmEntitySession().dispose();
254
//    	super.dispose();
255
//    }
256

    
257
}
(4-4/5)