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