001: /**
002: * $Id: SAPNamefinderPortlet.java,v 1.2 2005/10/19 10:25:58 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 com.sun.portal.sapportlet.namefinder.*;
017: import javax.portlet.GenericPortlet;
018: import javax.portlet.PortletContext;
019: import javax.portlet.PortletConfig;
020: import javax.portlet.PortletException;
021: import javax.portlet.RenderRequest;
022: import javax.portlet.RenderResponse;
023: import javax.portlet.PortletRequestDispatcher;
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletSession;
027: import javax.portlet.*;
028: import javax.portlet.PortletPreferences;
029: import java.io.IOException;
030: import java.util.List;
031: import java.util.Iterator;
032: import java.util.logging.Logger;
033: import java.rmi.RemoteException;
034: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
035:
036: /**
037: * SAP Time Portlet.
038: *
039: * @author nk137934
040: */
041:
042: public class SAPNamefinderPortlet extends GenericPortlet implements
043: SAPPortletConstants {
044:
045: private static Logger logger = Logger
046: .getLogger(SAPPortletConstants.LOGGER_NAMESPACE);
047:
048: /*
049: *
050: */
051: public void init(PortletConfig config) throws PortletException {
052: super .init(config);
053: }
054:
055: public void doView(RenderRequest request, RenderResponse response)
056: throws PortletException, IOException {
057:
058: PortletSession session = request.getPortletSession();
059: SAPUserConfig userConfig = (SAPUserConfig) session
060: .getAttribute(SESSION_USERCONFIG,
061: PortletSession.APPLICATION_SCOPE);
062: if (userConfig == null) {
063: userConfig = SAPAuthUtils
064: .checkForUserAuthentication(request);
065: }
066:
067: if (userConfig == null) {
068: // Dispatch to the configuration page
069: dispatchToConfigErrorPage(request, response);
070: return;
071: }
072: //Retrieve the delegate object from the session
073: NamefinderManager manager = (NamefinderManager) session
074: .getAttribute(SESSION_NAMEMANAGER);
075: if (manager == null
076: || !manager.getUserConfig().equals(userConfig)) {
077: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
078: try {
079: manager = createNamefinderManager(userConfig, session,
080: endPoint);
081: session.setAttribute(SESSION_NAMEMANAGER, manager);
082: } catch (Exception gEx) {
083: logger.severe("failed to create namefinder manager");
084: // Dispatch to the configuration page
085: dispatchToConfigErrorPage(request, response);
086: return;
087: }
088: }
089: response.setContentType(request.getResponseContentType());
090: PortletRequestDispatcher dispatcher = getPortletContext()
091: .getRequestDispatcher(NAME_FINDER_PAGE);
092: dispatcher.include(request, response);
093:
094: }
095:
096: private NamefinderManager createNamefinderManager(
097: SAPUserConfig userConfig, PortletSession session,
098: String endPoint) throws PortletException,
099: java.io.IOException {
100:
101: NamefinderManager manager = new NamefinderManager();
102: // If the init of the manager fails, there is some misconfiguration.
103: try {
104: manager.init(userConfig, endPoint);
105: } catch (Throwable throwable) {
106: logger.severe("Failed to initialize employee delegate");
107: }
108:
109: return manager;
110: }
111:
112: public void doHelp(RenderRequest request, RenderResponse response)
113: throws PortletException, IOException {
114:
115: response.setContentType(request.getResponseContentType());
116: PortletRequestDispatcher dispatcher = getPortletContext()
117: .getRequestDispatcher(NAMEFINDER_HELP_PAGE);
118: dispatcher.include(request, response);
119:
120: }
121:
122: public void dispatchToConfigErrorPage(RenderRequest request,
123: RenderResponse response) throws PortletException,
124: IOException {
125:
126: response.setContentType(request.getResponseContentType());
127: PortletRequestDispatcher dispatcher = getPortletContext()
128: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
129: dispatcher.include(request, response);
130:
131: }
132:
133: /*
134: * Invoke the appropriate search depending on the actual operation.
135: */
136: public void processAction(ActionRequest request,
137: ActionResponse response) throws PortletException,
138: java.io.IOException {
139:
140: PortletSession session = request.getPortletSession();
141: NamefinderManager manager = (NamefinderManager) session
142: .getAttribute(SESSION_NAMEMANAGER);
143: String action = request.getParameter(HTML_FIELD_OPERATION);
144:
145: if (action.equals(OPERATION_SEARCH)) {
146:
147: String firstName = request
148: .getParameter(HTML_FIELD_FIRST_NAME);
149: String lastName = request
150: .getParameter(HTML_FIELD_LAST_NAME);
151: String employeeNumber = request
152: .getParameter(HTML_FIELD_EMP_NUM);
153: String roomNumber = request
154: .getParameter(HTML_FIELD_ROOM_NUM);
155: String buildingNumber = request
156: .getParameter(HTML_FIELD_BUILD_NUM);
157: String extension = request.getParameter(HTML_FIELD_EXTN);
158:
159: NamefinderDetail namefinderQuery = new NamefinderDetail();
160: namefinderQuery.setFirstName(firstName);
161: namefinderQuery.setLastName(lastName);
162: namefinderQuery.setEmployeeNumber(employeeNumber);
163: namefinderQuery.setRoomNumber(roomNumber);
164: namefinderQuery.setBuildingNumber(buildingNumber);
165: namefinderQuery.setExtension(extension);
166:
167: List list = manager.getDetails(namefinderQuery);
168:
169: NamefinderDetailPaginator paginator = new NamefinderDetailPaginator(
170: list);
171: storeVariablesInSession(session, namefinderQuery, paginator);
172: } else if (action.equals(OPERATION_ITERATE)) {
173:
174: NamefinderDetailPaginator paginator = (NamefinderDetailPaginator) session
175: .getAttribute(SESSION_NAMEFINDER_RECORDS);
176:
177: String pageNumberString = request
178: .getParameter(HTML_FIELD_PAGENUMBER);
179: if (pageNumberString != null) {
180: int pageNumber = 1;
181: try {
182: pageNumber = Integer.parseInt(pageNumberString);
183: } catch (NumberFormatException nfe) {
184: //Ignore. The page number will be set to 1
185: }
186: paginator.setPageNumber(pageNumber);
187: } else {
188:
189: String directionString = request
190: .getParameter(HTML_FIELD_DIRECTION);
191: if (directionString == null) {
192: return;
193: }
194: int direction = 0;
195: try {
196: direction = Integer.parseInt(directionString);
197: } catch (NumberFormatException nfe) {
198: //Ignore.
199: }
200: switch (direction) {
201:
202: case DIRECTION_FIRST:
203: paginator.setFirstPage();
204: break;
205: case DIRECTION_PREV:
206: paginator.setPreviousPage();
207: break;
208: case DIRECTION_NEXT:
209: paginator.setNextPage();
210: break;
211: case DIRECTION_LAST:
212: paginator.setLastPage();
213: break;
214: default:
215: break;
216: }
217: }
218:
219: } else if (action.equals(OPERATION_RESET)) {
220:
221: // clear session
222: session.removeAttribute(SESSION_NAMEFINDER_QUERY);
223: session.removeAttribute(SESSION_NAMEFINDER_RECORDS);
224:
225: } else if (action.equals(OPERATION_SORT)) {
226: // Needs clean up: switch case is better
227: NamefinderDetailPaginator paginator = (NamefinderDetailPaginator) session
228: .getAttribute(SESSION_NAMEFINDER_RECORDS);
229:
230: String sortField = request
231: .getParameter(HTML_FIELD_SORT_FIELD);
232: String sortOrder = request
233: .getParameter(HTML_FIELD_SORT_ORDER);
234:
235: if (sortField.equals(EMP_NUM)) {
236: if (sortOrder.equals(SORT_ORDER_DSC)) {
237: paginator.sortDscByEmpNumber();
238: } else {
239: paginator.sortAscByEmpNumber();
240: }
241: } else if (sortField.equals(FIRST_NAME)) {
242: if (sortOrder.equals(SORT_ORDER_DSC)) {
243: paginator.sortDscByFirstName();
244: } else {
245: paginator.sortAscByFirstName();
246: }
247: } else if (sortField.equals(LAST_NAME)) {
248: if (sortOrder.equals(SORT_ORDER_DSC)) {
249: paginator.sortDscByLastName();
250: } else {
251: paginator.sortAscByLastName();
252: }
253: } else if (sortField.equals(ROOM_NUMBER)) {
254: if (sortOrder.equals(SORT_ORDER_DSC)) {
255: paginator.sortDscByRoomNum();
256: } else {
257: paginator.sortAscByRoomNum();
258: }
259: } else if (sortField.equals(BUILDING_NUMBER)) {
260: if (sortOrder.equals(SORT_ORDER_DSC)) {
261: paginator.sortDscByBuildingNum();
262: } else {
263: paginator.sortAscByBuildingNum();
264: }
265: } else if (sortField.equals(EXTENSION)) {
266: if (sortOrder.equals(SORT_ORDER_DSC)) {
267: paginator.sortDscByExtension();
268: } else {
269: paginator.sortAscByExtension();
270: }
271: } else if (sortField.equals(EMAIL)) {
272: if (sortOrder.equals(SORT_ORDER_DSC)) {
273: paginator.sortDscByEmail();
274: } else {
275: paginator.sortAscByEmail();
276: }
277: }
278: }
279:
280: }
281:
282: private void storeVariablesInSession(PortletSession session,
283: NamefinderDetail namefinderQuery,
284: NamefinderDetailPaginator paginator) {
285:
286: session.setAttribute(SESSION_NAMEFINDER_QUERY, namefinderQuery);
287: session.setAttribute(SESSION_NAMEFINDER_RECORDS, paginator);
288:
289: }
290:
291: public void doEdit(RenderRequest request, RenderResponse response)
292: throws PortletException, IOException {
293:
294: response.setContentType(request.getResponseContentType());
295: }
296:
297: }
|