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.FxAssignment;
038: import com.flexive.shared.structure.FxPropertyAssignment;
039: import com.flexive.shared.structure.FxType;
040: import com.flexive.shared.value.FxString;
041: import com.flexive.shared.value.FxValue;
042: import com.flexive.shared.value.mapper.IdentityInputMapper;
043: import com.flexive.shared.value.mapper.InputMapper;
044:
045: import java.util.Arrays;
046: import java.util.List;
047:
048: /**
049: * A query node representing a structure property.
050: *
051: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: */
053: public class AssignmentValueNode extends
054: QueryValueNode<FxValue, PropertyValueComparator> {
055:
056: private static final long serialVersionUID = 4285245221573681218L;
057: private FxAssignment assignment;
058:
059: /**
060: * Create a new property query node.
061: *
062: * @param id Internal and unique ID of the node
063: * @param assignment the property to be set with this node
064: */
065: public AssignmentValueNode(int id, FxAssignment assignment) {
066: super (id);
067: this .assignment = assignment;
068: this .comparator = PropertyValueComparator.EQ;
069: if (assignment instanceof FxPropertyAssignment) {
070: value = ((FxPropertyAssignment) assignment).getEmptyValue();
071: }
072: if (this .value == null) {
073: setValue(new FxString(""));
074: }
075: }
076:
077: public FxAssignment getAssignment() {
078: return assignment;
079: }
080:
081: public void setAssignment(FxAssignment property) {
082: this .assignment = property;
083: }
084:
085: /** {@inheritDoc} */
086: @Override
087: public boolean isValid() {
088: try {
089: this .comparator.getSql(assignment, value);
090: // if we can generate a SQL epxression, check additional assignment constraints
091: return assignment.isValid(value);
092: } catch (RuntimeException e) {
093: // if we can't generate a SQL expression, we aren't valid
094: return false;
095: }
096: }
097:
098: /** {@inheritDoc} */
099: @Override
100: public void buildSqlQuery(SqlQueryBuilder builder) {
101: try {
102: builder.condition(assignment, comparator, value);
103: } catch (FxRuntimeException e) {
104: throw new FxInvalidQueryNodeException(getId(), e
105: .getConverted()).asRuntimeException();
106: }
107: }
108:
109: /** {@inheritDoc} */
110: @Override
111: public FxString getLabel() {
112: final String typeLabel = assignment.getAssignedType().getId() != FxType.ROOT_ID ? assignment
113: .getAssignedType().getDisplayName()
114: + "/"
115: : "";
116: return new FxString(
117: typeLabel
118: + (assignment.getParentGroupAssignment() != null ? assignment
119: .getParentGroupAssignment()
120: .getDisplayName()
121: + "/"
122: : "") + assignment.getDisplayName());
123: }
124:
125: /** {@inheritDoc} */
126: @Override
127: public List<PropertyValueComparator> getNodeComparators() {
128: return assignment instanceof FxPropertyAssignment ? PropertyValueComparator
129: .getAvailable(((FxPropertyAssignment) assignment)
130: .getProperty().getDataType())
131: : Arrays.asList(PropertyValueComparator.values());
132: }
133:
134: /** {@inheritDoc} */
135: @Override
136: public InputMapper getInputMapper() {
137: return inputMapper != null ? inputMapper
138: : assignment instanceof FxPropertyAssignment ? getPropertyInputMapper(((FxPropertyAssignment) assignment)
139: .getProperty())
140: : IdentityInputMapper.getInstance();
141: }
142:
143: }
|