KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > BasicQueryBuilder


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.cayenne.exp.Expression;
26 import org.apache.cayenne.exp.ExpressionFactory;
27 import org.apache.cayenne.query.SelectQuery;
28
29 public class BasicQueryBuilder {
30
31     private ObjEntityView queryTarget;
32     private List JavaDoc conditions = new ArrayList JavaDoc();
33
34     public BasicQueryBuilder(ObjEntityView queryTarget) {
35         this.queryTarget = queryTarget;
36     }
37
38     public void addEqual(String JavaDoc fieldName, Object JavaDoc value) {
39         ObjEntityViewField field = queryTarget.getField(fieldName);
40         String JavaDoc path = null;
41         if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) {
42             path = field.getObjAttribute().getName();
43         }
44         else if (field.isLookup()) {
45             path = field.getObjRelationship().getName();
46         }
47         Object JavaDoc rawValue = field.toRawValue(value);
48         conditions.add(ExpressionFactory.matchExp(path, rawValue));
49     }
50
51     public void addRange(String JavaDoc fieldName, Object JavaDoc start, Object JavaDoc end) {
52         ObjEntityViewField field = queryTarget.getField(fieldName);
53         String JavaDoc path = null;
54         if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) {
55             path = field.getObjAttribute().getName();
56         }
57         else if (field.isLookup()) {
58             path = field.getObjRelationship().getName();
59         }
60         Object JavaDoc rawStart = field.toRawValue(start);
61         Object JavaDoc rawEnd = field.toRawValue(end);
62         Expression expr = null;
63         if (rawStart != null && rawEnd != null)
64             expr = ExpressionFactory.betweenExp(path, rawStart, rawEnd);
65         else if (rawStart != null)
66             expr = ExpressionFactory.greaterOrEqualExp(path, rawStart);
67         else if (rawEnd != null)
68             expr = ExpressionFactory.lessOrEqualExp(path, rawEnd);
69
70         if (expr != null)
71             conditions.add(expr);
72     }
73
74     public void addLike(String JavaDoc fieldName, Object JavaDoc value, boolean caseSensetive) {
75         ObjEntityViewField field = queryTarget.getField(fieldName);
76         String JavaDoc path = null;
77         if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) {
78             path = field.getObjAttribute().getName();
79         }
80         else if (field.isLookup()) {
81             path = field.getObjRelationship().getName();
82         }
83         Object JavaDoc rawValue = field.toRawValue(value);
84         String JavaDoc pattern = (rawValue != null ? rawValue.toString() : "");
85         Expression expr = (caseSensetive
86                 ? ExpressionFactory.likeExp(path, pattern)
87                 : ExpressionFactory.likeIgnoreCaseExp(path, pattern));
88         conditions.add(expr);
89     }
90
91     public SelectQuery getSelectQuery() {
92         SelectQuery query = new SelectQuery(queryTarget.getObjEntity());
93         if (!conditions.isEmpty()) {
94             Expression qualifier = ExpressionFactory.joinExp(Expression.AND, conditions);
95             query.setQualifier(qualifier);
96         }
97         return query;
98     }
99 }
100
Popular Tags