001: /**
002: * $Id: SAPTimePortlet.java,v 1.4 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.time.*;
017: import com.sun.portal.sapportlet.employee.*;
018: import javax.portlet.GenericPortlet;
019: import javax.portlet.PortletContext;
020: import javax.portlet.PortletConfig;
021: import javax.portlet.PortletException;
022: import javax.portlet.RenderRequest;
023: import javax.portlet.RenderResponse;
024: import javax.portlet.PortletRequestDispatcher;
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletSession;
028: import javax.portlet.*;
029: import javax.portlet.PortletPreferences;
030: import java.io.IOException;
031: import java.util.List;
032: import java.util.Iterator;
033: import java.util.logging.Logger;
034: import java.rmi.RemoteException;
035: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
036:
037: /**
038: * SAP Time Portlet.
039: *
040: * @author nk137934
041: */
042:
043: public class SAPTimePortlet 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: String employeeNumber = "";
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: //Retrieve the delegate object from the session
075: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
076: TimeManager manager = (TimeManager) session
077: .getAttribute(SESSION_TIMEMANAGER);
078: if (manager == null
079: || !manager.getUserConfig().equals(userConfig)) {
080: try {
081: manager = createTimeManager(userConfig, session,
082: endPoint);
083: session.setAttribute(SESSION_TIMEMANAGER, manager);
084: } catch (Exception Ex) {
085: logger.severe("failed to create time manager");
086: // Dispatch to the configuration page
087: dispatchToConfigErrorPage(request, response);
088: return;
089: }
090: }
091: String endPointHost = SAPAuthUtils.getSAPEndpointHost(request);
092: try {
093: EmployeeManager empManager = createEmpManager(userConfig,
094: session, endPoint);
095: employeeNumber = empManager.getEmployee(
096: userConfig.getUserName(), endPointHost)
097: .getEmployeeNumber();
098: } catch (Exception Ex) {
099: logger
100: .severe("Exception occured in Emp manager operations");
101: // redirect to the misconfiguration page.
102: response.setContentType(request.getResponseContentType());
103: PortletRequestDispatcher dispatcher = getPortletContext()
104: .getRequestDispatcher(SAP_CONFIG_ERROR_PAGE);
105: dispatcher.include(request, response);
106: return;
107: }
108: List absenceRecords = manager.getAbsenceDetails(employeeNumber);
109: if (session.getAttribute(SESSION_ABSENCE_RECORDS) == null) {
110: AbsenceRecordPaginator paginator = new AbsenceRecordPaginator(
111: absenceRecords);
112: session.setAttribute(SESSION_ABSENCE_RECORDS, paginator);
113: }
114:
115: response.setContentType(request.getResponseContentType());
116: PortletRequestDispatcher dispatcher = getPortletContext()
117: .getRequestDispatcher(EMP_TIME_PAGE);
118: dispatcher.include(request, response);
119:
120: }
121:
122: private TimeManager createTimeManager(SAPUserConfig userConfig,
123: PortletSession session, String endPoint)
124: throws PortletException, java.io.IOException {
125:
126: TimeManager manager = new TimeManager();
127: // If the init of the manager fails, there is some misconfiguration.
128: try {
129: manager.init(userConfig, endPoint);
130: } catch (Throwable throwable) {
131: logger.severe("Failed to initialize employee delegate");
132: }
133:
134: return manager;
135: }
136:
137: private EmployeeManager createEmpManager(SAPUserConfig userConfig,
138: PortletSession session, String endPoint)
139: throws PortletException, java.io.IOException {
140:
141: EmployeeManager manager = new EmployeeManager();
142: // If the init of the manager fails, there is some misconfiguration.
143: try {
144: manager.init(userConfig, endPoint);
145: } catch (Throwable throwable) {
146: logger.severe("Failed to initialize employee delegate");
147: }
148: return manager;
149: }
150:
151: public void doHelp(RenderRequest request, RenderResponse response)
152: throws PortletException, IOException {
153:
154: response.setContentType(request.getResponseContentType());
155: PortletRequestDispatcher dispatcher = getPortletContext()
156: .getRequestDispatcher(ABSENCE_HELP_PAGE);
157: dispatcher.include(request, response);
158: }
159:
160: public void dispatchToConfigErrorPage(RenderRequest request,
161: RenderResponse response) throws PortletException,
162: IOException {
163:
164: response.setContentType(request.getResponseContentType());
165: PortletRequestDispatcher dispatcher = getPortletContext()
166: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
167: dispatcher.include(request, response);
168:
169: }
170:
171: /*
172: * Invoke the appropriate search depending on the actual operation.
173: */
174: public void processAction(ActionRequest request,
175: ActionResponse response) throws PortletException,
176: java.io.IOException {
177:
178: PortletSession session = request.getPortletSession();
179:
180: String action = request.getParameter(HTML_FIELD_OPERATION);
181: if (action.equals(OPERATION_ITERATE)) {
182: AbsenceRecordPaginator paginator = (AbsenceRecordPaginator) session
183: .getAttribute(SESSION_ABSENCE_RECORDS);
184: String pageNumberString = request
185: .getParameter(HTML_FIELD_PAGENUMBER);
186: if (pageNumberString != null) {
187: int pageNumber = 1;
188: try {
189: pageNumber = Integer.parseInt(pageNumberString);
190: } catch (NumberFormatException nfe) {
191: //Ignore. The page number will be set to 1
192: }
193: paginator.setPageNumber(pageNumber);
194: } else {
195:
196: String directionString = request
197: .getParameter(HTML_FIELD_DIRECTION);
198: if (directionString == null) {
199: return;
200: }
201: int direction = 0;
202: try {
203: direction = Integer.parseInt(directionString);
204: } catch (NumberFormatException nfe) {
205: //Ignore.
206: }
207: switch (direction) {
208:
209: case DIRECTION_FIRST:
210: paginator.setFirstPage();
211: break;
212: case DIRECTION_PREV:
213: paginator.setPreviousPage();
214: break;
215: case DIRECTION_NEXT:
216: paginator.setNextPage();
217: break;
218: case DIRECTION_LAST:
219: paginator.setLastPage();
220: break;
221: default:
222: break;
223: }
224: }
225:
226: } else if (action.equals(OPERATION_SORT)) {
227:
228: AbsenceRecordPaginator paginator = (AbsenceRecordPaginator) session
229: .getAttribute(SESSION_ABSENCE_RECORDS);
230:
231: String sortField = request
232: .getParameter(HTML_FIELD_SORT_FIELD);
233: String sortOrder = request
234: .getParameter(HTML_FIELD_SORT_ORDER);
235:
236: if (sortField.equals(ABS_TYPE)) {
237: if (sortOrder.equals(SORT_ORDER_DSC)) {
238: paginator.sortDscByAbsType();
239: } else {
240: paginator.sortAscByAbsType();
241: }
242: } else if (sortField.equals(ABS_BEGIN)) {
243:
244: if (sortOrder.equals(SORT_ORDER_DSC)) {
245: paginator.sortDscByBeginDate();
246: } else {
247: paginator.sortAscByBeginDate();
248: }
249: } else if (sortField.equals(ABS_END)) {
250: if (sortOrder.equals(SORT_ORDER_DSC)) {
251: paginator.sortDscByEndDate();
252: } else {
253: paginator.sortAscByEndDate();
254: }
255: } else if (sortField.equals(ABS_DAYS)) {
256:
257: if (sortOrder.equals(SORT_ORDER_DSC)) {
258: paginator.sortDscByDays();
259: } else {
260: paginator.sortAscByDays();
261: }
262: } else if (sortField.equals(ABS_HOURS)) {
263:
264: if (sortOrder.equals(SORT_ORDER_DSC)) {
265: paginator.sortDscByHours();
266: } else {
267: paginator.sortAscByHours();
268: }
269: }
270: }
271: }
272:
273: public void doEdit(RenderRequest request, RenderResponse response)
274: throws PortletException, IOException {
275:
276: response.setContentType(request.getResponseContentType());
277: }
278:
279: }
|