Source code: com/RuntimeCollective/webapps/bean/AuditInfo.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/AuditInfo.java,v 1.4 2003/09/30 15:13:09 joe Exp $
2 * $Revision: 1.4 $
3 * $Date: 2003/09/30 15:13:09 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.bean;
31
32 import com.RuntimeCollective.webapps.RuntimeDataSource;
33 import com.RuntimeCollective.webapps.EntityBeanStore;
34 import com.RuntimeCollective.webapps.RuntimeParameters;
35 import com.RuntimeCollective.webapps.bean.EntityBean;
36 import com.RuntimeCollective.webapps.bean.User;
37
38 import java.sql.SQLException;
39 import java.util.Vector;
40 import java.util.Date;
41 import java.util.List;
42
43 /**
44 * A store for historical audit information about beans.
45 *
46 * @version $Id: AuditInfo.java,v 1.4 2003/09/30 15:13:09 joe Exp $
47 */
48 public class AuditInfo {
49
50 public static final String DATABASE_TABLE = "webapps_audit_info";
51
52 public static void recordEdit(EntityBean edited, User editor, String notes) {
53 String editedSql = null;
54 String editorSql = null;
55
56 if (edited != null) {
57 editedSql = ""+edited.getId();
58 } else {
59 editedSql = "-1";
60 }
61
62 if (editor != null) {
63 editorSql = ""+editor.getId();
64 } else {
65 editedSql = "-1";
66 }
67
68 try {
69 Date now = new Date();
70 StringBuffer audit = new StringBuffer()
71 .append("INSERT INTO ")
72 .append(DATABASE_TABLE)
73 .append(" (id, audit_date, user_id, notes) VALUES (")
74 .append(editedSql)
75 .append(", ")
76 .append(RuntimeDataSource.toSqlString(now))
77 .append(", ")
78 .append(editorSql)
79 .append(", '")
80 .append(RuntimeDataSource.escape(notes))
81 .append("')");
82
83 RuntimeDataSource.update(audit.toString());
84 } catch (SQLException e) {
85 RuntimeParameters.logError("AuditInfo", "failed to record audit information", e);
86 }
87 }
88
89
90 /**
91 * Get the date of the latest action (of a given type/notes) for a bean/
92 * Useful to get the last approval date of a bean, for example.
93 *
94 * @param edited, the object which has been edited
95 * @param notes, the type of the action performed
96 * @return a List made of two items: a User and a Date. Null if none were found
97 */
98 public static List getLastEditUserAndDate(EntityBean edited, String notes) {
99
100 if (edited == null) {
101 RuntimeParameters.logError("AuditInfo", "getLastEditUserAndDate given a null edited bean.");
102 return null;
103 }
104
105 try {
106 String dbalias = RuntimeDataSource.getDefaultAlias();
107 int no_fields = 2;
108 Object[][] results = RuntimeDataSource.queryRows(dbalias, SELECT_USER_DATE_WHERE_ID+edited.getId()+ORDER_BY_DATE_DESC);
109
110 // no data found
111 if (results.length == 0)
112 return null;
113
114 Vector returned = new Vector(2);
115
116 // get the user
117 if (results[0][0] != null) {
118 returned.add(RuntimeParameters.getStore().get(User.class.getName(), Integer.parseInt(results[0][0].toString())));
119 } else {
120 returned.add(null);
121 }
122
123 // get the date
124 if (results[0][1] != null) {
125 returned.add(RuntimeDataSource.toDate(results[0][1].toString()));
126 } else {
127 returned.add(null);
128 }
129
130 return returned;
131
132 } catch (SQLException e) {
133 RuntimeParameters.logError("AuditInfo", "Failed to getLastEditUserAndDate("+edited.getId()+", "+notes+").", e);
134 return null;
135 }
136 }
137
138 protected static String SELECT_USER_DATE_WHERE_ID = "select t.user_id, t.audit_date from "+DATABASE_TABLE+" t where t.id = ";
139
140 protected static String ORDER_BY_DATE_DESC = " order by t.audit_date desc";
141
142 /** Get the last approval date of an edited object.
143 * This takes the date of the latest approval action for a given bean.
144 * @param edited, the bean you're looking for
145 * @return a Date, possibly null (won't necessarily be null if the bean is not approved)
146 */
147 public static Date getLastApprovalDate(EntityBean edited) {
148 List userdate = AuditInfo.getLastEditUserAndDate(edited, APPROVAL_ACTION);
149 if ((userdate != null) && (userdate.get(1) != null)) {
150 return (Date) userdate.get(1);
151 } else {
152 return null;
153 }
154 }
155
156 /** Get the last creation date of an edited object.
157 * This takes the date of the latest creation action for a given bean.
158 * @param edited, the bean you're looking for
159 * @return a Date, possibly null
160 */
161 public static Date getLastCreationDate(EntityBean edited) {
162 List userdate = AuditInfo.getLastEditUserAndDate(edited, CREATION_ACTION);
163 if ((userdate != null) && (userdate.get(1) != null)) {
164 return (Date) userdate.get(1);
165 } else {
166 return null;
167 }
168 }
169
170 /** Get the last approval user of an edited object.
171 * This takes the user of the latest approval action for a given bean.
172 * @param edited, the bean you're looking for
173 * @return a User, possibly null (won't necessarily be null if the bean is not approved)
174 */
175 public static User getLastApprovalUser(EntityBean edited) {
176 List userdate = AuditInfo.getLastEditUserAndDate(edited, APPROVAL_ACTION);
177 if ((userdate != null) && (userdate.get(0) != null)) {
178 return (User) userdate.get(0);
179 } else {
180 return null;
181 }
182 }
183
184 /** Get the last creation user of an edited object.
185 * This takes the user of the latest creation action for a given bean.
186 * @param edited, the bean you're looking for
187 * @return a User, possibly null
188 */
189 public static User getLastCreationUser(EntityBean edited) {
190 List userdate = AuditInfo.getLastEditUserAndDate(edited, CREATION_ACTION);
191 if ((userdate != null) && (userdate.get(0) != null)) {
192 return (User) userdate.get(0);
193 } else {
194 return null;
195 }
196 }
197
198 /** The action (notes) to use when approving a bean. */
199 public static String APPROVAL_ACTION = "approved";
200
201 /** The action (notes) to use when creating a bean. */
202 public static String CREATION_ACTION = "created";
203 }