001: package com.teamkonzept.webman.field;
002:
003: import com.teamkonzept.lib.TKHashtable;
004: import com.teamkonzept.lib.TKVector;
005: import com.teamkonzept.lib.TKNull;
006: import com.teamkonzept.lib.TKLib;
007: import com.teamkonzept.lib.TKUnregisteredClassException;
008: import com.teamkonzept.web.TKHTMLTemplate;
009: import com.teamkonzept.field.*;
010: import com.teamkonzept.field.db.*;
011: import java.text.DateFormat;
012: import java.util.Calendar;
013: import java.util.TimeZone;
014: import java.util.Date;
015: import java.util.Locale;
016: import com.teamkonzept.international.LanguageManager;
017: import org.w3c.dom.Element;
018: import org.w3c.dom.DOMException;
019: import org.apache.log4j.Category;
020:
021: /**
022: * The date field control.
023: *
024: * @author $Author: uli $
025: * @version $Revision: 1.17 $
026: */
027: public class TKDateField extends TKAtomField {
028: private static final Category CATEGORY = Category
029: .getInstance(TKDateField.class);
030:
031: final static int THOUSAND = 1000;
032:
033: /**
034: * The class identifier.
035: */
036: public static final String CLASS_ID = "DATE";
037: public static final String SET_ON_CREATE_KEY = "SETCREATE";
038: public static final String SET_ON_SAVE_KEY = "SETSAVE";
039: public static final String CHANGEABLE_KEY = "CHANGE";
040: public static final String WITH_TIME_KEY = "WITHTIME";
041:
042: public static final String DATE_KEY = "DATE";
043: public static final String DAY_KEY = "DAY";
044: public static final String MONTH_KEY = "MONTH";
045: public static final String YEAR_KEY = "YEAR";
046: public static final String HOUR_KEY = "HOUR";
047: public static final String MINUTE_KEY = "MINUTE";
048:
049: public static final String YES_VALUE = "YES";
050: public static final String NO_VALUE = "NO";
051:
052: /** change for localization ! */
053: public static final Locale DEFAULT_LOCALE = Locale.GERMAN;
054:
055: protected boolean fSetOnCreate;
056: protected boolean fSetOnSave;
057: protected boolean fChangeable;
058: protected boolean fWithTime;
059:
060: private DateFormat fDateFormat;
061: private DateFormat shortDateFormat;
062:
063: /**
064: * Creates an empty date field control.
065: */
066: public TKDateField() {
067: super ();
068: }
069:
070: public TKDateField(String name, String showName) {
071: this (name, showName, true, false, true, true);
072: }
073:
074: public TKDateField(String name, String showName,
075: boolean setOnCreation, boolean setOnSave,
076: boolean changeable, boolean withTime) {
077: initDateField(CLASS_ID, name, showName, setOnCreation,
078: setOnSave, changeable, withTime);
079: }
080:
081: public final void initDateField(String type, String name,
082: String showName, boolean setOnCreate, boolean setOnSave,
083: boolean changeable, boolean withTime) {
084: initAtomField(type, name, showName);
085: fSetOnCreate = setOnCreate;
086: fSetOnSave = setOnSave;
087: fChangeable = changeable;
088: fWithTime = withTime;
089: initCalendar();
090: }
091:
092: protected void initCalendar() {
093: fDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
094: DateFormat.SHORT, DEFAULT_LOCALE);
095: shortDateFormat = DateFormat.getDateTimeInstance(
096: DateFormat.SHORT, DateFormat.SHORT, DEFAULT_LOCALE);
097: // fuer Linux, sollte mit Locale auch so funktionieren ...
098: // fDateFormat.setTimeZone( TimeZone.getTimeZone( "ECT" ) );
099: }
100:
101: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
102: TKFieldSwitchList allSwitchList) {
103: TKVector options = new TKVector(2);
104: options.addElement(new TKOptionFieldEntry(LanguageManager
105: .getText(LanguageManager.GENERAL, "YES"), YES_VALUE));
106: options.addElement(new TKOptionFieldEntry(LanguageManager
107: .getText(LanguageManager.GENERAL, "NO"), NO_VALUE));
108:
109: TKBaseField[] inputArray = {
110: new TKInputField(NAME_KEY,
111: TKInputField.SMALL_DEFAULT_SIZE,
112: TKInputField.SMALL_DEFAULT_LENGTH,
113: LanguageManager.getText(LANGUAGE_CONTEXT,
114: "DATE_NAME"), TKInputField.CHECK_STRING),
115: new TKInputField(SHOW_NAME_KEY,
116: TKInputField.LARGE_DEFAULT_SIZE,
117: TKInputField.LARGE_DEFAULT_LENGTH,
118: LanguageManager.getText(LANGUAGE_CONTEXT,
119: "DATE_SHOWNAME"),
120: TKInputField.CHECK_STRING),
121: new TKSelectField(SET_ON_CREATE_KEY, LanguageManager
122: .getText(LANGUAGE_CONTEXT, "DATE_INITIALIZE"),
123: options),
124: new TKSelectField(SET_ON_SAVE_KEY, LanguageManager
125: .getText(LANGUAGE_CONTEXT, "DATE_UPDATE"),
126: options),
127: new TKSelectField(CHANGEABLE_KEY, LanguageManager
128: .getText(LANGUAGE_CONTEXT, "DATE_MANUAL"),
129: options),
130: new TKSelectField(WITH_TIME_KEY, LanguageManager
131: .getText(LANGUAGE_CONTEXT, "DATE_TIME_EDIT"),
132: options) };
133: TKFieldGroup inputGroup = new TKFieldGroup(CLASS_ID,
134: new TKVector(inputArray), LanguageManager.getText(
135: LANGUAGE_CONTEXT, CLASS_ID));
136:
137: return inputGroup;
138: }
139:
140: public void init(String classId, Object initData)
141: throws TKUnregisteredClassException,
142: ClassNotFoundException, InstantiationException,
143: IllegalAccessException {
144: super .init(classId, initData);
145: TKHashtable data = (TKHashtable) initData;
146: fSetOnCreate = ((String) data.get(SET_ON_CREATE_KEY))
147: .equals(YES_VALUE);
148: fSetOnSave = ((String) data.get(SET_ON_SAVE_KEY))
149: .equals(YES_VALUE);
150: fChangeable = ((String) data.get(CHANGEABLE_KEY))
151: .equals(YES_VALUE);
152: fWithTime = ((String) data.get(WITH_TIME_KEY))
153: .equals(YES_VALUE);
154: initCalendar();
155: }
156:
157: /**
158: * Returns the internal representation of this field.
159: *
160: * @return the internal representation of this field.
161: */
162: public Object toData() {
163: TKHashtable result = (TKHashtable) super .toData();
164: result.put(SET_ON_CREATE_KEY, (fSetOnCreate ? YES_VALUE
165: : NO_VALUE));
166: result
167: .put(SET_ON_SAVE_KEY, (fSetOnSave ? YES_VALUE
168: : NO_VALUE));
169: result
170: .put(CHANGEABLE_KEY, (fChangeable ? YES_VALUE
171: : NO_VALUE));
172: result.put(WITH_TIME_KEY, (fWithTime ? YES_VALUE : NO_VALUE));
173: return result;
174: }
175:
176: public Object getDefault() {
177: return fDateFormat.format(fSetOnCreate ? new Date() : new Date(
178: 0));
179: }
180:
181: public Object compileData(String prefix, TKHashtable data,
182: TKHashtable context) {
183: String result = null;
184: if (fChangeable) {
185: prefix += fieldName + ".";
186: try {
187: int day = Integer.parseInt((String) data.get(prefix
188: + DAY_KEY));
189: int month = Integer.parseInt((String) data.get(prefix
190: + MONTH_KEY)) - 1;
191: int year = Integer.parseInt((String) data.get(prefix
192: + YEAR_KEY));
193: int hour = !fWithTime ? 0 : Integer
194: .parseInt((String) data.get(prefix + HOUR_KEY));
195: int minute = !fWithTime ? 0 : Integer
196: .parseInt((String) data
197: .get(prefix + MINUTE_KEY));
198: Calendar c = fDateFormat.getCalendar();
199: c.set(year, month, day, hour, minute);
200: result = fDateFormat.format(c.getTime());
201: } catch (Exception e) {
202: CATEGORY.error("compileData: ", e);
203: }
204: } else {
205: result = (String) data.get(prefix + getName());
206: }
207: if (result == null)
208: return getDefault();
209: return result;
210: }
211:
212: public void fillIntoTemplate(TKHTMLTemplate t, Object value,
213: String prefix) {
214: super .fillIntoTemplate(t, value, prefix);
215:
216: if (fWithTime)
217: t.set(WITH_TIME_KEY, Boolean.TRUE);
218:
219: if (!fChangeable)
220: return;
221:
222: t.set(CHANGEABLE_KEY, Boolean.TRUE);
223:
224: if (value instanceof String) {
225:
226: Date date;
227: try {
228: date = fDateFormat.parse((String) value);
229: } catch (java.text.ParseException e) {
230: return;
231: }
232: Calendar c = fDateFormat.getCalendar();
233: c.setTime(date);
234: t.set(DAY_KEY, new Integer(c.get(Calendar.DAY_OF_MONTH)));
235: t.set(MONTH_KEY, new Integer(c.get(Calendar.MONTH) + 1));
236: int year = c.get(Calendar.YEAR);
237:
238: t.set(YEAR_KEY, new Integer(getCorrectedYear(year,
239: (String) value)));
240: if (fWithTime) {
241: t.set(HOUR_KEY,
242: new Integer(c.get(Calendar.HOUR_OF_DAY)));
243: t.set(MINUTE_KEY, new Integer(c.get(Calendar.MINUTE)));
244: }
245: }
246: }
247:
248: // fuer Legacy konvertiert alte Datumsangaben !
249: private int getCorrectedYear(int year, String data) {
250: if (year < THOUSAND) {
251: Date date;
252: try {
253: date = shortDateFormat.parse((String) data);
254: } catch (java.text.ParseException e) {
255: return 0;
256: }
257: Calendar c = shortDateFormat.getCalendar();
258: c.setTime(date);
259: return c.get(Calendar.YEAR);
260: }
261: return year;
262: }
263:
264: public void fillIntoPresentation(TKHTMLTemplate t, Object data,
265: String scope) {
266: super .fillIntoPresentation(t, data, scope);
267: if (data instanceof String) {
268:
269: Date date;
270: try {
271: date = fDateFormat.parse((String) data);
272: } catch (java.text.ParseException e) {
273: return;
274: }
275: Calendar c = fDateFormat.getCalendar();
276: c.setTime(date);
277: scope += "." + getName() + ".";
278: t.set(scope + DAY_KEY, TKLib.fillInt(String.valueOf(c
279: .get(Calendar.DAY_OF_MONTH)), 2));
280: t.set(scope + MONTH_KEY, TKLib.fillInt(String.valueOf(c
281: .get(Calendar.MONTH) + 1), 2));
282: t.set(scope + YEAR_KEY, new Integer(getCorrectedYear(c
283: .get(Calendar.YEAR), (String) data)));
284: t.set(scope + HOUR_KEY, TKLib.fillInt(String.valueOf(c
285: .get(Calendar.HOUR_OF_DAY)), 2));
286: t.set(scope + MINUTE_KEY, TKLib.fillInt(String.valueOf(c
287: .get(Calendar.MINUTE)), 2));
288: }
289: }
290:
291: public void fillAttributesIntoNode(Element node, Object data)
292: throws DOMException {
293: super .fillAttributesIntoNode(node, data);
294: if (data instanceof String) {
295: Date date;
296: try {
297: date = fDateFormat.parse((String) data);
298: } catch (java.text.ParseException e) {
299: return;
300: }
301: Calendar c = fDateFormat.getCalendar();
302: c.setTime(date);
303: node.setAttribute(DAY_KEY, TKLib.fillInt(String.valueOf(c
304: .get(Calendar.DAY_OF_MONTH)), 2));
305: node.setAttribute(MONTH_KEY, TKLib.fillInt(String.valueOf(c
306: .get(Calendar.MONTH) + 1), 2));
307: node.setAttribute(YEAR_KEY, String
308: .valueOf(getCorrectedYear(c.get(Calendar.YEAR),
309: (String) data)));
310: node.setAttribute(HOUR_KEY, TKLib.fillInt(String.valueOf(c
311: .get(Calendar.HOUR_OF_DAY)), 2));
312: node.setAttribute(MINUTE_KEY, TKLib.fillInt(String
313: .valueOf(c.get(Calendar.MINUTE)), 2));
314: }
315: }
316:
317: public void initFromDB(String classId, TKFormDBData db,
318: TKVector otherFields) throws TKUnregisteredClassException,
319: ClassNotFoundException, InstantiationException,
320: IllegalAccessException {
321: super .initFromDB(classId, db, otherFields);
322: fSetOnCreate = Boolean.valueOf(
323: getFieldAttribute(db, SET_ON_CREATE_KEY, 0))
324: .booleanValue();
325: fSetOnSave = Boolean.valueOf(
326: getFieldAttribute(db, SET_ON_SAVE_KEY, 0))
327: .booleanValue();
328: fChangeable = Boolean.valueOf(
329: getFieldAttribute(db, CHANGEABLE_KEY, 0))
330: .booleanValue();
331: fWithTime = Boolean.valueOf(
332: getFieldAttribute(db, WITH_TIME_KEY, 0)).booleanValue();
333: initCalendar();
334: }
335:
336: public int realInsertIntoDB(TKFormDBData db, int formId) {
337: if (super .realInsertIntoDB(db, formId) == -1)
338: return -1;
339: insertNewFieldAttribute(db, formId, SET_ON_CREATE_KEY, 0,
340: String.valueOf(fSetOnCreate));
341: insertNewFieldAttribute(db, formId, SET_ON_SAVE_KEY, 0, String
342: .valueOf(fSetOnSave));
343: insertNewFieldAttribute(db, formId, CHANGEABLE_KEY, 0, String
344: .valueOf(fChangeable));
345: insertNewFieldAttribute(db, formId, WITH_TIME_KEY, 0, String
346: .valueOf(fWithTime));
347: return fieldId;
348: }
349:
350: public int insertDataIntoDB(TKContentDBData db, Object data,
351: int contentId, int leftNr) {
352: if (fSetOnSave) {
353: data = fDateFormat.format(new Date());
354: }
355: return super .insertDataIntoDB(db, data, contentId, leftNr);
356: }
357:
358: /**
359: * Checks wether this object and the specified object
360: * may be treated as equal.
361: *
362: * @param object the object to checked for equality.
363: * @return <CODE>true</CODE> if this object and the
364: * specified object may be treated as equal, otherwise
365: * <CODE>false</CODE>.
366: */
367: public boolean equals(Object object) {
368: if (!super .equals(object)) {
369: return false;
370: }
371:
372: TKDateField field = (TKDateField) object;
373:
374: return (this .fSetOnCreate == field.fSetOnCreate)
375: && (this .fSetOnSave == field.fSetOnSave)
376: && (this .fChangeable == field.fChangeable)
377: && (this .fWithTime == field.fWithTime);
378: }
379:
380: /**
381: * Returns the hash code for this object.
382: *
383: * @return the hash code for this object.
384: */
385: public int hashCode() {
386: // Implementation for JTest only ;-(
387: return super.hashCode();
388: }
389:
390: }
|