001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.ofbiz.order.order;
020:
021: import java.util.*;
022: import javax.servlet.http.*;
023: import javolution.util.*;
024: import org.ofbiz.base.util.*;
025: import org.ofbiz.entity.*;
026: import org.ofbiz.entity.condition.*;
027: import org.ofbiz.entity.util.*;
028:
029: /**
030: * Session object for keeping track of the list of orders.
031: * The state of the list is preserved here instead of
032: * via url parameters, which can get messy. There
033: * are three types of state: Order State, Order Type,
034: * and pagination position.
035: *
036: * Also provides convenience methods for retrieving
037: * the right set of data for a particular state.
038: *
039: * TODO: this can be generalized to use a set of State
040: * objects, including Pagination. Think about design
041: * patterns in Fowler.
042: */
043: public class OrderListState {
044:
045: public static final String module = OrderListState.class.getName();
046: public static final String SESSION_KEY = "__ORDER_LIST_STATUS__";
047: public static final String VIEW_SIZE_PARAM = "viewSize";
048: public static final String VIEW_INDEX_PARAM = "viewIndex";
049:
050: // state variables
051: protected int viewSize;
052: protected int viewIndex;
053: protected Map orderStatusState;
054: protected Map orderTypeState;
055: protected Map orderFilterState;
056: protected int orderListSize;
057:
058: // parameter to ID maps
059: protected static final Map parameterToOrderStatusId;
060: protected static final Map parameterToOrderTypeId;
061: protected static final Map parameterToFilterId;
062: static {
063: Map map = FastMap.newInstance();
064: map.put("viewcompleted", "ORDER_COMPLETED");
065: map.put("viewcancelled", "ORDER_CANCELLED");
066: map.put("viewrejected", "ORDER_REJECTED");
067: map.put("viewapproved", "ORDER_APPROVED");
068: map.put("viewcreated", "ORDER_CREATED");
069: map.put("viewprocessing", "ORDER_PROCESSING");
070: map.put("viewsent", "ORDER_SENT");
071: map.put("viewhold", "ORDER_HOLD");
072: parameterToOrderStatusId = map;
073:
074: map = FastMap.newInstance();
075: map.put("view_SALES_ORDER", "SALES_ORDER");
076: map.put("view_PURCHASE_ORDER", "PURCHASE_ORDER");
077: parameterToOrderTypeId = map;
078:
079: map = FastMap.newInstance();
080: map.put("filterInventoryProblems", "filterInventoryProblems");
081: map.put("filterAuthProblems", "filterAuthProblems");
082: map.put("filterPartiallyReceivedPOs",
083: "filterPartiallyReceivedPOs");
084: map.put("filterPOsOpenPastTheirETA",
085: "filterPOsOpenPastTheirETA");
086: map.put("filterPOsWithRejectedItems",
087: "filterPOsWithRejectedItems");
088: parameterToFilterId = map;
089: }
090:
091: //============= Initialization and Request methods ===================//
092:
093: /**
094: * Initializes the order list state with default values. Do not use directly,
095: * instead use getInstance().
096: */
097: protected OrderListState() {
098: viewSize = 10;
099: viewIndex = 0;
100: orderStatusState = FastMap.newInstance();
101: orderTypeState = FastMap.newInstance();
102: orderFilterState = FastMap.newInstance();
103:
104: // defaults (TODO: configuration)
105: orderStatusState.put("viewcreated", "Y");
106: orderStatusState.put("viewprocessing", "Y");
107: orderStatusState.put("viewapproved", "Y");
108: orderStatusState.put("viewhold", "N");
109: orderStatusState.put("viewcompleted", "N");
110: orderStatusState.put("viewsent", "N");
111: orderStatusState.put("viewrejected", "N");
112: orderStatusState.put("viewcancelled", "N");
113: orderTypeState.put("view_SALES_ORDER", "Y");
114: }
115:
116: /**
117: * Retrieves the current user's OrderListState from the session
118: * or creates a new one with defaults.
119: */
120: public static OrderListState getInstance(HttpServletRequest request) {
121: HttpSession session = request.getSession();
122: OrderListState status = (OrderListState) session
123: .getAttribute(SESSION_KEY);
124: if (status == null) {
125: status = new OrderListState();
126: session.setAttribute(SESSION_KEY, status);
127: }
128: return status;
129: }
130:
131: /**
132: * Given a request, decides what state to change. If a parameter changeStatusAndTypeState
133: * is present with value "Y", the status and type state will be updated. Otherwise, if the
134: * viewIndex and viewSize parameters are present, the pagination changes.
135: */
136: public void update(HttpServletRequest request) {
137: if ("Y"
138: .equals(request
139: .getParameter("changeStatusAndTypeState"))) {
140: changeOrderListStates(request);
141: } else {
142: String viewSizeParam = request
143: .getParameter(VIEW_SIZE_PARAM);
144: String viewIndexParam = request
145: .getParameter(VIEW_INDEX_PARAM);
146: if (!UtilValidate.isEmpty(viewSizeParam)
147: && !UtilValidate.isEmpty(viewIndexParam))
148: changePaginationState(viewSizeParam, viewIndexParam);
149: }
150: }
151:
152: private void changePaginationState(String viewSizeParam,
153: String viewIndexParam) {
154: try {
155: viewSize = Integer.parseInt(viewSizeParam);
156: viewIndex = Integer.parseInt(viewIndexParam);
157: } catch (NumberFormatException e) {
158: Debug
159: .logWarning(
160: "Values of "
161: + VIEW_SIZE_PARAM
162: + " ["
163: + viewSizeParam
164: + "] and "
165: + VIEW_INDEX_PARAM
166: + " ["
167: + viewIndexParam
168: + "] must both be Integers. Not paginating order list.",
169: module);
170: }
171: }
172:
173: private void changeOrderListStates(HttpServletRequest request) {
174: for (Iterator iter = parameterToOrderStatusId.keySet()
175: .iterator(); iter.hasNext();) {
176: String param = (String) iter.next();
177: String value = request.getParameter(param);
178: if ("Y".equals(value)) {
179: orderStatusState.put(param, "Y");
180: } else {
181: orderStatusState.put(param, "N");
182: }
183: }
184: for (Iterator iter = parameterToOrderTypeId.keySet().iterator(); iter
185: .hasNext();) {
186: String param = (String) iter.next();
187: String value = request.getParameter(param);
188: if ("Y".equals(value)) {
189: orderTypeState.put(param, "Y");
190: } else {
191: orderTypeState.put(param, "N");
192: }
193: }
194: for (Iterator iter = parameterToFilterId.keySet().iterator(); iter
195: .hasNext();) {
196: String param = (String) iter.next();
197: String value = request.getParameter(param);
198: if ("Y".equals(value)) {
199: orderFilterState.put(param, "Y");
200: } else {
201: orderFilterState.put(param, "N");
202: }
203: }
204: viewIndex = 0;
205: }
206:
207: //============== Get and Set methods =================//
208:
209: public Map getOrderStatusState() {
210: return orderStatusState;
211: };
212:
213: public Map getOrderTypeState() {
214: return orderTypeState;
215: }
216:
217: public Map getorderFilterState() {
218: return orderFilterState;
219: }
220:
221: public boolean hasStatus(String param) {
222: return ("Y".equals(orderStatusState.get(param)));
223: }
224:
225: public boolean hasType(String param) {
226: return ("Y".equals(orderTypeState.get(param)));
227: }
228:
229: public boolean hasFilter(String param) {
230: return ("Y".equals(orderFilterState.get(param)));
231: }
232:
233: public boolean hasAllStatus() {
234: for (Iterator iter = orderStatusState.values().iterator(); iter
235: .hasNext();) {
236: if (!"Y".equals(iter.next()))
237: return false;
238: }
239: return true;
240: }
241:
242: public int getViewSize() {
243: return viewSize;
244: }
245:
246: public int getViewIndex() {
247: return viewIndex;
248: }
249:
250: public int getSize() {
251: return orderListSize;
252: }
253:
254: public boolean hasPrevious() {
255: return (viewIndex > 0);
256: }
257:
258: public boolean hasNext() {
259: return (viewIndex < getSize() / viewSize);
260: }
261:
262: /**
263: * Get the OrderHeaders corresponding to the state.
264: */
265: public List getOrders(String facilityId, GenericDelegator delegator)
266: throws GenericEntityException {
267: List allConditions = new ArrayList();
268:
269: if (facilityId != null) {
270: allConditions.add(new EntityExpr("originFacilityId",
271: EntityOperator.EQUALS, facilityId));
272: }
273:
274: List statusConditions = new ArrayList();
275: for (Iterator iter = orderStatusState.keySet().iterator(); iter
276: .hasNext();) {
277: String status = (String) iter.next();
278: if (!hasStatus(status))
279: continue;
280: statusConditions.add(new EntityExpr("statusId",
281: EntityOperator.EQUALS, parameterToOrderStatusId
282: .get(status)));
283: }
284: List typeConditions = new ArrayList();
285: for (Iterator iter = orderTypeState.keySet().iterator(); iter
286: .hasNext();) {
287: String type = (String) iter.next();
288: if (!hasType(type))
289: continue;
290: typeConditions.add(new EntityExpr("orderTypeId",
291: EntityOperator.EQUALS, parameterToOrderTypeId
292: .get(type)));
293: }
294:
295: EntityCondition statusConditionsList = new EntityConditionList(
296: statusConditions, EntityOperator.OR);
297: EntityCondition typeConditionsList = new EntityConditionList(
298: typeConditions, EntityOperator.OR);
299: if ((typeConditions.size() > 0)
300: && (statusConditions.size() > 0)) {
301: allConditions.add(statusConditionsList);
302: allConditions.add(typeConditionsList);
303: }
304:
305: EntityCondition queryConditionsList = new EntityConditionList(
306: allConditions, EntityOperator.AND);
307: EntityFindOptions options = new EntityFindOptions(true,
308: EntityFindOptions.TYPE_SCROLL_INSENSITIVE,
309: EntityFindOptions.CONCUR_READ_ONLY, true);
310: EntityListIterator iterator = delegator
311: .findListIteratorByCondition("OrderHeader",
312: queryConditionsList, null, null, UtilMisc
313: .toList("orderDate DESC"), options);
314:
315: // get subset corresponding to pagination state
316: List orders = iterator.getPartialList(viewSize * viewIndex,
317: viewSize);
318: iterator.last();
319: orderListSize = iterator.currentIndex();
320: iterator.close();
321: //Debug.logInfo("### size of list: " + orderListSize, module);
322: return orders;
323: }
324:
325: public String toString() {
326: StringBuffer buff = new StringBuffer("OrderListState:\n\t");
327: buff.append("viewIndex=").append(viewIndex).append(
328: ", viewSize=").append(viewSize).append("\n\t");
329: buff.append(getOrderStatusState().toString()).append("\n\t");
330: buff.append(getOrderTypeState().toString()).append("\n\t");
331: buff.append(getorderFilterState().toString()).append("\n\t");
332: return buff.toString();
333: }
334: }
|