1
|
/**
|
2
|
* Copyright (C) 2015 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.cdm.vaadin.component.taxon;
|
10
|
|
11
|
import java.sql.SQLException;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.springframework.transaction.TransactionStatus;
|
17
|
|
18
|
import com.vaadin.data.util.sqlcontainer.RowId;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
21
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
22
|
import eu.etaxonomy.cdm.api.service.INameService;
|
23
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
24
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
25
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
26
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
29
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
33
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
|
37
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
38
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
39
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
40
|
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
|
41
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
42
|
|
43
|
/**
|
44
|
* @author cmathew
|
45
|
* @since 2 Apr 2015
|
46
|
*/
|
47
|
public class NewTaxonBasePresenter implements INewTaxonBaseComponentListener {
|
48
|
|
49
|
private final CdmSQLContainer accTaxonSecRefContainer;
|
50
|
private final CdmSQLContainer synSecRefContainer;
|
51
|
|
52
|
private final IReferenceService referenceService;
|
53
|
private final ITaxonNodeService taxonNodeService;
|
54
|
private final ITaxonService taxonService;
|
55
|
private final IClassificationService classificationService;
|
56
|
private final INameService nameService;
|
57
|
private final ICdmRepository app;
|
58
|
|
59
|
final boolean includeUnpublished = true;
|
60
|
|
61
|
|
62
|
@Override
|
63
|
public CdmSQLContainer getAccTaxonSecRefContainer() {
|
64
|
return accTaxonSecRefContainer;
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public CdmSQLContainer getSynSecRefContainer() {
|
69
|
return synSecRefContainer;
|
70
|
}
|
71
|
|
72
|
public NewTaxonBasePresenter() throws SQLException {
|
73
|
accTaxonSecRefContainer = CdmSQLContainer.newInstance("Reference");
|
74
|
synSecRefContainer = CdmSQLContainer.newInstance("Reference");
|
75
|
referenceService = CdmSpringContextHelper.getReferenceService();
|
76
|
taxonNodeService = CdmSpringContextHelper.getTaxonNodeService();
|
77
|
taxonService = CdmSpringContextHelper.getTaxonService();
|
78
|
nameService = CdmSpringContextHelper.getNameService();
|
79
|
|
80
|
classificationService = CdmSpringContextHelper.getClassificationService();
|
81
|
app = CdmSpringContextHelper.getApplicationConfiguration();
|
82
|
}
|
83
|
|
84
|
private boolean checkIfNameExists(INonViralName nvn) {
|
85
|
TaxonName name = TaxonName.castAndDeproxy(nvn);
|
86
|
Pager<TaxonName> names = nameService.findByName(TaxonName.class,
|
87
|
name.getNameCache(),
|
88
|
MatchMode.EXACT,
|
89
|
null,
|
90
|
null,
|
91
|
-1,
|
92
|
null,
|
93
|
null);
|
94
|
if(names.getCount() > 0) {
|
95
|
return true;
|
96
|
}
|
97
|
return false;
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public IdUuidName newTaxon(String scientificName, Object secRefItemId, UUID classificationUuid) {
|
102
|
NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
103
|
INonViralName name = parser.parseFullName(scientificName);
|
104
|
|
105
|
if(checkIfNameExists(name)) {
|
106
|
throw new IllegalArgumentException("Given name already exists");
|
107
|
}
|
108
|
TransactionStatus tx = app.startTransaction();
|
109
|
UUID uuid = accTaxonSecRefContainer.getUuid(secRefItemId);
|
110
|
Reference sec = CdmBase.deproxy(referenceService.load(uuid), Reference.class);
|
111
|
|
112
|
//name.setTitleCache(scientificName, true);
|
113
|
Taxon newTaxon = Taxon.NewInstance(name, sec);
|
114
|
List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String []{
|
115
|
"rootNode.childNodes"
|
116
|
});
|
117
|
Classification classification = CdmBase.deproxy(classificationService.load(classificationUuid, CLASSIFICATION_INIT_STRATEGY), Classification.class);
|
118
|
TaxonNode newTaxonNode = classification.addChildTaxon(newTaxon, null, null);
|
119
|
taxonNodeService.saveOrUpdate(newTaxonNode);
|
120
|
newTaxonNode.setStatus(TaxonNodeStatus.UNPLACED);
|
121
|
|
122
|
app.commitTransaction(tx);
|
123
|
return new IdUuidName(newTaxonNode.getTaxon().getId(), newTaxonNode.getTaxon().getUuid(), newTaxonNode.getTaxon().getTitleCache());
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public IdUuidName newSynonym(String scientificName, Object synSecRefItemId, Object accTaxonSecRefItemId, UUID accTaxonUuid) {
|
128
|
NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
129
|
INonViralName name = parser.parseFullName(scientificName);
|
130
|
|
131
|
if(checkIfNameExists(name)) {
|
132
|
throw new IllegalArgumentException("Given name already exists");
|
133
|
}
|
134
|
TransactionStatus tx = app.startTransaction();
|
135
|
List<String> ACC_TAXON_INIT_STRATEGY = Arrays.asList(new String []{
|
136
|
"synonymRelations"
|
137
|
});
|
138
|
|
139
|
UUID synRefUuid = synSecRefContainer.getUuid(synSecRefItemId);
|
140
|
Reference synSec = CdmBase.deproxy(referenceService.load(synRefUuid), Reference.class);
|
141
|
|
142
|
//name.setTitleCache(scientificName, true);
|
143
|
Synonym newSynonym = Synonym.NewInstance(name, synSec);
|
144
|
|
145
|
|
146
|
UUID accTaxonRefUuid = accTaxonSecRefContainer.getUuid(accTaxonSecRefItemId);
|
147
|
Reference accTaxonSec = CdmBase.deproxy(referenceService.load(accTaxonRefUuid), Reference.class);
|
148
|
Taxon accTaxon = CdmBase.deproxy(taxonService.load(accTaxonUuid, includeUnpublished, ACC_TAXON_INIT_STRATEGY), Taxon.class);
|
149
|
accTaxon.setSec(accTaxonSec);
|
150
|
|
151
|
accTaxon.addSynonym(newSynonym, SynonymType.SYNONYM_OF());
|
152
|
|
153
|
UUID newUuid = taxonService.save(newSynonym).getUuid();
|
154
|
app.commitTransaction(tx);
|
155
|
return new IdUuidName(newSynonym.getId(), newUuid, newSynonym.getTitleCache());
|
156
|
}
|
157
|
|
158
|
|
159
|
@Override
|
160
|
public Object getAcceptedTaxonRefId(UUID accTaxonUuid) {
|
161
|
Taxon accTaxon = CdmBase.deproxy(taxonService.load(accTaxonUuid), Taxon.class);
|
162
|
if(accTaxon.getSec() != null) {
|
163
|
int refId = accTaxon.getSec().getId();
|
164
|
RowId itemId = new RowId(refId);
|
165
|
return itemId;
|
166
|
}
|
167
|
return null;
|
168
|
}
|
169
|
|
170
|
@Override
|
171
|
public Object getClassificationRefId(UUID classificationUuid) {
|
172
|
Classification classification = CdmBase.deproxy(classificationService.load(classificationUuid), Classification.class);
|
173
|
if(classification.getReference() != null) {
|
174
|
int refId = classification.getReference().getId();
|
175
|
RowId itemId = new RowId(refId);
|
176
|
return itemId;
|
177
|
}
|
178
|
return null;
|
179
|
}
|
180
|
|
181
|
|
182
|
|
183
|
}
|