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.util;
|
11
|
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.regex.Matcher;
|
18
|
import java.util.regex.Pattern;
|
19
|
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
|
22
|
import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
|
23
|
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
26
|
import eu.etaxonomy.cdm.model.common.Language;
|
27
|
import eu.etaxonomy.cdm.model.common.Representation;
|
28
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
29
|
import eu.etaxonomy.cdm.vaadin.statement.CdmStatementDelegate;
|
30
|
|
31
|
/**
|
32
|
* @author cmathew
|
33
|
* @date 1 Apr 2015
|
34
|
*
|
35
|
*/
|
36
|
public class CdmQueryFactory {
|
37
|
|
38
|
|
39
|
public static final String DTYPE_COLUMN = "DTYPE";
|
40
|
public static final String ID_COLUMN = "id";
|
41
|
public static final String UUID_COLUMN = "uuid";
|
42
|
public static final String CLASSIFICATION_COLUMN = "classification";
|
43
|
public static final String RANK_COLUMN = "Rang";
|
44
|
public static final String TAXON_COLUMN = "Taxon";
|
45
|
|
46
|
public static final String ID = "id";
|
47
|
public static final String UUID_ID = "uuid";
|
48
|
|
49
|
public static QueryDelegate generateTaxonTreeQuery(String name_id, String classificationId) {
|
50
|
String FROM_QUERY = " FROM TaxonBase tb " +
|
51
|
"INNER JOIN TaxonNode tn on tn.taxon_id=tb.id " +
|
52
|
"INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
|
53
|
"INNER JOIN Classification cl on cl.id=tn.classification_id and cl.id='"+classificationId+"'";
|
54
|
String SELECT_QUERY="SELECT tn.id as " + ID +
|
55
|
", tb.uuid as " + UUID_ID +
|
56
|
", tnb.titleCache as " + name_id +
|
57
|
", tn.parent_id as parent" +
|
58
|
FROM_QUERY;
|
59
|
String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
|
60
|
String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.id = ?";
|
61
|
|
62
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
63
|
}
|
64
|
|
65
|
public static QueryDelegate generateTaxonBaseQuery(String name_id,
|
66
|
String pb_id,
|
67
|
String unp_id,
|
68
|
String rank_id,
|
69
|
String has_syn_id) {
|
70
|
String FROM_QUERY = " FROM TaxonBase tb " +
|
71
|
"INNER JOIN TaxonNode tn on tn.taxon_id=tb.id " +
|
72
|
"INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
|
73
|
"INNER JOIN DefinedTermBase dtb on tnb.rank_id = dtb.id";
|
74
|
String SELECT_QUERY="SELECT tb.id as " + ID +
|
75
|
", tb.uuid as " + UUID_ID +
|
76
|
", tnb.titleCache as " + name_id +
|
77
|
", tb.publish as " + pb_id +
|
78
|
", tb.unplaced as " + unp_id +
|
79
|
", dtb.titleCache as " + rank_id +
|
80
|
", (SELECT COUNT(*) FROM SynonymRelationship sr WHERE tb.id = sr.relatedto_id) as " + has_syn_id +
|
81
|
FROM_QUERY;
|
82
|
String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
|
83
|
String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.id = ?";
|
84
|
|
85
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
86
|
}
|
87
|
|
88
|
public static QueryDelegate generateTaxonDistributionQuery(List<Integer> taxonNodeIds, Collection<NamedArea> namedAreas) {
|
89
|
|
90
|
String idString = "";
|
91
|
Iterator<Integer> nodeIterator = taxonNodeIds.iterator();
|
92
|
while (nodeIterator.hasNext()) {
|
93
|
Integer integer = nodeIterator.next();
|
94
|
idString += String.valueOf(integer);
|
95
|
if(nodeIterator.hasNext()){
|
96
|
idString += ", ";
|
97
|
}
|
98
|
}
|
99
|
String FROM_QUERY =
|
100
|
"FROM TaxonNameBase tnb "
|
101
|
+ "INNER JOIN TaxonBase tb on tnb.id = tb.name_id and tb.DTYPE='Taxon' " + // # name<->taxon
|
102
|
"INNER JOIN TaxonNode tn on tn.taxon_id = tb.id "+
|
103
|
"INNER JOIN DefinedTermBase rank on tnb.rank_id = rank.id "+// # rank <-> name
|
104
|
"LEFT OUTER JOIN DescriptionBase descr on descr.taxon_id = tb.id "+// # taxon <-> taxon description (not every taxon has a description)
|
105
|
"LEFT OUTER JOIN DescriptionElementBase descrEl on descrEl.indescription_id = descr.id and descrEl.DTYPE = 'Distribution' "+// # distribution <-> description
|
106
|
"LEFT OUTER JOIN DefinedTermBase statusTerm on statusTerm.id = descrEl.status_id "+
|
107
|
"LEFT OUTER JOIN DefinedTermBase area on area.id = descrEl.area_id ";
|
108
|
if(CdmUtils.isNotBlank(idString)){
|
109
|
FROM_QUERY += "WHERE tn.id IN ("+ idString +") ";
|
110
|
}
|
111
|
|
112
|
String GROUP_BY = " GROUP BY tb.uuid, tn.id ";
|
113
|
|
114
|
String ORDER_BY = " ORDER BY tb.titleCache ";
|
115
|
|
116
|
String SELECT_QUERY= "SELECT "
|
117
|
+ "tb.DTYPE AS "+DTYPE_COLUMN+", "
|
118
|
+ "tb.id AS "+ID_COLUMN+", "
|
119
|
+ "tb.uuid AS "+UUID_COLUMN+", "
|
120
|
+ "tn.classification_id AS "+CLASSIFICATION_COLUMN+", "+
|
121
|
"tb.titleCache AS "+TAXON_COLUMN+", " +
|
122
|
"rank.titleCache AS "+RANK_COLUMN+", ";
|
123
|
|
124
|
Map<String, Integer> labels = new HashMap<>();
|
125
|
for(NamedArea namedArea : namedAreas){
|
126
|
String label = null;
|
127
|
String fullLabel = null;
|
128
|
String abbreviatedLabel = null;
|
129
|
Representation representation = namedArea.getRepresentation(Language.DEFAULT());
|
130
|
if(representation!=null){
|
131
|
fullLabel = representation.getLabel();
|
132
|
abbreviatedLabel = representation.getAbbreviatedLabel();
|
133
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
134
|
label = abbreviatedLabel;
|
135
|
}
|
136
|
else{
|
137
|
label = fullLabel;
|
138
|
}
|
139
|
}
|
140
|
//fallback
|
141
|
if(label==null){
|
142
|
label = namedArea.getTitleCache();
|
143
|
}
|
144
|
|
145
|
//check if label already exists
|
146
|
Integer count = labels.get(label);
|
147
|
if(count!=null){
|
148
|
//combine label and abbreviated and check again
|
149
|
if(abbreviatedLabel!=null && fullLabel!= null){
|
150
|
label = abbreviatedLabel+"-"+fullLabel;
|
151
|
}
|
152
|
}
|
153
|
count = labels.get(label);
|
154
|
if(count==null){
|
155
|
labels.put(label, 1);
|
156
|
}
|
157
|
else{
|
158
|
labels.put(label, count+1);
|
159
|
label += "("+count+")";
|
160
|
}
|
161
|
SELECT_QUERY += "MAX( IF(area.uuid = '"+ namedArea.getUuid() +"', statusTerm.titleCache, NULL) ) as '"+ label +"'," ;
|
162
|
}
|
163
|
SELECT_QUERY = StringUtils.stripEnd(SELECT_QUERY, ",")+" ";
|
164
|
SELECT_QUERY= SELECT_QUERY + FROM_QUERY + GROUP_BY + ORDER_BY;
|
165
|
String COUNT_QUERY = "SELECT count(DISTINCT tb.id)" + FROM_QUERY;
|
166
|
String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.uuid = ?";
|
167
|
//Escape SQL control character '
|
168
|
Pattern p = Pattern.compile("(\\w+)'(\\w+)");
|
169
|
Matcher m = p.matcher(SELECT_QUERY);
|
170
|
if (m.find()) {
|
171
|
SELECT_QUERY = m.replaceAll("$1\\\\'$2");
|
172
|
}
|
173
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
174
|
}
|
175
|
|
176
|
public static QueryDelegate generateSynonymofTaxonQuery(String name_id) {
|
177
|
String FROM_QUERY = " FROM TaxonBase tb " +
|
178
|
"INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
|
179
|
"INNER JOIN SynonymRelationship sr on tb.id=sr.relatedfrom_id ";
|
180
|
String SELECT_QUERY="SELECT tb.id as " + ID +
|
181
|
", tnb.titleCache as " + name_id +
|
182
|
FROM_QUERY;
|
183
|
String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
|
184
|
String CONTAINS_QUERY = "SELECT * FROM SynonymRelationship sr WHERE sr.relatedfrom_id = ?";
|
185
|
|
186
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
187
|
}
|
188
|
|
189
|
public static QueryDelegate generateTaxonRelatedToQuery(String reluuid_id,
|
190
|
String reltype_id,
|
191
|
String to_id,
|
192
|
String touuid_id,
|
193
|
String toname_id) {
|
194
|
String FROM_QUERY = " FROM TaxonRelationship tr " +
|
195
|
"INNER JOIN TaxonBase tb on tr.relatedto_id = tb.id " +
|
196
|
"INNER JOIN TaxonNode tn on tb.id = tn.taxon_id ";
|
197
|
String SELECT_QUERY= "SELECT tr.id as " + ID +
|
198
|
", tr.uuid as " + reluuid_id +
|
199
|
", tr.type_id as " + reltype_id +
|
200
|
", tr.relatedto_id as " + to_id +
|
201
|
", tb.uuid as " + touuid_id +
|
202
|
", tb.titleCache as " + toname_id +
|
203
|
FROM_QUERY;
|
204
|
String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
|
205
|
String CONTAINS_QUERY = "SELECT * FROM TaxonRelationship tr where tr.relatedfrom_id = ?";
|
206
|
|
207
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Creates a FreeformQuery which mimics a TableQuery.
|
212
|
* This method works around the bug at http://dev.vaadin.com/ticket/12370
|
213
|
*
|
214
|
* @param tableName
|
215
|
* @return
|
216
|
*/
|
217
|
public static QueryDelegate generateTableQuery(String tableName) {
|
218
|
String FROM_QUERY = " FROM " + tableName;
|
219
|
String SELECT_QUERY=" SELECT * " +
|
220
|
FROM_QUERY;
|
221
|
String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
|
222
|
String CONTAINS_QUERY = "SELECT * FROM " + tableName + " WHERE id = ?";
|
223
|
|
224
|
return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
225
|
}
|
226
|
|
227
|
public static QueryDelegate generateQueryDelegate(String SELECT_QUERY, String COUNT_QUERY, String CONTAINS_QUERY) {
|
228
|
FreeformQuery query = new FreeformQuery("This query is not used", CdmSpringContextHelper.getCurrent().getConnectionPool(), ID);
|
229
|
CdmStatementDelegate delegate = new CdmStatementDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
|
230
|
query.setDelegate(delegate);
|
231
|
return query;
|
232
|
}
|
233
|
}
|