001: /*
002: * AuditHandler.java
003: *
004: * Created on June 25, 2004, 6:11 PM
005: */
006:
007: package org.manentia.kasai.audit;
008:
009: import java.sql.SQLException;
010: import java.text.DateFormat;
011: import java.text.SimpleDateFormat;
012: import java.util.Collection;
013: import java.util.ResourceBundle;
014:
015: import org.apache.commons.lang.StringUtils;
016: import org.manentia.kasai.Constants;
017: import org.manentia.kasai.exceptions.CannotAuditException;
018: import org.manentia.kasai.exceptions.DataAccessException;
019: import org.w3c.dom.Document;
020:
021: import com.manentia.commons.NonCriticalException;
022: import com.manentia.commons.audit.AuditBean;
023: import com.manentia.commons.log.Log;
024: import com.manentia.commons.xml.XMLUtil;
025:
026: /**
027: *
028: * @author rzuasti
029: */
030: public class AuditHandler {
031:
032: public static Collection listEntries(java.util.Date dateFrom,
033: java.util.Date dateTo, java.lang.String user,
034: java.lang.String operation) throws NonCriticalException,
035: DataAccessException {
036:
037: Log.write("Enter (dateFrom=" + dateToString(dateFrom)
038: + ", dateTo=" + dateToString(dateTo) + ", user="
039: + StringUtils.defaultString(user, "<null>")
040: + ", operation="
041: + StringUtils.defaultString(operation, "<null>") + ")",
042: Log.INFO, "listEntries", AuditHandler.class);
043:
044: ResourceBundle res = ResourceBundle
045: .getBundle(Constants.CONFIG_PROPERTY_FILE);
046: int maxRecords = Integer.parseInt(res
047: .getString("lists.maxTotalRows"));
048: Collection result = null;
049:
050: try {
051: result = AuditBean.list(dateFrom, dateTo, user, operation,
052: "kasai_audit", Constants.CONFIG_PROPERTY_FILE,
053: Constants.DATABASE_SOURCE, null, null, maxRecords);
054: } catch (SQLException sqle) {
055: Log.write("SQL Error", sqle, Log.ERROR, "listEntries",
056: AuditHandler.class);
057:
058: throw new DataAccessException(sqle);
059: }
060:
061: Log.write("Exit", Log.INFO, "listEntries", AuditHandler.class);
062:
063: return result;
064: }
065:
066: public static void createEntry(String userId, int returnCode,
067: String errorDescription, long duration, String clientIP,
068: String operation, String objectID, Document transactionData)
069: throws CannotAuditException {
070:
071: Log.write("Enter (userId="
072: + StringUtils.defaultString(userId, "<null>")
073: + ", returnCode=" + returnCode + ", errorDescription="
074: + StringUtils.defaultString(errorDescription, "<null>")
075: + ", duration=" + duration + ", clientIP="
076: + StringUtils.defaultString(clientIP, "<null>")
077: + ", operation="
078: + StringUtils.defaultString(operation, "<null>")
079: + ", objectID="
080: + StringUtils.defaultString(objectID, "<null>")
081: + ", transactionData="
082: + (transactionData == null ? "<null>" : "<data>"),
083: Log.INFO, "createEntry", AuditHandler.class);
084:
085: if (StringUtils.isEmpty(userId)) {
086: Log.write("User was not specified", Log.ERROR,
087: "createEntry", AuditHandler.class);
088:
089: throw new CannotAuditException(AuditHandler.class.getName()
090: + ".createAuditEntry.noUser");
091: }
092:
093: if (StringUtils.isEmpty(operation)) {
094: Log.write("Operation was not specified", Log.ERROR,
095: "createEntry", AuditHandler.class);
096:
097: throw new CannotAuditException(AuditHandler.class.getName()
098: + ".createAuditEntry.noOperation");
099: }
100:
101: AuditBean auditBean = new AuditBean("kasai_audit",
102: Constants.CONFIG_PROPERTY_FILE,
103: Constants.DATABASE_SOURCE, null, null);
104: auditBean.setClientIP(clientIP);
105: auditBean.setDateTime(new java.util.Date());
106: auditBean.setDuration(duration);
107: auditBean.setErrorDescription(errorDescription);
108: auditBean.setOperation(operation);
109: auditBean.setReturnCode(returnCode);
110: auditBean.setUser(userId);
111: auditBean.setProperty("object_id", objectID);
112: auditBean.setProperty("transaction_data", XMLUtil
113: .documentToString(transactionData));
114:
115: try {
116: auditBean.commit();
117: } catch (NonCriticalException nce) {
118: throw new CannotAuditException(nce);
119: } catch (SQLException sqle) {
120: Log.write("SQL Error", sqle, Log.ERROR, "createEntry",
121: AuditHandler.class);
122:
123: throw new CannotAuditException(sqle);
124: }
125:
126: Log.write("Exit", Log.INFO, "createEntry", AuditHandler.class);
127: }
128:
129: public static String dateToString(java.util.Date date) {
130: DateFormat format = new SimpleDateFormat(
131: "MM-dd-yyyy hh:mm:ss:SSS");
132: String result = "<null>";
133:
134: if (date != null) {
135: result = format.format(date);
136: }
137:
138: return result;
139: }
140: }
|