001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * QueryFilterClause.java
019: *
020: * Created on 4 ottobre 2003, 10.37
021: */
022:
023: package it.biobytes.ammentos.query;
024:
025: import java.util.*;
026: import java.util.logging.*;
027: import java.sql.*;
028:
029: /**
030: *
031: * @author davide
032: */
033: public class Query implements Cloneable {
034: private List m_filters;
035: private List m_orderFields;
036: private int m_orderingDir;
037: private int m_maxResults;
038: private int m_offset;
039: private String m_domain; // Query domain
040: private Logger m_logger = Logger.getLogger("ammentos");
041:
042: public static final int ORDERING_ASC = 0;
043: public static final int ORDERING_DESC = 1;
044:
045: /** Creates a new instance of QueryFilterClause */
046: public Query() {
047: m_filters = new ArrayList();
048: m_orderFields = new ArrayList();
049: }
050:
051: public Query(String domain) {
052: this ();
053: m_domain = domain;
054: }
055:
056: public Query(QueryFilter filter) {
057: this ();
058: appendFilter(filter);
059: }
060:
061: public void reverseOrderingDir() {
062: if (m_orderingDir == ORDERING_ASC)
063: m_orderingDir = ORDERING_DESC;
064: else
065: m_orderingDir = ORDERING_ASC;
066: }
067:
068: public void setOrderingDir(int direction) {
069: m_orderingDir = direction;
070: }
071:
072: public int getOrderingDir() {
073: return m_orderingDir;
074: }
075:
076: public String getOrderFilterAt(int pos) {
077: String res = null;
078: try {
079: res = (String) m_orderFields.get(pos);
080: } catch (Exception e) {
081: }
082: return res;
083: }
084:
085: public void appendOrderFilter(String fieldName) {
086: m_orderFields.add(fieldName);
087: }
088:
089: public void clearOrderFilters() {
090: m_orderFields.clear();
091: }
092:
093: public QueryFilter appendFilter(QueryFilter filter) {
094: m_filters.add(filter);
095: return filter;
096: }
097:
098: public QueryFilter appendFilter(String fieldName) {
099: return appendFilter(QueryFilter.APP_AND, fieldName);
100: }
101:
102: public QueryFilter appendFilter(int previousAppender,
103: String fieldName) {
104: return (appendFilter(new SimpleQueryFilter(previousAppender,
105: fieldName)));
106: }
107:
108: public QueryFilter getFilterAt(int index) {
109: return (QueryFilter) m_filters.get(index);
110: }
111:
112: public int getFiltersCount() {
113: return m_filters.size();
114: }
115:
116: public String getWhereClauseSql() {
117: StringBuffer res = new StringBuffer();
118: Iterator it = m_filters.iterator();
119:
120: // building where clause
121: if (!m_filters.isEmpty()) {
122: res.append(" WHERE (");
123:
124: int i = 0;
125: while (it.hasNext()) {
126: QueryFilter filter = (QueryFilter) it.next();
127: if (filter != null) {
128: if (i != 0) {
129: res.append(filter.getPreviousAppender());
130: }
131: res.append(filter.getSql());
132: }
133: i++;
134: }
135: res.append(")");
136: }
137: return res.toString();
138: }
139:
140: public String getSql() {
141: StringBuffer res = new StringBuffer();
142:
143: res.append(getWhereClauseSql());
144:
145: // building order by clause
146: if (!m_orderFields.isEmpty()) {
147: res.append(" ORDER BY ");
148: Iterator fieldsIt = m_orderFields.iterator();
149: while (fieldsIt.hasNext()) {
150: res.append(fieldsIt.next().toString());
151: if (fieldsIt.hasNext())
152: res.append(", ");
153: }
154:
155: if (m_orderingDir == ORDERING_DESC)
156: res.append(" DESC");
157: }
158:
159: m_logger.info("query: " + res.toString());
160: return res.toString();
161: }
162:
163: public int setParamValues(PreparedStatement pstmt, int initialIndex)
164: throws SQLException {
165: Iterator it = m_filters.iterator();
166: while (it.hasNext()) {
167: QueryFilter filter = (QueryFilter) it.next();
168: initialIndex = filter.setParamValues(pstmt, initialIndex);
169: }
170:
171: return initialIndex;
172: }
173:
174: public void removeFilterAt(int index) {
175: m_filters.remove(index);
176: }
177:
178: public void removeAllFilters() {
179: m_filters.clear();
180: }
181:
182: public void setFilterAt(int index, QueryFilter filter) {
183: m_filters.set(index, filter);
184: }
185:
186: public boolean isEmpty() {
187: return m_filters.isEmpty() && m_orderFields.isEmpty();
188: }
189:
190: public void setMaxResults(int number) {
191: m_maxResults = number;
192: }
193:
194: public int getMaxResults() {
195: return m_maxResults;
196: }
197:
198: public void setOffset(int offset) {
199: m_offset = offset;
200: }
201:
202: public int getOffset() {
203: return m_offset;
204: }
205:
206: public String getDomain() {
207: return m_domain;
208: }
209:
210: public void setDomain(String m_domain) {
211: this.m_domain = m_domain;
212: }
213: }
|