1 25 package org.ofbiz.shark.audit; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilMisc; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.entity.GenericEntityException; 38 import org.ofbiz.entity.GenericValue; 39 40 import org.enhydra.shark.api.internal.eventaudit.DataEventAuditPersistenceInterface; 41 42 49 public class DataEventAudit extends EventAudit implements DataEventAuditPersistenceInterface { 50 51 public static final String module = AssignmentEventAudit.class.getName(); 52 protected GenericValue dataEventAudit = null; 53 private boolean newValue = false; 54 55 public DataEventAudit(EntityAuditMgr mgr, GenericDelegator delegator, String eventAuditId) { 56 super(mgr, delegator, eventAuditId); 57 if (this.delegator != null) { 58 try { 59 this.dataEventAudit = delegator.findByPrimaryKey("WfDataEventAudit", UtilMisc.toMap("eventAuditId", eventAuditId)); 60 } catch (GenericEntityException e) { 61 Debug.logError(e, module); 62 } 63 } else { 64 Debug.logError("Invalid delegator object passed", module); 65 } 66 } 67 68 public DataEventAudit(EntityAuditMgr mgr, GenericDelegator delegator) { 69 super(mgr, delegator); 70 this.newValue = true; 71 this.dataEventAudit = delegator.makeValue("WfDataEventAudit", UtilMisc.toMap("eventAuditId", this.eventAuditId)); 72 } 73 74 public DataEventAudit(EntityAuditMgr mgr, GenericValue dataEventAudit) { 75 super(mgr, dataEventAudit.getDelegator(), dataEventAudit.getString("eventAuditId")); 76 this.dataEventAudit = dataEventAudit; 77 } 78 79 public void setOldData(Map od) { 80 byte[] value = serialize(od); 81 dataEventAudit.setBytes("oldData", (value != null ? value : null)); 82 } 83 84 public Map getOldData() { 85 byte[] value = dataEventAudit.getBytes("oldData"); 86 if (value != null) { 87 return deserialize(value); 88 } 89 return null; 90 } 91 92 public void setNewData(Map nd) { 93 byte[] value = serialize(nd); 94 dataEventAudit.setBytes("newData", (value != null ? value : null)); 95 } 96 97 public Map getNewData() { 98 byte[] value = dataEventAudit.getBytes("newData"); 99 if (value != null) { 100 return deserialize(value); 101 } 102 return null; 103 } 104 105 public void store() throws GenericEntityException { 106 super.store(); 107 if (newValue) { 108 newValue = false; 109 delegator.createOrStore(dataEventAudit); 110 } else { 111 delegator.store(dataEventAudit); 112 } 113 } 114 115 public void reload() throws GenericEntityException { 116 super.reload(); 117 if (!newValue) { 118 dataEventAudit.refresh(); 119 } 120 } 121 122 public void remove() throws GenericEntityException { 123 super.remove(); 124 if (!newValue) { 125 delegator.removeValue(dataEventAudit); 126 } 127 } 128 129 private Map deserialize(byte[] bytes) { 130 ByteArrayInputStream bis = null; 131 ObjectInputStream ois = null; 132 Map map = null; 133 134 try { 135 bis = new ByteArrayInputStream (bytes); 136 ois = new ObjectInputStream (bis); 137 map = (Map ) ois.readObject(); 138 } catch (IOException e) { 139 Debug.logError(e, module); 140 } catch (ClassCastException e) { 141 Debug.logError(e, module); 142 } catch (ClassNotFoundException e) { 143 Debug.logError(e, module); 144 } finally { 145 if (ois != null) { 146 try { 147 ois.close(); 148 } catch (IOException e) { 149 Debug.logError(e, module); 150 } 151 } 152 if (bis != null) { 153 try { 154 bis.close(); 155 } catch (IOException e) { 156 Debug.logError(e, module); 157 } 158 } 159 } 160 return map; 161 } 162 163 private byte[] serialize(Map map) { 164 ByteArrayOutputStream bos = null; 165 ObjectOutputStream oos = null; 166 byte[] bytes = null; 167 168 try { 169 bos = new ByteArrayOutputStream (); 170 oos = new ObjectOutputStream (bos); 171 oos.writeObject(map); 172 oos.flush(); 173 bytes = bos.toByteArray(); 174 } catch (IOException e) { 175 Debug.logError(e, module); 176 } finally { 177 if (oos != null) { 178 try { 179 oos.close(); 180 } catch (IOException e) { 181 Debug.logError(e, module); 182 } 183 } 184 if (bos != null) { 185 try { 186 bos.close(); 187 } catch (IOException e) { 188 Debug.logError(e, module); 189 } 190 } 191 } 192 return bytes; 193 } 194 } 195 | Popular Tags |