KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > audit > DataEventAudit


1 /*
2  * $Id: DataEventAudit.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.audit;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.util.Map JavaDoc;
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 /**
43  * Persistance Object
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.1
48  */

49 public class DataEventAudit extends EventAudit implements DataEventAuditPersistenceInterface {
50
51     public static final String JavaDoc module = AssignmentEventAudit.class.getName();
52     protected GenericValue dataEventAudit = null;
53     private boolean newValue = false;
54
55     public DataEventAudit(EntityAuditMgr mgr, GenericDelegator delegator, String JavaDoc 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 JavaDoc od) {
80         byte[] value = serialize(od);
81         dataEventAudit.setBytes("oldData", (value != null ? value : null));
82     }
83
84     public Map JavaDoc 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 JavaDoc nd) {
93         byte[] value = serialize(nd);
94         dataEventAudit.setBytes("newData", (value != null ? value : null));
95     }
96
97     public Map JavaDoc 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 JavaDoc deserialize(byte[] bytes) {
130         ByteArrayInputStream JavaDoc bis = null;
131         ObjectInputStream JavaDoc ois = null;
132         Map JavaDoc map = null;
133
134         try {
135             bis = new ByteArrayInputStream JavaDoc(bytes);
136             ois = new ObjectInputStream JavaDoc(bis);
137             map = (Map JavaDoc) ois.readObject();
138         } catch (IOException JavaDoc e) {
139             Debug.logError(e, module);
140         } catch (ClassCastException JavaDoc e) {
141             Debug.logError(e, module);
142         } catch (ClassNotFoundException JavaDoc e) {
143             Debug.logError(e, module);
144         } finally {
145             if (ois != null) {
146                 try {
147                     ois.close();
148                 } catch (IOException JavaDoc e) {
149                     Debug.logError(e, module);
150                 }
151             }
152             if (bis != null) {
153                 try {
154                     bis.close();
155                 } catch (IOException JavaDoc e) {
156                     Debug.logError(e, module);
157                 }
158             }
159         }
160         return map;
161     }
162
163     private byte[] serialize(Map JavaDoc map) {
164         ByteArrayOutputStream JavaDoc bos = null;
165         ObjectOutputStream JavaDoc oos = null;
166         byte[] bytes = null;
167
168         try {
169             bos = new ByteArrayOutputStream JavaDoc();
170             oos = new ObjectOutputStream JavaDoc(bos);
171             oos.writeObject(map);
172             oos.flush();
173             bytes = bos.toByteArray();
174         } catch (IOException JavaDoc e) {
175             Debug.logError(e, module);
176         } finally {
177             if (oos != null) {
178                 try {
179                     oos.close();
180                 } catch (IOException JavaDoc e) {
181                     Debug.logError(e, module);
182                 }
183             }
184             if (bos != null) {
185                 try {
186                     bos.close();
187                 } catch (IOException JavaDoc e) {
188                     Debug.logError(e, module);
189                 }
190             }
191         }
192         return bytes;
193     }
194 }
195
Popular Tags