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;
034:
035: import com.flexive.shared.FxSharedUtils;
036: import com.flexive.shared.exceptions.FxRuntimeException;
037: import com.flexive.shared.structure.FxEnvironment;
038: import org.apache.commons.lang.StringUtils;
039:
040: import java.io.Serializable;
041:
042: /**
043: * A displayed column info object for the result preferences.
044: *
045: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
046: * @version $Rev: 181 $
047: */
048: public class ResultColumnInfo implements Serializable {
049: private static final long serialVersionUID = -3605807783224372957L;
050:
051: private final Table table;
052: private final String propertyName;
053: private final String suffix;
054:
055: /**
056: * Create a displayed column info object.
057: *
058: * @param table the table containing the type/assignment
059: * @param propertyName the type name
060: * @param suffix an (optional) suffix
061: */
062: public ResultColumnInfo(Table table, String propertyName,
063: String suffix) {
064: FxSharedUtils.checkParameterEmpty(table, "TABLE");
065: FxSharedUtils.checkParameterEmpty(propertyName, "TYPENAME");
066: this .table = table;
067: this .propertyName = propertyName;
068: this .suffix = suffix;
069: }
070:
071: public Table getTable() {
072: return table;
073: }
074:
075: public String getPropertyName() {
076: return propertyName;
077: }
078:
079: public String getSuffix() {
080: return suffix;
081: }
082:
083: /**
084: * Returns the label to be used e.g. as result column header.
085: *
086: * @param environment the current environment
087: * @return the label to be used e.g. as result column header.
088: */
089: public String getLabel(FxEnvironment environment) {
090: if (isProperty()) {
091: try {
092: return environment.getProperty(propertyName).getLabel()
093: .getBestTranslation();
094: } catch (FxRuntimeException e) {
095: return propertyName;
096: }
097: }
098: return propertyName;
099: }
100:
101: /**
102: * Return the full column name to be used in SQL queries.
103: * For example:<br/>
104: * <code>new ColumnInfo(Table.CONTENT, "mandator", "name").getColumnName() -> co.mandator.name</code>
105: * @return the full column name to be used in SQL queries.
106: */
107: public String getColumnName() {
108: return table.getAlias() + "." + propertyName
109: + (StringUtils.isNotBlank(suffix) ? "." + suffix : "");
110: }
111:
112: /**
113: * Return true if the object info's property is a "real" structure
114: * property.
115: * @return true if this info object is for a "real" structure property
116: */
117: public boolean isProperty() {
118: return !propertyName.startsWith("@");
119: }
120:
121: /** {@inheritDoc} */
122: @Override
123: public boolean equals(Object o) {
124: if (this == o)
125: return true;
126: if (o == null || getClass() != o.getClass())
127: return false;
128:
129: ResultColumnInfo that = (ResultColumnInfo) o;
130:
131: return StringUtils.equalsIgnoreCase(propertyName,
132: that.propertyName)
133: && table.equals(that.table)
134: && StringUtils.equalsIgnoreCase(suffix, that.suffix);
135:
136: }
137:
138: /** {@inheritDoc} */
139: @Override
140: public int hashCode() {
141: int result;
142: result = table.hashCode();
143: result = 31 * result + propertyName.hashCode();
144: result = 31 * result + (suffix != null ? suffix.hashCode() : 0);
145: return result;
146: }
147:
148: /** {@inheritDoc} */
149: @Override
150: public String toString() {
151: return "ColumnInfo{" + table + ", " + propertyName
152: + (StringUtils.isNotBlank(suffix) ? ", " + suffix : "")
153: + "}";
154: }
155: }
|