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.exceptions.FxInvalidStateException;
036: import com.flexive.shared.FxFormatUtils;
037:
038: import java.io.Serializable;
039: import java.text.DateFormat;
040: import java.util.Date;
041: import java.util.Map;
042:
043: /**
044: * A multilingual DateTime, internally represented as java.util.Date; EMPTY is a Date with a timestamp of <code>0</code> (usually 01/01/1970)
045: *
046: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: public class FxDateTime extends FxValue<Date, FxDateTime> implements
049: Serializable {
050: private static final long serialVersionUID = -6368191431091556952L;
051: public final static Date EMPTY = new Date(0);
052:
053: /**
054: * Constructor
055: *
056: * @param multiLanguage multilanguage value?
057: * @param defaultLanguage the default language
058: * @param translations HashMap containing language->translation mapping
059: */
060: public FxDateTime(boolean multiLanguage, long defaultLanguage,
061: Map<Long, Date> translations) {
062: super (multiLanguage, defaultLanguage, translations);
063: }
064:
065: /**
066: * Constructor
067: *
068: * @param defaultLanguage the default language
069: * @param translations HashMap containing language->translation mapping
070: */
071: public FxDateTime(long defaultLanguage, Map<Long, Date> translations) {
072: super (defaultLanguage, translations);
073: }
074:
075: /**
076: * Constructor
077: *
078: * @param multiLanguage multilanguage value?
079: * @param translations HashMap containing language->translation mapping
080: */
081: public FxDateTime(boolean multiLanguage,
082: Map<Long, Date> translations) {
083: super (multiLanguage, translations);
084: }
085:
086: /**
087: * Constructor
088: *
089: * @param translations HashMap containing language->translation mapping
090: */
091: public FxDateTime(Map<Long, Date> translations) {
092: super (translations);
093: }
094:
095: /**
096: * Constructor - create value from an array of translations
097: *
098: * @param translations HashMap containing language->translation mapping
099: * @param pos position (index) in the array to use
100: */
101: public FxDateTime(Map<Long, Date[]> translations, int pos) {
102: super (translations, pos);
103: }
104:
105: /**
106: * Constructor
107: *
108: * @param multiLanguage multilanguage value?
109: * @param defaultLanguage the default language
110: * @param value single initializing value
111: */
112: public FxDateTime(boolean multiLanguage, long defaultLanguage,
113: Date value) {
114: super (multiLanguage, defaultLanguage, value);
115: }
116:
117: /**
118: * Constructor
119: *
120: * @param defaultLanguage the default language
121: * @param value single initializing value
122: */
123: public FxDateTime(long defaultLanguage, Date value) {
124: super (defaultLanguage, value);
125: }
126:
127: /**
128: * Constructor
129: *
130: * @param multiLanguage multilanguage value?
131: * @param value single initializing value
132: */
133: public FxDateTime(boolean multiLanguage, Date value) {
134: super (multiLanguage, value);
135: }
136:
137: /**
138: * Constructor
139: *
140: * @param value single initializing value
141: */
142: public FxDateTime(Date value) {
143: super (value);
144: }
145:
146: /**
147: * Constructor
148: *
149: * @param clone original FxValue to be cloned
150: */
151: public FxDateTime(FxValue<Date, FxDateTime> clone) {
152: super (clone);
153: }
154:
155: /** {@inheritDoc} */
156: @Override
157: public Class<Date> getValueClass() {
158: return Date.class;
159: }
160:
161: /** {@inheritDoc} */
162: @Override
163: public Date fromString(String value) {
164: return FxValueConverter.toDateTime(value);
165: }
166:
167: /** {@inheritDoc} */
168: @Override
169: public FxDateTime copy() {
170: return new FxDateTime(this );
171: }
172:
173: /** {@inheritDoc} */
174: @Override
175: public String getStringValue(Date value) {
176: //TODO: use a better date parser
177: return DateFormat.getDateInstance().format(value);
178: }
179:
180: /** {@inheritDoc} */
181: @Override
182: public boolean isImmutableValueType() {
183: return true;
184: }
185:
186: /** {@inheritDoc} */
187: @Override
188: public String getSqlValue() {
189: if (isEmpty()) {
190: throw new FxInvalidStateException(
191: "ex.content.value.sql.empty").asRuntimeException();
192: }
193: return "'"
194: + FxFormatUtils.getDateTimeFormat().format(
195: getDefaultTranslation()) + "'";
196: }
197: }
|