001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.server.j2ee.repository;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.obe.OBERuntimeException;
050: import org.obe.client.api.repository.RepositoryException;
051: import org.obe.server.j2ee.ejb.AbstractDAO;
052: import org.obe.server.j2ee.ejb.Sequence;
053: import org.obe.server.util.ObjectUtil;
054: import org.wfmc.audit.WMAAuditEntry;
055: import org.wfmc.audit.WMAEventCode;
056: import org.wfmc.wapi.WMFilter;
057: import org.wfmc.wapi.WMProcessInstanceState;
058:
059: import java.io.IOException;
060: import java.io.InputStream;
061: import java.sql.*;
062: import java.util.ArrayList;
063: import java.util.List;
064:
065: /**
066: * @author Adrian Price
067: */
068: final class AuditEntryDAO extends AbstractDAO {
069: private static final Log _logger = LogFactory
070: .getLog(AuditEntryDAO.class);
071: private static final String TABLE = "OBEAUDITENTRY";
072: private static final int KEY_INCREMENT = 100;
073: private static final Sequence SEQUENCE = new Sequence(TABLE,
074: KEY_INCREMENT);
075: private static final AuditEntryDAO _theInstance = new AuditEntryDAO();
076: private static final String INSERT_STMT = "INSERT INTO OBEAUDITENTRY (AUDITENTRYID,PROCESSDEFINITIONID,"
077: + "ACTIVITYDEFINITIONID,INITIALPROCESSINSTANCEID,"
078: + "CURRENTPROCESSINSTANCEID,ACTIVITYINSTANCEID,WORKITEMID,PROCESSSTATE,"
079: + "EVENT,DOMAINID,NODEID,USERID,ROLEID,AUDITDATE,AUDITENTRY) "
080: + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
081: private static final String SELECT_STMT = "SELECT AUDITENTRY FROM OBEAUDITENTRY";
082: private static final String DELETE_STMT = "DELETE FROM OBEAUDITENTRY";
083:
084: static AuditEntryDAO getInstance() {
085: return _theInstance;
086: }
087:
088: // Prevent instantiation.
089: private AuditEntryDAO() {
090: }
091:
092: protected Log getLogger() {
093: return _logger;
094: }
095:
096: long getNewId() {
097: try {
098: return getNewId(SEQUENCE);
099: } catch (RepositoryException e) {
100: throw new OBERuntimeException(e);
101: }
102: }
103:
104: void insert(WMAAuditEntry entry) throws RepositoryException,
105: SQLException {
106:
107: Connection con = getConnection();
108: PreparedStatement stmt = null;
109: try {
110: stmt = con.prepareStatement(INSERT_STMT);
111: stmt.setLong(1, getNewId());
112: stmt.setString(2, entry.getProcessDefinitionId());
113: stmt.setString(3, entry.getActivityDefinitionId());
114: stmt.setString(4, entry.getInitialProcessInstanceId());
115: stmt.setString(5, entry.getCurrentProcessInstanceId());
116: stmt.setString(6, entry.getActivityInstanceId());
117: stmt.setString(7, entry.getWorkItemId());
118: String processState = entry.getProcessState();
119: if (processState != null) {
120: stmt.setInt(8, WMProcessInstanceState.valueOf(
121: processState).value());
122: } else {
123: stmt.setNull(8, Types.INTEGER);
124: }
125: WMAEventCode eventCode = entry.getEventCode();
126: stmt.setInt(9, (eventCode == null ? WMAEventCode.UNKNOWN
127: : eventCode).value());
128: stmt.setString(10, entry.getDomainId());
129: stmt.setString(11, entry.getNodeId());
130: stmt.setString(12, entry.getUserId());
131: stmt.setString(13, entry.getRoleId());
132: stmt.setTimestamp(14, new Timestamp(entry.getTimestamp()
133: .getTime()));
134: InputStream in = ObjectUtil.serializeToInputStream(entry);
135: stmt.setBinaryStream(15, in, in.available());
136: stmt.executeUpdate();
137: } catch (IOException e) {
138: throw new RepositoryException(e);
139: } finally {
140: close(con, stmt, null);
141: }
142: }
143:
144: WMAAuditEntry[] findByFilter(WMFilter filter)
145: throws RepositoryException, SQLException {
146:
147: List results = new ArrayList();
148: Connection con = getConnection();
149: PreparedStatement stmt = null;
150: ResultSet rs = null;
151: try {
152: stmt = prepareStatement(con, SELECT_STMT, filter);
153: rs = stmt.executeQuery();
154: while (rs.next()) {
155: results.add(ObjectUtil.deserializeFromInputStream(rs
156: .getBinaryStream(1)));
157: }
158: return (WMAAuditEntry[]) results
159: .toArray(new WMAAuditEntry[results.size()]);
160: } catch (IOException e) {
161: throw new RepositoryException(e);
162: } catch (ClassNotFoundException e) {
163: throw new RepositoryException(e);
164: } finally {
165: close(con, stmt, rs);
166: }
167: }
168:
169: int deleteByFilter(WMFilter filter) throws RepositoryException,
170: SQLException {
171:
172: Connection con = getConnection();
173: PreparedStatement stmt = null;
174: try {
175: stmt = prepareStatement(con, DELETE_STMT, filter);
176: return stmt.executeUpdate();
177: } finally {
178: close(con, stmt, null);
179: }
180: }
181: }
|