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