KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > category > CategoryServices


1 /*
2  * $Id: CategoryServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2005 The Open For Business Project (www.ofbiz.org)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.product.category;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javolution.util.FastList;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilDateTime;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericEntityException;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.entity.condition.EntityCondition;
43 import org.ofbiz.entity.condition.EntityConditionList;
44 import org.ofbiz.entity.condition.EntityExpr;
45 import org.ofbiz.entity.condition.EntityOperator;
46 import org.ofbiz.entity.util.EntityFindOptions;
47 import org.ofbiz.entity.util.EntityListIterator;
48 import org.ofbiz.entity.util.EntityUtil;
49 import org.ofbiz.product.catalog.CatalogWorker;
50 import org.ofbiz.service.DispatchContext;
51 import org.ofbiz.service.ModelService;
52 import org.ofbiz.service.ServiceUtil;
53
54 /**
55  * CategoryServices - Category Services
56  *
57  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
58  * @version $Rev: 5462 $
59  * @since 2.0
60  */

61 public class CategoryServices {
62     
63     public static final String JavaDoc module = CategoryServices.class.getName();
64
65     public static Map JavaDoc getCategoryMembers(DispatchContext dctx, Map JavaDoc context) {
66         GenericDelegator delegator = dctx.getDelegator();
67         String JavaDoc categoryId = (String JavaDoc) context.get("categoryId");
68         GenericValue productCategory = null;
69         List JavaDoc members = null;
70
71         try {
72             productCategory = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", categoryId));
73             members = EntityUtil.filterByDate(productCategory.getRelatedCache("ProductCategoryMember", null, UtilMisc.toList("sequenceNum")), true);
74             if (Debug.verboseOn()) Debug.logVerbose("Category: " + productCategory + " Member Size: " + members.size() + " Members: " + members, module);
75         } catch (GenericEntityException e) {
76             String JavaDoc errMsg = "Problem reading product categories: " + e.getMessage();
77             Debug.logError(e, errMsg, module);
78             return ServiceUtil.returnError(errMsg);
79         }
80         Map JavaDoc result = ServiceUtil.returnSuccess();
81         result.put("category", productCategory);
82         result.put("categoryMembers", members);
83         return result;
84     }
85
86     public static Map JavaDoc getNextPreviousCategoryMembers(DispatchContext dctx, Map JavaDoc context) {
87         GenericDelegator delegator = dctx.getDelegator();
88         String JavaDoc categoryId = (String JavaDoc) context.get("categoryId");
89         String JavaDoc productId = (String JavaDoc) context.get("productId");
90         Integer JavaDoc index = (Integer JavaDoc) context.get("index");
91
92         if (index == null && productId == null) {
93             return ServiceUtil.returnError("Both Index and ProductID cannot be null.");
94         }
95
96         Map JavaDoc values = getCategoryMembers(dctx, context);
97
98         if (values.containsKey(ModelService.ERROR_MESSAGE)) {
99             return values;
100         }
101         if (!values.containsKey("categoryMembers") || values.get("categoryMembers") == null) {
102             return ServiceUtil.returnError("Problem reading category data.");
103         }
104
105         Collection JavaDoc memberCol = (Collection JavaDoc) values.get("categoryMembers");
106         if (memberCol == null || memberCol.size() == 0) {
107             // this is not going to be an error condition because we don't want it to be so critical, ie rolling back the transaction and such
108
return ServiceUtil.returnSuccess("Product not found in the current category.");
109         }
110
111         List JavaDoc memberList = new ArrayList JavaDoc(memberCol);
112
113         if (productId != null && index == null) {
114             Iterator JavaDoc i = memberList.iterator();
115
116             while (i.hasNext()) {
117                 GenericValue v = (GenericValue) i.next();
118
119                 if (v.getString("productId").equals(productId))
120                     index = new Integer JavaDoc(memberList.indexOf(v));
121             }
122         }
123
124         if (index == null) {
125             // this is not going to be an error condition because we don't want it to be so critical, ie rolling back the transaction and such
126
return ServiceUtil.returnSuccess("Product not found in the current category.");
127         }
128
129         Map JavaDoc result = ServiceUtil.returnSuccess();
130         result.put("category", values.get("category"));
131
132         String JavaDoc previous = null;
133         String JavaDoc next = null;
134
135         if (index.intValue() - 1 >= 0 && index.intValue() - 1 < memberList.size()) {
136             previous = ((GenericValue) memberList.get(index.intValue() - 1)).getString("productId");
137             result.put("previousProductId", previous);
138         } else {
139             previous = ((GenericValue) memberList.get(memberList.size() - 1)).getString("productId");
140             result.put("previousProductId", previous);
141         }
142
143         if (index.intValue() + 1 < memberList.size()) {
144             next = ((GenericValue) memberList.get(index.intValue() + 1)).getString("productId");
145             result.put("nextProductId", next);
146         } else {
147             next = ((GenericValue) memberList.get(0)).getString("productId");
148             result.put("nextProductId", next);
149         }
150         return result;
151     }
152
153     public static Map JavaDoc getProductCategoryAndLimitedMembers(DispatchContext dctx, Map JavaDoc context) {
154         GenericDelegator delegator = dctx.getDelegator();
155         String JavaDoc productCategoryId = (String JavaDoc) context.get("productCategoryId");
156         boolean limitView = ((Boolean JavaDoc) context.get("limitView")).booleanValue();
157         int defaultViewSize = ((Integer JavaDoc) context.get("defaultViewSize")).intValue();
158
159         String JavaDoc prodCatalogId = (String JavaDoc) context.get("prodCatalogId");
160
161         boolean useCacheForMembers = (context.get("useCacheForMembers") != null ? ((Boolean JavaDoc) context.get("useCacheForMembers")).booleanValue() : true);
162         boolean activeOnly = (context.get("activeOnly") != null ? ((Boolean JavaDoc) context.get("activeOnly")).booleanValue() : true);
163         // checkViewAllow defaults to false, must be set to true and pass the prodCatalogId to enable
164
boolean checkViewAllow = (context.get("checkViewAllow") != null ? ((Boolean JavaDoc) context.get("checkViewAllow")).booleanValue() : false);
165         
166         Timestamp JavaDoc nowTimestamp = UtilDateTime.nowTimestamp();
167
168         int viewIndex = 1;
169         try {
170             viewIndex = Integer.valueOf((String JavaDoc) context.get("viewIndexString")).intValue();
171         } catch (Exception JavaDoc e) {
172             viewIndex = 1;
173         }
174         
175         int viewSize = defaultViewSize;
176         try {
177             viewSize = Integer.valueOf((String JavaDoc) context.get("viewSizeString")).intValue();
178         } catch (Exception JavaDoc e) {
179             viewSize = defaultViewSize;
180         }
181
182         GenericValue productCategory = null;
183         try {
184             productCategory = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryId));
185         } catch (GenericEntityException e) {
186             Debug.logWarning(e.getMessage(), module);
187             productCategory = null;
188         }
189
190         int listSize = 0;
191         int lowIndex = 0;
192         int highIndex = 0;
193
194         if (limitView) {
195             // get the indexes for the partial list
196
lowIndex = (((viewIndex - 1) * viewSize) + 1);
197             highIndex = viewIndex * viewSize;
198         }
199         
200         List JavaDoc productCategoryMembers = null;
201         if (productCategory != null) {
202             try {
203                 if (useCacheForMembers) {
204                     productCategoryMembers = productCategory.getRelatedCache("ProductCategoryMember", null, UtilMisc.toList("sequenceNum"));
205                     if (activeOnly) {
206                         productCategoryMembers = EntityUtil.filterByDate(productCategoryMembers, true);
207                     }
208                     listSize = productCategoryMembers.size();
209                     if (highIndex > listSize) {
210                         highIndex = listSize;
211                     }
212
213                     if (limitView) {
214                         productCategoryMembers = productCategoryMembers.subList(lowIndex-1, highIndex);
215                     } else {
216                         lowIndex = 1;
217                         highIndex = listSize;
218                     }
219                 } else {
220                     List JavaDoc mainCondList = UtilMisc.toList(new EntityExpr("productCategoryId", EntityOperator.EQUALS, productCategory.getString("productCategoryId")));
221                     if (activeOnly) {
222                         mainCondList.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp));
223                         mainCondList.add(new EntityExpr(new EntityExpr("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, new EntityExpr("thruDate", EntityOperator.GREATER_THAN, nowTimestamp)));
224                     }
225                     EntityCondition mainCond = new EntityConditionList(mainCondList, EntityOperator.AND);
226                 
227                     // set distinct on so we only get one row per order
228
EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true);
229                     // using list iterator
230
EntityListIterator pli = delegator.findListIteratorByCondition("ProductCategoryMember", mainCond, null, null, UtilMisc.toList("sequenceNum", "productId"), findOpts);
231                 
232                     // get the partial list for this page
233
if (limitView) {
234                         productCategoryMembers = pli.getPartialList(lowIndex, viewSize);
235                         // attempt to get the full size
236
pli.last();
237                         listSize = pli.currentIndex();
238                     } else {
239                         productCategoryMembers = pli.getCompleteList();
240                         listSize = productCategoryMembers.size();
241                         lowIndex = 1;
242                         highIndex = listSize;
243                     }
244                     if (productCategoryMembers == null) {
245                         productCategoryMembers = FastList.newInstance();
246                     }
247                 
248                     if (highIndex > listSize) {
249                         highIndex = listSize;
250                     }
251                 
252                     // close the list iterator
253
pli.close();
254                 }
255                 
256                 // first check to see if there is a view allow category and if this product is in it...
257
if (checkViewAllow && prodCatalogId != null && productCategoryMembers != null && productCategoryMembers.size() > 0) {
258                     String JavaDoc viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
259                     if (viewProductCategoryId != null) {
260                         productCategoryMembers = CategoryWorker.filterProductsInCategory(delegator, productCategoryMembers, viewProductCategoryId);
261                     }
262                 }
263             } catch (GenericEntityException e) {
264                 Debug.logError(e, module);
265             }
266         }
267
268         Map JavaDoc result = new HashMap JavaDoc();
269         result.put("viewIndex", new Integer JavaDoc(viewIndex));
270         result.put("viewSize", new Integer JavaDoc(viewSize));
271         result.put("lowIndex", new Integer JavaDoc(lowIndex));
272         result.put("highIndex", new Integer JavaDoc(highIndex));
273         result.put("listSize", new Integer JavaDoc(listSize));
274         if (productCategory != null) result.put("productCategory", productCategory);
275         if (productCategoryMembers != null) result.put("productCategoryMembers", productCategoryMembers);
276         return result;
277     }
278 }
279
Popular Tags