001: /**
002: * $Id: SAPCustSearchPortlet.java,v 1.10 2005/10/28 08:44:23 ks161616 Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.sapportlet;
014:
015: import com.sun.portal.sapportlet.config.SAPUserConfig;
016: import javax.portlet.GenericPortlet;
017: import javax.portlet.PortletContext;
018: import javax.portlet.PortletConfig;
019: import javax.portlet.PortletException;
020: import javax.portlet.RenderRequest;
021: import javax.portlet.RenderResponse;
022: import javax.portlet.PortletRequestDispatcher;
023: import javax.portlet.ActionRequest;
024: import javax.portlet.ActionResponse;
025: import javax.portlet.PortletSession;
026: import java.io.IOException;
027: import java.util.List;
028: import java.util.Iterator;
029: import java.util.logging.Logger;
030: import com.sun.portal.sapportlet.customer.*;
031: import java.rmi.RemoteException;
032: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
033:
034: /**
035: * This is the SAP Customer Search Portlet. It can be used
036: * to search for customers from an existing SAP system.
037: *
038: * @author nk137934
039: */
040:
041: public class SAPCustSearchPortlet extends GenericPortlet implements
042: SAPPortletConstants {
043:
044: private static Logger logger = Logger
045: .getLogger(SAPPortletConstants.LOGGER_NAMESPACE);
046:
047: /*
048: *
049: */
050: public void init(PortletConfig config) throws PortletException {
051:
052: super .init(config);
053:
054: }
055:
056: /*
057: * The doView method of the SAPCustSearchPortlet.
058: * If the query has already been executed, the results
059: * are available in the session. If not, execute the default
060: * query.
061: */
062: public void doView(RenderRequest request, RenderResponse response)
063: throws PortletException, IOException {
064:
065: PortletSession session = request.getPortletSession();
066:
067: SAPUserConfig userConfig = (SAPUserConfig) session
068: .getAttribute(SESSION_USERCONFIG,
069: PortletSession.APPLICATION_SCOPE);
070: if (userConfig == null) {
071: userConfig = SAPAuthUtils
072: .checkForUserAuthentication(request);
073: }
074:
075: if (userConfig == null) {
076: // Dispatch to the configuration page
077: dispatchToConfigErrorPage(request, response);
078: return;
079: }
080:
081: //Retrieve the delegate object from the session
082: CustomerManager manager = (CustomerManager) session
083: .getAttribute(SESSION_CUSTMANAGER);
084:
085: // If no delegate has been created, create one!
086: // The delegate also needs to be created if the user config object has changed
087: boolean userConfigChanged = false;
088: if (manager == null
089: || !manager.getUserConfig().equals(userConfig)) {
090: userConfigChanged = true;
091: }
092: if (userConfigChanged) {
093: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
094: try {
095: manager = createCustManager(userConfig, session,
096: endPoint);
097: session.setAttribute(SESSION_CUSTMANAGER, manager);
098: } catch (Exception Ex) {
099: logger.severe("failed to create customer manager");
100: dispatchToConfigErrorPage(request, response);
101: return;
102: }
103: }
104:
105: if (session.getAttribute(SESSION_QUERY_STRING) == null) {
106: //No previous query.So execute default query.
107: //Store query info in session
108: List customers = null;
109: try {
110: customers = executeDefaultQuery(manager, session);
111: CustomerPaginator customerIter = new CustomerPaginator(
112: customers);
113: storeVariablesInSession(session, customerIter,
114: DEFAULT_SEARCH_STRING, QUERY_ON_NAME);
115: } catch (RemoteException rExcp) {
116: //logger.warning("Failed to get employee");
117: //logger.warning(rExcp.getMessage());
118: response.setContentType(request
119: .getResponseContentType());
120: PortletRequestDispatcher dispatcher = getPortletContext()
121: .getRequestDispatcher(SAP_CONFIG_ERROR_PAGE);
122: dispatcher.include(request, response);
123: return;
124: }
125: }
126: response.setContentType(request.getResponseContentType());
127: PortletRequestDispatcher dispatcher = getPortletContext()
128: .getRequestDispatcher(SEARCH_RESULTS_PAGE);
129: dispatcher.include(request, response);
130: }
131:
132: /*
133: * Invoke the appropriate search depending on the actual operation.
134: * 4 kinds of actions are possible.
135: * When the search button is clicked, the action is OPERATION_SEARCH
136: * When a customer is selected, the action is OPERATION_SELECT
137: * When the list of customers is iterated, the action is OPERATION_ITERATE
138: * When the list of customers is iterated, the action is OPERATION_SORT
139: */
140: public void processAction(ActionRequest request,
141: ActionResponse response) {
142:
143: PortletSession session = request.getPortletSession();
144: //logger.finest("In processAction of SAPCustSearchPortlet");
145: CustomerManager manager = (CustomerManager) session
146: .getAttribute(SESSION_CUSTMANAGER);
147:
148: String action = request.getParameter(HTML_FIELD_OPERATION);
149: if (action.equals(OPERATION_SEARCH)) {
150:
151: logger.finest("This is a Search Action");
152: //Get the new query String
153: String queryString = request
154: .getParameter(HTML_FIELD_SEARCH_TERM);
155: //Get the new query field
156: String queryField = request
157: .getParameter(HTML_FIELD_QUERY_FIELD);
158: //Now execute the new query
159: List customers = null;
160: try {
161: customers = executeQuery(manager, session, queryString,
162: queryField);
163: CustomerPaginator customerIter = new CustomerPaginator(
164: customers);
165: storeVariablesInSession(session, customerIter,
166: queryString, queryField);
167: // Unset any previously selected customers
168: session.removeAttribute(SESSION_SELECTED_CUST,
169: PortletSession.APPLICATION_SCOPE);
170: } catch (RemoteException rExcp) {
171: // This means some error has occured in manager.getEmployee
172: // A misconfig perhaps
173: //redirect to error page
174:
175: } catch (Exception excp) {
176: // Any exception is considered as no users are found
177: storeVariablesInSession(session, null, queryString,
178: queryField);
179: session.removeAttribute(SESSION_SELECTED_CUST,
180: PortletSession.APPLICATION_SCOPE);
181: }
182:
183: } else if (action.equals(OPERATION_SELECT)) {
184:
185: //logger.finest("This is a Select Action");
186: String customerNumber = request
187: .getParameter(HTML_FIELD_SEL_CUST);
188:
189: // Search through all the customers to find this customer
190: CustomerPaginator customerIter = (CustomerPaginator) session
191: .getAttribute(SESSION_CUSTOMERS);
192: List customers = customerIter.getCurrentPage();
193: Customer cust = findCustomerByNumber(customers,
194: customerNumber);
195: //logger.finest("Selected Customer is "+cust.getCustomerName());
196:
197: // Store this customer object in application scope to be used by
198: // other portlets.
199: if (cust != null) {
200: session.setAttribute(SESSION_SELECTED_CUST, cust,
201: PortletSession.APPLICATION_SCOPE);
202: //logger.finest("Customer stored in session");
203: }
204:
205: } else if (action.equals(OPERATION_ITERATE)) {
206:
207: //logger.finest("This is a Iterate Action");
208: CustomerPaginator customerIter = (CustomerPaginator) session
209: .getAttribute(SESSION_CUSTOMERS);
210: if (customerIter != null) {
211: String pageNumberString = request
212: .getParameter(HTML_FIELD_PAGENUMBER);
213: if (pageNumberString != null) {
214: int pageNumber = 1;
215: try {
216: pageNumber = Integer.parseInt(pageNumberString);
217: } catch (NumberFormatException nfe) {
218: //Ignore. The page number will be set to 1
219: }
220: customerIter.setPageNumber(pageNumber);
221: } else {
222: String directionString = request
223: .getParameter(HTML_FIELD_DIRECTION);
224:
225: if (directionString == null) {
226: return;
227: }
228:
229: int direction = 0;
230:
231: try {
232: direction = Integer.parseInt(directionString);
233: } catch (NumberFormatException nfe) {
234: //Ignore.
235: }
236:
237: switch (direction) {
238:
239: case DIRECTION_FIRST:
240: customerIter.setFirstPage();
241: break;
242: case DIRECTION_PREV:
243: customerIter.setPreviousPage();
244: break;
245: case DIRECTION_NEXT:
246: customerIter.setNextPage();
247: break;
248: case DIRECTION_LAST:
249: customerIter.setLastPage();
250: break;
251: default:
252: break;
253: }
254: }
255: } else {
256: logger
257: .warning("User not set. Possible Cause: no records for the current user");
258: }
259: } else if (action.equals(OPERATION_SORT)) {
260: //logger.finest("This is a Sort Action");
261: CustomerPaginator customerIter = (CustomerPaginator) session
262: .getAttribute(SESSION_CUSTOMERS);
263:
264: String sortField = request
265: .getParameter(HTML_FIELD_SORT_FIELD);
266: String sortOrder = request
267: .getParameter(HTML_FIELD_SORT_ORDER);
268:
269: if (sortField.equals(CUST_NUMBER)) {
270: if (sortOrder.equals(SORT_ORDER_DSC)) {
271: customerIter.sortDscByCustomerNumber();
272: } else {
273: customerIter.sortAscByCustomerNumber();
274: }
275: } else if (sortField.equals(CUST_NAME)) {
276: if (sortOrder.equals(SORT_ORDER_DSC)) {
277: customerIter.sortDscByCustomerName();
278: } else {
279: customerIter.sortAscByCustomerName();
280: }
281: } else { // Else sort by City
282: if (sortOrder.equals(SORT_ORDER_DSC)) {
283: customerIter.sortDscByCustomerCity();
284: } else {
285: customerIter.sortAscByCustomerCity();
286: }
287: }
288: }
289: }
290:
291: public void doHelp(RenderRequest request, RenderResponse response)
292: throws PortletException, IOException {
293: response.setContentType(request.getResponseContentType());
294: PortletRequestDispatcher dispatcher = getPortletContext()
295: .getRequestDispatcher(CUST_SEARCH_HELP_PAGE);
296: dispatcher.include(request, response);
297: }
298:
299: /* Find the customer object using the id, from a given list */
300: private Customer findCustomerByNumber(List list, String id) {
301: Iterator iter = list.iterator();
302: while (iter.hasNext()) {
303: Customer this Customer = (Customer) iter.next();
304: if (this Customer.getCustomerNumber().equals(id)) {
305: return this Customer;
306: }
307: }
308: // Should never happen.This would be a bug
309: logger.severe("Unable to get customer with id " + id
310: + " in the list of customers");
311: return null;
312: }
313:
314: /* Store session variables */
315: private void storeVariablesInSession(PortletSession session,
316: CustomerPaginator customerIter, String queryString,
317: String queryField) {
318:
319: session.setAttribute(SESSION_CUSTOMERS, customerIter);
320: session.setAttribute(SESSION_QUERY_STRING, queryString);
321: session.setAttribute(SESSION_QUERY_FIELD, queryField);
322: }
323:
324: private List executeDefaultQuery(CustomerManager manager,
325: PortletSession session) throws RemoteException {
326: return executeQuery(manager, session, DEFAULT_SEARCH_STRING,
327: QUERY_ON_NAME);
328: }
329:
330: /* Execute queries on the customer manager object*/
331: private List executeQuery(CustomerManager manager,
332: PortletSession session, String queryString,
333: String queryField) throws RemoteException {
334:
335: List customers = null;
336: if (queryField.equals(QUERY_ON_NAME)) {
337: customers = manager.findCustomerByName(queryString);
338: } else {
339: customers = manager.findCustomerByNumber(queryString);
340: }
341: return customers;
342: }
343:
344: public void dispatchToConfigErrorPage(RenderRequest request,
345: RenderResponse response) throws PortletException,
346: IOException {
347: response.setContentType(request.getResponseContentType());
348: PortletRequestDispatcher dispatcher = getPortletContext()
349: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
350: dispatcher.include(request, response);
351: }
352:
353: private CustomerManager createCustManager(SAPUserConfig userConfig,
354: PortletSession session, String endPointAddress)
355: throws PortletException, java.io.IOException {
356:
357: CustomerManager manager = new CustomerManager();
358: // If the init of the manager fails, there is some misconfiguration.
359: try {
360: manager.init(userConfig, endPointAddress);
361: } catch (Throwable throwable) {
362: logger.severe("Failed to initialize employee delegate");
363: }
364: return manager;
365: }
366: }
|