Project

General

Profile

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

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

    
16
import eu.etaxonomy.cdm.api.service.INameService;
17
import eu.etaxonomy.cdm.api.service.ITaxonService;
18
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
19
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
20
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
21
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
22
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23
import eu.etaxonomy.cdm.model.name.HybridRelationship;
24
import eu.etaxonomy.cdm.model.name.TaxonName;
25
import eu.etaxonomy.cdm.model.taxon.Synonym;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
29
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
30
import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.TaxonCreator;
31
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.IdentifiableEntitySortProvider;
32
import eu.etaxonomy.taxeditor.model.MessagingUtils;
33
import eu.etaxonomy.taxeditor.store.CdmStore;
34

    
35
public class TaxonEditorInput extends AbstractBulkEditorInput<TaxonBase>{
36

    
37
	private static TaxonEditorInput instance;
38

    
39
	public static final String ID = "bulkeditor.input.taxon";
40

    
41
	private static final String PROPERTY_PUBLISH = "Publish";
42

    
43
	private List<TaxonName> saveNameCandidates = new ArrayList<>();
44

    
45
	public static AbstractBulkEditorInput getInstance() {
46
		if (instance == null) {
47
			instance = new TaxonEditorInput();
48
		}
49

    
50
		return instance;
51
	}
52

    
53
    @Override
54
    protected List<String> getPropertyKeys_internal() {
55
        List<String> propertyKeysInternal = new ArrayList<>();
56
        propertyKeysInternal.add(PROPERTY_PUBLISH);
57
        return propertyKeysInternal;
58
    }
59

    
60
    @Override
61
    public Object getPropertyValue(TaxonBase cdmBase, String property) {
62
        if(property.equals(PROPERTY_PUBLISH)){
63
            return cdmBase.isPublish();
64
        }
65
        return super.getPropertyValue(cdmBase, property);
66
    }
67

    
68
    @Override
69
    public boolean isBooleanProperty(String property) {
70
        if(property.equals(PROPERTY_PUBLISH)){
71
            return true;
72
        }
73
        return super.isBooleanProperty(property);
74
    }
75

    
76
    @Override
77
	public String getName() {
78
		return BulkEditorInputType.TAXON.label;
79
	}
80

    
81
	public static Object getID() {
82
		return ID;
83
	}
84

    
85
	@Override
86
    public List<IBulkEditorSortProvider<TaxonBase>> getSortProviders() {
87
        List<IBulkEditorSortProvider<TaxonBase>> sortProviders = super.getSortProviders();
88

    
89
        sortProviders.add(0, new IdentifiableEntitySortProvider<TaxonBase>());
90

    
91
        return sortProviders;
92
    }
93

    
94
	@Override
95
	public boolean isMergingEnabled() {
96
		return false;
97
	}
98
	@Override
99
	public TaxonBase save(TaxonBase entity) {
100
	    return CdmStore.getService(ITaxonService.class).merge(entity, true).getMergedEntity();
101
	}
102

    
103
    @Override
104
    protected long countEntities(IIdentifiableEntityServiceConfigurator configurator) {
105
        return CdmStore.getService(ITaxonService.class).countByTitle(configurator);
106
    }
107

    
108
	@Override
109
	public List listEntities(IIdentifiableEntityServiceConfigurator configurator) {
110
//		IFindTaxaAndNamesConfigurator<TaxonBase> newConfig = new FindTaxaAndNamesConfiguratorImpl<>();
111
//		newConfig.setTitleSearchString(configurator.getTitleSearchStringSqlized());
112
//		newConfig.setMatchMode(MatchMode.ANYWHERE);
113
	    List<String> propertyPaths = getPropertyPaths();
114
	    configurator.setPropertyPaths(propertyPaths);
115
		List<TaxonBase> taxa =  CdmStore.getSearchManager().findTaxa(configurator);
116
		List<TaxonBase> taxaCopy = new ArrayList<TaxonBase>();
117
		for (TaxonBase taxon:taxa){
118

    
119
			if (taxon instanceof Taxon){
120
				taxaCopy.add(HibernateProxyHelper.deproxy(taxon, Taxon.class));
121
			}else{
122
				taxaCopy.add(HibernateProxyHelper.deproxy(taxon, Synonym.class));
123
			}
124
		}
125
		return taxaCopy;
126
	}
127

    
128
	@Override
129
	protected TaxonBase loadEntity(UUID entityUuid) {
130
		List<String> propertyPaths = Arrays.asList(new String[]{});
131
		return CdmStore.getService(ITaxonService.class).load(entityUuid, propertyPaths);
132
	}
133

    
134
	@Override
135
    public boolean delete(TaxonBase entity, DeleteConfiguratorBase config) {
136
		if (entity instanceof Taxon){
137
			TaxonDeletionConfigurator taxonConfig = null;
138
			if (config instanceof TaxonDeletionConfigurator){
139
				taxonConfig = (TaxonDeletionConfigurator)config;
140
			}else{
141

    
142
			}
143

    
144
			return CdmStore.getService(ITaxonService.class).deleteTaxon(entity.getUuid(), taxonConfig, null) != null;
145
		} else{
146
			SynonymDeletionConfigurator synConfig = null;
147
			if (config instanceof SynonymDeletionConfigurator){
148
				synConfig = (SynonymDeletionConfigurator)config;
149
			}else{
150

    
151
			}
152

    
153
			return CdmStore.getService(ITaxonService.class).deleteSynonym(entity.getUuid(), synConfig) != null;
154
		}
155
	}
156

    
157
	@Override
158
	protected IEntityCreator<TaxonBase> createEntityCreator() {
159
		return new TaxonCreator();
160
	}
161

    
162
    @Override
163
    public void merge() {
164

    
165
    }
166

    
167
    @Override
168
    public void addSaveCandidate(TaxonBase taxonBase){
169
        if (taxonBase == null){
170
            MessagingUtils.info("The focus on the selected taxon was lost somehow, please try again.");
171
            return;
172
        }
173
        if (!taxonBase.getName().getHybridChildRelations().isEmpty()){
174
            for (HybridRelationship rel: taxonBase.getName().getHybridChildRelations()){
175
                if (!rel.getParentName().isPersited()){
176
                    this.saveNameCandidates.add(rel.getParentName());
177
                }
178
                if (!rel.getHybridName().isPersited()){
179
                    this.saveNameCandidates.add(rel.getHybridName());
180
                }
181
            }
182
        }
183

    
184
        super.addSaveCandidate(taxonBase);
185
    }
186

    
187
    @Override
188
    public void saveModel(boolean resetMerge){
189
        CdmStore.getService(INameService.class).save(this.saveNameCandidates);
190
        super.saveModel(resetMerge);
191
    }
192

    
193
    private List<String> getPropertyPaths(){
194
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
195
        "descriptions.descriptionElements.*",
196
        "typeDesignations"});
197

    
198
        return taxonBasePropertyPaths;
199
   }
200

    
201
}
(10-10/11)