Project

General

Profile

Download (7.52 KB) Statistics
| Branch: | Tag: | Revision:
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.sql.SQLException;
13
import java.util.Collection;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
18

    
19
import org.apache.commons.lang.StringUtils;
20

    
21
import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
22
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
23

    
24
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.vaadin.statement.CdmStatementDelegate;
27

    
28
/**
29
 * @author cmathew
30
 * @date 1 Apr 2015
31
 *
32
 */
33
public class CdmQueryFactory {
34

    
35

    
36
	public static final String RANK_COLUMN = "Rank";
37
	public static final String TAXON_COLUMN = "Taxon";
38

    
39
    public static final String ID = "id";
40
    public static final String UUID_ID = "uuid";
41

    
42
    public static QueryDelegate generateTaxonBaseQuery(String name_id,
43
            String pb_id,
44
            String unp_id,
45
            String rank_id,
46
            String has_syn_id) throws SQLException {
47
        String FROM_QUERY = " FROM TaxonBase tb " +
48
                "INNER JOIN TaxonNode tn on tn.taxon_id=tb.id " +
49
                "INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
50
                "INNER JOIN DefinedTermBase dtb on tnb.rank_id = dtb.id";
51
        String SELECT_QUERY="SELECT tb.id as " + ID +
52
                ", tb.uuid as " + UUID_ID +
53
                ", tnb.titleCache as " + name_id +
54
                ", tb.publish as " + pb_id +
55
                ", tb.unplaced as " + unp_id +
56
                ", dtb.titleCache as " + rank_id +
57
                ", (SELECT COUNT(*) FROM  SynonymRelationship sr WHERE tb.id = sr.relatedto_id) as " + has_syn_id +
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 generateTaxonDistributionQuery(List<Integer> taxonNodeIds, Collection<NamedArea> namedAreas, boolean abbreviatedLabels) throws SQLException {
66

    
67
    	String idString = "";
68
    	Iterator<Integer> nodeIterator = taxonNodeIds.iterator();
69
    	while (nodeIterator.hasNext()) {
70
			Integer integer = nodeIterator.next();
71
			idString += String.valueOf(integer);
72
			if(nodeIterator.hasNext()){
73
				idString += ", ";
74
			}
75
		}
76
        String FROM_QUERY =
77
                "FROM TaxonNameBase tnb "
78
                + "INNER JOIN TaxonBase tb on tnb.id = tb.name_id and tb.DTYPE='Taxon' " + // # name<->taxon
79
        "INNER JOIN TaxonNode tn on tn.taxon_id = tb.id "+
80
        "INNER JOIN DefinedTermBase rank on tnb.rank_id = rank.id "+// # rank <-> name
81
        "LEFT OUTER JOIN DescriptionBase descr on descr.taxon_id = tb.id "+// # taxon <-> taxon description (not every taxon has a description)
82
        "LEFT OUTER JOIN DescriptionElementBase descrEl on descrEl.indescription_id = descr.id and descrEl.DTYPE = 'Distribution' "+// # distribution <-> description
83
        "LEFT OUTER JOIN DefinedTermBase statusTerm on statusTerm.id = descrEl.status_id "+
84
        "LEFT OUTER JOIN DefinedTermBase area on area.id = descrEl.area_id "+
85
        "WHERE tn.id IN ("+ idString +") ";
86

    
87
        String GROUP_BY = " GROUP BY tb.uuid, tn.id ";
88

    
89
        String ORDER_BY = " ORDER BY tb.titleCache ";
90

    
91
        String SELECT_QUERY= "SELECT "
92
                + "tb.DTYPE, "
93
                + "tb.id, "
94
                + "tb.uuid, "
95
                + "tn.classification_id, "+
96
        		"tb.titleCache AS "+TAXON_COLUMN+", " +
97
        		"rank.titleCache AS "+RANK_COLUMN+", ";
98

    
99
        for(NamedArea namedArea : namedAreas){
100
            String label;
101
            if(abbreviatedLabels){
102
                label = namedArea.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel();
103
            }
104
            else{
105
                label = namedArea.getTitleCache();
106
            }
107
            SELECT_QUERY += "MAX( IF(area.titleCache = '"+ label +"', statusTerm.titleCache, NULL) ) as '"+ label +"'," ;
108
        }
109
        SELECT_QUERY = StringUtils.stripEnd(SELECT_QUERY, ",")+" ";
110
        SELECT_QUERY= SELECT_QUERY + FROM_QUERY + GROUP_BY + ORDER_BY;
111
        String COUNT_QUERY = "SELECT count(DISTINCT tb.id)" + FROM_QUERY;
112
        String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.uuid = ?";
113
        //Escape SQL control character '
114
        Pattern p = Pattern.compile("(\\w+)'(\\w+)");
115
        Matcher m = p.matcher(SELECT_QUERY);
116
        if (m.find()) {
117
            SELECT_QUERY = m.replaceAll("$1\\\\'$2");
118
        }
119
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
120
    }
121

    
122
    public static QueryDelegate generateSynonymofTaxonQuery(String name_id) throws SQLException {
123
    	String FROM_QUERY = " FROM TaxonBase tb " +
124
    			"INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
125
    			"INNER JOIN SynonymRelationship sr on tb.id=sr.relatedfrom_id ";
126
    	String SELECT_QUERY="SELECT tb.id as " + ID +
127
    			", tnb.titleCache as " + name_id +
128
    			FROM_QUERY;
129
    	String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
130
    	String CONTAINS_QUERY = "SELECT * FROM SynonymRelationship sr WHERE sr.relatedfrom_id = ?";
131

    
132
    	return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
133
    }
134

    
135
    public static QueryDelegate generateTaxonRelatedToQuery(String reluuid_id,
136
            String reltype_id,
137
            String to_id,
138
            String touuid_id,
139
            String toname_id) throws SQLException {
140
        String FROM_QUERY = "     FROM TaxonRelationship tr " +
141
                "INNER JOIN TaxonBase tb on tr.relatedto_id = tb.id " +
142
                "INNER JOIN TaxonNode tn on tb.id = tn.taxon_id ";
143
        String SELECT_QUERY= "SELECT tr.id as " + ID +
144
                ", tr.uuid as " + reluuid_id +
145
                ", tr.type_id as " + reltype_id +
146
                ", tr.relatedto_id as " + to_id +
147
                ", tb.uuid as " + touuid_id +
148
                ", tb.titleCache as " + toname_id +
149
                FROM_QUERY;
150
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
151
        String CONTAINS_QUERY = "SELECT * FROM TaxonRelationship tr where tr.relatedfrom_id = ?";
152

    
153
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
154
    }
155

    
156
    /**
157
     * Creates a FreeformQuery which mimics a TableQuery.
158
     * This method works around the bug at http://dev.vaadin.com/ticket/12370
159
     *
160
     * @param tableName
161
     * @return
162
     * @throws SQLException
163
     */
164
    public static QueryDelegate generateTableQuery(String tableName) throws SQLException {
165
        String FROM_QUERY = " FROM " + tableName;
166
        String SELECT_QUERY=" SELECT * " +
167
                FROM_QUERY;
168
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
169
        String CONTAINS_QUERY = "SELECT * FROM " + tableName + "  WHERE id = ?";
170

    
171
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
172
    }
173

    
174
    public static QueryDelegate generateQueryDelegate(String SELECT_QUERY, String COUNT_QUERY, String CONTAINS_QUERY) throws SQLException {
175
        FreeformQuery query = new FreeformQuery("This query is not used", CdmSpringContextHelper.getCurrent().getConnectionPool(), ID);
176
        CdmStatementDelegate delegate = new CdmStatementDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
177
        query.setDelegate(delegate);
178
        return query;
179
    }
180

    
181
}
(1-1/10)