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.exceptions.FxInvalidQueryNodeException;
036: import com.flexive.shared.exceptions.FxRuntimeException;
037: import com.flexive.shared.structure.FxProperty;
038: import com.flexive.shared.value.FxString;
039: import com.flexive.shared.value.FxValue;
040: import com.flexive.shared.value.mapper.InputMapper;
041:
042: import java.util.List;
043:
044: /**
045: * A property query condition
046: *
047: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: * @version $Rev: 181 $
049: */
050: public class PropertyValueNode extends
051: QueryValueNode<FxValue, PropertyValueComparator> {
052: private static final long serialVersionUID = -8094968229492570983L;
053: private FxProperty property;
054:
055: /**
056: * Create a new property query node.
057: *
058: * @param id Internal and unique ID of the node
059: * @param property the property to be set with this node
060: */
061: public PropertyValueNode(int id, FxProperty property) {
062: super (id);
063: this .property = property;
064: this .comparator = PropertyValueComparator.EQ;
065: if (property != null) {
066: this .value = property.getEmptyValue();
067: } else {
068: setValue(new FxString(""));
069: }
070: }
071:
072: public FxProperty getProperty() {
073: return property;
074: }
075:
076: public void setProperty(FxProperty property) {
077: this .property = property;
078: }
079:
080: /** {@inheritDoc} */
081: @Override
082: public boolean isValid() {
083: try {
084: this .comparator.getSql(property, value);
085: // if we can generate a SQL epxression, check additional property constraints
086: return true; // TODO property check //return property.isValid(value);
087: } catch (RuntimeException e) {
088: // if we can't generate a SQL expression, we aren't valid
089: return false;
090: }
091: }
092:
093: /** {@inheritDoc} */
094: @Override
095: public void buildSqlQuery(SqlQueryBuilder builder) {
096: try {
097: builder.condition(property, comparator, value);
098: } catch (FxRuntimeException e) {
099: throw new FxInvalidQueryNodeException(getId(), e
100: .getConverted()).asRuntimeException();
101: }
102: }
103:
104: /** {@inheritDoc} */
105: @Override
106: public FxString getLabel() {
107: return property.getLabel();
108: }
109:
110: /** {@inheritDoc} */
111: @Override
112: public List<PropertyValueComparator> getNodeComparators() {
113: return PropertyValueComparator.getAvailable(property
114: .getDataType());
115: }
116:
117: /** {@inheritDoc} */
118: @Override
119: public InputMapper getInputMapper() {
120: return inputMapper != null ? inputMapper
121: : getPropertyInputMapper(property);
122: }
123: }
|