KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > supplier > SupplierProductServices


1 package org.ofbiz.product.supplier;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.ofbiz.base.util.Debug;
10 import org.ofbiz.base.util.UtilDateTime;
11 import org.ofbiz.base.util.UtilMisc;
12 import org.ofbiz.entity.GenericDelegator;
13 import org.ofbiz.entity.GenericEntityException;
14 import org.ofbiz.entity.GenericValue;
15 import org.ofbiz.entity.condition.EntityExpr;
16 import org.ofbiz.entity.condition.EntityOperator;
17 import org.ofbiz.entity.util.EntityUtil;
18 import org.ofbiz.product.product.ProductWorker;
19 import org.ofbiz.service.DispatchContext;
20 import org.ofbiz.service.ServiceUtil;
21
22 /**
23  * Services for suppliers of products
24  *
25  * @author <a HREF="mailto:schen@graciousstyle.com">Si Chen</a>
26  * @version $Revision: 6357 $
27  * @since 3.0
28  */

29 public class SupplierProductServices {
30
31     public static final String JavaDoc module = SupplierProductServices.class.getName();
32     public static final String JavaDoc resource = "ProductUiLabels";
33     
34     /*
35      * Parameters: productId, partyId, currencyUomId, quantity
36      * Result: a List of SupplierProduct entities for productId,
37      * filtered by date and optionally by partyId, ordered with lowest price first
38      */

39     public static Map JavaDoc getSuppliersForProduct(DispatchContext dctx, Map JavaDoc context) {
40         Map JavaDoc results = new HashMap JavaDoc();
41         GenericDelegator delegator = dctx.getDelegator();
42         
43         GenericValue product = null;
44         String JavaDoc productId = (String JavaDoc) context.get("productId");
45         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
46         String JavaDoc currencyUomId = (String JavaDoc) context.get("currencyUomId");
47         Double JavaDoc quantity =(Double JavaDoc) context.get("quantity");
48         try {
49             product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId));
50             if (product == null) {
51                 results = ServiceUtil.returnSuccess();
52                 results.put("supplierProducts",null);
53                 return results;
54             }
55             List JavaDoc supplierProducts = product.getRelatedCache("SupplierProduct");
56             
57             // if there were no related SupplierProduct entities and the item is a variant, then get the SupplierProducts of the virtual parent product
58
if (supplierProducts.size() == 0 && product.getString("isVariant") != null && product.getString("isVariant").equals("Y")) {
59                 String JavaDoc virtualProductId = ProductWorker.getVariantVirtualId(product);
60                 GenericValue virtualProduct = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", virtualProductId));
61                 if (virtualProduct != null) {
62                     supplierProducts = virtualProduct.getRelatedCache("SupplierProduct");
63                 }
64             }
65             
66             // filter the list down by the partyId if one is provided
67
if (partyId != null) {
68                 supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("partyId", partyId));
69             }
70             
71             // filter the list down by the currencyUomId if one is provided
72
if (currencyUomId != null) {
73                 supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("currencyUomId", currencyUomId));
74             }
75             
76             // filter the list down by the minimumOrderQuantity if one is provided
77
if (quantity != null) {
78                 //minimumOrderQuantity
79
supplierProducts = EntityUtil.filterByCondition(supplierProducts, new EntityExpr("minimumOrderQuantity", EntityOperator.LESS_THAN_EQUAL_TO, quantity));
80             }
81             
82             // filter the list down again by date before returning it
83
supplierProducts = EntityUtil.filterByDate(supplierProducts, UtilDateTime.nowTimestamp(), "availableFromDate", "availableThruDate", true);
84             
85             //sort resulting list of SupplierProduct entities by price in ASCENDING order
86
supplierProducts = EntityUtil.orderBy(supplierProducts, UtilMisc.toList("lastPrice ASC"));
87             
88             results = ServiceUtil.returnSuccess();
89             results.put("supplierProducts", supplierProducts);
90         } catch (GenericEntityException ex) {
91             Debug.logError(ex, ex.getMessage(), module);
92             return ServiceUtil.returnError(ex.getMessage());
93         }catch(Exception JavaDoc ex){
94             Debug.logError(ex, ex.getMessage(), module);
95             return ServiceUtil.returnError(ex.getMessage());
96         }
97         return results;
98     }
99
100     /*
101      * Parameters: partyId of a supplier and productFeatures, a Collection (usually List) of product features
102      * Service will convert each feature in the Collection, changing their idCode and description based on the
103      * SupplierProduct entity for that supplier party and feature, and return it as convertedProductFeatures
104      */

105     public static Map JavaDoc convertFeaturesForSupplier(DispatchContext dctx, Map JavaDoc context) {
106         Map JavaDoc results = new HashMap JavaDoc();
107         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
108         Collection JavaDoc features = (Collection JavaDoc) context.get("productFeatures");
109
110         try {
111             if (partyId != null && features != null && features.size() > 0) {
112                 // loop through all the features, find the related SupplierProductFeature for the given partyId, and
113
// substitue description and idCode
114
for (Iterator JavaDoc fI = features.iterator(); fI.hasNext(); ) {
115                     GenericValue nextFeature = (GenericValue) fI.next();
116                     List JavaDoc supplierFeatures = EntityUtil.filterByAnd(nextFeature.getRelated("SupplierProductFeature"),
117                                                                    UtilMisc.toMap("partyId", partyId));
118                     GenericValue supplierFeature = null;
119
120                     if ((supplierFeatures != null) && (supplierFeatures.size() > 0)) {
121                         supplierFeature = (GenericValue) supplierFeatures.get(0);
122                         if (supplierFeature.get("description") != null) {
123                             nextFeature.put("description", supplierFeature.get("description"));
124                         }
125                         if (supplierFeature.get("idCode") != null) {
126                             nextFeature.put("idCode", supplierFeature.get("idCode"));
127                         }
128                         // TODO: later, do some kind of uom/quantity conoversion with the UomConversion entity
129
}
130                 }
131             }
132             results = ServiceUtil.returnSuccess();
133             results.put("convertedProductFeatures", features);
134         } catch (GenericEntityException ex) {
135             Debug.logError(ex, ex.getMessage(), module);
136             return ServiceUtil.returnError(ex.getMessage());
137         }
138         return results;
139     }
140 }
141
Popular Tags