KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > utilities > hibernate > Buffer


1 /*
2  * Shark Hibernate Buffer - Open Wide
3  */

4
5 package org.enhydra.shark.utilities.hibernate;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Arrays JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.ListIterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import net.sf.hibernate.HibernateException;
16 import net.sf.hibernate.Session;
17
18 import org.enhydra.shark.api.TransactionException;
19 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException;
20
21 /**
22  * Hibernate Buffer class
23  *
24  * @author Vladislav Pernin
25  */

26 public class Buffer {
27
28    private static final List JavaDoc order = Arrays.asList(new String JavaDoc[] {
29             //working objects
30
HibernateUtilities.HibernateActivityStateClassName,
31                HibernateUtilities.HibernateProcessStateClassName,
32                HibernateUtilities.HibernateResourceClassName,
33                HibernateUtilities.HibernateProcessMgrClassName,
34                HibernateUtilities.HibernateProcessClassName,
35                HibernateUtilities.HibernateProcessVariableClassName,
36                HibernateUtilities.HibernateActivityClassName,
37                HibernateUtilities.HibernateActivityVariableClassName,
38                HibernateUtilities.HibernateAndJoinEntryClassName,
39                HibernateUtilities.HibernateDeadlineClassName,
40                HibernateUtilities.HibernateAssignmentClassName,
41                HibernateUtilities.HibernateProcessRequesterClassName,
42                // event audits
43
HibernateUtilities.HibernateEventTypeClassName,
44                HibernateUtilities.HibernateProcessStateEventAuditClassName,
45                HibernateUtilities.HibernateActivityStateEventAuditClassName,
46                HibernateUtilities.HibernateStateEventAuditClassName,
47                HibernateUtilities.HibernateCreateProcessEventAuditClassName,
48                HibernateUtilities.HibernateAssignmentEventAuditClassName,
49                HibernateUtilities.HibernateDataEventAuditClassName });
50
51    private Map JavaDoc storage;
52    private Map JavaDoc erasage;
53    private Map JavaDoc readage;
54
55    protected net.sf.hibernate.Transaction transaction = null;
56    protected Session session = null;
57
58    protected boolean deleteFinishedProcesses = false;
59
60    public Buffer(net.sf.hibernate.Transaction dbt) throws NullPointerException JavaDoc {
61       transaction = dbt;
62       storage = new HashMap JavaDoc();
63       erasage = new HashMap JavaDoc();
64       readage = new HashMap JavaDoc();
65    }
66
67    protected void setSession(Session sessionToSet) {
68       session = sessionToSet;
69    }
70
71    public void clear() {
72       storage.clear();
73       erasage.clear();
74       readage.clear();
75    }
76
77    public void clear(String JavaDoc type) {
78       storage.remove(type);
79       erasage.remove(type);
80       readage.remove(type);
81    }
82
83    public void read(Object JavaDoc o) {
84       if (o != null)
85          read(o, o.getClass().toString());
86    }
87
88    public void read(Object JavaDoc o, String JavaDoc type) {
89       List JavaDoc l = (List JavaDoc) readage.get(type);
90       if (null == l) {
91          l = new ArrayList JavaDoc();
92          readage.put(type, l);
93       }
94       if (!l.contains(o))
95          l.add(o);
96    }
97
98    public void read(List JavaDoc l) {
99       if (l != null && l.size() != 0) {
100          String JavaDoc type = l.get(0).getClass().toString();
101          List JavaDoc r = (List JavaDoc) readage.get(type);
102          if (null != r) {
103             for (Iterator JavaDoc it = l.iterator(); it.hasNext();) {
104                Object JavaDoc o = (Object JavaDoc) it.next();
105                if (!r.contains(o))
106                   r.add(o);
107             }
108          } else {
109             readage.put(type, l);
110          }
111       }
112    }
113
114    public void store(Object JavaDoc o) {
115       store(o, o.getClass().toString());
116    }
117
118    public void erase(Object JavaDoc o) {
119       erase(o, o.getClass().toString());
120    }
121
122    public void store(Object JavaDoc o, String JavaDoc type) {
123       List JavaDoc l = (List JavaDoc) storage.get(type);
124       if (null == l) {
125          l = new ArrayList JavaDoc();
126          storage.put(type, l);
127       }
128       if (!l.contains(o)) {
129          l.add(o);
130       }
131       l = (List JavaDoc) readage.get(type);
132       if (l != null && l.contains(o))
133          l.remove(o);
134    }
135
136    public void erase(Object JavaDoc o, String JavaDoc type) {
137       List JavaDoc l = (List JavaDoc) erasage.get(type);
138       if (null == l) {
139          l = new ArrayList JavaDoc();
140          erasage.put(type, l);
141       }
142       if (!l.contains(o)) {
143          l.add(o);
144       }
145       l = (List JavaDoc) readage.get(type);
146       if (l != null && l.contains(o)) {
147          l.remove(o);
148       }
149    }
150
151    private static ArrayList JavaDoc empty = new ArrayList JavaDoc(1);
152
153    public Iterator JavaDoc iterator4type(String JavaDoc type) {
154       List JavaDoc s = (List JavaDoc) storage.get(type);
155       List JavaDoc r = (List JavaDoc) readage.get(type);
156       List JavaDoc l = new ArrayList JavaDoc();
157       l = (ArrayList JavaDoc) empty.clone();
158       if (null != r) {
159          l.addAll(r);
160       }
161       if (null != s) {
162          l.addAll(s);
163       }
164       return l.iterator();
165    }
166
167    public void iterateType(String JavaDoc type) throws TransactionException {
168       List JavaDoc ret = (List JavaDoc) storage.get(type);
169       if (null != ret) {
170          for (Iterator JavaDoc it = ret.iterator(); it.hasNext();) {
171             HibernateGeneric o = (HibernateGeneric) it.next();
172             try {
173                if (o.getDbId() == -1) {
174                   if (type.indexOf("EventAudit") != -1) {
175                      int i = HibernateObjectUtil.getNextDecId("class org.enhydra.shark.eventaudit.HibernateEventAudit").intValue();
176                      //System.out.println("ID ASSiGNED EQUALS SAVE EV
177
// AUDIT type= " + type + "id =" + i);
178
o.setDbId(i);
179                   } else {
180                      int i = HibernateObjectUtil.getNextDecId(type).intValue();
181                      //System.out.println("ID ASSiGNED EQUALS SAVE
182
// GENERIC type= " + type + "id =" + i);
183
o.setDbId(i);
184                   }
185                   session.save(o);
186                   //System.out.println("Buffer SAVED object o DONE \n" +
187
// o + " -----of type= " + type);
188
} else {
189                   session.update(o);
190                }
191                session.flush();
192                session.clear();
193
194             } catch (HibernateException e) {
195                //e.printStackTrace();
196
} catch (PersistenceException e) {
197                //e.printStackTrace();
198
} catch (Exception JavaDoc e) {
199                //e.printStackTrace();
200
}
201          }
202       }
203    }
204
205    private void deleteType(String JavaDoc type) throws TransactionException {
206       List JavaDoc ret = (List JavaDoc) erasage.get(type);
207       if (null != ret) {
208          for (Iterator JavaDoc it = ret.iterator(); it.hasNext();) {
209             try {
210                HibernateGeneric o = (HibernateGeneric) it.next();
211                session.delete(o);
212             } catch (Exception JavaDoc e) {
213                throw new TransactionException(e);
214             }
215          }
216       }
217    }
218
219    private void deleteTypeForProcess(String JavaDoc type) throws TransactionException {
220       List JavaDoc ret = (List JavaDoc) erasage.get(type);
221       if (null != ret) {
222          for (Iterator JavaDoc it = ret.iterator(); it.hasNext();) {
223             try {
224                HibernateGeneric o = (HibernateGeneric) it.next();
225                session.delete(o);
226             } catch (Exception JavaDoc e) {
227                throw new TransactionException(e);
228             }
229          }
230       }
231    }
232
233    public void deleteProcess() throws TransactionException {
234       for (ListIterator JavaDoc it = order.listIterator(order.size()); it.hasPrevious();) {
235          String JavaDoc type = (String JavaDoc) it.previous();
236          deleteTypeForProcess(type);
237       }
238    }
239
240    public void write() throws TransactionException {
241       for (Iterator JavaDoc it = order.iterator(); it.hasNext();) {
242          String JavaDoc type = (String JavaDoc) it.next();
243          iterateType(type);
244       }
245       deleteType(HibernateUtilities.HibernateAssignmentClassName);
246       deleteType(HibernateUtilities.HibernateAndJoinEntryClassName);
247       deleteType(HibernateUtilities.HibernateDeadlineClassName);
248       if (deleteFinishedProcesses)
249          deleteProcess();
250       deleteType(HibernateUtilities.HibernateProcessMgrClassName);
251    }
252
253    public void infoBuffer() throws TransactionException {
254       for (Iterator JavaDoc it = order.iterator(); it.hasNext();) {
255          String JavaDoc type = (String JavaDoc) it.next();
256          iterateTypeInfo(type);
257       }
258    }
259
260    public void iterateTypeInfo(String JavaDoc type) throws TransactionException {
261       List JavaDoc ret = (List JavaDoc) storage.get(type);
262       if (null != ret) {
263          for (Iterator JavaDoc it = ret.iterator(); it.hasNext();) {
264             HibernateGeneric o = (HibernateGeneric) it.next();
265             try {
266                System.out.println("OBJECT in buffer type= " + type + "o =" + o);
267             } catch (Exception JavaDoc e) {
268                e.printStackTrace();
269             }
270          }
271       }
272    }
273
274 }
275
Popular Tags