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.HashMap;
|
13
|
import java.util.Map;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.springframework.transaction.TransactionStatus;
|
18
|
|
19
|
import com.vaadin.data.util.filter.Compare;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
22
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
23
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
26
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
28
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
29
|
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
|
30
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
31
|
|
32
|
/**
|
33
|
* @author cmathew
|
34
|
* @since 13 Apr 2015
|
35
|
*
|
36
|
*/
|
37
|
public class EditConceptRelationshipPresenter {
|
38
|
|
39
|
private CdmSQLContainer taxonRTypeContainer;
|
40
|
private CdmSQLContainer taxonRContainer;
|
41
|
|
42
|
private final ITaxonService taxonService;
|
43
|
private final ITermService termService;
|
44
|
private final ICdmRepository app;
|
45
|
|
46
|
public final static String REL_TYPE_KEY = "relTypeIun";
|
47
|
public final static String TO_TAXON_KEY = "toTaxonIun";
|
48
|
|
49
|
public EditConceptRelationshipPresenter() {
|
50
|
taxonService = CdmSpringContextHelper.getTaxonService();
|
51
|
termService = CdmSpringContextHelper.getTermService();
|
52
|
app = CdmSpringContextHelper.getApplicationConfiguration();
|
53
|
}
|
54
|
|
55
|
public CdmSQLContainer loadTaxonRelationshipTypeContainer() throws SQLException {
|
56
|
taxonRTypeContainer = CdmSQLContainer.newInstance("DefinedTermBase");
|
57
|
taxonRTypeContainer.addContainerFilter(new Compare.Equal("DTYPE","TaxonRelationshipType"));
|
58
|
taxonRTypeContainer.setPageLength(100);
|
59
|
return taxonRTypeContainer;
|
60
|
}
|
61
|
|
62
|
public CdmSQLContainer getTaxonRTypeContainer() {
|
63
|
return taxonRTypeContainer;
|
64
|
}
|
65
|
|
66
|
public CdmSQLContainer loadTaxonRelationshipContainer(Object itemId) throws SQLException {
|
67
|
taxonRContainer = CdmSQLContainer.newInstance("TaxonRelationship");
|
68
|
taxonRContainer.addContainerFilter(new Compare.Equal("relatedfrom_id", itemId.toString()));
|
69
|
return taxonRContainer;
|
70
|
}
|
71
|
|
72
|
public CdmSQLContainer getTaxonRContainer() {
|
73
|
return taxonRContainer;
|
74
|
}
|
75
|
|
76
|
public IdUuidName createRelationship(UUID fromTaxonUuid, UUID relTypeUuid, UUID toTaxonUuid) {
|
77
|
TransactionStatus tx = app.startTransaction();
|
78
|
Taxon fromTaxon = CdmBase.deproxy(taxonService.load(fromTaxonUuid), Taxon.class);
|
79
|
Taxon toTaxon = CdmBase.deproxy(taxonService.load(toTaxonUuid), Taxon.class);
|
80
|
TaxonRelationshipType relType = CdmBase.deproxy(termService.load(relTypeUuid), TaxonRelationshipType.class);
|
81
|
TaxonRelationship tr = fromTaxon.addTaxonRelation(toTaxon, relType, null, null);
|
82
|
app.commitTransaction(tx);
|
83
|
return new IdUuidName(tr.getId(), tr.getUuid(), tr.getType().getTitleCache());
|
84
|
}
|
85
|
|
86
|
|
87
|
public void updateRelationship(UUID fromTaxonUuid, UUID taxonRelUuid, UUID newRelTypeUuid , UUID newToTaxonUuid) {
|
88
|
TransactionStatus tx = app.startTransaction();
|
89
|
Taxon fromTaxon = CdmBase.deproxy(taxonService.load(fromTaxonUuid), Taxon.class);
|
90
|
for(TaxonRelationship tr : fromTaxon.getRelationsFromThisTaxon()) {
|
91
|
if(tr.getUuid().equals(taxonRelUuid)) {
|
92
|
if(newRelTypeUuid != null) {
|
93
|
TaxonRelationshipType relType = CdmBase.deproxy(termService.load(newRelTypeUuid), TaxonRelationshipType.class);
|
94
|
tr.setType(relType);
|
95
|
}
|
96
|
if(newToTaxonUuid != null) {
|
97
|
Taxon toTaxon = CdmBase.deproxy(taxonService.load(newToTaxonUuid), Taxon.class);
|
98
|
tr.setToTaxon(toTaxon);
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
app.commitTransaction(tx);
|
103
|
}
|
104
|
|
105
|
|
106
|
public void deleteRelationship(UUID fromTaxonUuid, UUID taxonRelUuid) {
|
107
|
TransactionStatus tx = app.startTransaction();
|
108
|
Taxon fromTaxon = CdmBase.deproxy(taxonService.load(fromTaxonUuid), Taxon.class);
|
109
|
TaxonRelationship trToDelete = null;
|
110
|
Set<TaxonRelationship> trList = fromTaxon.getRelationsFromThisTaxon();
|
111
|
for(TaxonRelationship tr : trList) {
|
112
|
if(tr.getUuid().equals(taxonRelUuid)) {
|
113
|
trToDelete = tr;
|
114
|
}
|
115
|
}
|
116
|
if(trToDelete != null) {
|
117
|
trList.remove(trToDelete);
|
118
|
}
|
119
|
app.commitTransaction(tx);
|
120
|
}
|
121
|
|
122
|
|
123
|
|
124
|
public Map<String, IdUuidName> getRelTypeToTaxonIunMap(UUID fromTaxonUuid, UUID taxonRelUuid) {
|
125
|
Map<String, IdUuidName> relTypeToTaxonIunMap = new HashMap<String, IdUuidName>();
|
126
|
TransactionStatus tx = app.startTransaction();
|
127
|
Taxon fromTaxon = CdmBase.deproxy(taxonService.load(fromTaxonUuid), Taxon.class);
|
128
|
for(TaxonRelationship tr : fromTaxon.getRelationsFromThisTaxon()) {
|
129
|
if(tr.getUuid().equals(taxonRelUuid)) {
|
130
|
relTypeToTaxonIunMap.put(REL_TYPE_KEY,
|
131
|
new IdUuidName(tr.getType().getId(),tr.getType().getUuid(), tr.getType().getTitleCache()));
|
132
|
relTypeToTaxonIunMap.put(TO_TAXON_KEY,
|
133
|
new IdUuidName(tr.getToTaxon().getId(), tr.getToTaxon().getUuid(), tr.getToTaxon().getName().getTitleCache()));
|
134
|
}
|
135
|
}
|
136
|
app.commitTransaction(tx);
|
137
|
return relTypeToTaxonIunMap;
|
138
|
}
|
139
|
|
140
|
public boolean canCreateRelationship(UUID fromTaxonUuid) {
|
141
|
TransactionStatus tx = app.startTransaction();
|
142
|
Taxon fromTaxon = CdmBase.deproxy(taxonService.load(fromTaxonUuid), Taxon.class);
|
143
|
boolean canCreateRelationship = true;
|
144
|
Set<TaxonRelationship> trList = fromTaxon.getRelationsFromThisTaxon();
|
145
|
for(TaxonRelationship tr : trList) {
|
146
|
if(tr.getType() != null && tr.getType().equals(TaxonRelationshipType.CONGRUENT_TO())) {
|
147
|
canCreateRelationship = false;
|
148
|
break;
|
149
|
}
|
150
|
}
|
151
|
app.commitTransaction(tx);
|
152
|
return canCreateRelationship;
|
153
|
}
|
154
|
|
155
|
}
|