1 package org.enhydra.shark.utilities.dods; 2 3 import java.util.*; 4 5 import com.lutris.appserver.server.sql.CoreDO; 6 import com.lutris.appserver.server.sql.DBTransaction; 7 import com.lutris.dods.builder.generator.dataobject.GenericDO; 8 import org.enhydra.shark.api.TransactionException; 9 import java.sql.SQLException ; 10 import com.lutris.appserver.server.sql.DatabaseManagerException; 11 import com.lutris.dods.builder.generator.query.RefAssertionException; 12 import com.lutris.dods.builder.generator.query.DataObjectException; 13 import com.lutris.dods.builder.generator.query.QueryException; 14 15 public class Buffer { 16 private static final List order = Arrays.asList(new String []{ 17 "class org.enhydra.shark.instancepersistence.data.ActivityStateDO", 19 "class org.enhydra.shark.instancepersistence.data.ProcessStateDO", 20 "class org.enhydra.shark.instancepersistence.data.ResourceDO", 21 "class org.enhydra.shark.instancepersistence.data.ProcessDefinitionDO", 22 "class org.enhydra.shark.instancepersistence.data.ProcessDO", 23 "class org.enhydra.shark.instancepersistence.data.ProcessDataDO", 24 "class org.enhydra.shark.instancepersistence.data.ProcessDataWOBDO", 25 "class org.enhydra.shark.instancepersistence.data.ProcessDataBLOBDO", 26 "class org.enhydra.shark.instancepersistence.data.ActivityDO", 27 "class org.enhydra.shark.instancepersistence.data.ActivityDataDO", 28 "class org.enhydra.shark.instancepersistence.data.ActivityDataWOBDO", 29 "class org.enhydra.shark.instancepersistence.data.ActivityDataBLOBDO", 30 "class org.enhydra.shark.instancepersistence.data.ProcessRequesterDO", 34 "class org.enhydra.shark.eventaudit.data.EventTypeDO", 36 "class org.enhydra.shark.eventaudit.data.ProcessStateEventAuditDO", 37 "class org.enhydra.shark.eventaudit.data.ActivityStateEventAuditDO", 38 "class org.enhydra.shark.eventaudit.data.StateEventAuditDO", 39 "class org.enhydra.shark.eventaudit.data.CreateProcessEventAuditDO", 40 "class org.enhydra.shark.eventaudit.data.AssignmentEventAuditDO", 41 "class org.enhydra.shark.eventaudit.data.DataEventAuditDO", 42 "class org.enhydra.shark.eventaudit.data.OldEventAuditDataDO", 43 "class org.enhydra.shark.eventaudit.data.NewEventAuditDataDO", 44 "class org.enhydra.shark.eventaudit.data.OldEventAuditDataWOBDO", 45 "class org.enhydra.shark.eventaudit.data.OldEventAuditDataBLOBDO", 46 "class org.enhydra.shark.eventaudit.data.NewEventAuditDataWOBDO", 47 "class org.enhydra.shark.eventaudit.data.NewEventAuditDataBLOBDO" 48 }); 49 50 private Map storage; 51 private Map erasage; 52 private Map readage; 53 protected DBTransaction transaction; 54 55 public Buffer(DBTransaction dbt) throws NullPointerException { 56 transaction = dbt; 57 storage = new HashMap(); 58 erasage = new HashMap(); 59 readage = new HashMap(); 60 } 61 62 public void clear() { 63 storage.clear(); 64 erasage.clear(); 65 readage.clear(); 66 } 67 68 public void clear(String type) { 69 storage.remove(type); 70 erasage.remove(type); 71 readage.remove(type); 72 } 73 74 public void store(CoreDO aDO) { 75 if (null != aDO) 76 store(aDO, aDO.getClass().toString()); 77 } 78 79 public void erase(CoreDO aDO) { 80 if (null != aDO) 81 erase(aDO, aDO.getClass().toString()); 82 } 83 84 public void _read(CoreDO aDO) { 85 if (null != aDO) 86 _read(aDO, aDO.getClass().toString()); 87 } 88 89 public void store(CoreDO aDO, String type) { 90 List l = (List)storage.get(type); 92 if (null == l) { 93 l = new ArrayList(); 94 storage.put(type, l); 95 } 96 if (!l.contains(aDO)) 97 l.add(aDO); 98 l = (List)readage.get(type); 99 if (null != l && l.contains(aDO)) 100 l.remove(aDO); 101 } 102 103 public void erase(CoreDO aDO, String type) { 104 List l = (List)readage.get(type); 106 if (null != l && l.contains(aDO)) 107 l.remove(aDO); 108 l = (List)storage.get(type); 109 if (null != l && l.contains(aDO)) 110 l.remove(aDO); 111 112 if (aDO.isPersistent()) { 113 l = (List)erasage.get(type); 114 if (null == l) { 115 l = new ArrayList(); 116 erasage.put(type, l); 117 } 118 if (!l.contains(aDO)) 119 l.add(aDO); 120 } 121 } 122 123 public void erase(Set s) { 124 if (null != s && s.size() > 0) { 125 Iterator it=s.iterator(); 126 while (it.hasNext()) { 127 CoreDO cDO=(CoreDO)it.next(); 128 erase(cDO,cDO.getClass().toString()); 129 } 130 } 131 } 132 133 public void _read(CoreDO aDO, String type) { 134 List l = (List)readage.get(type); 136 if (null == l) { 137 l = new ArrayList(); 138 readage.put(type, l); 139 } 140 if (!l.contains(aDO)) 141 l.add(aDO); 142 } 143 144 private static ArrayList empty = new ArrayList(1); 145 public Iterator iterator4type(String type) { 146 List s = (List)storage.get(type); 147 List r = (List)readage.get(type); 149 List l = new ArrayList(); 150 l = (ArrayList)empty.clone(); 151 if (null != r) { 152 l.addAll(r); 153 } 154 if (null != s) { 155 l.addAll(s); 156 } 157 return l.iterator(); 158 } 159 160 public Iterator iterator4typeDeleted(String type) { 161 List e = (List)erasage.get(type); 162 List l = new ArrayList(); 163 l = (ArrayList)empty.clone(); 164 if (null != e) { 165 l.addAll(e); 166 } 167 return l.iterator(); 168 } 169 170 private void iterateType(String type) throws TransactionException { 171 List ret = (List)storage.get(type); 172 if (null != ret) { 173 for (Iterator it = ret.iterator(); it.hasNext();) { 174 CoreDO aDO = (CoreDO)it.next(); 175 transaction.insert(aDO); 176 } 178 } 179 } 180 181 private boolean deleteType(String type) throws TransactionException { 182 boolean hasDeletedEntities=false; 183 List ret = (List)erasage.get(type); 184 if (null != ret) { 185 for (Iterator it = ret.iterator(); it.hasNext();) { 186 GenericDO aDO = (GenericDO)it.next(); 187 try { 190 aDO.delete(); 192 hasDeletedEntities=true; 193 } catch (Exception e) { 194 throw new TransactionException(e); 195 } 196 } 198 } 199 return hasDeletedEntities; 200 } 201 202 public void write() throws TransactionException { 203 for (Iterator it = order.iterator(); it.hasNext();) { 204 String type = (String )it.next(); 205 iterateType(type); 207 } 209 210 boolean write=deleteType("class org.enhydra.shark.instancepersistence.data.AssignmentDO"); 212 if (write) { 213 try { 214 transaction.write(); 215 } catch (SQLException e) { 216 throw new TransactionException(e); 217 } 218 } 219 iterateType("class org.enhydra.shark.instancepersistence.data.AssignmentDO"); 220 221 iterateType("class org.enhydra.shark.instancepersistence.data.DeadlineDO"); 222 write=deleteType("class org.enhydra.shark.instancepersistence.data.DeadlineDO"); 223 224 iterateType("class org.enhydra.shark.instancepersistence.data.AndJoinEntryDO"); 225 write=deleteType("class org.enhydra.shark.instancepersistence.data.AndJoinEntryDO") || write; 226 227 write=deleteType("class org.enhydra.shark.instancepersistence.data.ProcessRequesterDO") || write; 228 write=deleteType("class org.enhydra.shark.instancepersistence.data.ActivityDataBLOBDO") || write; 229 write=deleteType("class org.enhydra.shark.instancepersistence.data.ActivityDataWOBDO") || write; 230 write=deleteType("class org.enhydra.shark.instancepersistence.data.ActivityDataDO") || write; 231 232 if (write) { 233 try { 234 transaction.write(); 235 } catch (SQLException e) { 236 throw new TransactionException(e); 237 } 238 } 239 write=deleteType("class org.enhydra.shark.instancepersistence.data.ActivityDO"); 240 write=deleteType("class org.enhydra.shark.instancepersistence.data.ProcessDataBLOBDO") || write; 241 write=deleteType("class org.enhydra.shark.instancepersistence.data.ProcessDataWOBDO") || write; 242 write=deleteType("class org.enhydra.shark.instancepersistence.data.ProcessDataDO") || write; 243 244 if (write) { 246 try { 247 transaction.write(); 248 } catch (SQLException e) { 249 throw new TransactionException(e); 250 } 251 } 252 253 write=deleteType("class org.enhydra.shark.instancepersistence.data.ProcessDO"); 254 if (write) { 255 try { 256 transaction.write(); 257 } catch (SQLException e) { 258 throw new TransactionException(e); 259 } 260 } 261 deleteType("class org.enhydra.shark.instancepersistence.data.ProcessDefinitionDO"); 262 263 } 264 265 } 266 267 268 | Popular Tags |