001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.oracleportlet.util;
039:
040: import java.util.*;
041: import java.io.IOException;
042: import java.io.Writer;
043: import java.sql.*;
044: import java.util.logging.*;
045:
046: import javax.portlet.*;
047: import javax.servlet.http.HttpServletRequest;
048:
049: /**
050: * This is a utility class that contains methods used by Oracle Receipts portlet
051: *
052: * @author Zahid Syed
053: */
054:
055: public class ReceiptQueryUtils implements OraclePortletConstants {
056:
057: private static Logger logger = OraclePortletLogger.getLogger();
058:
059: /**
060: * This method is used to obtain records pertaining to summary of receipts, as a Vector
061: **/
062:
063: public static Vector getReceiptSummaryRecords(
064: HttpServletRequest httpReq, String numberOfRecords) {
065:
066: Vector records = new Vector();
067: Connection conn = null;
068: Statement stmt = null;
069: ResultSet rset = null;
070:
071: try {
072: conn = (Connection) OracleConnectionUtils
073: .getOracleConnection(httpReq);
074:
075: if (conn == null) {
076: logMessage(Level.SEVERE,
077: "getReceiptSummaryRecords : Could not obtain connection :");
078: return null;
079: } else {
080: stmt = conn.createStatement();
081: rset = stmt.executeQuery(RECEIPT_SUMMARY_QUERY
082: + numberOfRecords);
083:
084: while (rset.next()) {
085: ReceiptsBean rb = new ReceiptsBean();
086:
087: rb.setReceiptNumber(rset.getString(1));
088: rb.setReceiptDate(rset.getString(2));
089: rb.setAmount(rset.getString(3));
090: rb.setCurrency(rset.getString(4));
091: rb.setStatus(rset.getString(5));
092:
093: records.addElement(rb);
094:
095: }
096:
097: conn.close();
098: return records;
099: }
100:
101: } catch (Exception ex) {
102:
103: logMessage(Level.SEVERE,
104: "getReceiptSummaryRecords : query execution and reading resultset :"
105: + ex);
106: return null;
107: }
108: }
109:
110: /**
111: * This method is used to obtain details related to a receipt, from the result set
112: **/
113:
114: public static ReceiptDetailsBean getReceiptDetails(ResultSet rset) {
115:
116: ReceiptDetailsBean rdb = new ReceiptDetailsBean();
117:
118: try {
119: while (rset.next()) {
120: rdb.setReceiptNumber(rset.getString(1));
121: rdb.setReceiptDate(rset.getString(2));
122: rdb.setAmount(rset.getString(3));
123: rdb.setCurrency(rset.getString(4));
124: rdb.setReceiptType(rset.getString(5));
125: rdb.setPaymentMethod(rset.getString(6));
126: rdb.setCustomerName(rset.getString(7));
127: rdb.setRemitAccount(rset.getString(8));
128:
129: }
130: } catch (Exception ex) {
131:
132: logMessage(Level.SEVERE,
133: "getReceiptDetails : reading resultset :" + ex);
134: }
135: return rdb;
136:
137: }
138:
139: /**
140: * This method retrieves the details of a receipt based on receipt no. and amount, and dispatches the
141: * control to the relevant JSP, to render the details
142: **/
143: public static void appendReceiptDetails(String queryValue1,
144: String queryValue2, RenderRequest request,
145: RenderResponse response, PortletSession session,
146: HttpServletRequest httpReq) throws PortletException,
147: IOException {
148:
149: ReceiptDetailsBean rdb = new ReceiptDetailsBean();
150: PortletContext ctx = session.getPortletContext();
151: PortletRequestDispatcher reqDisp = null;
152:
153: Connection conn = null;
154: ResultSet rset = null;
155: Statement stmt = null;
156:
157: try {
158:
159: conn = (Connection) OracleConnectionUtils
160: .getOracleConnection(httpReq);
161: if (conn == null) {
162: logMessage(Level.SEVERE,
163: "appendReceiptDetails : Could not obtain connection :");
164: // Dispatch the request to a JSP that displays error
165: reqDisp = ctx.getRequestDispatcher(NO_RECORDS_JSP);
166: reqDisp.include(request, response);
167: } else {
168: stmt = conn.createStatement();
169:
170: // Use the values "queryValue1" and "queryValue2" in the query
171: rset = stmt.executeQuery(RECEIPT_DETAILS_QUERY
172: + RECEIPT_DETAILS_QC1 + queryValue1
173: + RECEIPT_DETAILS_QC2 + queryValue2);
174:
175: // Obtain the details in the form of ReceiptDetailsBean by passing the result set
176: //to a utility method
177: rdb = (ReceiptDetailsBean) getReceiptDetails(rset);
178:
179: // Set the details record as an attribute before dispatching to the JSP
180: request.setAttribute("ReceiptDetails", rdb);
181:
182: conn.close();
183:
184: reqDisp = ctx.getRequestDispatcher(RECEIPT_DETAILS_JSP);
185: reqDisp.include(request, response);
186:
187: }
188:
189: } catch (Exception ex) {
190:
191: logMessage(Level.SEVERE,
192: "appendReceiptDetails : Query execution and dispatch to JSP :"
193: + ex);
194:
195: // Dispatch the request to a JSP that displays error
196: reqDisp = ctx.getRequestDispatcher(NO_DETAILS_JSP);
197: reqDisp.include(request, response);
198: }
199: }
200:
201: /**
202: * This method returns the number of records to display, by reading a properties file
203: **/
204: public static String getNumberOfRecordsToDisplay() {
205:
206: int numberOfRecords = 0;
207: int defaultNumberOfRecords = 15;
208: ResourceBundle rbundle = null;
209:
210: try {
211: rbundle = ResourceBundle.getBundle("oracleconfig");
212: } catch (MissingResourceException excp) {
213: logMessage(Level.WARNING,
214: "getNumberOfRecordsToDisplay : File oracleconfig.properties NOT found : "
215: + excp);
216: return (Integer.toString(defaultNumberOfRecords));
217: }
218:
219: String numberOfRecordsStr = rbundle
220: .getString(NUMBER_OF_RECEIPT_RECORDS);
221: try {
222: numberOfRecords = Integer.parseInt(numberOfRecordsStr);
223: } catch (Exception ex) {
224: logMessage(Level.INFO,
225: "getNumberOfRecordsToDisplay : Could not parse the property value : "
226: + ex);
227: return (Integer.toString(defaultNumberOfRecords));
228: }
229:
230: if (numberOfRecords < 1) {
231: numberOfRecords = defaultNumberOfRecords;
232: }
233: return (Integer.toString(numberOfRecords));
234: }
235:
236: /**
237: * This method logs the debug messages
238: **/
239: private static void logMessage(Object debugLevel, String msg) {
240:
241: logger.log((Level) debugLevel, msg);
242: }
243:
244: }
|