9ac5e37413f7c2ab6582f95645128b7d62f4cfc9
[mir.git] / source / mir / entity / adapter / EntityAdapterDefinition.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30 package mir.entity.adapter;
31
32 import java.util.Calendar;
33 import java.util.Date;
34 import java.util.GregorianCalendar;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.TimeZone;
38
39 import mir.entity.Entity;
40 import mir.misc.StringUtil;
41 import mir.util.GeneratorFormatAdapters;
42
43 /**
44  * <code>EntityAdapterDefinition</code> defines the fields for an entity adapter of
45  *  a particular "type". Custom calculated fields can be defined.
46  *
47  */
48 public class EntityAdapterDefinition {
49   private Map calculatedFields;
50
51   public EntityAdapterDefinition() {
52     calculatedFields = new HashMap();
53   }
54
55   /**
56    *
57    *
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
61    */
62   public EntityAdapter makeEntityAdapter(Entity anEntity, EntityAdapterModel aModel) {
63     return new EntityAdapter(anEntity, this, aModel);
64   }
65
66   /**
67    *
68    * @param aFieldName
69    * @return the <code>CalculatedField</code> if it exists. <code>null</code> otherwise
70    */
71   public CalculatedField getCalculatedField(String aFieldName) {
72     return (CalculatedField) calculatedFields.get(aFieldName);
73   }
74
75   /**
76    *
77    * @param aFieldName
78    * @return boolean true if aFieldName is a calculated Field
79    */
80   public boolean hasCalculatedField(String aFieldName) {
81     return calculatedFields.containsKey(aFieldName);
82   }
83
84   /**
85    *
86    * @param aFieldName
87    * @param aField
88    */
89   public void addCalculatedField(String aFieldName, CalculatedField aField) {
90     calculatedFields.put(aFieldName, aField);
91   }
92
93   /**
94    * To add a calculated field based on a "mir" date field: a string field of the form yyyyMMdd[hhmm]
95    *
96    * @param aDestinationFieldName
97    * @param aSourceFieldName
98    * @param aDefaultTimezone
99    */
100   public void addMirDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) {
101     addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName, aDefaultTimezone));
102   }
103
104   /**
105    * Adds a calculated field based on a db date field
106    *
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
110    */
111   public void addDBDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) {
112     addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName, aDefaultTimezone));
113   }
114
115   /**
116    * a calculated field: field gets its value from other fields.
117    */
118   public interface CalculatedField {
119     /**
120      * Method to retrieve the calculated value of the field.
121      *
122      * @param anEntityAdapter the entity
123      * @return the value of the field.
124      */
125     public Object getValue(EntityAdapter anEntityAdapter);
126   }
127
128   private class MirDateField implements CalculatedField {
129     private String fieldName;
130     private String defaultTimezone;
131
132     public MirDateField(String aFieldName, String aDefaultTimezone) {
133       fieldName = aFieldName;
134       defaultTimezone = aDefaultTimezone;
135     }
136
137     public Object getValue(EntityAdapter anEntityAdapter) {
138          Object result = null;
139          String textValue = anEntityAdapter.getEntity().getFieldValue(fieldName);
140          Calendar calendar = GregorianCalendar.getInstance();
141          int year;
142          int month;
143          int day;
144          Date date;
145
146          if (textValue!=null) {
147            try {
148              year = Integer.parseInt(textValue.substring(0,4));
149              month = Integer.parseInt(textValue.substring(4,6));
150              day = Integer.parseInt(textValue.substring(6,8));
151
152              calendar.setTimeZone(TimeZone.getTimeZone(defaultTimezone));
153              calendar.set(year, month-1, day);
154
155              date = calendar.getTime();
156
157              result = new GeneratorFormatAdapters.DateFormatAdapter(date, defaultTimezone);
158            }
159            catch (Throwable t) {
160              result=null;
161            }
162          }
163          return result;
164        }
165   }
166
167   private class DBDateField implements CalculatedField {
168     private String fieldName;
169     private String defaultTimezone;
170
171     public DBDateField(String aFieldName, String aDefaultTimezone) {
172       fieldName = aFieldName;
173       defaultTimezone = aDefaultTimezone;
174     }
175
176     public Object getValue(EntityAdapter anEntityAdapter) {
177       Object result = null;
178       String text = anEntityAdapter.getEntity().getFieldValue(fieldName);
179
180       if (text!=null) {
181         try {
182           result = new GeneratorFormatAdapters.DateFormatAdapter(StringUtil.convertMirInternalDateToDate(text), defaultTimezone);
183         }
184         catch (Throwable t) {
185         }
186       }
187
188       return result;
189     }
190   }
191 }