crude producer queue listing added
[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       return result;
79     }
80   }
81
82   private class DBDateField implements CalculatedField {
83     private String fieldName;
84
85     public DBDateField(String aFieldName) {
86       fieldName = aFieldName;
87     }
88
89     public Object getValue(EntityAdapter anEntityAdapter) {
90
91       Map result = new HashMap();
92       String textValue = anEntityAdapter.getEntity().getValue(fieldName);
93       Calendar calendar = GregorianCalendar.getInstance();
94       int year;
95       int month;
96       int day;
97       int hours;
98       int minutes;
99
100       Date date;
101
102       if (textValue!=null) {
103         try {
104           year = Integer.parseInt(textValue.substring(0,4));
105           month = Integer.parseInt(textValue.substring(5,7));
106           day = Integer.parseInt(textValue.substring(8,10));
107           hours = Integer.parseInt(textValue.substring(11,13));
108           minutes = Integer.parseInt(textValue.substring(14,16));
109
110           calendar.set(year, month-1, day, hours, minutes);
111           date = calendar.getTime();
112
113           result.put("date", date);
114           result.put("formatted", new DateToMapAdapter(date));
115           result.put("raw", textValue);
116         }
117         catch (Throwable t) {
118           result=null;
119         }
120       }
121
122       return result;
123     }
124   }
125
126   static protected Object getRelation(StorageObject aStorageObject, String aWhereClause, String anOrderByClause, EntityAdapterDefinition aDefinition) {
127     try {
128       return
129           new CachingRewindableIterator( new EntityIteratorAdapter(
130               new EntityBrowser( aStorageObject, aWhereClause, anOrderByClause, -1),
131               aDefinition));
132     }
133     catch (Throwable t) {
134       throw new RuntimeException(t.getMessage());
135     }
136   }
137 }