At last the EntityBatchingProducerNode is working. This will replace the old
[mir.git] / source / mir / entity / adapter / EntityAdapterDefinition.java
1 package mir.entity.adapter;
2
3 import java.util.*;
4 import mir.entity.*;
5 import mir.storage.*;
6 import mir.util.*;
7
8 public class EntityAdapterDefinition {
9   Map calculatedFields;
10
11   public EntityAdapterDefinition() {
12     calculatedFields = new HashMap();
13   }
14
15   public EntityAdapter makeEntityAdapter(Entity anEntity) {
16     return new EntityAdapter(anEntity, this);
17   }
18
19   public CalculatedField getCalculatedField(String aFieldName) {
20     return (CalculatedField) calculatedFields.get(aFieldName);
21   }
22
23   public boolean hasCalculatedField(String aFieldName) {
24     return calculatedFields.containsKey(aFieldName);
25   }
26
27   public void addCalculatedField(String aFieldName, CalculatedField aField) {
28     calculatedFields.put(aFieldName, aField);
29   }
30
31   public void addMirDateField(String aDestinationFieldName, String aSourceFieldName) {
32     addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName));
33   }
34
35   public void addDBDateField(String aDestinationFieldName, String aSourceFieldName) {
36     addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName));
37   }
38
39   public interface CalculatedField {
40     public Object getValue(EntityAdapter anEntityAdapter);
41   }
42
43   private class MirDateField implements CalculatedField {
44     private String fieldName;
45
46     public MirDateField(String aFieldName) {
47       fieldName = aFieldName;
48     }
49
50     public Object getValue(EntityAdapter anEntityAdapter) {
51
52       Map result = new HashMap();
53       String textValue = anEntityAdapter.getEntity().getValue(fieldName);
54       Calendar calendar = GregorianCalendar.getInstance();
55       int year;
56       int month;
57       int day;
58       Date date;
59
60       if (textValue!=null) {
61         try {
62           year = Integer.parseInt(textValue.substring(0,4));
63           month = Integer.parseInt(textValue.substring(4,6));
64           day = Integer.parseInt(textValue.substring(6,8));
65
66           calendar.set(year, month-1, day);
67           date = calendar.getTime();
68           ;
69
70           result.put("date", date);
71           result.put("formatted", new DateToMapAdapter(date));
72
73         }
74         catch (Throwable t) {
75           result=null;
76         }
77       }
78
79       return result;
80
81     }
82
83   }
84
85   private class DBDateField implements CalculatedField {
86     private String fieldName;
87
88     public DBDateField(String aFieldName) {
89       fieldName = aFieldName;
90     }
91
92     public Object getValue(EntityAdapter anEntityAdapter) {
93
94       Map result = new HashMap();
95       String textValue = anEntityAdapter.getEntity().getValue(fieldName);
96       Calendar calendar = GregorianCalendar.getInstance();
97       int year;
98       int month;
99       int day;
100       int hours;
101       int minutes;
102
103       Date date;
104
105       if (textValue!=null) {
106         try {
107           year = Integer.parseInt(textValue.substring(0,4));
108           month = Integer.parseInt(textValue.substring(5,7));
109           day = Integer.parseInt(textValue.substring(8,10));
110           hours = Integer.parseInt(textValue.substring(11,13));
111           minutes = Integer.parseInt(textValue.substring(14,16));
112
113           calendar.set(year, month-1, day, hours, minutes);
114           date = calendar.getTime();
115
116           result.put("date", date);
117           result.put("formatted", new DateToMapAdapter(date));
118           result.put("raw", textValue);
119         }
120         catch (Throwable t) {
121           result=null;
122         }
123       }
124
125       return result;
126
127     }
128
129   }
130
131   static protected Object getRelation(StorageObject aStorageObject, String aWhereClause, String anOrderByClause, EntityAdapterDefinition aDefinition) {
132     try {
133       return
134           new EntityIteratorAdapter(
135               new EntityBrowser( aStorageObject, aWhereClause, anOrderByClause, -1),
136               aDefinition);
137     }
138     catch (Throwable t) {
139       throw new RuntimeException(t.getMessage());
140     }
141   }
142 }