Project

General

Profile

Download (8.64 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.common.Representation;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.vaadin.statement.CdmStatementDelegate;
28

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

    
36

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

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

    
43
    public static QueryDelegate generateTaxonTreeQuery(String name_id, String classificationId) throws SQLException {
44
        String FROM_QUERY = " FROM TaxonBase tb " +
45
                "INNER JOIN TaxonNode tn on tn.taxon_id=tb.id " +
46
                "INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
47
                "INNER JOIN Classification cl on cl.id=tn.classification_id and cl.id='"+classificationId+"'";
48
        String SELECT_QUERY="SELECT tn.id as " + ID +
49
                ", tb.uuid as " + UUID_ID +
50
                ", tnb.titleCache as " + name_id +
51
                ", tn.parent_id as parent" +
52
                FROM_QUERY;
53
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
54
        String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.id = ?";
55

    
56
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
57
    }
58

    
59
    public static QueryDelegate generateTaxonBaseQuery(String name_id,
60
            String pb_id,
61
            String unp_id,
62
            String rank_id,
63
            String has_syn_id) throws SQLException {
64
        String FROM_QUERY = " FROM TaxonBase tb " +
65
                "INNER JOIN TaxonNode tn on tn.taxon_id=tb.id " +
66
                "INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
67
                "INNER JOIN DefinedTermBase dtb on tnb.rank_id = dtb.id";
68
        String SELECT_QUERY="SELECT tb.id as " + ID +
69
                ", tb.uuid as " + UUID_ID +
70
                ", tnb.titleCache as " + name_id +
71
                ", tb.publish as " + pb_id +
72
                ", tb.unplaced as " + unp_id +
73
                ", dtb.titleCache as " + rank_id +
74
                ", (SELECT COUNT(*) FROM  SynonymRelationship sr WHERE tb.id = sr.relatedto_id) as " + has_syn_id +
75
                FROM_QUERY;
76
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
77
        String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.id = ?";
78

    
79
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
80
    }
81

    
82
    public static QueryDelegate generateTaxonDistributionQuery(List<Integer> taxonNodeIds, Collection<NamedArea> namedAreas) throws SQLException {
83

    
84
    	String idString = "";
85
    	Iterator<Integer> nodeIterator = taxonNodeIds.iterator();
86
    	while (nodeIterator.hasNext()) {
87
			Integer integer = nodeIterator.next();
88
			idString += String.valueOf(integer);
89
			if(nodeIterator.hasNext()){
90
				idString += ", ";
91
			}
92
		}
93
        String FROM_QUERY =
94
                "FROM TaxonNameBase tnb "
95
                + "INNER JOIN TaxonBase tb on tnb.id = tb.name_id and tb.DTYPE='Taxon' " + // # name<->taxon
96
        "INNER JOIN TaxonNode tn on tn.taxon_id = tb.id "+
97
        "INNER JOIN DefinedTermBase rank on tnb.rank_id = rank.id "+// # rank <-> name
98
        "LEFT OUTER JOIN DescriptionBase descr on descr.taxon_id = tb.id "+// # taxon <-> taxon description (not every taxon has a description)
99
        "LEFT OUTER JOIN DescriptionElementBase descrEl on descrEl.indescription_id = descr.id and descrEl.DTYPE = 'Distribution' "+// # distribution <-> description
100
        "LEFT OUTER JOIN DefinedTermBase statusTerm on statusTerm.id = descrEl.status_id "+
101
        "LEFT OUTER JOIN DefinedTermBase area on area.id = descrEl.area_id "+
102
        "WHERE tn.id IN ("+ idString +") ";
103

    
104
        String GROUP_BY = " GROUP BY tb.uuid, tn.id ";
105

    
106
        String ORDER_BY = " ORDER BY tb.titleCache ";
107

    
108
        String SELECT_QUERY= "SELECT "
109
                + "tb.DTYPE, "
110
                + "tb.id, "
111
                + "tb.uuid, "
112
                + "tn.classification_id, "+
113
        		"tb.titleCache AS "+TAXON_COLUMN+", " +
114
        		"rank.titleCache AS "+RANK_COLUMN+", ";
115

    
116
        for(NamedArea namedArea : namedAreas){
117
            String label = null;
118
            Representation representation = namedArea.getRepresentation(Language.DEFAULT());
119
            if(representation!=null){
120
				if(DistributionEditorUtil.isAbbreviatedLabels()){
121
            		label = representation.getAbbreviatedLabel();
122
            	}
123
            	else{
124
            		label = representation.getLabel();
125
            	}
126
            }
127
            if(label==null){
128
            	label = namedArea.getTitleCache();
129
            }
130
            SELECT_QUERY += "MAX( IF(area.titleCache = '"+ namedArea.getTitleCache() +"', statusTerm.titleCache, NULL) ) as '"+ label +"'," ;
131
        }
132
        SELECT_QUERY = StringUtils.stripEnd(SELECT_QUERY, ",")+" ";
133
        SELECT_QUERY= SELECT_QUERY + FROM_QUERY + GROUP_BY + ORDER_BY;
134
        String COUNT_QUERY = "SELECT count(DISTINCT tb.id)" + FROM_QUERY;
135
        String CONTAINS_QUERY = "SELECT * FROM TaxonBase tb WHERE tb.uuid = ?";
136
        //Escape SQL control character '
137
        Pattern p = Pattern.compile("(\\w+)'(\\w+)");
138
        Matcher m = p.matcher(SELECT_QUERY);
139
        if (m.find()) {
140
            SELECT_QUERY = m.replaceAll("$1\\\\'$2");
141
        }
142
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
143
    }
144

    
145
    public static QueryDelegate generateSynonymofTaxonQuery(String name_id) throws SQLException {
146
    	String FROM_QUERY = " FROM TaxonBase tb " +
147
    			"INNER JOIN TaxonNameBase tnb on tb.name_id=tnb.id " +
148
    			"INNER JOIN SynonymRelationship sr on tb.id=sr.relatedfrom_id ";
149
    	String SELECT_QUERY="SELECT tb.id as " + ID +
150
    			", tnb.titleCache as " + name_id +
151
    			FROM_QUERY;
152
    	String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
153
    	String CONTAINS_QUERY = "SELECT * FROM SynonymRelationship sr WHERE sr.relatedfrom_id = ?";
154

    
155
    	return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
156
    }
157

    
158
    public static QueryDelegate generateTaxonRelatedToQuery(String reluuid_id,
159
            String reltype_id,
160
            String to_id,
161
            String touuid_id,
162
            String toname_id) throws SQLException {
163
        String FROM_QUERY = "     FROM TaxonRelationship tr " +
164
                "INNER JOIN TaxonBase tb on tr.relatedto_id = tb.id " +
165
                "INNER JOIN TaxonNode tn on tb.id = tn.taxon_id ";
166
        String SELECT_QUERY= "SELECT tr.id as " + ID +
167
                ", tr.uuid as " + reluuid_id +
168
                ", tr.type_id as " + reltype_id +
169
                ", tr.relatedto_id as " + to_id +
170
                ", tb.uuid as " + touuid_id +
171
                ", tb.titleCache as " + toname_id +
172
                FROM_QUERY;
173
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
174
        String CONTAINS_QUERY = "SELECT * FROM TaxonRelationship tr where tr.relatedfrom_id = ?";
175

    
176
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
177
    }
178

    
179
    /**
180
     * Creates a FreeformQuery which mimics a TableQuery.
181
     * This method works around the bug at http://dev.vaadin.com/ticket/12370
182
     *
183
     * @param tableName
184
     * @return
185
     * @throws SQLException
186
     */
187
    public static QueryDelegate generateTableQuery(String tableName) throws SQLException {
188
        String FROM_QUERY = " FROM " + tableName;
189
        String SELECT_QUERY=" SELECT * " +
190
                FROM_QUERY;
191
        String COUNT_QUERY = "SELECT count(*) " + FROM_QUERY;
192
        String CONTAINS_QUERY = "SELECT * FROM " + tableName + "  WHERE id = ?";
193

    
194
        return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
195
    }
196

    
197
    public static QueryDelegate generateQueryDelegate(String SELECT_QUERY, String COUNT_QUERY, String CONTAINS_QUERY) throws SQLException {
198
        FreeformQuery query = new FreeformQuery("This query is not used", CdmSpringContextHelper.getCurrent().getConnectionPool(), ID);
199
        CdmStatementDelegate delegate = new CdmStatementDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
200
        query.setDelegate(delegate);
201
        return query;
202
    }
203

    
204
}
(1-1/10)