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.obe.client.api.repository.RepositoryException;
048: import org.obe.server.j2ee.ejb.AbstractDAO;
049: import org.obe.server.j2ee.ejb.EJBHelper;
050: import org.obe.xpdl.model.workflow.WorkflowProcess;
051: import org.wfmc.wapi.WMFilter;
052:
053: import java.beans.PropertyDescriptor;
054: import java.sql.Connection;
055: import java.sql.PreparedStatement;
056: import java.sql.ResultSet;
057: import java.sql.SQLException;
058: import java.util.ArrayList;
059: import java.util.List;
060:
061: /**
062: * Abstract DAO class for entities with associated attribute collections.
063: *
064: * @author Adrian Price
065: */
066: abstract class AttributedEntityDAO extends AbstractDAO {
067: protected AttributedEntityDAO() {
068: }
069:
070: protected int count(String table, int ownerType, String pkField,
071: PropertyDescriptor[] columns, WorkflowProcess workflow,
072: WMFilter filter) throws SQLException, RepositoryException {
073:
074: return count(table, ownerType, pkField, columns, workflow,
075: filter == null ? null : new WMFilter[] { filter });
076: }
077:
078: protected int count(String table, int ownerType, String pkField,
079: PropertyDescriptor[] sysAttrs, WorkflowProcess workflow,
080: WMFilter[] filters) throws SQLException,
081: RepositoryException {
082:
083: Connection con = null;
084: PreparedStatement stmt = null;
085: ResultSet rs = null;
086: try {
087: con = EJBHelper.getConnection();
088:
089: String sql = "SELECT COUNT(*) FROM " + table;
090: stmt = prepareStatement(con, sql, filters, ownerType,
091: pkField, sysAttrs, workflow);
092: rs = stmt.executeQuery();
093: rs.next();
094: return rs.getInt(1);
095: } finally {
096: close(con, stmt, rs);
097: }
098: }
099:
100: List findByFilter(String table, int ownerType, String pkField,
101: PropertyDescriptor[] columns, WorkflowProcess workflow,
102: WMFilter filter) throws SQLException, RepositoryException {
103:
104: return findByFilter(table, ownerType, pkField, columns,
105: workflow, filter == null ? null
106: : new WMFilter[] { filter });
107: }
108:
109: List findByFilter(String table, int ownerType, String pkField,
110: PropertyDescriptor[] sysAttrs, WorkflowProcess workflow,
111: WMFilter[] filters) throws SQLException,
112: RepositoryException {
113:
114: Connection con = null;
115: PreparedStatement stmt = null;
116: ResultSet rs = null;
117: try {
118: List entities = new ArrayList();
119: con = EJBHelper.getConnection();
120:
121: String sql = "SELECT T." + pkField + " FROM " + table;
122: stmt = prepareStatement(con, sql, filters, ownerType,
123: pkField, sysAttrs, workflow);
124:
125: rs = stmt.executeQuery();
126: while (rs.next())
127: entities.add(rs.getString(1));
128: return entities;
129: } finally {
130: close(con, stmt, rs);
131: }
132: }
133: }
|