1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.util.ArrayList ; 59 import java.util.List ; 60 61 import org.objectstyle.cayenne.exp.Expression; 62 import org.objectstyle.cayenne.exp.ExpressionFactory; 63 import org.objectstyle.cayenne.query.SelectQuery; 64 65 public class BasicQueryBuilder { 66 private ObjEntityView queryTarget; 67 private List conditions = new ArrayList (); 68 69 public BasicQueryBuilder(ObjEntityView queryTarget) { 70 this.queryTarget = queryTarget; 71 } 72 73 public void addEqual(String fieldName, Object value) { 74 ObjEntityViewField field = queryTarget.getField(fieldName); 75 String path = null; 76 if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) { 77 path = field.getObjAttribute().getName(); 78 } else if (field.isLookup()) { 79 path = field.getObjRelationship().getName(); 80 } 81 Object rawValue = field.toRawValue(value); 82 conditions.add(ExpressionFactory.matchExp(path, rawValue)); 83 } 84 85 public void addRange(String fieldName, Object start, Object end) { 86 ObjEntityViewField field = queryTarget.getField(fieldName); 87 String path = null; 88 if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) { 89 path = field.getObjAttribute().getName(); 90 } else if (field.isLookup()) { 91 path = field.getObjRelationship().getName(); 92 } 93 Object rawStart = field.toRawValue(start); 94 Object rawEnd = field.toRawValue(end); 95 Expression expr = null; 96 if (rawStart != null && rawEnd != null) 97 expr = ExpressionFactory.betweenExp(path, rawStart, rawEnd); 98 else if (rawStart != null) 99 expr = ExpressionFactory.greaterOrEqualExp(path, rawStart); 100 else if (rawEnd != null) 101 expr = ExpressionFactory.lessOrEqualExp(path, rawStart); 102 103 if (expr != null) 104 conditions.add(expr); 105 } 106 107 public void addLike(String fieldName, Object value, boolean caseSensetive) { 108 ObjEntityViewField field = queryTarget.getField(fieldName); 109 String path = null; 110 if (field.getCalcType().getValue() == CalcTypeEnum.NO_CALC_TYPE_VALUE) { 111 path = field.getObjAttribute().getName(); 112 } else if (field.isLookup()) { 113 path = field.getObjRelationship().getName(); 114 } 115 Object rawValue = field.toRawValue(value); 116 String pattern = (rawValue != null ? rawValue.toString() : ""); 117 Expression expr = (caseSensetive ? 118 ExpressionFactory.likeExp(path, pattern) : 119 ExpressionFactory.likeIgnoreCaseExp(path, pattern)); 120 conditions.add(expr); 121 } 122 123 public SelectQuery getSelectQuery() { 124 SelectQuery query = new SelectQuery(queryTarget.getObjEntity()); 125 if (!conditions.isEmpty()) { 126 Expression qualifier = ExpressionFactory.joinExp(Expression.AND, conditions); 127 query.setQualifier(qualifier); 128 } 129 return query; 130 } 131 } | Popular Tags |