Project

General

Profile

Download (6.39 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
 * @author a.oppermann
32
 * @date 25.04.2014
33
 */
34
public class ChecklistEditorInput extends CdmEntitySessionInput<TaxonNode> implements IPersistable {
35

    
36
    /**
37
     * The selected classification
38
     */
39
    private TaxonNode taxonNode = null;
40

    
41
    private List<TaxonBase> taxa = new ArrayList<>();
42

    
43
    private Classification classification = null;
44

    
45
    private final ConversationHolder conversation;
46

    
47
    private List<TaxonBase> taxaToSave = new ArrayList<>();
48

    
49

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

    
63
    }
64

    
65
    private void getChildTaxa(TaxonNode taxonNode2) {
66
       taxonNode2.removeNullValueFromChildren();
67
       if (taxonNode2.hasChildNodes()){
68
           for (TaxonNode node: taxonNode2.getChildNodes()){
69
        	   node = CdmStore.getService(ITaxonNodeService.class).load(node.getUuid(), getTaxonNodePropertyPaths());
70
               taxa.add(HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class));
71
               getChildTaxa(node);
72
           }
73
       }
74
    }
75

    
76
    /**
77
     * Creates an editor input for the {@link ChecklistView} with the currently selected classification.
78
     * @param classificationUuid
79
     */
80
    public ChecklistEditorInput(Classification classification) {
81
        super(true);
82
        this.conversation = CdmStore.createConversation();
83
        initSession();
84
        this.classification = CdmStore.getCurrentApplicationConfiguration().getClassificationService().load(classification.getUuid());
85
        getChildTaxa(classification.getRootNode());
86

    
87
    }
88

    
89
    public List<TaxonBase> getTaxa() {
90
		return taxa;
91
	}
92

    
93
    public String getName() {
94
        if(taxonNode != null && taxonNode.getTaxon()!=null){
95
            return taxonNode.getTaxon().getName().getTitleCache();
96
        }else{
97
            return classification.getTitleCache();
98
        }
99
    }
100

    
101
    public TaxonNode getTaxonNode() {
102
        return taxonNode;
103
    }
104

    
105
    public Classification getClassification(){
106
    	return classification;
107
    }
108

    
109
//    public ConversationHolder getConversationHolder() {
110
//        return conversationHolder;
111
//    }
112

    
113
//    @Override
114
//    public int hashCode() {
115
//        final int prime = 31;
116
//        int result = 1;
117
//        result = prime * result + ((classification == null) ? 0 : classification.hashCode());
118
//        result = prime * result + ((taxonNode == null) ? 0 : taxonNode.hashCode());
119
//        return result;
120
//    }
121

    
122
    @Override
123
    public boolean equals(Object obj) {
124
        if (this == obj) {
125
            return true;
126
        }
127
        if (obj == null) {
128
            return false;
129
        }
130
        if (getClass() != obj.getClass()) {
131
            return false;
132
        }
133
        ChecklistEditorInput other = (ChecklistEditorInput) obj;
134
        if (classification == null) {
135
            if (other.classification != null) {
136
                return false;
137
            }
138
        } else if (!classification.equals(other.classification)) {
139
            return false;
140
        }
141
        if (taxonNode == null) {
142
            if (other.taxonNode != null) {
143
                return false;
144
            }
145
        } else if (!taxonNode.equals(other.taxonNode)) {
146
            return false;
147
        }
148
        return true;
149
    }
150

    
151
    @Override
152
    public void saveState(IMemento memento) {
153
        UUID uuid = classification.getUuid();
154
    }
155

    
156
    @Override
157
    public void merge() {
158
        if (!getCdmEntitySession().isActive()){
159
            getCdmEntitySession().bind();
160
        }
161
        CdmStore.getService(ITaxonService.class).merge(taxaToSave, true);
162
    }
163

    
164
    @Override
165
    public List<TaxonNode> getRootEntities() {
166
        return Arrays.asList(taxonNode);
167
    }
168

    
169
    public ConversationHolder getConversation() {
170
        return conversation;
171
    }
172

    
173
    private List<String> getTaxonNodePropertyPaths() {
174
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
175
        for(String propertyPath : getTaxonBasePropertyPaths()) {
176
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
177
        }
178
        return taxonNodePropertyPaths;
179
    }
180

    
181
    private List<String> getTaxonBasePropertyPaths() {
182
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
183
                "sec", //$NON-NLS-1$
184
                "rights", //$NON-NLS-1$
185
                "sources", //$NON-NLS-1$
186
                "descriptions", //$NON-NLS-1$
187
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
188
                "descriptions.descriptionElements.area", //$NON-NLS-1$
189
                "descriptions.descriptionElements.status", //$NON-NLS-1$
190
                "descriptions.markers", //$NON-NLS-1$
191

    
192
        });
193

    
194
        return taxonBasePropertyPaths;
195
    }
196

    
197
	public void setTaxa(List<TaxonBase> taxonList) {
198
		this.taxa = taxonList;
199

    
200
	}
201

    
202
    /**
203
     * @param element
204
     */
205
    public void addTaxonToSave(Taxon element) {
206
        this.taxaToSave.add(element);
207

    
208
    }
209

    
210
//    @Override
211
//    public void dispose(){
212
//    	conversation.unbind();
213
//    	conversation.close();
214
//    	getCdmEntitySession().dispose();
215
//    	super.dispose();
216
//    }
217

    
218
}
(3-3/3)