001: /**
002: * $Id: SAPEmpDetailsPortlet.java,v 1.7 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.*;
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 javax.portlet.*;
027: import javax.portlet.PortletPreferences;
028: import java.io.IOException;
029: import java.util.Iterator;
030: import java.util.logging.Logger;
031: import com.sun.portal.sapportlet.employee.*;
032: import java.rmi.RemoteException;
033: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
034:
035: /**
036: * This is the SAP Employee Details Portlet. It can be used
037: * to display the details of any given employee in the SAP
038: * system.
039: *
040: * @author nk137934
041: */
042:
043: public class SAPEmpDetailsPortlet extends GenericPortlet implements
044: SAPPortletConstants {
045:
046: private static Logger logger = Logger
047: .getLogger(SAPPortletConstants.LOGGER_NAMESPACE);
048:
049: /*
050: *
051: */
052: public void init(PortletConfig config) throws PortletException {
053: super .init(config);
054: }
055:
056: public void doView(RenderRequest request, RenderResponse response)
057: throws PortletException, IOException {
058:
059: PortletSession session = request.getPortletSession();
060: SAPUserConfig userConfig = (SAPUserConfig) session
061: .getAttribute(SESSION_USERCONFIG,
062: PortletSession.APPLICATION_SCOPE);
063: if (userConfig == null) {
064: userConfig = SAPAuthUtils
065: .checkForUserAuthentication(request);
066: }
067:
068: if (userConfig == null) {
069: // Dispatch to the configuration page
070: dispatchToConfigErrorPage(request, response);
071: return;
072: }
073: //Retrieve the delegate object from the session
074: EmployeeManager manager = (EmployeeManager) session
075: .getAttribute(SESSION_EMPMANAGER);
076:
077: // If no delegate has been created, create one!
078: // The delegate also needs to be created if the user config object has changed
079: boolean userConfigChanged = false;
080: if (manager == null
081: || !manager.getUserConfig().equals(userConfig)) {
082: userConfigChanged = true;
083: }
084: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
085:
086: if (userConfigChanged) {
087: try {
088: manager = createEmpManager(userConfig, session,
089: endPoint);
090: session.setAttribute(SESSION_EMPMANAGER, manager);
091: } catch (Exception Ex) {
092: logger.severe("failed to create emp manager");
093: // Dispatch to the configuration page
094: dispatchToConfigErrorPage(request, response);
095: return;
096: }
097: }
098: Employee employee = (Employee) session
099: .getAttribute(SESSION_EMPLOYEE);
100: if (employee == null || userConfigChanged) {
101: String endPointHost = SAPAuthUtils
102: .getSAPEndpointHost(request);
103: try {
104: employee = manager.getEmployee(
105: userConfig.getUserName(), endPointHost);
106: } catch (RemoteException rExcp) {
107: // This means some error has occured in manager.getEmployee
108: // A misconfig perhaps
109: //redirect to error page
110: logger.warning("Failed to get employee");
111: logger.warning(rExcp.getMessage());
112: response.setContentType(request
113: .getResponseContentType());
114: PortletRequestDispatcher dispatcher = getPortletContext()
115: .getRequestDispatcher(SAP_CONFIG_ERROR_PAGE);
116: dispatcher.include(request, response);
117: return;
118: }
119: session.setAttribute(SESSION_EMPLOYEE, employee);
120: }
121: response.setContentType(request.getResponseContentType());
122: PortletRequestDispatcher dispatcher = getPortletContext()
123: .getRequestDispatcher(EMP_DETAILS_PAGE);
124: dispatcher.include(request, response);
125: }
126:
127: public void doHelp(RenderRequest request, RenderResponse response)
128: throws PortletException, IOException {
129:
130: response.setContentType(request.getResponseContentType());
131: PortletRequestDispatcher dispatcher = getPortletContext()
132: .getRequestDispatcher(EMPLOYEE_DETAILS_HELP_PAGE);
133: dispatcher.include(request, response);
134:
135: }
136:
137: public void dispatchToConfigErrorPage(RenderRequest request,
138: RenderResponse response) throws PortletException,
139: IOException {
140:
141: response.setContentType(request.getResponseContentType());
142: PortletRequestDispatcher dispatcher = getPortletContext()
143: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
144: dispatcher.include(request, response);
145:
146: }
147:
148: /*
149: * Invoke the appropriate search depending on the actual operation.
150: */
151: public void processAction(ActionRequest request,
152: ActionResponse response) throws PortletException,
153: java.io.IOException {
154:
155: }
156:
157: public void doEdit(RenderRequest request, RenderResponse response)
158: throws PortletException, IOException {
159:
160: response.setContentType(request.getResponseContentType());
161: }
162:
163: private EmployeeManager createEmpManager(SAPUserConfig userConfig,
164: PortletSession session, String endPoint)
165: throws PortletException, java.io.IOException {
166:
167: EmployeeManager manager = new EmployeeManager();
168: // If the init of the manager fails, there is some misconfiguration.
169: try {
170: manager.init(userConfig, endPoint);
171: } catch (Throwable throwable) {
172: logger.severe("Failed to initialize employee delegate");
173: }
174: return manager;
175: }
176:
177: }
|