001: /**
002: * $Id: SAPManagerViewPortlet.java,v 1.3 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.List;
030: import java.util.Iterator;
031: import java.util.ArrayList;
032: import java.util.logging.Logger;
033: import java.rmi.RemoteException;
034: import com.sun.portal.sapportlet.manager.*;
035: import com.sun.portal.sapportlet.employee.*;
036: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
037:
038: /**
039: * SAP manger view portlet
040: *
041: * @author nk137934
042: */
043:
044: public class SAPManagerViewPortlet extends GenericPortlet implements
045: SAPPortletConstants {
046:
047: private static Logger logger = Logger
048: .getLogger(SAPPortletConstants.LOGGER_NAMESPACE);
049:
050: /*
051: *
052: */
053: public void init(PortletConfig config) throws PortletException {
054: super .init(config);
055: }
056:
057: public void doView(RenderRequest request, RenderResponse response)
058: throws PortletException, IOException {
059:
060: PortletSession session = request.getPortletSession();
061: SAPUserConfig userConfig = (SAPUserConfig) session
062: .getAttribute(SESSION_USERCONFIG,
063: PortletSession.APPLICATION_SCOPE);
064: if (userConfig == null) {
065: userConfig = SAPAuthUtils
066: .checkForUserAuthentication(request);
067: }
068:
069: if (userConfig == null) {
070: // Dispatch to the configuration page
071: dispatchToConfigErrorPage(request, response);
072: return;
073: }
074:
075: //Retrieve the delegate object from the session
076: DirectReportManager manager = (DirectReportManager) session
077: .getAttribute(SESSION_DRMANAGER);
078:
079: // If no delegate has been created, create one!
080: // The delegate also needs to be created if the user config object has changed
081: if (manager == null
082: || !manager.getUserConfig().equals(userConfig)) {
083: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
084: try {
085: manager = createDRManager(userConfig, session, endPoint);
086: } catch (Exception Ex) {
087: logger.severe("failed to create DR Manager");
088: // Dispatch to the configuration page
089: dispatchToConfigErrorPage(request, response);
090: return;
091: }
092: }
093: if (session.getAttribute(SESSION_DIRECT_REPORTS) == null) {
094: try {
095: List listOfDirectReports = manager
096: .findDirectReportsForManager(userConfig
097: .getUserName());
098: DirectReportPaginator paginator = new DirectReportPaginator(
099: listOfDirectReports);
100: session.setAttribute(SESSION_DIRECT_REPORTS, paginator);
101: } catch (RemoteException Ex) {
102: // This means some error has occured in manager.getEmployee
103: // A misconfig perhaps
104: //redirect to error page
105: logger.warning("Failed to get direct reports");
106:
107: // redirect to the misconfiguration page.
108: response.setContentType(request
109: .getResponseContentType());
110: PortletRequestDispatcher dispatcher = getPortletContext()
111: .getRequestDispatcher(SAP_CONFIG_ERROR_PAGE);
112: dispatcher.include(request, response);
113: return;
114: }
115: }
116: //
117: response.setContentType(request.getResponseContentType());
118: PortletRequestDispatcher dispatcher = getPortletContext()
119: .getRequestDispatcher(DIRECT_REPORTS_PAGE);
120: dispatcher.include(request, response);
121:
122: }
123:
124: public void doHelp(RenderRequest request, RenderResponse response)
125: throws PortletException, IOException {
126:
127: response.setContentType(request.getResponseContentType());
128: PortletRequestDispatcher dispatcher = getPortletContext()
129: .getRequestDispatcher(MANAGER_VIEW_HELP_PAGE);
130: dispatcher.include(request, response);
131:
132: }
133:
134: public void dispatchToConfigErrorPage(RenderRequest request,
135: RenderResponse response) throws PortletException,
136: IOException {
137:
138: response.setContentType(request.getResponseContentType());
139: PortletRequestDispatcher dispatcher = getPortletContext()
140: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
141: dispatcher.include(request, response);
142:
143: }
144:
145: /*
146: * Invoke the appropriate search depending on the actual operation.
147: */
148: public void processAction(ActionRequest request,
149: ActionResponse response) throws PortletException,
150: java.io.IOException {
151:
152: PortletSession session = request.getPortletSession();
153:
154: String action = request.getParameter(HTML_FIELD_OPERATION);
155: if (action.equals(OPERATION_ITERATE)) {
156: DirectReportPaginator paginator = (DirectReportPaginator) session
157: .getAttribute(SESSION_DIRECT_REPORTS);
158: String pageNumberString = request
159: .getParameter(HTML_FIELD_PAGENUMBER);
160: if (pageNumberString != null) {
161: int pageNumber = 1;
162: try {
163: pageNumber = Integer.parseInt(pageNumberString);
164: } catch (NumberFormatException nfe) {
165: //Ignore. The page number will be set to 1
166: }
167: paginator.setPageNumber(pageNumber);
168: } else {
169:
170: String directionString = request
171: .getParameter(HTML_FIELD_DIRECTION);
172: if (directionString == null) {
173: return;
174: }
175: int direction = 0;
176: try {
177: direction = Integer.parseInt(directionString);
178: } catch (NumberFormatException nfe) {
179: //Ignore.
180: }
181: switch (direction) {
182:
183: case DIRECTION_FIRST:
184: paginator.setFirstPage();
185: break;
186: case DIRECTION_PREV:
187: paginator.setPreviousPage();
188: break;
189: case DIRECTION_NEXT:
190: paginator.setNextPage();
191: break;
192: case DIRECTION_LAST:
193: paginator.setLastPage();
194: break;
195: default:
196: break;
197: }
198: }
199:
200: } else if (action.equals(OPERATION_SORT)) {
201:
202: DirectReportPaginator paginator = (DirectReportPaginator) session
203: .getAttribute(SESSION_DIRECT_REPORTS);
204:
205: String sortField = request
206: .getParameter(HTML_FIELD_SORT_FIELD);
207: String sortOrder = request
208: .getParameter(HTML_FIELD_SORT_ORDER);
209:
210: if (sortField.equals(DR_EMPNO)) {
211: if (sortOrder.equals(SORT_ORDER_DSC)) {
212: paginator.sortDscByEmpNumber();
213: } else {
214: paginator.sortAscByEmpNumber();
215: }
216: } else if (sortField.equals(DR_FULLNAME)) {
217:
218: if (sortOrder.equals(SORT_ORDER_DSC)) {
219: paginator.sortDscByFullName();
220: } else {
221: paginator.sortAscByFullName();
222: }
223: } else if (sortField.equals(DR_SHORTNAME)) {
224: if (sortOrder.equals(SORT_ORDER_DSC)) {
225: paginator.sortDscByShortName();
226: } else {
227: paginator.sortAscByShortName();
228: }
229: } else if (sortField.equals(DR_BEGINDATE)) {
230:
231: if (sortOrder.equals(SORT_ORDER_DSC)) {
232: paginator.sortDscByBeginDate();
233: } else {
234: paginator.sortAscByBeginDate();
235: }
236: }
237: }
238: }
239:
240: public void doEdit(RenderRequest request, RenderResponse response)
241: throws PortletException, IOException {
242:
243: response.setContentType(request.getResponseContentType());
244: }
245:
246: private DirectReportManager createDRManager(
247: SAPUserConfig userConfig, PortletSession session,
248: String endPoint) throws PortletException,
249: java.io.IOException, UnConfiguredException {
250:
251: DirectReportManager manager = new DirectReportManager();
252: // If the init of the manager fails, there is some misconfiguration.
253: try {
254: manager.init(userConfig, endPoint);
255: } catch (Throwable throwable) {
256: //logger.severe("Failed to initialize employee delegate");
257: }
258: session.setAttribute(SESSION_DRMANAGER, manager);
259: return manager;
260: }
261: }
|