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 Account Contact 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 SiebelContactRequestUtils {
062:
063: // Name of the Siebel business object from where the Contact data
064: // should be obtained.
065: static String businessMethod = "<Siebel location=\"S/BO/Contact/Contact/query\">";
066:
067: static Logger logger = SiebelLogger.getLogger();
068: static Hashtable contactsCache = new Hashtable();
069:
070: // Fields that should be obtained in the result set
071: public static final String[] queryFields = { "Full Name",
072: "Job Title", "Street Address", "City", "State",
073: "Postal Code", "Work Phone #", "Cellular Phone #",
074: "Fax Phone #", "Email Address" };
075:
076: /**
077: * This method returns the XML query to get the Siebel Contact Details
078: */
079: private static String getSiebelContactDetailsQuery(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 Contact Details Tab request
098: **/
099: public static void handleContactsRequest(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 reqType = request.getParameter("RequestType");
110:
111: if (sortOrder == null) {
112: sortOrder = "DOWN";
113: }
114:
115: if (sortColumn == null) {
116: sortColumn = "Full_spcName";
117: }
118: debug("handleContactsRequest()", "reqType " + reqType);
119: debug("handleContactsRequest()", "sortOrder " + sortOrder);
120: debug("handleContactsRequest()", "sortColumn " + sortColumn);
121:
122: try {
123:
124: if (query != null) {
125:
126: if ((reqType != null) && (reqType.equals("Paginate"))) {
127: String queryField = request
128: .getParameter("QueryField");
129: String queryValue = request
130: .getParameter("QueryValue");
131: String tableSize = request
132: .getParameter("TableSize");
133: if (queryField.equals("goTo")) {
134: queryValue = SiebelAccountRequestUtils
135: .checkGoToValue(queryValue, tableSize);
136: }
137: request.setAttribute("CurrentPage", queryValue);
138: query = request.getParameter("CurrentQuery");
139: setAccountID(query, request);
140: }
141: String curPage = (String) request
142: .getAttribute("CurrentPage");
143:
144: if (curPage == null) {
145: curPage = "1";
146: }
147: request.setAttribute("CurrentAccountsPage", curPage);
148: debug("handleContactsRequest()", "Query is : " + query);
149: records = getCachedContactsData(sessionID, query,
150: request);
151:
152: if (records == null) {
153: String reqStr = getSiebelContactDetailsQuery(query);
154: String xmlOutput = SiebelIwayUtils
155: .getResponseString(reqStr);
156: debug("handleContactsRequest()", "xmlOutput : "
157: + xmlOutput);
158: records = SiebelResponseUtils
159: .getContactDetails(xmlOutput);
160: debug("handleContactsRequest()", "Got Response : ");
161: debug("handleContactsRequest()", "Sorting ");
162: records = TableSorting.sortTable(records,
163: sortColumn, sortOrder);
164: int rowsToShow = getRowsToShow(request);
165: request.setAttribute("CurrentPage", "1");
166: request.setAttribute("CurrentAccountsPage", "1");
167: putCachedContactsData(rowsToShow, sessionID, query,
168: records);
169: records = getCachedContactsData(sessionID, query,
170: request);
171: }
172: if ((reqType != null) && (reqType.equals("Sort"))) {
173: debug("handleContactsRequest()",
174: "Sorting due to Request ");
175: Hashtable sessionData = (Hashtable) contactsCache
176: .get(sessionID);
177: Vector responseData = SiebelAccountRequestUtils
178: .getMergedCacheData(sessionData, query);
179: records = TableSorting.sortTable(responseData,
180: sortColumn, sortOrder);
181: int rowsToShow = getRowsToShow(request);
182: request.setAttribute("CurrentPage", "1");
183: request.setAttribute("CurrentAccountsPage", "1");
184: putCachedContactsData(rowsToShow, sessionID, query,
185: records);
186: records = getCachedContactsData(sessionID, query,
187: request);
188: //records = TableSorting.sortTable(records, sortColumn, sortOrder);
189: }
190: Hashtable sessionData = (Hashtable) contactsCache
191: .get(sessionID);
192: Vector pages = (Vector) sessionData.get(query);
193: int size = pages.size();
194: TablePagination.setPaginationRow(request, response,
195: "PaginateContact", query, size);
196: request.setAttribute("SortOrder", sortOrder);
197: request.setAttribute("SortColumn", sortColumn);
198: request.setAttribute("SiebelContacts", records);
199: }
200: reqDisp = ctx
201: .getRequestDispatcher("/jsps/SiebelContactDetails.jsp");
202: reqDisp.include(request, response);
203: } catch (Exception ex) {
204: debug("handleContactsRequest()", "dispatchRequest Exp : "
205: + ex);
206: }
207: }
208:
209: private static void putCachedContactsData(int rowsToShow,
210: String sessionID, String query, Vector records) {
211:
212: Hashtable sessionData = (Hashtable) contactsCache
213: .get(sessionID);
214:
215: if (sessionData == null) {
216: sessionData = new Hashtable();
217: }
218: Vector pages = SiebelAccountRequestUtils.setPageCacheData(
219: records, sessionID, rowsToShow);
220: debug("putCachedContactsData()", "pages size : "
221: + pages.size());
222: sessionData.put(query, pages);
223: contactsCache.put(sessionID, sessionData);
224: }
225:
226: private static Vector getCachedContactsData(String sessionID,
227: String query, RenderRequest request) {
228:
229: Hashtable sessionData = (Hashtable) contactsCache
230: .get(sessionID);
231: Vector records = null;
232:
233: if (sessionData != null) {
234: debug("getCachedContactsData()", "Getting Cached Data : ");
235: records = SiebelAccountRequestUtils.getPageCacheData(
236: request, sessionData, "CurrentAccountsPage", query);
237: debug("getCachedContactsData()", "Got Cached Data : ");
238: }
239: return records;
240: }
241:
242: public static void resetCache(String sessionID) {
243: contactsCache.remove(sessionID);
244: }
245:
246: private static int getRowsToShow(RenderRequest request) {
247: PortletPreferences prefs = request.getPreferences();
248: String row = prefs.getValue("ContactRowPreference",
249: SiebelConfigUtils.defaultContactRows);
250: int retVal = 0;
251:
252: try {
253: retVal = Integer.parseInt(row);
254: } catch (Exception ex) {
255: retVal = Integer
256: .parseInt(SiebelConfigUtils.defaultContactRows);
257: }
258: if (retVal == 0) {
259: retVal = Integer
260: .parseInt(SiebelConfigUtils.defaultContactRows);
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.SiebelContactRequestUtils:"
280: + methodName + ":" + msg);
281: }
282: }
|