1 4 5 package org.enhydra.shark.utilities.hibernate; 6 7 import java.util.ArrayList ; 8 import java.util.Arrays ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.ListIterator ; 13 import java.util.Map ; 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 26 public class Buffer { 27 28 private static final List order = Arrays.asList(new String [] { 29 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 HibernateUtilities.HibernateEventTypeClassName, 44 HibernateUtilities.HibernateProcessStateEventAuditClassName, 45 HibernateUtilities.HibernateActivityStateEventAuditClassName, 46 HibernateUtilities.HibernateStateEventAuditClassName, 47 HibernateUtilities.HibernateCreateProcessEventAuditClassName, 48 HibernateUtilities.HibernateAssignmentEventAuditClassName, 49 HibernateUtilities.HibernateDataEventAuditClassName }); 50 51 private Map storage; 52 private Map erasage; 53 private Map 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 { 61 transaction = dbt; 62 storage = new HashMap (); 63 erasage = new HashMap (); 64 readage = new HashMap (); 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 type) { 78 storage.remove(type); 79 erasage.remove(type); 80 readage.remove(type); 81 } 82 83 public void read(Object o) { 84 if (o != null) 85 read(o, o.getClass().toString()); 86 } 87 88 public void read(Object o, String type) { 89 List l = (List ) readage.get(type); 90 if (null == l) { 91 l = new ArrayList (); 92 readage.put(type, l); 93 } 94 if (!l.contains(o)) 95 l.add(o); 96 } 97 98 public void read(List l) { 99 if (l != null && l.size() != 0) { 100 String type = l.get(0).getClass().toString(); 101 List r = (List ) readage.get(type); 102 if (null != r) { 103 for (Iterator it = l.iterator(); it.hasNext();) { 104 Object o = (Object ) 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 o) { 115 store(o, o.getClass().toString()); 116 } 117 118 public void erase(Object o) { 119 erase(o, o.getClass().toString()); 120 } 121 122 public void store(Object o, String type) { 123 List l = (List ) storage.get(type); 124 if (null == l) { 125 l = new ArrayList (); 126 storage.put(type, l); 127 } 128 if (!l.contains(o)) { 129 l.add(o); 130 } 131 l = (List ) readage.get(type); 132 if (l != null && l.contains(o)) 133 l.remove(o); 134 } 135 136 public void erase(Object o, String type) { 137 List l = (List ) erasage.get(type); 138 if (null == l) { 139 l = new ArrayList (); 140 erasage.put(type, l); 141 } 142 if (!l.contains(o)) { 143 l.add(o); 144 } 145 l = (List ) readage.get(type); 146 if (l != null && l.contains(o)) { 147 l.remove(o); 148 } 149 } 150 151 private static ArrayList empty = new ArrayList (1); 152 153 public Iterator iterator4type(String type) { 154 List s = (List ) storage.get(type); 155 List r = (List ) readage.get(type); 156 List l = new ArrayList (); 157 l = (ArrayList ) 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 type) throws TransactionException { 168 List ret = (List ) storage.get(type); 169 if (null != ret) { 170 for (Iterator 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 o.setDbId(i); 179 } else { 180 int i = HibernateObjectUtil.getNextDecId(type).intValue(); 181 o.setDbId(i); 184 } 185 session.save(o); 186 } else { 189 session.update(o); 190 } 191 session.flush(); 192 session.clear(); 193 194 } catch (HibernateException e) { 195 } catch (PersistenceException e) { 197 } catch (Exception e) { 199 } 201 } 202 } 203 } 204 205 private void deleteType(String type) throws TransactionException { 206 List ret = (List ) erasage.get(type); 207 if (null != ret) { 208 for (Iterator it = ret.iterator(); it.hasNext();) { 209 try { 210 HibernateGeneric o = (HibernateGeneric) it.next(); 211 session.delete(o); 212 } catch (Exception e) { 213 throw new TransactionException(e); 214 } 215 } 216 } 217 } 218 219 private void deleteTypeForProcess(String type) throws TransactionException { 220 List ret = (List ) erasage.get(type); 221 if (null != ret) { 222 for (Iterator it = ret.iterator(); it.hasNext();) { 223 try { 224 HibernateGeneric o = (HibernateGeneric) it.next(); 225 session.delete(o); 226 } catch (Exception e) { 227 throw new TransactionException(e); 228 } 229 } 230 } 231 } 232 233 public void deleteProcess() throws TransactionException { 234 for (ListIterator it = order.listIterator(order.size()); it.hasPrevious();) { 235 String type = (String ) it.previous(); 236 deleteTypeForProcess(type); 237 } 238 } 239 240 public void write() throws TransactionException { 241 for (Iterator it = order.iterator(); it.hasNext();) { 242 String type = (String ) 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 it = order.iterator(); it.hasNext();) { 255 String type = (String ) it.next(); 256 iterateTypeInfo(type); 257 } 258 } 259 260 public void iterateTypeInfo(String type) throws TransactionException { 261 List ret = (List ) storage.get(type); 262 if (null != ret) { 263 for (Iterator 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 e) { 268 e.printStackTrace(); 269 } 270 } 271 } 272 } 273 274 } 275 | Popular Tags |