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.structure.FxDataType;
036: import com.flexive.shared.structure.FxProperty;
037: import com.flexive.shared.value.FxDate;
038: import com.flexive.shared.value.FxDateTime;
039: import com.flexive.shared.value.FxValue;
040: import com.flexive.shared.value.mapper.IdentityInputMapper;
041: import com.flexive.shared.value.mapper.InputMapper;
042: import com.flexive.shared.value.mapper.VoidInputMapper;
043: import com.flexive.shared.value.renderer.FxValueFormatter;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: /**
050: * Base class for value types.
051: *
052: * @param <T> the value type
053: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
054: */
055: public abstract class QueryValueNode<T extends FxValue, VC extends ValueComparator>
056: extends QueryNode {
057: private static final long serialVersionUID = 914234426030223950L;
058:
059: protected T value;
060: protected VC comparator;
061: protected InputMapper inputMapper;
062:
063: /**
064: * Default constructor
065: */
066: protected QueryValueNode() {
067: }
068:
069: /**
070: * Protected constructor.
071: * @param id the node ID
072: */
073: protected QueryValueNode(int id) {
074: super (id);
075: }
076:
077: public T getValue() {
078: return value;
079: }
080:
081: public void setValue(T value) {
082: this .value = value;
083: }
084:
085: /** {@inheritDoc} */
086: @Override
087: public void visit(QueryNodeVisitor visitor) {
088: visitor.visit(this );
089: }
090:
091: /** {@inheritDoc} */
092: @Override
093: public boolean isValueNode() {
094: return true;
095: }
096:
097: /**
098: * Return true if the input fields should be read only for this node.
099: *
100: * @return true if the input fields should be read only for this node.
101: */
102: public boolean isReadOnly() {
103: return false;
104: }
105:
106: /**
107: * Override this method to provide your own formatter for read-only mode.
108: *
109: * @return the {@link com.flexive.shared.value.renderer.FxValueFormatter} to be used for rendering read-only mode.
110: */
111: public FxValueFormatter getValueFormatter() {
112: return null;
113: }
114:
115: public VC getComparator() {
116: return comparator;
117: }
118:
119: public void setComparator(VC comparator) {
120: this .comparator = comparator;
121: }
122:
123: /**
124: * Returns the input mapper to be used for this query node. Input mappers allow
125: * to use "fancier" inputs for common properties, e.g. a select list for an internal
126: * ordinal value like the ACL.
127: *
128: * @return the input mapper for this node
129: * @see com.flexive.shared.value.mapper.InputMapper
130: * @see com.flexive.shared.value.mapper.InputMapper#getInstance(com.flexive.shared.structure.FxProperty)
131: */
132: public InputMapper getInputMapper() {
133: return inputMapper != null ? inputMapper : IdentityInputMapper
134: .getInstance();
135: }
136:
137: public void setInputMapper(InputMapper inputMapper) {
138: this .inputMapper = inputMapper;
139: }
140:
141: /** {@inheritDoc} */
142: @Override
143: public boolean isWideInput() {
144: return value instanceof FxDate || value instanceof FxDateTime;
145: }
146:
147: /**
148: * Override this method to set the available value comparators of a node instance.
149: *
150: * @return all available enum values for this query node.
151: */
152: protected abstract List<VC> getNodeComparators();
153:
154: /**
155: * Returns all available enum values for this query node.
156: * The returned comparator list is the intersection of the property value comparators
157: * returned by {@link #getNodeComparators()} and those defined by the input mapper, if any.
158: *
159: * @return all available enum values for this query node.
160: */
161: public List<VC> getAvailableComparators() {
162: final List<VC> result = new ArrayList<VC>();
163: result.addAll(getNodeComparators());
164: final List inputMapperResult = getInputMapper()
165: .getAvailableValueComparators();
166: if (!inputMapperResult.isEmpty()) {
167: for (Iterator<VC> iterator = result.iterator(); iterator
168: .hasNext();) {
169: // remove all comparators that are not available both in the input mapper and the query node
170: if (!inputMapperResult.contains(iterator.next())) {
171: iterator.remove();
172: }
173: }
174: }
175: return result;
176: }
177:
178: /**
179: * Return the input mapper for the given property.
180: *
181: * @param property the property to be rendered
182: * @return the input mapper for the given property assignmen.t
183: */
184: protected InputMapper getPropertyInputMapper(FxProperty property) {
185: if (FxDataType.Binary.equals(property.getDataType())) {
186: return VoidInputMapper.getInstance();
187: } else {
188: return InputMapper.getInstance(property);
189: }
190: }
191: }
|