001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.search.query;
034:
035: import com.flexive.shared.FxFormatUtils;
036: import com.flexive.shared.FxSharedUtils;
037: import com.flexive.shared.exceptions.FxInvalidStateException;
038: import com.flexive.shared.search.Table;
039: import com.flexive.shared.structure.FxAssignment;
040: import com.flexive.shared.structure.FxDataType;
041: import com.flexive.shared.structure.FxProperty;
042: import com.flexive.shared.value.FxString;
043: import com.flexive.shared.value.FxValue;
044:
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.List;
048:
049: /**
050: * Supported value comparators for FxValues.
051: *
052: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: */
054: public enum PropertyValueComparator implements ValueComparator {
055: EQ("="), NE("!="), LT("<"), LE("<="), GT(">"), GE(">="), EMPTY(
056: "NULL", false) {
057: @Override
058: public String getSql(String leftHandSide, String rightHandSide) {
059: return leftHandSide + " IS NULL";
060: }
061: },
062: NOT_EMPTY("NOTNULL", false) {
063: @Override
064: public String getSql(String leftHandSide, String rightHandSide) {
065: return leftHandSide + " IS NOT NULL";
066: }
067:
068: },
069: LIKE("LIKE");
070:
071: private static final List<PropertyValueComparator> NUMERIC_OPERATORS = Collections
072: .unmodifiableList(Arrays.asList(EQ, NE, LT, LE, GT, GE));
073: private static final List<PropertyValueComparator> STRING_OPERATORS = Collections
074: .unmodifiableList(Arrays.asList(EQ, NE, EMPTY, NOT_EMPTY,
075: LIKE));
076: private static final List<PropertyValueComparator> ORDINAL_OPERATORS = Collections
077: .unmodifiableList(Arrays.asList(EQ, NE));
078: private static final List<PropertyValueComparator> DATE_OPERATORS = Collections
079: .unmodifiableList(Arrays.asList(EQ, NE, LT, LE, GT, GE,
080: EMPTY, NOT_EMPTY));
081: private static final List<PropertyValueComparator> DATERANGE_OPERATORS = Collections
082: .unmodifiableList(Arrays.asList(LT, LE, GT, GE, EMPTY,
083: NOT_EMPTY));
084: private static final List<PropertyValueComparator> EMPTY_OPERATORS = Collections
085: .unmodifiableList(Arrays.asList(EMPTY, NOT_EMPTY));
086:
087: private String id;
088: private boolean needsInput;
089:
090: private PropertyValueComparator(String id) {
091: this (id, true);
092: }
093:
094: private PropertyValueComparator(String id, boolean needsInput) {
095: this .id = id;
096: this .needsInput = needsInput;
097: }
098:
099: public final String getSql(FxAssignment assignment, FxValue value) {
100: final String sqlValue;
101: if (needsInput) {
102: if (value.isEmpty()) {
103: throw new FxInvalidStateException(
104: "ex.sqlQueryBuilder.value.empty", assignment
105: .getLabel(), id).asRuntimeException();
106: }
107: try {
108: sqlValue = value.getSqlValue();
109: } catch (RuntimeException e) {
110: throw new FxInvalidStateException(
111: "ex.sqlQueryBuilder.value.invalid", assignment
112: .getLabel(), id, value)
113: .asRuntimeException();
114: }
115: } else {
116: sqlValue = null;
117: }
118: return "/*" + assignment.getAlias()
119: + "*/ " // include assignment alias in comment for better readability
120: + getSql(Table.CONTENT.getColumnName("#"
121: + assignment.getId()), sqlValue);
122: }
123:
124: public final String getSql(FxProperty property, FxValue value) {
125: final String sqlValue;
126: if (needsInput) {
127: try {
128: sqlValue = value.getSqlValue();
129: } catch (RuntimeException e) {
130: throw new FxInvalidStateException(
131: "ex.sqlQueryBuilder.value.invalid", property
132: .getName(), id, value)
133: .asRuntimeException();
134: }
135: } else {
136: sqlValue = null;
137: }
138: return getSql(Table.CONTENT.getColumnName(property.getName()),
139: sqlValue);
140: }
141:
142: public final String getSql(String columnName, FxValue value) {
143: return getSql(columnName, value != null ? value.getSqlValue()
144: : null);
145: }
146:
147: public final String getSql(String columnName, Object value) {
148: return getSql(columnName, FxFormatUtils.escapeForSql(value));
149: }
150:
151: /**
152: * Returns the SQL expression generated by combining both parameters with
153: * this comparator.
154: *
155: * @param leftHandSide the left-hand-side of the comparison
156: * @param rightHandSide the right-hand-side of the comparison
157: * @return the SQL query
158: */
159: protected String getSql(String leftHandSide, String rightHandSide) {
160: return leftHandSide + " " + id + " " + rightHandSide;
161: }
162:
163: /** {@inheritDoc} */
164: public boolean isNeedsInput() {
165: return needsInput;
166: }
167:
168: /** {@inheritDoc} */
169: public FxString getLabel() {
170: return FxSharedUtils.getEnumLabel(this );
171: }
172:
173: /**
174: * Returns a list of value comparators applicable to the given data type.
175: *
176: * @param dataType the data type
177: * @return a list of value comparators applicable to the given data type.
178: */
179: public static List<PropertyValueComparator> getAvailable(
180: FxDataType dataType) {
181: switch (dataType) {
182: case Double:
183: case Float:
184: case LargeNumber:
185: case Number:
186: return NUMERIC_OPERATORS;
187: case String1024:
188: case Text:
189: case HTML:
190: return STRING_OPERATORS;
191: case Binary:
192: return EMPTY_OPERATORS;
193: case Date:
194: case DateTime:
195: return DATE_OPERATORS;
196: case DateRange:
197: case DateTimeRange:
198: return DATERANGE_OPERATORS;
199: default:
200: return ORDINAL_OPERATORS;
201: }
202: }
203:
204: }
|