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