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.client.api.model.ActivityInstanceAttributes;
050: import org.obe.client.api.model.ProcessInstanceAttributes;
051: import org.obe.client.api.repository.RepositoryException;
052: import org.obe.server.j2ee.ejb.AbstractDAO;
053: import org.obe.server.j2ee.ejb.EJBHelper;
054: import org.obe.server.j2ee.ejb.Sequence;
055: import org.obe.spi.model.ActivityInstance;
056: import org.obe.spi.model.AttributedEntity;
057: import org.obe.spi.model.WorkItem;
058: import org.wfmc.wapi.WMFilter;
059:
060: import java.sql.Connection;
061: import java.sql.PreparedStatement;
062: import java.sql.ResultSet;
063: import java.sql.SQLException;
064: import java.util.ArrayList;
065: import java.util.List;
066:
067: /**
068: * @author Adrian Price
069: */
070: final class AttributeInstanceDAO extends AbstractDAO {
071: private static final String TABLE = "OBEATTRIBUTEINSTANCE";
072: private static final String SELECT_PK_SQL = "SELECT OWNERID, NAME, OWNERTYPE FROM OBEATTRIBUTEINSTANCE";
073: private static final String DELETE_BY_PROCESS_SQL = "DELETE FROM OBEATTRIBUTEINSTANCE WHERE PROCESSINSTANCEID=?";
074: private static final String DELETE_BY_OWNER_SQL = "DELETE FROM OBEATTRIBUTEINSTANCE WHERE OWNERID=? AND OWNERTYPE=?";
075: private static final int KEY_INCREMENT = 100;
076: private static final Sequence SEQUENCE = new Sequence(TABLE,
077: KEY_INCREMENT);
078: private static final AttributeInstanceDAO _theInstance = new AttributeInstanceDAO();
079: private static final Log _logger = LogFactory
080: .getLog(AttributeInstanceDAO.class);
081:
082: public static AttributeInstanceDAO getInstance() {
083: return _theInstance;
084: }
085:
086: // Prevent instantiation.
087: private AttributeInstanceDAO() {
088: }
089:
090: protected Log getLogger() {
091: return _logger;
092: }
093:
094: String getNewId() throws RepositoryException {
095: return String.valueOf(getNewId(SEQUENCE));
096: }
097:
098: int count(String processDefinitionId, String processInstanceId,
099: String activityDefinitionId, String ownerId, int ownerType,
100: WMFilter filter, String attrName) throws SQLException,
101: RepositoryException {
102:
103: int count = count(TABLE, createFilters(processDefinitionId,
104: processInstanceId, activityDefinitionId, ownerId,
105: ownerType, attrName, filter));
106:
107: // If the query is selecting all attributes for a single entity, we can
108: // include that entity's system attributes in the count.
109: if (processDefinitionId == null && activityDefinitionId == null
110: && processInstanceId == null && ownerId != null
111: && filter == null && attrName == null) {
112:
113: switch (ownerType) {
114: case AttributedEntity.PROCESS_INSTANCE_TYPE:
115: count += ProcessInstanceAttributes.SYSTEM_PROPERTIES.length;
116: break;
117: case AttributedEntity.ACTIVITY_INSTANCE_TYPE:
118: count += ActivityInstance.propertyDescriptors.length;
119: break;
120: case AttributedEntity.WORKITEM_TYPE:
121: count += WorkItem.propertyDescriptors.length;
122: break;
123: default:
124: throw new IllegalArgumentException(String
125: .valueOf(ownerType));
126: }
127: }
128: return count;
129: }
130:
131: List findByFilter(String processDefinitionId,
132: String processInstanceId, String activityDefinitionId,
133: String ownerId, String attrName, int ownerType,
134: WMFilter filter) throws SQLException, RepositoryException {
135:
136: Connection con = null;
137: PreparedStatement stmt = null;
138: ResultSet rs = null;
139: try {
140: List pkgs = new ArrayList();
141: con = EJBHelper.getConnection();
142: stmt = prepareStatement(con, SELECT_PK_SQL, createFilters(
143: processDefinitionId, processInstanceId,
144: activityDefinitionId, ownerId, ownerType, attrName,
145: filter));
146: rs = stmt.executeQuery();
147: while (rs.next()) {
148: pkgs.add(new AttributeInstancePK(rs.getString(1), rs
149: .getString(2), rs.getInt(3)));
150: }
151: return pkgs;
152: } finally {
153: close(con, stmt, rs);
154: }
155: }
156:
157: int deleteByProcessId(String processInstanceId) throws SQLException {
158:
159: Connection con = null;
160: PreparedStatement stmt = null;
161: try {
162: con = EJBHelper.getConnection();
163: stmt = con.prepareStatement(DELETE_BY_PROCESS_SQL);
164: stmt.setString(1, processInstanceId);
165: return stmt.executeUpdate();
166: } finally {
167: close(con, stmt, null);
168: }
169: }
170:
171: int deleteByOwnerId(String ownerId, int ownerType)
172: throws SQLException {
173:
174: Connection con = null;
175: PreparedStatement stmt = null;
176: try {
177: con = EJBHelper.getConnection();
178: stmt = con.prepareStatement(DELETE_BY_OWNER_SQL);
179: stmt.setString(1, ownerId);
180: stmt.setInt(2, ownerType);
181: return stmt.executeUpdate();
182: } finally {
183: close(con, stmt, null);
184: }
185: }
186:
187: private WMFilter[] createFilters(String processDefinitionId,
188: String processInstanceId, String activityDefinitionId,
189: String ownerId, int ownerType, String attrName,
190: WMFilter filter) {
191:
192: int count = 1;
193: if (processDefinitionId != null)
194: count++;
195: if (processInstanceId != null)
196: count++;
197: if (activityDefinitionId != null)
198: count++;
199: if (ownerId != null)
200: count++;
201: if (attrName != null)
202: count++;
203: if (filter != null)
204: count++;
205: WMFilter[] filters = new WMFilter[count];
206:
207: int n = 0;
208: if (processDefinitionId != null) {
209: filters[n++] = new WMFilter("processDefinitionId",
210: WMFilter.EQ, processDefinitionId);
211: }
212: if (processInstanceId != null) {
213: filters[n++] = new WMFilter("processInstanceId",
214: WMFilter.EQ, processInstanceId);
215: }
216: if (activityDefinitionId != null) {
217: filters[n++] = new WMFilter("activityDefinitionId",
218: WMFilter.EQ, activityDefinitionId);
219: }
220: if (attrName != null) {
221: filters[n++] = new WMFilter("name", WMFilter.EQ, attrName);
222: }
223: if (ownerId != null) {
224: filters[n++] = new WMFilter(
225: ActivityInstanceAttributes.OWNER_ID, WMFilter.EQ,
226: ownerId);
227: }
228: // ownerType is always mandatory.
229: filters[n++] = new WMFilter(
230: ActivityInstanceAttributes.OWNER_TYPE, WMFilter.EQ,
231: String.valueOf(ownerType));
232: if (filter != null) {
233: filters[n++] = filter;
234: }
235: return filters;
236: }
237: }
|