1 package org.ofbiz.product.supplier; 2 3 import java.util.Collection ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 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 29 public class SupplierProductServices { 30 31 public static final String module = SupplierProductServices.class.getName(); 32 public static final String resource = "ProductUiLabels"; 33 34 39 public static Map getSuppliersForProduct(DispatchContext dctx, Map context) { 40 Map results = new HashMap (); 41 GenericDelegator delegator = dctx.getDelegator(); 42 43 GenericValue product = null; 44 String productId = (String ) context.get("productId"); 45 String partyId = (String ) context.get("partyId"); 46 String currencyUomId = (String ) context.get("currencyUomId"); 47 Double quantity =(Double ) 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 supplierProducts = product.getRelatedCache("SupplierProduct"); 56 57 if (supplierProducts.size() == 0 && product.getString("isVariant") != null && product.getString("isVariant").equals("Y")) { 59 String 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 if (partyId != null) { 68 supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("partyId", partyId)); 69 } 70 71 if (currencyUomId != null) { 73 supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("currencyUomId", currencyUomId)); 74 } 75 76 if (quantity != null) { 78 supplierProducts = EntityUtil.filterByCondition(supplierProducts, new EntityExpr("minimumOrderQuantity", EntityOperator.LESS_THAN_EQUAL_TO, quantity)); 80 } 81 82 supplierProducts = EntityUtil.filterByDate(supplierProducts, UtilDateTime.nowTimestamp(), "availableFromDate", "availableThruDate", true); 84 85 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 ex){ 94 Debug.logError(ex, ex.getMessage(), module); 95 return ServiceUtil.returnError(ex.getMessage()); 96 } 97 return results; 98 } 99 100 105 public static Map convertFeaturesForSupplier(DispatchContext dctx, Map context) { 106 Map results = new HashMap (); 107 String partyId = (String ) context.get("partyId"); 108 Collection features = (Collection ) context.get("productFeatures"); 109 110 try { 111 if (partyId != null && features != null && features.size() > 0) { 112 for (Iterator fI = features.iterator(); fI.hasNext(); ) { 115 GenericValue nextFeature = (GenericValue) fI.next(); 116 List 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 } 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 |