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: package com.sun.portal.siebelportlet.util;
038:
039: import java.util.Enumeration;
040: import java.util.Vector;
041: import java.util.Hashtable;
042: import java.io.IOException;
043: import java.io.Writer;
044: import java.util.logging.*;
045:
046: import javax.portlet.*;
047:
048: import com.sun.portal.iwayutil.sorting.*;
049: import com.sun.portal.iwayutil.config.*;
050:
051: /**
052: *
053: * This class provided the utility methods to get the Invoices related
054: * information from Siebel Server.
055: *
056: * @version 1.0
057: * @author Deepak H P
058: * @date 25 Feb 2005
059: *
060: **/
061: public class SiebelInvoiceRequestUtils {
062:
063: // Name of the Siebel business object from where the Invoices data
064: // should be obtained.
065: static String businessMethod = "<Siebel location=\"S/BO/FS Invoice/FS Invoice/query\">";
066:
067: static Logger logger = SiebelLogger.getLogger();
068:
069: // Fields that should be obtained in the result set
070: public static final String[] queryFields = { "Invoice Number",
071: "Invoice Date", "Total Amount", "Total Paid", "Total Due",
072: "Status", "Currency Code" };
073:
074: static Hashtable invoicesCache = new Hashtable();
075:
076: /**
077: * This method returns the XML query to get the Siebel Invoices Details
078: */
079: private static String getSiebelInvoiceDetailsQuery(String query) {
080:
081: StringBuffer qBuf = new StringBuffer();
082: qBuf.append(businessMethod);
083: qBuf.append("<select>");
084: qBuf.append(query);
085: qBuf.append("</select>");
086:
087: for (int i = 0; i < queryFields.length; i++) {
088: qBuf.append("<field>");
089: qBuf.append(queryFields[i]);
090: qBuf.append("</field>");
091: }
092: qBuf.append("</Siebel>");
093: return qBuf.toString();
094: }
095:
096: /**
097: * This method will handle the Invoice Details Tab request
098: **/
099: public static void handleInvoicesRequest(String query,
100: PortletSession session, RenderRequest request,
101: RenderResponse response) {
102: PortletContext ctx = session.getPortletContext();
103: PortletRequestDispatcher reqDisp = null;
104: Vector records = null;
105: String sessionID = session.getId();
106:
107: String sortOrder = request.getParameter("SortOrder");
108: String sortColumn = request.getParameter("SortColumn");
109: String sortElementType = request
110: .getParameter("SortElementType");
111: String reqType = request.getParameter("RequestType");
112:
113: if (sortOrder == null) {
114: sortOrder = "DOWN";
115: }
116:
117: if (sortColumn == null) {
118: sortColumn = "Invoice_spcNumber";
119: }
120: debug("handleInvoicesRequest()", "sortOrder " + sortOrder);
121: debug("handleInvoicesRequest()", "sortColumn " + sortColumn);
122: debug("handleInvoicesRequest()", "reqType " + reqType);
123:
124: try {
125:
126: if (query != null) {
127:
128: if ((reqType != null) && (reqType.equals("Paginate"))) {
129: String queryField = request
130: .getParameter("QueryField");
131: String queryValue = request
132: .getParameter("QueryValue");
133: String tableSize = request
134: .getParameter("TableSize");
135: if (queryField.equals("goTo")) {
136: queryValue = SiebelAccountRequestUtils
137: .checkGoToValue(queryValue, tableSize);
138: }
139: request.setAttribute("CurrentPage", queryValue);
140: query = request.getParameter("CurrentQuery");
141: setAccountID(query, request);
142: }
143: String curPage = (String) request
144: .getAttribute("CurrentPage");
145:
146: if (curPage == null) {
147: curPage = "1";
148: }
149: request.setAttribute("CurrentInvoicePage", curPage);
150: debug("handleInvoicesRequest()", "Query is : " + query);
151: records = getCachedInvoicesData(sessionID, query,
152: request);
153:
154: if (records == null) {
155: String reqStr = getSiebelInvoiceDetailsQuery(query);
156: String xmlOutput = SiebelIwayUtils
157: .getResponseString(reqStr);
158: records = SiebelResponseUtils
159: .getInvoiceDetails(xmlOutput);
160: debug("handleInvoicesRequest()", "Got Response ");
161: debug("handleInvoicesRequest()", "Sorting ");
162: Hashtable sortingParams = new Hashtable();
163: sortingParams.put("SortOrder", sortOrder);
164: sortingParams.put("SortColumn", sortColumn);
165: if (sortElementType != null) {
166: sortingParams.put("SortElementType",
167: sortElementType);
168: }
169: records = TableSorting.sortTable(records,
170: sortingParams);
171: int rowsToShow = getRowsToShow(request);
172: request.setAttribute("CurrentPage", "1");
173: request.setAttribute("CurrentInvoicePage", "1");
174: putCachedInvoicesData(rowsToShow, sessionID, query,
175: records);
176: records = getCachedInvoicesData(sessionID, query,
177: request);
178: }
179: if ((reqType != null) && (reqType.equals("Sort"))) {
180: debug("handleInvoicesRequest()",
181: "Sorting due to Request ");
182: Hashtable sortingParams = new Hashtable();
183: sortingParams.put("SortOrder", sortOrder);
184: sortingParams.put("SortColumn", sortColumn);
185: if (sortElementType != null) {
186: sortingParams.put("SortElementType",
187: sortElementType);
188: }
189: Hashtable sessionData = (Hashtable) invoicesCache
190: .get(sessionID);
191: Vector responseData = SiebelAccountRequestUtils
192: .getMergedCacheData(sessionData, query);
193: records = TableSorting.sortTable(responseData,
194: sortingParams);
195: int rowsToShow = getRowsToShow(request);
196: request.setAttribute("CurrentPage", "1");
197: request.setAttribute("CurrentForecastPage", "1");
198: putCachedInvoicesData(rowsToShow, sessionID, query,
199: records);
200: records = getCachedInvoicesData(sessionID, query,
201: request);
202: //records = TableSorting.sortTable(records, sortingParams);
203: }
204: Hashtable sessionData = (Hashtable) invoicesCache
205: .get(sessionID);
206: Vector pages = (Vector) sessionData.get(query);
207: int size = pages.size();
208: TablePagination.setPaginationRow(request, response,
209: "PaginateInvoice", query, size);
210: request.setAttribute("SortOrder", sortOrder);
211: request.setAttribute("SortColumn", sortColumn);
212: request.setAttribute("SiebelInvoices", records);
213: }
214: reqDisp = ctx
215: .getRequestDispatcher("/jsps/SiebelInvoiceDetails.jsp");
216:
217: reqDisp.include(request, response);
218: } catch (Exception ex) {
219: debug("handleInvoicesRequest()", "dispatchRequest Exp : "
220: + ex);
221: }
222: }
223:
224: private static void putCachedInvoicesData(int rowsToShow,
225: String sessionID, String query, Vector records) {
226:
227: Hashtable sessionData = (Hashtable) invoicesCache
228: .get(sessionID);
229:
230: if (sessionData == null) {
231: sessionData = new Hashtable();
232: }
233: Vector pages = SiebelAccountRequestUtils.setPageCacheData(
234: records, sessionID, rowsToShow);
235: debug("putCachedInvoicesData()", "pages size : "
236: + pages.size());
237: sessionData.put(query, pages);
238: invoicesCache.put(sessionID, sessionData);
239: }
240:
241: private static Vector getCachedInvoicesData(String sessionID,
242: String query, RenderRequest request) {
243:
244: Hashtable sessionData = (Hashtable) invoicesCache
245: .get(sessionID);
246: Vector records = null;
247:
248: if (sessionData != null) {
249: debug("getCachedInvoicesData()", "Getting Cached Data : ");
250: records = SiebelAccountRequestUtils.getPageCacheData(
251: request, sessionData, "CurrentInvoicePage", query);
252: debug("getCachedInvoicesData()", "Got Cached Data : ");
253: }
254: return records;
255: }
256:
257: public static void resetCache(String sessionID) {
258: invoicesCache.remove(sessionID);
259: }
260:
261: private static int getRowsToShow(RenderRequest request) {
262: PortletPreferences prefs = request.getPreferences();
263: String row = prefs.getValue("InvoiceRowPreference",
264: SiebelConfigUtils.defaultInvoiceRows);
265: int retVal = 0;
266:
267: try {
268: retVal = Integer.parseInt(row);
269: } catch (Exception ex) {
270: retVal = Integer
271: .parseInt(SiebelConfigUtils.defaultInvoiceRows);
272: }
273: if (retVal == 0) {
274: retVal = Integer
275: .parseInt(SiebelConfigUtils.defaultInvoiceRows);
276: }
277: return retVal;
278: }
279:
280: private static void setAccountID(String query, RenderRequest request) {
281:
282: int index = (new String("<Account_spcId>")).length();
283: String accId = query.substring(index, query
284: .indexOf("</Account_spcId>"));
285: debug("setAccountID()", "accId : " + accId);
286: request.setAttribute("AccountID", accId);
287: }
288:
289: /**
290: * This method will print the log the messages.
291: **/
292: private static void debug(String methodName, String msg) {
293: logger.log(Level.INFO,
294: "com.sun.portal.siebelportlet.util.SiebelInvoiceRequestUtils:"
295: + methodName + ":" + msg);
296: }
297:
298: }
|