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 Revenue Forecasts 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 SiebelForecastRequestUtils {
062:
063: // Name of the Siebel business object from where the Revenue Forecasts data
064: // should be obtained.
065: static String businessMethod = "<Siebel location=\"S/BO/Revenue/Revenue/query\">";
066: static Logger logger = SiebelLogger.getLogger();
067:
068: // Fields that should be obtained in the result set
069: public static final String[] queryFields = { "Date", "Sales Rep",
070: "Committed", "Account", "Opportunity", "Product",
071: "Quantity", "Price", "Revenue", "Sales Stage",
072: "Win Probability", "Functional Upside",
073: "Functional Downside", "Comments" };
074:
075: static Hashtable forecastsCache = new Hashtable();
076:
077: /**
078: * This method returns the XML query to get the Siebel Revenue Forecasts
079: * Details
080: */
081: private static String getSiebelForecastDetailsQuery(String query) {
082:
083: StringBuffer qBuf = new StringBuffer();
084: qBuf.append(businessMethod);
085: qBuf.append("<select>");
086: qBuf.append(query);
087: qBuf.append("</select>");
088:
089: for (int i = 0; i < queryFields.length; i++) {
090: qBuf.append("<field>");
091: qBuf.append(queryFields[i]);
092: qBuf.append("</field>");
093: }
094: qBuf.append("</Siebel>");
095: return qBuf.toString();
096: }
097:
098: /**
099: * This method will handle the Revenue Forecast Details Tab request
100: **/
101: public static void handleForecastsRequest(String query,
102: PortletSession session, RenderRequest request,
103: RenderResponse response) {
104: PortletContext ctx = session.getPortletContext();
105: PortletRequestDispatcher reqDisp = null;
106: Vector records = null;
107: String sessionID = session.getId();
108:
109: String sortOrder = request.getParameter("SortOrder");
110: String sortColumn = request.getParameter("SortColumn");
111: String sortElementType = request
112: .getParameter("SortElementType");
113: String reqType = request.getParameter("RequestType");
114:
115: if (sortOrder == null) {
116: sortOrder = "DOWN";
117: }
118:
119: if (sortColumn == null) {
120: sortColumn = "Account";
121: }
122: debug("handleForecastsRequest()", "sortOrder " + sortOrder);
123: debug("handleForecastsRequest()", "sortColumn " + sortColumn);
124: debug("handleForecastsRequest()", "reqType " + reqType);
125:
126: try {
127:
128: if (query != null) {
129:
130: if ((reqType != null) && (reqType.equals("Paginate"))) {
131: String queryField = request
132: .getParameter("QueryField");
133: String queryValue = request
134: .getParameter("QueryValue");
135: String tableSize = request
136: .getParameter("TableSize");
137: if (queryField.equals("goTo")) {
138: queryValue = SiebelAccountRequestUtils
139: .checkGoToValue(queryValue, tableSize);
140: }
141: request.setAttribute("CurrentPage", queryValue);
142: query = request.getParameter("CurrentQuery");
143: setAccountID(query, request);
144: }
145: String curPage = (String) request
146: .getAttribute("CurrentPage");
147:
148: if (curPage == null) {
149: curPage = "1";
150: }
151: request.setAttribute("CurrentForecastPage", curPage);
152: debug("handleForecastsRequest()", "Query is : " + query);
153: records = getCachedForecastsData(sessionID, query,
154: request);
155:
156: if (records == null) {
157: String reqStr = getSiebelForecastDetailsQuery(query);
158: String xmlOutput = SiebelIwayUtils
159: .getResponseString(reqStr);
160: records = SiebelResponseUtils
161: .getForecastDetails(xmlOutput);
162: debug("handleForecastsRequest()", "Got Response ");
163: debug("handleForecastsRequest()", "Sorting ");
164: Hashtable sortingParams = new Hashtable();
165: sortingParams.put("SortOrder", sortOrder);
166: sortingParams.put("SortColumn", sortColumn);
167: if (sortElementType != null) {
168: sortingParams.put("SortElementType",
169: sortElementType);
170: }
171: records = TableSorting.sortTable(records,
172: sortingParams);
173: int rowsToShow = getRowsToShow(request);
174: request.setAttribute("CurrentPage", "1");
175: request.setAttribute("CurrentForecastPage", "1");
176: putCachedForecastsData(rowsToShow, sessionID,
177: query, records);
178: records = getCachedForecastsData(sessionID, query,
179: request);
180: }
181: if ((reqType != null) && (reqType.equals("Sort"))) {
182: debug("handleForecastsRequest()",
183: "Sorting due to Request ");
184: Hashtable sortingParams = new Hashtable();
185: sortingParams.put("SortOrder", sortOrder);
186: sortingParams.put("SortColumn", sortColumn);
187: if (sortElementType != null) {
188: sortingParams.put("SortElementType",
189: sortElementType);
190: }
191: Hashtable sessionData = (Hashtable) forecastsCache
192: .get(sessionID);
193: Vector responseData = SiebelAccountRequestUtils
194: .getMergedCacheData(sessionData, query);
195: records = TableSorting.sortTable(responseData,
196: sortingParams);
197: int rowsToShow = getRowsToShow(request);
198: request.setAttribute("CurrentPage", "1");
199: request.setAttribute("CurrentForecastPage", "1");
200: putCachedForecastsData(rowsToShow, sessionID,
201: query, records);
202: records = getCachedForecastsData(sessionID, query,
203: request);
204: //records = TableSorting.sortTable(records, sortingParams);
205: }
206: Hashtable sessionData = (Hashtable) forecastsCache
207: .get(sessionID);
208: Vector pages = (Vector) sessionData.get(query);
209: int size = pages.size();
210: TablePagination.setPaginationRow(request, response,
211: "PaginateForecast", query, size);
212: request.setAttribute("SortOrder", sortOrder);
213: request.setAttribute("SortColumn", sortColumn);
214: request.setAttribute("SiebelForecasts", records);
215: }
216: reqDisp = ctx
217: .getRequestDispatcher("/jsps/SiebelForecastDetails.jsp");
218:
219: reqDisp.include(request, response);
220: } catch (Exception ex) {
221: debug("handleForecastsRequest()", "dispatchRequest Exp : "
222: + ex);
223: }
224: }
225:
226: private static void putCachedForecastsData(int rowsToShow,
227: String sessionID, String query, Vector records) {
228:
229: Hashtable sessionData = (Hashtable) forecastsCache
230: .get(sessionID);
231:
232: if (sessionData == null) {
233: sessionData = new Hashtable();
234: }
235: Vector pages = SiebelAccountRequestUtils.setPageCacheData(
236: records, sessionID, rowsToShow);
237: debug("putCachedForecastsData()", "pages size : "
238: + pages.size());
239: sessionData.put(query, pages);
240: forecastsCache.put(sessionID, sessionData);
241: }
242:
243: private static Vector getCachedForecastsData(String sessionID,
244: String query, RenderRequest request) {
245:
246: Hashtable sessionData = (Hashtable) forecastsCache
247: .get(sessionID);
248: Vector records = null;
249:
250: if (sessionData != null) {
251: debug("getCachedForecastsData()", "Getting Cached Data : ");
252: records = SiebelAccountRequestUtils.getPageCacheData(
253: request, sessionData, "CurrentForecastPage", query);
254: debug("getCachedForecastsData()", "Got Cached Data : ");
255: }
256: return records;
257: }
258:
259: public static void resetCache(String sessionID) {
260: forecastsCache.remove(sessionID);
261: }
262:
263: private static int getRowsToShow(RenderRequest request) {
264: PortletPreferences prefs = request.getPreferences();
265: String row = prefs.getValue("ForecastRowPreference",
266: SiebelConfigUtils.defaultForecastRows);
267: int retVal = 0;
268:
269: try {
270: retVal = Integer.parseInt(row);
271: } catch (Exception ex) {
272: retVal = Integer
273: .parseInt(SiebelConfigUtils.defaultForecastRows);
274: }
275: if (retVal == 0) {
276: retVal = Integer
277: .parseInt(SiebelConfigUtils.defaultForecastRows);
278: }
279: return retVal;
280: }
281:
282: private static void setAccountID(String query, RenderRequest request) {
283:
284: int index = (new String("<Account_spcId>")).length();
285: String accId = query.substring(index, query
286: .indexOf("</Account_spcId>"));
287: debug("setAccountID()", "accId : " + accId);
288: request.setAttribute("AccountID", accId);
289: }
290:
291: /**
292: * This method will print the log the messages.
293: **/
294: private static void debug(String methodName, String msg) {
295: logger.log(Level.INFO,
296: "com.sun.portal.siebelportlet.util.SiebelForecastRequestUtils:"
297: + methodName + ":" + msg);
298: }
299:
300: }
|