3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.vaadin
.statement
;
12 import java
.sql
.Connection
;
13 import java
.sql
.SQLException
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
17 import com
.vaadin
.data
.Container
.Filter
;
18 import com
.vaadin
.data
.util
.filter
.Compare
;
19 import com
.vaadin
.data
.util
.sqlcontainer
.RowItem
;
20 import com
.vaadin
.data
.util
.sqlcontainer
.SQLUtil
;
21 import com
.vaadin
.data
.util
.sqlcontainer
.query
.FreeformStatementDelegate
;
22 import com
.vaadin
.data
.util
.sqlcontainer
.query
.OrderBy
;
23 import com
.vaadin
.data
.util
.sqlcontainer
.query
.generator
.StatementHelper
;
30 public class CdmStatementDelegate
implements FreeformStatementDelegate
{
32 private List
<Filter
> filters
;
33 private List
<OrderBy
> orderBys
;
35 private final String select_query
;
36 private final String count_query
;
37 private final String contains_query
;
39 public CdmStatementDelegate(String select_query
, String count_query
, String contains_query
) {
40 this.select_query
= select_query
;
41 this.count_query
= count_query
;
42 this.contains_query
= contains_query
;
46 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#getQueryString(int, int)
49 public String
getQueryString(int offset
, int limit
) throws UnsupportedOperationException
{
50 throw new UnsupportedOperationException("Use getQueryStatement method.");
54 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#getCountQuery()
57 public String
getCountQuery() throws UnsupportedOperationException
{
58 throw new UnsupportedOperationException("Use getCountStatement method.");
62 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#setFilters(java.util.List)
65 public void setFilters(List
<Filter
> filters
) throws UnsupportedOperationException
{
66 this.filters
= filters
;
71 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#setOrderBy(java.util.List)
74 public void setOrderBy(List
<OrderBy
> orderBys
) throws UnsupportedOperationException
{
75 this.orderBys
= orderBys
;
79 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#storeRow(java.sql.Connection, com.vaadin.data.util.sqlcontainer.RowItem)
82 public int storeRow(Connection conn
, RowItem row
) throws UnsupportedOperationException
, SQLException
{
83 // TODO Auto-generated method stub
88 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#removeRow(java.sql.Connection, com.vaadin.data.util.sqlcontainer.RowItem)
91 public boolean removeRow(Connection conn
, RowItem row
) throws UnsupportedOperationException
, SQLException
{
92 // TODO Auto-generated method stub
97 * @see com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate#getContainsRowQueryString(java.lang.Object[])
100 public String
getContainsRowQueryString(Object
... keys
) throws UnsupportedOperationException
{
101 throw new UnsupportedOperationException("Use getContainsRowQueryStatement method.");
105 * @see com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate#getQueryStatement(int, int)
108 public StatementHelper
getQueryStatement(int offset
, int limit
) throws UnsupportedOperationException
{
109 StatementHelper sh
= new StatementHelper();
110 StringBuffer query
= new StringBuffer(select_query
);
111 if (filters
!= null) {
112 String filterString
= getWhereStringForFilters(filters
, sh
);
113 query
.append(filterString
);
115 query
.append(getOrderByString());
116 if (offset
!= 0 || limit
!= 0) {
117 query
.append(" LIMIT ").append(limit
);
118 query
.append(" OFFSET ").append(offset
);
120 sh
.setQueryString(query
.toString());
124 private String
getOrderByString() {
125 StringBuffer orderBuffer
= new StringBuffer("");
126 if (orderBys
!= null && !orderBys
.isEmpty()) {
127 orderBuffer
.append(" ORDER BY ");
128 OrderBy lastOrderBy
= orderBys
.get(orderBys
.size() - 1);
129 for (OrderBy orderBy
: orderBys
) {
130 orderBuffer
.append(SQLUtil
.escapeSQL(orderBy
.getColumn()));
131 if (orderBy
.isAscending()) {
132 orderBuffer
.append(" ASC");
134 orderBuffer
.append(" DESC");
136 if (orderBy
!= lastOrderBy
) {
137 orderBuffer
.append(", ");
141 return orderBuffer
.toString();
145 * @see com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate#getCountStatement()
148 public StatementHelper
getCountStatement() throws UnsupportedOperationException
{
149 StatementHelper sh
= new StatementHelper();
150 StringBuffer query
= new StringBuffer(count_query
);
151 if (filters
!= null) {
152 String filterString
= getWhereStringForFilters(filters
, sh
);
153 query
.append(filterString
);
155 sh
.setQueryString(query
.toString());
160 * @see com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate#getContainsRowQueryStatement(java.lang.Object[])
163 public StatementHelper
getContainsRowQueryStatement(Object
... keys
) throws UnsupportedOperationException
{
164 StatementHelper sh
= new StatementHelper();
165 StringBuffer query
= new StringBuffer(contains_query
);
166 sh
.addParameterValue(keys
[0]);
167 sh
.setQueryString(query
.toString());
171 public static String
getWhereStringForFilters(List
<Filter
> filters
,
172 StatementHelper sh
) {
173 if (filters
== null || filters
.isEmpty()) {
176 StringBuilder where
= new StringBuilder(" WHERE ");
177 where
.append(getJoinedFilterString(filters
, "AND", sh
));
178 return where
.toString();
181 public static String
getJoinedFilterString(Collection
<Filter
> filters
,
182 String joinString
, StatementHelper sh
) {
183 StringBuilder result
= new StringBuilder();
184 for (Filter f
: filters
) {
185 result
.append(getWhereStringForFilter(f
, sh
));
186 result
.append(" ").append(joinString
).append(" ");
188 // Remove the last instance of joinString
189 result
.delete(result
.length() - joinString
.length() - 2,
191 return result
.toString();
194 public static String
getWhereStringForFilter(Filter filter
, StatementHelper sh
) {
195 Compare compare
= (Compare
) filter
;
196 sh
.addParameterValue(compare
.getValue());
197 String prop
= compare
.getPropertyId().toString();
198 switch (compare
.getOperation()) {
200 return prop
+ " = ?";
202 return prop
+ " > ?";
203 case GREATER_OR_EQUAL
:
204 return prop
+ " >= ?";
206 return prop
+ " < ?";
208 return prop
+ " <= ?";