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.value;
034:
035: import com.flexive.shared.FxSharedUtils;
036: import com.flexive.shared.FxFormatUtils;
037: import com.flexive.shared.exceptions.FxInvalidStateException;
038:
039: import java.io.Serializable;
040: import java.util.Date;
041: import java.util.Map;
042: import java.text.SimpleDateFormat;
043:
044: /**
045: * A multilingual Date range, internally represented as java.util.Date; EMPTY is a Date with a timestamp of <code>0</code> (usually 01/01/1970)
046: *
047: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: */
049: public class FxDateRange extends FxValue<DateRange, FxDateRange>
050: implements Serializable {
051: private static final long serialVersionUID = 8158482067603102306L;
052:
053: public final static DateRange EMPTY = new DateRange(new Date(0),
054: new Date(0));
055:
056: /**
057: * Constructor
058: *
059: * @param multiLanguage multilanguage value?
060: * @param defaultLanguage the default language
061: * @param translations HashMap containing language->translation mapping
062: */
063: public FxDateRange(boolean multiLanguage, long defaultLanguage,
064: Map<Long, DateRange> translations) {
065: super (multiLanguage, defaultLanguage, translations);
066: }
067:
068: /**
069: * Constructor
070: *
071: * @param defaultLanguage the default language
072: * @param translations HashMap containing language->translation mapping
073: */
074: public FxDateRange(long defaultLanguage,
075: Map<Long, DateRange> translations) {
076: super (defaultLanguage, translations);
077: }
078:
079: /**
080: * Constructor
081: *
082: * @param multiLanguage multilanguage value?
083: * @param translations HashMap containing language->translation mapping
084: */
085: public FxDateRange(boolean multiLanguage,
086: Map<Long, DateRange> translations) {
087: super (multiLanguage, translations);
088: }
089:
090: /**
091: * Constructor
092: *
093: * @param translations HashMap containing language->translation mapping
094: */
095: public FxDateRange(Map<Long, DateRange> translations) {
096: super (translations);
097: }
098:
099: /**
100: * Constructor - create value from an array of translations
101: *
102: * @param translations HashMap containing language->translation mapping
103: * @param pos position (index) in the array to use
104: */
105: public FxDateRange(Map<Long, DateRange[]> translations, int pos) {
106: super (translations, pos);
107: }
108:
109: /**
110: * Constructor
111: *
112: * @param multiLanguage multilanguage value?
113: * @param defaultLanguage the default language
114: * @param value single initializing value
115: */
116: public FxDateRange(boolean multiLanguage, long defaultLanguage,
117: DateRange value) {
118: super (multiLanguage, defaultLanguage, value);
119: }
120:
121: /**
122: * Constructor
123: *
124: * @param defaultLanguage the default language
125: * @param value single initializing value
126: */
127: public FxDateRange(long defaultLanguage, DateRange value) {
128: super (defaultLanguage, value);
129: }
130:
131: /**
132: * Constructor
133: *
134: * @param multiLanguage multilanguage value?
135: * @param value single initializing value
136: */
137: public FxDateRange(boolean multiLanguage, DateRange value) {
138: super (multiLanguage, value);
139: }
140:
141: /**
142: * Constructor
143: *
144: * @param value single initializing value
145: */
146: public FxDateRange(DateRange value) {
147: super (value);
148: }
149:
150: /**
151: * Constructor
152: *
153: * @param clone original FxValue to be cloned
154: */
155: public FxDateRange(FxValue<DateRange, FxDateRange> clone) {
156: super (clone);
157: }
158:
159: /**
160: * Return true if T is immutable (e.g. java.lang.String). This prevents cloning
161: * of the translations in copy constructors.
162: *
163: * @return true if T is immutable (e.g. java.lang.String)
164: */
165: @Override
166: public boolean isImmutableValueType() {
167: return true;
168: }
169:
170: /**
171: * Evaluates the given string value to an object of type T.
172: *
173: * @param value string value to be evaluated
174: * @return the value interpreted as T
175: */
176: @Override
177: public DateRange fromString(String value) {
178: return new DateRange(value);
179: }
180:
181: /**
182: * {@inheritDoc}
183: */
184: @Override
185: public String getStringValue(DateRange value) {
186: return value.toString();
187: }
188:
189: /**
190: * {@inheritDoc}
191: */
192: @Override
193: public String getSqlValue() {
194: throw new FxInvalidStateException(
195: "ex.content.value.sql.notSupported", getClass()
196: .getSimpleName()).asRuntimeException();
197: }
198:
199: /**
200: * Creates a copy of the given object (useful if the actual type is unknown).
201: *
202: * @return a copy of the given object (useful if the actual type is unknown).
203: */
204: @Override
205: public FxDateRange copy() {
206: return new FxDateRange(this );
207: }
208:
209: /**
210: * {@inheritDoc}
211: */
212: @Override
213: public Class<DateRange> getValueClass() {
214: return DateRange.class;
215: }
216:
217: @Override
218: public int compareTo(FxValue o) {
219: if (o instanceof FxDateRange && !isEmpty() && !o.isEmpty()) {
220: return getBestTranslation().getLower().compareTo(
221: ((FxDateRange) o).getBestTranslation().getLower());
222: } else if (o instanceof FxDateTimeRange && !isEmpty()
223: && !o.isEmpty()) {
224: return getBestTranslation().getLower().compareTo(
225: ((FxDateTimeRange) o).getBestTranslation()
226: .getLower());
227: } else if (o instanceof FxDate && !isEmpty() && !o.isEmpty()) {
228: return getBestTranslation().getLower().compareTo(
229: ((FxDate) o).getBestTranslation());
230: } else if (o instanceof FxDateTime && !isEmpty()
231: && !o.isEmpty()) {
232: return getBestTranslation().getLower().compareTo(
233: ((FxDateTime) o).getBestTranslation());
234: }
235: return super.compareTo(o);
236: }
237: }
|