001: /*
002: * (C) Copyright 2004 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.portlet.mvcportlet.actionprocessor;
022:
023: import java.io.IOException;
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.SQLException;
027: import java.text.ParseException;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.portlet.PortletException;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
036: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
037: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
038: import com.nabhinc.portlet.mvcportlet.core.Constants;
039: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
040: import com.nabhinc.util.StringUtil;
041: import com.nabhinc.util.XMLUtil;
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.util.db.IDCreator;
046:
047: /**
048: * Inserts or updates a one to many mapping records.
049: * <ul>
050: * <li>sql - SQL used to update or insert a record.</li>
051: * </ul>
052: * Optional configuration parameters
053: * <ul>
054: * <li>params - Comma separated list of names of SQL parameters to be
055: * inserted. In general, these are the request parameter names. The
056: * following specialtokens are used to insert other values:
057: * <ul>
058: * <li>$userName - Current user name returned by request.getRemoteUser()</li>
059: * <li>$currentDate - Today's date</li>
060: * <li>$currentTimestamp - Current time</li>
061: * </ul>
062: * </li>
063: * <li>param-types - Comma separated list of SQL parameter types. This
064: * parameter must be specified if "params" are specified. Possible parameter
065: * types are: VARCHAR, INTEGER, DECIMAL, BOOLEAN, SMALLINT, DATE,
066: * TIME, TIMESTAMP, FLOAT, DOUBLE, ARRAY, BIGINT, BINARY, BIT,
067: * BLOB, CHAR, CLOB, LONGVARBINARY, LONGVARCHAR, JAVA_OBJECT
068: * </li>
069: * </ul>
070: *
071: * @author Padmanabh Dabke
072: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
073: */
074:
075: public class InsertUpdateOneToManyRelation extends BaseRequestProcessor
076: implements ActionProcessor {
077:
078: private String iuSQL = null;
079: private String[] iuParams = null;
080: private int[] iuParamTypes = null;
081:
082: private IDCreator iuIDCreator = null;
083:
084: /**
085: * Initialization.
086: * @param config XML configuration element
087: * @throws PortletException Thrown if sql parameter is not specified,
088: * "params" parameter is specified but corresponding "param-types"
089: * are not specified.
090: */
091: public void init(Element config, ControllerPortletConfig cpConfig)
092: throws PortletException {
093: super .init(config, cpConfig);
094: Element idCreatorElem = XMLUtil.getSubElement(config,
095: "id-creator");
096: if (idCreatorElem != null) {
097: try {
098: iuIDCreator = (IDCreator) Class.forName(
099: XMLUtil.getSubElementText(idCreatorElem,
100: "class")).newInstance();
101: iuIDCreator.init(idCreatorElem);
102: } catch (Exception ex) {
103: throw new PortletException(
104: "Failed to create ID creator.", ex);
105: }
106: }
107:
108: iuSQL = XMLUtil.getSubElementText(config, "sql");
109: if (iuSQL == null)
110: throw new PortletException("Missing required element: sql.");
111:
112: String params = XMLUtil.getSubElementText(config, "params");
113: if (params != null) {
114: iuParams = StringUtil.split(params, ",");
115: String paramTypes = XMLUtil.getSubElementText(config,
116: "param-types");
117: if (paramTypes == null) {
118: throw new PortletException(
119: "You must specify param-types if you specify params.");
120: }
121: String[] typeArray = StringUtil.split(paramTypes, ",");
122: if (typeArray.length != iuParams.length) {
123: throw new PortletException(
124: "Number of param-types must be equal to number of params.");
125: }
126: iuParamTypes = new int[typeArray.length];
127: for (int i = 0; i < typeArray.length; i++) {
128: try {
129: iuParamTypes[i] = DBConfigUtil
130: .getSQLType(typeArray[i]);
131: } catch (Exception ex) {
132: throw new PortletException(
133: "Failed to parse sql type.", ex);
134: }
135: }
136: }
137: }
138:
139: /**
140: * Inserts or updates a database record<p>
141: * @param request Action request
142: * @param response Action response
143: * @param config Action config
144: * @return "success"
145: * @throws PortletException
146: * @throws IOException
147: */
148: public String process(ActionRequest request,
149: ActionResponse response, ActionConfig config)
150: throws PortletException, IOException {
151:
152: Connection conn = null;
153: PreparedStatement st = null;
154:
155: try {
156: conn = brpConfig.getDataSource().getConnection();
157: conn.setAutoCommit(false);
158:
159: int id = -1;
160: Integer idObj = (Integer) request
161: .getAttribute(Constants.RECORD_ID_ATTRIB);
162: if (idObj == null) {
163: if (iuIDCreator != null) {
164: id = iuIDCreator.getNextID(conn);
165: request.setAttribute(Constants.RECORD_ID_ATTRIB,
166: new Integer(id));
167: }
168: } else {
169: id = idObj.intValue();
170: }
171:
172: st = conn.prepareStatement(iuSQL);
173: //Form form = config.getForm();
174: DBParamUtil.setSQLParam(st, 0, iuParams[0],
175: iuParamTypes[0], request, id);
176: String[] values = request.getParameterValues(iuParams[1]);
177: if (values != null) {
178: for (int i = 0; i < values.length; i++) {
179: st.setString(2, values[i]);
180: st.execute();
181: }
182: }
183:
184: conn.commit();
185: return "success";
186:
187: } catch (SQLException sqe) {
188: try {
189: conn.rollback();
190: } catch (SQLException se) {
191: }
192: throw new PortletException("Database exception.", sqe);
193: } catch (ParseException pex) {
194: try {
195: conn.rollback();
196: } catch (SQLException se) {
197: }
198: throw new PortletException("Malformed request parameter.",
199: pex);
200: } catch (Throwable e) {
201: try {
202: conn.rollback();
203: } catch (SQLException se) {
204: }
205: throw new PortletException("Unknown exception.", e);
206: } finally {
207: DBUtil.close(st);
208: DBUtil.close(conn);
209: }
210: }
211: }
|