1 17 18 package org.ofbiz.manufacturing.bom; 19 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 25 import org.ofbiz.base.util.UtilMisc; 26 import org.ofbiz.entity.GenericDelegator; 27 import org.ofbiz.entity.GenericEntityException; 28 import org.ofbiz.entity.GenericValue; 29 import org.ofbiz.entity.util.EntityUtil; 30 import org.ofbiz.service.LocalDispatcher; 31 32 import org.ofbiz.product.store.ProductStoreWorker; 33 34 39 40 public class BOMTree { 41 public static final int EXPLOSION = 0; 42 public static final int EXPLOSION_SINGLE_LEVEL = 1; 43 public static final int EXPLOSION_MANUFACTURING = 2; 44 public static final int IMPLOSION = 3; 45 46 protected LocalDispatcher dispatcher = null; 47 protected GenericDelegator delegator = null; 48 49 BOMNode root; 50 double rootQuantity; 51 double rootAmount; 52 Date inDate; 53 String bomTypeId; 54 GenericValue inputProduct; 55 56 68 public BOMTree(String productId, String bomTypeId, Date inDate, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) throws GenericEntityException { 69 this(productId, bomTypeId, inDate, EXPLOSION, delegator, dispatcher, userLogin); 70 } 71 72 89 public BOMTree(String productId, String bomTypeId, Date inDate, int type, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) throws GenericEntityException { 90 if (productId == null || bomTypeId == null || delegator == null || dispatcher == null) return; 92 if (inDate == null) inDate = new Date (); 94 95 this.delegator = delegator; 96 this.dispatcher = dispatcher; 97 98 inputProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 99 100 String productIdForRules = productId; 101 List productFeaturesAppl = delegator.findByAnd("ProductFeatureAppl", 103 UtilMisc.toMap("productId", productId, 104 "productFeatureApplTypeId", "STANDARD_FEATURE")); 105 List productFeatures = new ArrayList (); 106 GenericValue oneProductFeatureAppl = null; 107 for (int i = 0; i < productFeaturesAppl.size(); i++) { 108 oneProductFeatureAppl = (GenericValue)productFeaturesAppl.get(i); 109 productFeatures.add(delegator.findByPrimaryKey("ProductFeature", 110 UtilMisc.toMap("productFeatureId", oneProductFeatureAppl.getString("productFeatureId")))); 111 112 } 113 GenericValue manufacturedAsProduct = manufacturedAsProduct(productId, inDate); 116 GenericValue product = delegator.findByPrimaryKey("Product", 119 UtilMisc.toMap("productId", 120 (manufacturedAsProduct != null? manufacturedAsProduct.getString("productIdTo"): productId))); 121 if (product == null) return; 122 BOMNode originalNode = new BOMNode(product, dispatcher, userLogin); 123 originalNode.setTree(this); 124 if (!hasBom(product, inDate)) { 128 List virtualProducts = product.getRelatedByAnd("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_VARIANT")); 129 if (virtualProducts != null && virtualProducts.size() > 0) { 130 virtualProducts = EntityUtil.filterByDate(virtualProducts, inDate); 131 if (virtualProducts != null && virtualProducts.size() > 0) { 132 GenericValue virtualProduct = (GenericValue)virtualProducts.get(0); 133 productIdForRules = virtualProduct.getString("productId"); 136 manufacturedAsProduct = manufacturedAsProduct(virtualProduct.getString("productId"), inDate); 137 product = delegator.findByPrimaryKey("Product", 138 UtilMisc.toMap("productId", 139 (manufacturedAsProduct != null? manufacturedAsProduct.getString("productIdTo"): virtualProduct.get("productId")))); 140 } 141 } 142 } 143 if (product == null) return; 144 try { 145 root = new BOMNode(product, dispatcher, userLogin); 146 root.setTree(this); 147 root.setProductForRules(productIdForRules); 148 root.setSubstitutedNode(originalNode); 149 if (type == IMPLOSION) { 150 root.loadParents(bomTypeId, inDate, productFeatures); 151 } else { 152 root.loadChildren(bomTypeId, inDate, productFeatures, type); 153 } 154 } catch(GenericEntityException gee) { 155 root = null; 156 } 157 this.bomTypeId = bomTypeId; 158 this.inDate = inDate; 159 rootQuantity = 1; 160 rootAmount = 0; 161 } 162 163 public GenericValue getInputProduct() { 164 return inputProduct; 165 } 166 167 private GenericValue manufacturedAsProduct(String productId, Date inDate) throws GenericEntityException { 168 List manufacturedAsProducts = delegator.findByAnd("ProductAssoc", 169 UtilMisc.toMap("productId", productId, 170 "productAssocTypeId", "PRODUCT_MANUFACTURED")); 171 manufacturedAsProducts = EntityUtil.filterByDate(manufacturedAsProducts, inDate); 172 GenericValue manufacturedAsProduct = null; 173 if (manufacturedAsProducts != null && manufacturedAsProducts.size() > 0) { 174 manufacturedAsProduct = (GenericValue)manufacturedAsProducts.get(0); 175 } 176 return manufacturedAsProduct; 177 } 178 179 private boolean hasBom(GenericValue product, Date inDate) throws GenericEntityException { 180 List children = product.getRelatedByAnd("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", bomTypeId)); 181 children = EntityUtil.filterByDate(children, inDate); 182 return (children != null && children.size() > 0); 183 } 184 185 191 public boolean isConfigured() { 192 ArrayList notConfiguredParts = new ArrayList (); 193 root.isConfigured(notConfiguredParts); 194 return (notConfiguredParts.size() == 0); 195 } 196 197 201 public double getRootQuantity() { 202 return rootQuantity; 203 } 204 205 209 public void setRootQuantity(double rootQuantity) { 210 this.rootQuantity = rootQuantity; 211 } 212 213 217 public double getRootAmount() { 218 return rootAmount; 219 } 220 221 225 public void setRootAmount(double rootAmount) { 226 this.rootAmount = rootAmount; 227 } 228 229 233 public BOMNode getRoot() { 234 return root; 235 } 236 237 241 public Date getInDate() { 242 return inDate; 243 } 244 245 249 public String getBomTypeId() { 250 return bomTypeId; 251 } 252 253 258 public void print(StringBuffer sb) { 259 if (root != null) { 260 root.print(sb, getRootQuantity(), 0); 261 } 262 } 263 264 270 public void print(ArrayList arr, int initialDepth) { 271 print(arr, initialDepth, true); 272 } 273 274 public void print(ArrayList arr, int initialDepth, boolean excludeWIPs) { 275 if (root != null) { 276 root.print(arr, getRootQuantity(), initialDepth, excludeWIPs); 277 } 278 } 279 280 285 public void print(ArrayList arr) { 286 print(arr, 0, false); 287 } 288 289 public void print(ArrayList arr, boolean excludeWIPs) { 290 print(arr, 0, excludeWIPs); 291 } 292 293 298 public void sumQuantities(HashMap quantityPerNode) { 299 if (root != null) { 300 root.sumQuantity(quantityPerNode); 301 } 302 } 303 304 308 public ArrayList getAllProductsId() { 309 ArrayList nodeArr = new ArrayList (); 310 ArrayList productsId = new ArrayList (); 311 print(nodeArr); 312 for (int i = 0; i < nodeArr.size(); i++) { 313 productsId.add(((BOMNode)nodeArr.get(i)).getProduct().getString("productId")); 314 } 315 return productsId; 316 } 317 318 326 public void createManufacturingOrders(String orderId, String orderItemSeqId, String shipmentId, Date date, GenericValue userLogin) throws GenericEntityException { 327 if (root != null) { 328 String facilityId = null; 329 if (orderId != null) { 330 GenericValue order = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); 331 String productStoreId = order.getString("productStoreId"); 332 if (productStoreId != null) { 333 GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); 334 if (productStore != null) { 335 facilityId = productStore.getString("inventoryFacilityId"); 336 } 337 } 338 339 } 340 if (facilityId == null && shipmentId != null) { 341 GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 342 facilityId = shipment.getString("originFacilityId"); 343 } 344 root.createManufacturingOrder(orderId, orderItemSeqId, shipmentId, facilityId, date, true); 345 } 346 } 347 348 public void getProductsInPackages(ArrayList arr) { 349 if (root != null) { 350 root.getProductsInPackages(arr, getRootQuantity(), 0, false); 351 } 352 } 353 354 } 355 | Popular Tags |