001: /*
002: * Created on Jan 17, 2005
003: */
004: package org.openedit.store.modules;
005:
006: import java.io.File;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import org.openedit.store.Store;
011: import org.openedit.store.orders.OrderArchive;
012: import org.openedit.store.orders.OrderId;
013: import org.openedit.store.orders.OrderSearcher;
014: import org.openedit.store.orders.OrderState;
015: import org.openedit.store.orders.SubmittedOrder;
016:
017: import com.openedit.WebPageRequest;
018: import com.openedit.hittracker.HitTracker;
019: import com.openedit.modules.BaseModule;
020:
021: /**
022: * @author dbrown
023: *
024: */
025: public class OrderModule extends BaseModule {
026: protected static final String ORDERMODULE = "ordermodule";
027: protected static final String ORDERLIST = "orderlist";
028: protected static final String ORDERIDLIST = "orderidlist";
029: protected static final String ORDERLISTLASTMODIFIED = "orderlistlastmodified";
030: protected static final String CUSTOMERLIST = "customerlist";
031: protected static final String ITEMLIST = "itemlist";
032: protected OrderSearcher fieldOrderSearcher;
033:
034: /* public List getCustomerList( WebPageRequest inContext ) throws Exception
035: {
036: getOrderList( inContext );
037: List orderList = (List)inContext.getPageValue( ORDERLIST );
038: List customerList = new ArrayList();
039: List idList = new ArrayList();
040: for ( Iterator iter = orderList.iterator(); iter.hasNext(); )
041: {
042: SubmittedOrder order = (SubmittedOrder) iter.next();
043: String id = order.getCustomer().getUserName();
044: if ( idList.contains( id ) )
045: {
046: continue;
047: }
048: idList.add(id);
049: customerList.add( order.getCustomer() );
050: }
051: inContext.putPageValue( CUSTOMERLIST, customerList );
052: return customerList;
053: }
054: */
055:
056: public SubmittedOrder getOrderFromNumber(WebPageRequest inContext,
057: List inOrderIdList, String inOrderNumber) throws Exception {
058: Store store = getStore(inContext);
059: if (inOrderNumber != null) {
060: for (Iterator it = inOrderIdList.iterator(); it.hasNext();) {
061: OrderId orderid = (OrderId) it.next();
062: if (inOrderNumber.equals(orderid.getOrderId())) {
063: return store.getOrderArchive().loadSubmittedOrder(
064: store, orderid.getUsername(),
065: orderid.getOrderId());
066: }
067: }
068: }
069: return null;
070: }
071:
072: public HitTracker getOrdersForUser(WebPageRequest inReq)
073: throws Exception {
074: String pagenum = inReq.getRequestParameter("page");
075: Store store = getStore(inReq);
076: if (pagenum == null) {
077: HitTracker orders = getOrderSearcher().fieldSearchForUser(
078: inReq, store, inReq.getUser());
079: inReq.putPageValue(ORDERIDLIST, orders);
080: return orders;
081: } else {
082: return getOrderSearcher().loadPageOfSearch(inReq,
083: store.getCatalogId(), store.getOrderSearch());
084: }
085: }
086:
087: public HitTracker getOrdersForAll(WebPageRequest inReq)
088: throws Exception {
089: Store store = getStore(inReq);
090: HitTracker orders = getOrderSearcher()
091: .fieldSearch(inReq, store);
092: inReq.putPageValue(ORDERIDLIST, orders);
093: return orders;
094:
095: }
096:
097: public List getOrderIdList(WebPageRequest inReq) throws Exception {
098: Store store = getStore(inReq);
099: List orderIds = store.getOrderSearch().listOrders(store, inReq);
100:
101: inReq.putPageValue(ORDERIDLIST, orderIds);
102: return orderIds;
103:
104: }
105:
106: public void changeOrderStatus(WebPageRequest inContext)
107: throws Exception {
108: String id = inContext.getRequestParameter("orderstatus");
109: String orderNumber = inContext
110: .getRequestParameter("ordernumber");
111: if (id != null && orderNumber != null) {
112: SubmittedOrder order = getOrderFromNumber(inContext,
113: getOrderIdList(inContext), orderNumber);
114: Store store = getStore(inContext);
115:
116: OrderState state = new OrderState();
117: state.setId(id);
118:
119: OrderArchive archive = store.getOrderArchive();
120: archive.changeOrderStatus(state, store, order);
121: order.setOrderState(state);
122: store.getOrderSearch().updateIndex(order);
123: }
124:
125: }
126:
127: public void captureOrder(WebPageRequest inContext) throws Exception {
128: String orderNumber = inContext
129: .getRequestParameter("ordernumber");
130: SubmittedOrder order = getOrderFromNumber(inContext,
131: getOrderIdList(inContext), orderNumber);
132: Store store = getStore(inContext);
133:
134: OrderArchive archive = store.getOrderArchive();
135: archive.captureOrder(inContext, store, order);
136: inContext.putPageValue("order", order);
137: }
138:
139: protected Store getStore(WebPageRequest inContext) throws Exception {
140: CartModule cartm = (CartModule) getModuleManager().getModule(
141: "CartModule");
142: return cartm.getStore(inContext);
143: }
144:
145: protected File getOrdersDirectory(Store inStore) {
146: return new File(inStore.getStoreDirectory(), "orders");
147: }
148:
149: public void loadOrder(WebPageRequest inRequest) throws Exception {
150: String path = inRequest.getPath();
151: if (path.endsWith(".html")) {
152: List orderIdList = (List) inRequest
153: .getPageValue("orderlist");
154: if (orderIdList == null) {
155: orderIdList = getOrderIdList(inRequest);
156: }
157: String orderNumber = path.substring(
158: path.lastIndexOf("/") + 1, path
159: .lastIndexOf(".html"));
160: SubmittedOrder order = getOrderFromNumber(inRequest,
161: orderIdList, orderNumber);
162: inRequest.putPageValue("order", order);
163: }
164: }
165:
166: public OrderSearcher getOrderSearcher() {
167: if (fieldOrderSearcher == null) {
168: setOrderSearcher(new OrderSearcher());
169: }
170: return fieldOrderSearcher;
171:
172: }
173:
174: public void setOrderSearcher(OrderSearcher inOrderSearcher) {
175: fieldOrderSearcher = inOrderSearcher;
176: }
177:
178: public void reIndexOrders(WebPageRequest inReq) throws Exception {
179: Store store = getStore(inReq);
180: store.getOrderSearch().reIndexAll();
181: }
182: }
|