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 Activity 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 SiebelActivityRequestUtils {
062:
063: // Name of the Siebel business object from where the Activity data
064: // should be obtained.
065: static String businessMethod = "<Siebel location=\"S/BO/Calendar/Calendar Activity/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 = { "Type", "Description",
071: "Status", "Start Date" };
072:
073: static Hashtable activitiesCache = new Hashtable();
074:
075: /**
076: * This method returns the XML query to get the Siebel Activity Details
077: */
078: private static String getSiebelActivityDetailsQuery(String query) {
079:
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 Activity Details Tab request
097: **/
098: public static void handleActivitiesRequest(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 reqType = request.getParameter("RequestType");
109:
110: if (sortOrder == null) {
111: sortOrder = "DOWN";
112: }
113:
114: if (sortColumn == null) {
115: sortColumn = "Type";
116: }
117: debug("handleActivitiesRequest()", "reqType " + reqType);
118: debug("handleActivitiesRequest()", "sortOrder " + sortOrder);
119: debug("handleActivitiesRequest()", "sortColumn " + sortColumn);
120:
121: try {
122:
123: if (query != null) {
124:
125: if ((reqType != null) && (reqType.equals("Paginate"))) {
126: String queryField = request
127: .getParameter("QueryField");
128: String queryValue = request
129: .getParameter("QueryValue");
130: String tableSize = request
131: .getParameter("TableSize");
132: if (queryField.equals("goTo")) {
133: queryValue = SiebelAccountRequestUtils
134: .checkGoToValue(queryValue, tableSize);
135: }
136: request.setAttribute("CurrentPage", queryValue);
137: query = request.getParameter("CurrentQuery");
138: setAccountID(query, request);
139: }
140: String curPage = (String) request
141: .getAttribute("CurrentPage");
142:
143: if (curPage == null) {
144: curPage = "1";
145: }
146: request.setAttribute("CurrentActivityPage", curPage);
147: debug("handleActivitiesRequest()", "Query is : "
148: + query);
149:
150: records = getCachedActivityData(sessionID, query,
151: request);
152:
153: if (records == null) {
154: String reqStr = getSiebelActivityDetailsQuery(query);
155: String xmlOutput = SiebelIwayUtils
156: .getResponseString(reqStr);
157: records = SiebelResponseUtils
158: .getActivityDetails(xmlOutput);
159: debug("handleActivitiesRequest()", "Got Response ");
160: debug("handleActivitiesRequest()", "Sorting ");
161: records = TableSorting.sortTable(records,
162: sortColumn, sortOrder);
163: int rowsToShow = getRowsToShow(request);
164: request.setAttribute("CurrentPage", "1");
165: request.setAttribute("CurrentActivityPage", "1");
166: putCachedActivityData(rowsToShow, sessionID, query,
167: records);
168: records = getCachedActivityData(sessionID, query,
169: request);
170: }
171: if ((reqType != null) && (reqType.equals("Sort"))) {
172: debug("handleActivitiesRequest()",
173: "Sorting due to Request ");
174: Hashtable sessionData = (Hashtable) activitiesCache
175: .get(sessionID);
176: Vector responseData = SiebelAccountRequestUtils
177: .getMergedCacheData(sessionData, query);
178: records = TableSorting.sortTable(responseData,
179: sortColumn, sortOrder);
180: int rowsToShow = getRowsToShow(request);
181: request.setAttribute("CurrentPage", "1");
182: request.setAttribute("CurrentActivityPage", "1");
183: putCachedActivityData(rowsToShow, sessionID, query,
184: records);
185: records = getCachedActivityData(sessionID, query,
186: request);
187: //records = TableSorting.sortTable(records, sortColumn, sortOrder);
188: }
189: Hashtable sessionData = (Hashtable) activitiesCache
190: .get(sessionID);
191: Vector pages = (Vector) sessionData.get(query);
192: int size = pages.size();
193: TablePagination.setPaginationRow(request, response,
194: "PaginateActivity", query, size);
195: request.setAttribute("SortOrder", sortOrder);
196: request.setAttribute("SortColumn", sortColumn);
197: request.setAttribute("SiebelActivities", records);
198: }
199: reqDisp = ctx
200: .getRequestDispatcher("/jsps/SiebelActivityDetails.jsp");
201:
202: reqDisp.include(request, response);
203: } catch (Exception ex) {
204: debug("handleActivitiesRequest()", "dispatchRequest Exp : "
205: + ex);
206: }
207: }
208:
209: private static void putCachedActivityData(int rowsToShow,
210: String sessionID, String query, Vector records) {
211:
212: Hashtable sessionData = (Hashtable) activitiesCache
213: .get(sessionID);
214:
215: if (sessionData == null) {
216: sessionData = new Hashtable();
217: }
218: Vector pages = SiebelAccountRequestUtils.setPageCacheData(
219: records, sessionID, rowsToShow);
220: debug("putCachedActivityData()", "pages size : "
221: + pages.size());
222: sessionData.put(query, pages);
223: activitiesCache.put(sessionID, sessionData);
224: }
225:
226: private static Vector getCachedActivityData(String sessionID,
227: String query, RenderRequest request) {
228:
229: Hashtable sessionData = (Hashtable) activitiesCache
230: .get(sessionID);
231: Vector records = null;
232:
233: if (sessionData != null) {
234: debug("getCachedActivityData()", "Getting Cached Data : ");
235: records = SiebelAccountRequestUtils.getPageCacheData(
236: request, sessionData, "CurrentActivityPage", query);
237: debug("getCachedActivityData()", "Got Cached Data : ");
238: }
239: return records;
240: }
241:
242: public static void resetCache(String sessionID) {
243: activitiesCache.remove(sessionID);
244: }
245:
246: private static int getRowsToShow(RenderRequest request) {
247: PortletPreferences prefs = request.getPreferences();
248: String row = prefs.getValue("ActivityRowPreference",
249: SiebelConfigUtils.defaultActivityRows);
250: int retVal = 0;
251:
252: try {
253: retVal = Integer.parseInt(row);
254: } catch (Exception ex) {
255: retVal = Integer
256: .parseInt(SiebelConfigUtils.defaultActivityRows);
257: }
258: if (retVal == 0) {
259: retVal = Integer
260: .parseInt(SiebelConfigUtils.defaultActivityRows);
261: }
262: return retVal;
263: }
264:
265: private static void setAccountID(String query, RenderRequest request) {
266:
267: int index = (new String("<Account_spcId>")).length();
268: String accId = query.substring(index, query
269: .indexOf("</Account_spcId>"));
270: debug("setAccountID()", "accId : " + accId);
271: request.setAttribute("AccountID", accId);
272: }
273:
274: /**
275: * This method will print the log the messages.
276: **/
277: private static void debug(String methodName, String msg) {
278: logger.log(Level.INFO,
279: "com.sun.portal.siebelportlet.util.SiebelActivityRequestUtils:"
280: + methodName + ":" + msg);
281: }
282: }
|