X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=source%2Fmir%2Fentity%2Fadapter%2FEntityAdapterDefinition.java;h=571df52ec2dbe1646ffc31805bdc3da663e190a5;hb=8b91e8d8bf4a31a88440a404e83238dcf32f8f4a;hp=c86fc7b2cb9fa095b2022af38dd069a0ecf7db01;hpb=e368bbb50c99177c97d0f3c7de877accc014a4b3;p=mir.git diff --git a/source/mir/entity/adapter/EntityAdapterDefinition.java b/source/mir/entity/adapter/EntityAdapterDefinition.java index c86fc7b2..571df52e 100755 --- a/source/mir/entity/adapter/EntityAdapterDefinition.java +++ b/source/mir/entity/adapter/EntityAdapterDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,17 +18,15 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - package mir.entity.adapter; import java.util.Calendar; @@ -36,121 +34,155 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; +import java.util.TimeZone; import mir.entity.Entity; -import mir.util.DateToMapAdapter; +import mir.misc.StringUtil; +import mir.util.GeneratorFormatAdapters; +/** + * EntityAdapterDefinition defines the fields for an entity adapter of + * a particular "type". Custom calculated fields can be defined. + * + */ public class EntityAdapterDefinition { - Map calculatedFields; + private Map calculatedFields; public EntityAdapterDefinition() { calculatedFields = new HashMap(); } + /** + * + * + * @param anEntity the entity that will be wrapped by the entity adapter + * @param aModel the base model + * @return the resulting entity adapter + */ public EntityAdapter makeEntityAdapter(Entity anEntity, EntityAdapterModel aModel) { return new EntityAdapter(anEntity, this, aModel); } + /** + * + * @param aFieldName + * @return the CalculatedField if it exists. null otherwise + */ public CalculatedField getCalculatedField(String aFieldName) { return (CalculatedField) calculatedFields.get(aFieldName); } + /** + * + * @param aFieldName + * @return + */ public boolean hasCalculatedField(String aFieldName) { return calculatedFields.containsKey(aFieldName); } + /** + * + * @param aFieldName + * @param aField + */ public void addCalculatedField(String aFieldName, CalculatedField aField) { calculatedFields.put(aFieldName, aField); } - public void addMirDateField(String aDestinationFieldName, String aSourceFieldName) { - addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName)); + /** + * To add a calculated field based on a "mir" date field: a string field of the form yyyyMMdd[hhmm] + * + * @param aDestinationFieldName + * @param aSourceFieldName + * @param aDefaultTimezone + */ + public void addMirDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) { + addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName, aDefaultTimezone)); } - public void addDBDateField(String aDestinationFieldName, String aSourceFieldName) { - addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName)); + /** + * Adds a calculated field based on a db date field + * + * @param aDestinationFieldName the field name of the calculated field + * @param aSourceFieldName the field name of the entity (must be a date field) + * @param aDefaultTimezone the default timezone to use + */ + public void addDBDateField(String aDestinationFieldName, String aSourceFieldName, String aDefaultTimezone) { + addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName, aDefaultTimezone)); } + /** + * a calculated field: field gets its value from other fields. + */ public interface CalculatedField { + /** + * Method to retrieve the calculated value of the field. + * + * @param anEntityAdapter the entity + * @return the value of the field. + */ public Object getValue(EntityAdapter anEntityAdapter); } private class MirDateField implements CalculatedField { private String fieldName; + private String defaultTimezone; - public MirDateField(String aFieldName) { + public MirDateField(String aFieldName, String aDefaultTimezone) { fieldName = aFieldName; + defaultTimezone = aDefaultTimezone; } public Object getValue(EntityAdapter anEntityAdapter) { - Map result = new HashMap(); - String textValue = anEntityAdapter.getEntity().getValue(fieldName); - Calendar calendar = GregorianCalendar.getInstance(); - int year; - int month; - int day; - Date date; - - if (textValue!=null) { - try { - year = Integer.parseInt(textValue.substring(0,4)); - month = Integer.parseInt(textValue.substring(4,6)); - day = Integer.parseInt(textValue.substring(6,8)); - - calendar.set(year, month-1, day); - date = calendar.getTime(); - ; - - result.put("date", date); - result.put("formatted", new DateToMapAdapter(date)); - - } - catch (Throwable t) { - result=null; - } - } - return result; - } + Object result = null; + String textValue = anEntityAdapter.getEntity().getFieldValue(fieldName); + Calendar calendar = GregorianCalendar.getInstance(); + int year; + int month; + int day; + Date date; + + if (textValue!=null) { + try { + year = Integer.parseInt(textValue.substring(0,4)); + month = Integer.parseInt(textValue.substring(4,6)); + day = Integer.parseInt(textValue.substring(6,8)); + + calendar.setTimeZone(TimeZone.getTimeZone(defaultTimezone)); + calendar.set(year, month-1, day); + + date = calendar.getTime(); + + result = new GeneratorFormatAdapters.DateFormatAdapter(date, defaultTimezone); + } + catch (Throwable t) { + result=null; + } + } + return result; + } } private class DBDateField implements CalculatedField { private String fieldName; + private String defaultTimezone; - public DBDateField(String aFieldName) { + public DBDateField(String aFieldName, String aDefaultTimezone) { fieldName = aFieldName; + defaultTimezone = aDefaultTimezone; } public Object getValue(EntityAdapter anEntityAdapter) { + Object result = null; + String text = anEntityAdapter.getEntity().getFieldValue(fieldName); - Map result = new HashMap(); - String textValue = anEntityAdapter.getEntity().getValue(fieldName); - Calendar calendar = GregorianCalendar.getInstance(); - int year; - int month; - int day; - int hours; - int minutes; - - Date date; - - if (textValue!=null) { + if (text!=null) { try { - year = Integer.parseInt(textValue.substring(0,4)); - month = Integer.parseInt(textValue.substring(5,7)); - day = Integer.parseInt(textValue.substring(8,10)); - hours = Integer.parseInt(textValue.substring(11,13)); - minutes = Integer.parseInt(textValue.substring(14,16)); - - calendar.set(year, month-1, day, hours, minutes); - date = calendar.getTime(); - - result.put("date", date); - result.put("formatted", new DateToMapAdapter(date)); - result.put("raw", textValue); + result = new GeneratorFormatAdapters.DateFormatAdapter(StringUtil.convertMirInternalDateToDate(text), defaultTimezone); } catch (Throwable t) { - result=null; } }