Project

General

Profile

Download (4.8 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.statement;
11

    
12
import java.sql.Connection;
13
import java.sql.SQLException;
14
import java.util.List;
15

    
16
import com.vaadin.data.Container.Filter;
17
import com.vaadin.data.util.sqlcontainer.RowItem;
18
import com.vaadin.data.util.sqlcontainer.SQLUtil;
19
import com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate;
20
import com.vaadin.data.util.sqlcontainer.query.OrderBy;
21
import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper;
22
import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder;
23

    
24

    
25
/**
26
 * @author cmathew
27
 * @date 10 Mar 2015
28
 *
29
 */
30
public class CdmStatementDelegate implements FreeformStatementDelegate {
31

    
32
	private static final long serialVersionUID = 8417860805854924886L;
33
	private List<Filter> filters;
34
    private List<OrderBy> orderBys;
35

    
36
    private final String select_query;
37
    private final String count_query;
38
    private final String contains_query;
39

    
40
    public CdmStatementDelegate(String select_query, String count_query, String contains_query) {
41
        this.select_query = select_query;
42
        this.count_query = count_query;
43
        this.contains_query = contains_query;
44
    }
45

    
46
    @Override
47
    public String getQueryString(int offset, int limit) throws UnsupportedOperationException {
48
        throw new UnsupportedOperationException("Use getQueryStatement method.");
49
    }
50

    
51
    @Override
52
    public String getCountQuery() throws UnsupportedOperationException {
53
        throw new UnsupportedOperationException("Use getCountStatement method.");
54
    }
55

    
56
    @Override
57
    public void setFilters(List<Filter> filters) throws UnsupportedOperationException {
58
        this.filters = filters;
59
    }
60

    
61
    @Override
62
    public void setOrderBy(List<OrderBy> orderBys) throws UnsupportedOperationException {
63
        this.orderBys = orderBys;
64
    }
65

    
66
    @Override
67
    public int storeRow(Connection conn, RowItem row) throws UnsupportedOperationException, SQLException {
68
        // TODO Auto-generated method stub
69
        return 0;
70
    }
71

    
72
    @Override
73
    public boolean removeRow(Connection conn, RowItem row) throws UnsupportedOperationException, SQLException {
74
        // TODO Auto-generated method stub
75
        return false;
76
    }
77

    
78
    @Override
79
    public String getContainsRowQueryString(Object... keys) throws UnsupportedOperationException {
80
        throw new UnsupportedOperationException("Use getContainsRowQueryStatement method.");
81
    }
82

    
83
    @Override
84
    public StatementHelper getQueryStatement(int offset, int limit) throws UnsupportedOperationException {
85
        StatementHelper sh = new StatementHelper();
86
        StringBuffer query = new StringBuffer(select_query);
87
        if (filters != null) {
88
            String filterString = QueryBuilder.getWhereStringForFilters(filters, sh);
89
            query.append(filterString);
90
        }
91
        query.append(getOrderByString());
92
        if (offset != 0 || limit != 0) {
93
            query.append(" LIMIT ").append(limit);
94
            query.append(" OFFSET ").append(offset);
95
        }
96
        sh.setQueryString(query.toString());
97
        return sh;
98
    }
99

    
100
    private String getOrderByString() {
101
        StringBuffer orderBuffer = new StringBuffer("");
102
        if (orderBys != null && !orderBys.isEmpty()) {
103
            orderBuffer.append(" ORDER BY ");
104
            OrderBy lastOrderBy = orderBys.get(orderBys.size() - 1);
105
            for (OrderBy orderBy : orderBys) {
106
                orderBuffer.append(SQLUtil.escapeSQL(orderBy.getColumn()));
107
                if (orderBy.isAscending()) {
108
                    orderBuffer.append(" ASC");
109
                } else {
110
                    orderBuffer.append(" DESC");
111
                }
112
                if (orderBy != lastOrderBy) {
113
                    orderBuffer.append(", ");
114
                }
115
            }
116
        }
117
        return orderBuffer.toString();
118
    }
119

    
120
    @Override
121
    public StatementHelper getCountStatement() throws UnsupportedOperationException {
122
        StatementHelper sh = new StatementHelper();
123
        StringBuffer query = new StringBuffer(count_query);
124
        if (filters != null) {
125
            String filterString = QueryBuilder.getWhereStringForFilters(filters, sh);
126
            query.append(filterString);
127
        }
128
        sh.setQueryString(query.toString());
129
        return sh;
130
    }
131

    
132
    @Override
133
    public StatementHelper getContainsRowQueryStatement(Object... keys) throws UnsupportedOperationException {
134
        StatementHelper sh = new StatementHelper();
135
        StringBuffer query = new StringBuffer(contains_query);
136
        sh.addParameterValue(keys[0]);
137
        sh.setQueryString(query.toString());
138
        return sh;
139
    }
140

    
141
}
    (1-1/1)