001: /**
002: * $Id: SAPSalesOrderPortlet.java,v 1.10 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 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.lang.StringBuffer;
028: import java.util.List;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.logging.Logger;
032: import java.util.regex.*;
033: import com.sun.portal.sapportlet.salesorder.*;
034: import com.sun.portal.sapportlet.customer.*;
035: import com.sun.portal.sapportlet.util.ssoa.SAPAuthUtils;
036:
037: /**
038: * This is the SAP Customer Order Portlet. It can be used
039: * to search for all sales order for a particular customer.
040: * This portlet depends on the Customer search portlet.
041: *
042: * @author nk137934
043: */
044:
045: public class SAPSalesOrderPortlet extends GenericPortlet implements
046: SAPPortletConstants {
047:
048: private static Logger logger = Logger
049: .getLogger(SAPPortletConstants.LOGGER_NAMESPACE);
050:
051: /*
052: * On init, the portlet creates & inits a SalesOrderManager object.
053: * The actual queries are delegated to this object
054: */
055: public void init(PortletConfig config) throws PortletException {
056: super .init(config);
057: }
058:
059: /*
060: * The doView method of the SAPSalesOrderPortlet.
061: * The customer object is obtained from the selection made from
062: * the customer search portlet
063: */
064: public void doView(RenderRequest request, RenderResponse response)
065: throws PortletException, IOException {
066:
067: PortletSession session = request.getPortletSession();
068: SAPUserConfig userConfig = (SAPUserConfig) session
069: .getAttribute(SESSION_USERCONFIG,
070: PortletSession.APPLICATION_SCOPE);
071: if (userConfig == null) {
072: userConfig = SAPAuthUtils
073: .checkForUserAuthentication(request);
074: }
075:
076: if (userConfig == null) {
077: // Dispatch to the configuration page
078: dispatchToConfigErrorPage(request, response);
079: return;
080: }
081: //Retrieve the delegate object from the session
082: SalesOrderManager manager = (SalesOrderManager) session
083: .getAttribute(SESSION_SOMANAGER);
084: // If no delegate has been created, create one!
085: // The delegate also needs to be created if the user config object has changed
086:
087: if (manager == null
088: || !manager.getUserConfig().equals(userConfig)) {
089: String endPoint = SAPAuthUtils.getSAPEndPoint(request);
090: try {
091: manager = createSOManager(userConfig, session, endPoint);
092: } catch (Exception gEx) {
093: logger.severe("failed to create so manager");
094: // Dispatch to the configuration page
095: dispatchToConfigErrorPage(request, response);
096: return;
097: }
098: }
099: Customer customer = (Customer) session.getAttribute(
100: SAPPortletConstants.SESSION_SELECTED_CUST,
101: PortletSession.APPLICATION_SCOPE);
102: String existingCustomerNumber = null;
103: String sessionState = (String) session
104: .getAttribute(SESSION_STATE);
105: if (sessionState != null) {
106: existingCustomerNumber = (String) session
107: .getAttribute(RENDER_SALESORDER_SEARCH);
108: session.removeAttribute(SESSION_STATE);
109: } else {
110: //remove session no records attribute,if present.
111: session.removeAttribute(SESSION_STATE_NO_RECORDS);
112: //existingCustomerNumber is made null
113: }
114: // If no customers are selected, nothing needs to be shown
115: if (customer != null) {
116:
117: // Create a new list if the currently selected customer is different
118: if (existingCustomerNumber == null
119: || !existingCustomerNumber.equals(customer
120: .getCustomerNumber())) {
121:
122: List orders = manager
123: .findSalesOrdersForCustomer(customer
124: .getCustomerNumber());
125: SalesOrderPaginator pagniator = new SalesOrderPaginator(
126: orders);
127: session.setAttribute(SESSION_SALESORDERS, pagniator);
128: //session.removeAttribute(RENDER_SALESORDER_SEARCH);
129: session.setAttribute(RENDER_SALESORDER_SEARCH, customer
130: .getCustomerNumber());
131: }
132: }
133:
134: response.setContentType(request.getResponseContentType());
135: PortletRequestDispatcher dispatcher = getPortletContext()
136: .getRequestDispatcher(SALES_ORDER_PAGE);
137: dispatcher.include(request, response);
138:
139: }
140:
141: /*
142: * Invoke the appropriate search depending on the actual operation.
143: */
144: public void processAction(ActionRequest request,
145: ActionResponse response) {
146:
147: PortletSession session = request.getPortletSession();
148: //set session processed attribute.
149: session.setAttribute(SESSION_STATE, SESSION_STATE);
150: //remove session no records attribute,if present.
151: session.removeAttribute(SESSION_STATE_NO_RECORDS);
152: //SalesOrderPaginator orPaginator = (SalesOrderPaginator)session.getAttribute(SESSION_SALESORDERS);
153: //List salesOrders = orPaginator.getAllItems();
154: String action = request.getParameter(HTML_FIELD_OPERATION);
155: if (action.equals(OPERATION_SEARCH)) {
156: // Recompute all sales order for this customer
157: Customer customer = (Customer) session.getAttribute(
158: SAPPortletConstants.SESSION_SELECTED_CUST,
159: PortletSession.APPLICATION_SCOPE);
160: SalesOrderManager manager = (SalesOrderManager) session
161: .getAttribute(SESSION_SOMANAGER);
162: List salesOrders = manager
163: .findSalesOrdersForCustomer(customer
164: .getCustomerNumber());
165: //Get the new query String
166: String queryString = request
167: .getParameter(HTML_FIELD_SEARCH_TERM);
168: //Get the new query field
169: String queryField = request
170: .getParameter(HTML_FIELD_QUERY_FIELD);
171: System.out.println("SAPSalesOrderPortlet queryString=>"
172: + queryString);
173: System.out.println("SAPSalesOrderPortlet queryField=>"
174: + queryField);
175: Pattern pattern = null;
176:
177: //formatting the regular expression.
178: StringBuffer queryStringBuffer = new StringBuffer(
179: queryString);
180: queryStringBuffer = queryStringBuffer.append(" ");
181: int queryBufferLength = queryStringBuffer.length();
182: int bufferIndex;
183: for (bufferIndex = 0; bufferIndex < queryBufferLength; bufferIndex++) {
184: if (queryStringBuffer.charAt(bufferIndex) == '*') {
185: queryStringBuffer = queryStringBuffer.replace(
186: bufferIndex, bufferIndex + 1, ".*");
187: bufferIndex++;
188: queryBufferLength++;
189: } else if (queryStringBuffer.charAt(bufferIndex) == '?') {
190: queryStringBuffer = queryStringBuffer.replace(
191: bufferIndex, bufferIndex + 1, ".");
192: }
193: }
194: queryString = queryStringBuffer.toString();
195: queryString = queryString.trim();
196: try {
197: pattern = Pattern.compile(queryString);
198: } catch (PatternSyntaxException pse) {
199: // In case of a malformed pattern, search all
200: pattern = Pattern.compile(".");
201: }
202: System.out.println("SAPSalesOrderPortlet pattern=>"
203: + pattern);
204: List filteredList = new ArrayList();
205:
206: if (queryField.equals(QUERY_ON_PURCHASE)) {
207: //search through salesOrders and return only those with the purchase number
208: for (int k = 0; k < salesOrders.size(); k++) {
209: SalesOrder order = (SalesOrder) salesOrders.get(k);
210: Matcher matcher = pattern.matcher(order
211: .getPurchaseNumber());
212: if (matcher.matches()) {
213: filteredList.add(order);
214: }
215: }
216: } else {
217: // This is a query on item number
218: //search through salesOrders and return only those with the item number
219: for (int k = 0; k < salesOrders.size(); k++) {
220: SalesOrder order = (SalesOrder) salesOrders.get(k);
221: Matcher matcher = pattern.matcher(order
222: .getItemNumber());
223: if (matcher.find()) {
224: filteredList.add(order);
225: }
226: }
227: }
228: SalesOrderPaginator paginator = new SalesOrderPaginator(
229: filteredList);
230: session.setAttribute(SESSION_SALESORDERS, paginator);
231: if (filteredList.isEmpty()) {
232: session.setAttribute(SESSION_STATE_NO_RECORDS,
233: SESSION_STATE_NO_RECORDS);
234: }
235: } else if (action.equals(OPERATION_ITERATE)) {
236:
237: SalesOrderPaginator paginator = (SalesOrderPaginator) session
238: .getAttribute(SESSION_SALESORDERS);
239: String pageNumberString = request
240: .getParameter(HTML_FIELD_PAGENUMBER);
241: if (pageNumberString != null) {
242: int pageNumber = 1;
243: try {
244: pageNumber = Integer.parseInt(pageNumberString);
245: } catch (NumberFormatException nfe) {
246: //Ignore. The page number will be set to 1
247: }
248: paginator.setPageNumber(pageNumber);
249: } else {
250:
251: String directionString = request
252: .getParameter(HTML_FIELD_DIRECTION);
253: if (directionString == null) {
254: return;
255: }
256: int direction = 0;
257: try {
258: direction = Integer.parseInt(directionString);
259: } catch (NumberFormatException nfe) {
260: //Ignore.
261: }
262: switch (direction) {
263:
264: case DIRECTION_FIRST:
265: paginator.setFirstPage();
266: break;
267: case DIRECTION_PREV:
268: paginator.setPreviousPage();
269: break;
270: case DIRECTION_NEXT:
271: paginator.setNextPage();
272: break;
273: case DIRECTION_LAST:
274: paginator.setLastPage();
275: break;
276: default:
277: break;
278: }
279: }
280:
281: } else if (action.equals(OPERATION_SORT)) {
282:
283: // Needs clean up: switch case is better
284: SalesOrderPaginator paginator = (SalesOrderPaginator) session
285: .getAttribute(SESSION_SALESORDERS);
286:
287: String sortField = request
288: .getParameter(HTML_FIELD_SORT_FIELD);
289: String sortOrder = request
290: .getParameter(HTML_FIELD_SORT_ORDER);
291:
292: if (sortField.equals(SO_PUR_NUM)) {
293: if (sortOrder.equals(SORT_ORDER_DSC)) {
294: paginator.sortDscByPurchaseNumber();
295: } else {
296: paginator.sortAscByPurchaseNumber();
297: }
298: } else if (sortField.equals(SO_STATUS)) {
299: if (sortOrder.equals(SORT_ORDER_DSC)) {
300: paginator.sortDscByStatus();
301: } else {
302: paginator.sortAscByStatus();
303: }
304: }
305: }
306: }
307:
308: public void doHelp(RenderRequest request, RenderResponse response)
309: throws PortletException, IOException {
310:
311: response.setContentType(request.getResponseContentType());
312: PortletRequestDispatcher dispatcher = getPortletContext()
313: .getRequestDispatcher(SALES_ORDER_HELP_PAGE);
314: dispatcher.include(request, response);
315:
316: }
317:
318: public void dispatchToConfigErrorPage(RenderRequest request,
319: RenderResponse response) throws PortletException,
320: IOException {
321:
322: response.setContentType(request.getResponseContentType());
323: PortletRequestDispatcher dispatcher = getPortletContext()
324: .getRequestDispatcher(USER_NO_CONFIG_PAGE);
325: dispatcher.include(request, response);
326:
327: }
328:
329: private SalesOrderManager createSOManager(SAPUserConfig userConfig,
330: PortletSession session, String endPoint)
331: throws PortletException, java.io.IOException {
332:
333: SalesOrderManager manager = new SalesOrderManager();
334: // If the init of the manager fails, there is some misconfiguration.
335: try {
336: manager.init(userConfig, endPoint);
337: } catch (Throwable throwable) {
338: //logger.severe("Failed to initialize employee delegate");
339: }
340: session.setAttribute(SESSION_SOMANAGER, manager);
341: return manager;
342: }
343: }
|