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