2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mir.entity.adapter;
32 import java.util.Calendar;
33 import java.util.Date;
34 import java.util.GregorianCalendar;
35 import java.util.HashMap;
37 import java.util.TimeZone;
39 import mir.entity.Entity;
40 import mir.misc.StringUtil;
41 import mir.util.GeneratorFormatAdapters;
44 * <code>EntityAdapterDefinition</code> defines the fields for an entity adapter of
45 * a particular "type". Custom calculated fields can be defined.
48 public class EntityAdapterDefinition {
49 private Map calculatedFields;
51 public EntityAdapterDefinition() {
52 calculatedFields = new HashMap();
58 * @param anEntity the entity that will be wrapped by the entity adapter
59 * @param aModel the base model
60 * @return the resulting entity adapter
62 public EntityAdapter makeEntityAdapter(Entity anEntity, EntityAdapterModel aModel) {
63 return new EntityAdapter(anEntity, this, aModel);
69 * @return the <code>CalculatedField</code> if it exists. <code>null</code> otherwise
71 public CalculatedField getCalculatedField(String aFieldName) {
72 return (CalculatedField) calculatedFields.get(aFieldName);
78 * @return boolean true if aFieldName is a calculated Field
80 public boolean hasCalculatedField(String aFieldName) {
81 return calculatedFields.containsKey(aFieldName);
89 public void addCalculatedField(String aFieldName, CalculatedField aField) {
90 calculatedFields.put(aFieldName, aField);
94 * To add a calculated field based on a "mir" date field: a string field of the form yyyyMMdd[hhmm]
96 * @param aDestinationFieldName
97 * @param aSourceFieldName
98 * @param aDefaultTimezone
100 public void addMirDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) {
101 addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName, aDefaultTimezone));
105 * Adds a calculated field based on a db date field
107 * @param aDestinationFieldName the field name of the calculated field
108 * @param aSourceFieldName the field name of the entity (must be a date field)
109 * @param aDefaultTimezone the default timezone to use
111 public void addDBDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) {
112 addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName, aDefaultTimezone));
116 * a calculated field: field gets its value from other fields.
118 public interface CalculatedField {
120 * Method to retrieve the calculated value of the field.
122 * @param anEntityAdapter the entity
123 * @return the value of the field.
125 public Object getValue(EntityAdapter anEntityAdapter);
128 private class MirDateField implements CalculatedField {
129 private String fieldName;
130 private String defaultTimezone;
132 public MirDateField(String aFieldName, String aDefaultTimezone) {
133 fieldName = aFieldName;
134 defaultTimezone = aDefaultTimezone;
137 public Object getValue(EntityAdapter anEntityAdapter) {
138 Object result = null;
139 String textValue = anEntityAdapter.getEntity().getFieldValue(fieldName);
140 Calendar calendar = GregorianCalendar.getInstance();
146 if (textValue!=null) {
148 year = Integer.parseInt(textValue.substring(0,4));
149 month = Integer.parseInt(textValue.substring(4,6));
150 day = Integer.parseInt(textValue.substring(6,8));
152 calendar.setTimeZone(TimeZone.getTimeZone(defaultTimezone));
153 calendar.set(year, month-1, day);
155 date = calendar.getTime();
157 result = new GeneratorFormatAdapters.DateFormatAdapter(date, defaultTimezone);
159 catch (Throwable t) {
167 private class DBDateField implements CalculatedField {
168 private String fieldName;
169 private String defaultTimezone;
171 public DBDateField(String aFieldName, String aDefaultTimezone) {
172 fieldName = aFieldName;
173 defaultTimezone = aDefaultTimezone;
176 public Object getValue(EntityAdapter anEntityAdapter) {
177 Object result = null;
178 String text = anEntityAdapter.getEntity().getFieldValue(fieldName);
182 result = new GeneratorFormatAdapters.DateFormatAdapter(StringUtil.convertMirInternalDateToDate(text), defaultTimezone);
184 catch (Throwable t) {