001: /*
002: * (C) Copyright 2006 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.condition;
022:
023: import java.sql.Connection;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027:
028: import javax.naming.NamingException;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletRequest;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.sql.DataSource;
034: import javax.xml.bind.Unmarshaller;
035: import javax.xml.bind.annotation.XmlElement;
036: import javax.xml.bind.annotation.XmlRootElement;
037: import javax.xml.bind.annotation.XmlTransient;
038:
039: import com.nabhinc.core.Defaults;
040: import com.nabhinc.core.WebServiceRequest;
041: import com.nabhinc.util.StringUtil;
042: import com.nabhinc.util.db.DBConfigUtil;
043: import com.nabhinc.util.db.DBParamUtil;
044: import com.nabhinc.util.db.DBUtil;
045: import com.nabhinc.ws.core.WebServiceException;
046:
047: /**
048: *
049: *
050: * @author Padmanabh Dabke
051: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
052: */
053: @XmlRootElement(name="sql-condition")
054: public class SQLCondition extends BaseCondition implements Condition {
055:
056: private static final long serialVersionUID = 1963352704753027576L;
057:
058: @XmlElement(name="data-source")
059: private String rpConfiguredDataSource = null;
060: private transient DataSource rpDataSource = null;
061:
062: @XmlElement(name="sql")
063: private String rpSQL = null;
064:
065: @XmlElement(name="params")
066: private String rpParamStr = null;
067:
068: @XmlElement(name="param-types")
069: private String rpParamTypeStr = null;
070:
071: @XmlTransient
072: private String[] rpParams = null;
073:
074: @XmlTransient
075: private int[] rpParamTypes = null;
076:
077: public SQLCondition() {
078: super ();
079: }
080:
081: public SQLCondition(String sql) throws NamingException {
082: rpDataSource = DBUtil.getDataSource(Defaults
083: .getDataSourceName());
084: rpSQL = sql;
085: }
086:
087: /**
088: * Initialize this object from an XML Element.
089: * @param config XML element specifying initialization properties
090: * @exception java.lang.Exception Implementation specific exception
091: */
092: public void afterUnmarshal(Unmarshaller um, Object parent)
093: throws IllegalArgumentException, NamingException {
094:
095: if (rpConfiguredDataSource == null) {
096: rpDataSource = DBUtil.getDataSource(Defaults
097: .getDataSourceName());
098: } else {
099: rpDataSource = DBUtil.getDataSource(rpConfiguredDataSource);
100: }
101: if (rpSQL == null)
102: throw new IllegalArgumentException(
103: "Missing required element <sql>.");
104:
105: if (rpParamStr != null) {
106: rpParams = StringUtil.split(rpParamStr, ",");
107: if (rpParamTypeStr == null) {
108: throw new IllegalArgumentException(
109: "You must specify param-types if you specify params.");
110: } else {
111: String[] typeArray = StringUtil.split(rpParamTypeStr,
112: ",");
113: if (typeArray.length != rpParams.length) {
114: throw new IllegalArgumentException(
115: "Number of param-types must be equal to number of params.");
116: } else {
117: rpParamTypes = new int[typeArray.length];
118: for (int i = 0; i < typeArray.length; i++) {
119: rpParamTypes[i] = DBConfigUtil
120: .getSQLType(typeArray[i]);
121: }
122: }
123: }
124: }
125: }
126:
127: /**
128: * @see com.nabhinc.rules.Precondition#isSatisfied(javax.servlet.http.HttpServletRequest)
129: */
130: public boolean isSatisfied(HttpServletRequest req, Object target)
131: throws ServletException {
132:
133: Connection conn = null;
134: PreparedStatement st = null;
135: ResultSet results = null;
136: try {
137: conn = rpDataSource.getConnection();
138: st = conn.prepareStatement(rpSQL);
139: // int id = IDCreationUtil.getRecordID(null, conn, req);
140: int id = -1;
141: if (rpParams != null) {
142: for (int i = 0; i < rpParams.length; i++) {
143: DBParamUtil.setSQLParam(st, i, rpParams[i],
144: rpParamTypes[i], req, id);
145: }
146: }
147: results = st.executeQuery();
148: if (results.next()) {
149: return true;
150: } else {
151: return false;
152: }
153:
154: } catch (SQLException sqe) {
155: try {
156: conn.rollback();
157: } catch (SQLException se) {
158: }
159: throw new ServletException("Database exception.", sqe);
160: } catch (Throwable e) {
161: try {
162: conn.rollback();
163: } catch (SQLException se) {
164: }
165: throw new ServletException("Unknown exception.", e);
166: } finally {
167: DBUtil.close(results);
168: DBUtil.close(st);
169: DBUtil.close(conn);
170: }
171:
172: }
173:
174: public boolean isSatisfied(PortletRequest req, Object target)
175: throws PortletException {
176:
177: Connection conn = null;
178: PreparedStatement st = null;
179: ResultSet results = null;
180: try {
181: conn = rpDataSource.getConnection();
182: st = conn.prepareStatement(rpSQL);
183: // int id = IDCreationUtil.getRecordID(null, conn, req);
184: int id = -1;
185: if (rpParams != null) {
186: for (int i = 0; i < rpParams.length; i++) {
187: DBParamUtil.setSQLParam(st, i, rpParams[i],
188: rpParamTypes[i], req, id);
189: }
190: }
191: results = st.executeQuery();
192: if (results.next()) {
193: return true;
194: } else {
195: return false;
196: }
197:
198: } catch (SQLException sqe) {
199: try {
200: conn.rollback();
201: } catch (SQLException se) {
202: }
203: throw new PortletException("Database exception.", sqe);
204: } catch (Throwable e) {
205: try {
206: conn.rollback();
207: } catch (SQLException se) {
208: }
209: throw new PortletException("Unknown exception.", e);
210: } finally {
211: DBUtil.close(results);
212: DBUtil.close(st);
213: DBUtil.close(conn);
214: }
215:
216: }
217:
218: public boolean isSatisfied(WebServiceRequest req, Object target)
219: throws WebServiceException {
220:
221: Connection conn = null;
222: PreparedStatement st = null;
223: ResultSet results = null;
224: try {
225: conn = rpDataSource.getConnection();
226: st = conn.prepareStatement(rpSQL);
227: // int id = IDCreationUtil.getRecordID(null, conn, req);
228: int id = -1;
229: if (rpParams != null) {
230: for (int i = 0; i < rpParams.length; i++) {
231: DBParamUtil.setSQLParam(st, i, rpParams[i],
232: rpParamTypes[i], req, id);
233: }
234: }
235: results = st.executeQuery();
236: if (results.next()) {
237: return true;
238: } else {
239: return false;
240: }
241:
242: } catch (SQLException sqe) {
243: try {
244: conn.rollback();
245: } catch (SQLException se) {
246: }
247: throw new WebServiceException("Database exception.", sqe);
248: } catch (Throwable e) {
249: try {
250: conn.rollback();
251: } catch (SQLException se) {
252: }
253: throw new WebServiceException("Unknown exception.", e);
254: } finally {
255: DBUtil.close(results);
256: DBUtil.close(st);
257: DBUtil.close(conn);
258: }
259:
260: }
261:
262: @XmlTransient
263: public String getSQL() {
264: return rpSQL;
265: }
266:
267: public void setSQL(String s) {
268: this .rpSQL = s;
269: }
270:
271: @XmlTransient
272: public String getDataSourceName() {
273: return this .rpConfiguredDataSource;
274: }
275:
276: public void setDataSourceName(String s) {
277: this .rpConfiguredDataSource = s;
278: }
279:
280: @XmlTransient
281: public String getParamStr() {
282: return rpParamStr;
283: }
284:
285: public void setParamStr(String s) {
286: this .rpParamStr = s;
287: }
288:
289: @XmlTransient
290: public String getParamTypeStr() {
291: return rpParamStr;
292: }
293:
294: public void setParamTypeStr(String s) {
295: this.rpParamTypeStr = s;
296: }
297:
298: }
|