KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > manufacturing > techdata > ProductHelper


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

26
27 package org.ofbiz.manufacturing.techdata;
28
29 import java.sql.Timestamp JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.TreeMap JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.entity.GenericEntityException;
40 import org.ofbiz.entity.GenericValue;
41 import org.ofbiz.entity.util.EntityUtil;
42 import org.ofbiz.manufacturing.mrp.MrpServices;
43 import org.ofbiz.service.GenericServiceException;
44 import org.ofbiz.service.LocalDispatcher;
45
46
47 /**
48  * Method to retrieve some manufacturing Product Information
49  *
50  * @author <a HREF="mailto:olivier.heintz@nereide.biz">Olivier Heintz</a>
51  * @author <a HREF="mailto:nicolas@librenberry.net">Nicolas MALIN</a>
52  * @author <a HREF=mailto:tgrauss@free.fr">Thierry GRAUSS</a>
53  */

54 public class ProductHelper {
55     
56     public static final String JavaDoc module = ProductHelper.class.getName();
57     public static final String JavaDoc resource = "ManufacturingUiLabels";
58     
59     
60     /**
61      * Get the routing object for a product.
62      * @param product : the product for which the method return the routing
63      * @param quantity : the quantity to build
64      * @param evenDate : the date used to filter the active routing
65      * @return GenericValue routing : the routing object
66      **/

67     // DEPRECATED METHOD
68
public static GenericValue getRouting(GenericValue product, double quantity, Timestamp JavaDoc eventDate, LocalDispatcher dispatcher){
69         Debug.logInfo("getRouting called", module);
70         
71         //Looks for the routing associated with the product
72
List JavaDoc listRouting = null;
73         try{
74             listRouting = product.getRelated("WorkEffortGoodStandard", UtilMisc.toMap("workEffortGoodStdTypeId", "ROU_PROD_TEMPLATE"),UtilMisc.toList("estimatedQuantity DESC"));
75             if (listRouting.size()>0) listRouting = EntityUtil.filterByDate(listRouting,eventDate);
76         } catch (GenericEntityException e) {
77             Debug.logError(e,"Error : product.getRelated routing... productId="+product.getString("productId"), module);
78             return null;
79         }
80         if (listRouting == null || listRouting.size()==0) {
81             // if the product is a configuration, probably the routing is linked to the virtual product
82
Map JavaDoc serviceResponse = null;
83             try {
84                 serviceResponse = dispatcher.runSync("getManufacturingComponents", UtilMisc.toMap("productId", product.getString("productId"), "quantity", new Double JavaDoc(quantity)));
85             } catch (GenericServiceException e) {
86                 Debug.logError("Error : getManufacturingComponents for productId ="+product.getString("productId")+"--"+e.getMessage(), module);
87                 return null;
88             }
89             String JavaDoc routingId = (String JavaDoc)serviceResponse.get("workEffortId");
90             try{
91                 return product.getDelegator().findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", routingId));
92             } catch (GenericEntityException e) {
93                 return null;
94             }
95         }
96         Iterator JavaDoc listRoutingIter = listRouting.iterator();
97         boolean found = false;
98         GenericValue routingProduct = null;
99         //Looks to determine which routing has a valid quantity
100
while (listRoutingIter.hasNext() && !found) {
101             routingProduct = (GenericValue) listRoutingIter.next();
102             if (routingProduct.getDouble("estimatedQuantity")==null ||
103             routingProduct.getDouble("estimatedQuantity").doubleValue() < quantity) found = true;
104         }
105         try{
106             return routingProduct.getRelatedOneCache("WorkEffort");
107         } catch (GenericEntityException e) {
108             Debug.logError(e,"Error : routingProduct.getRelated routing... workEffortId="+routingProduct.getString("workEffortId")+" productId="+routingProduct.getString("productId"), module);
109             return null;
110         }
111     }
112
113     /**
114      * Calcul the ATP Date of a list d'objet inventoryEventPlan
115      * @param List The list of inventoryEventPlan that transmit to ftl
116      * @return List of Double represant ATPDate
117      */

118     public static List JavaDoc getVariationProduct(List JavaDoc inventoryList, LocalDispatcher dispatcher){
119         
120         Debug.logInfo("coucou de la methode", module);
121         ArrayList JavaDoc inventoryProductList;
122         Map JavaDoc inventoryProductMap = new TreeMap JavaDoc();
123         Map JavaDoc cumulativeAtpByEventMap = new TreeMap JavaDoc();
124         GenericValue inventoryTmp;
125         boolean firstOfList = true;
126         
127         //regroupement des inventorys en fonction de l'article
128
Iterator JavaDoc iter = inventoryList.iterator();
129         while( iter.hasNext() ){
130             inventoryTmp = (GenericValue) iter.next();
131             inventoryProductList = (ArrayList JavaDoc)inventoryProductMap.get( inventoryTmp.getString("productId") );
132             if( inventoryProductList == null ){
133                 inventoryProductList = new ArrayList JavaDoc();
134                 inventoryProductMap.put( inventoryTmp.getString("productId"), inventoryProductList);
135             }
136             inventoryProductList.add( inventoryTmp );
137         }
138         
139         // iteration on the product found
140
ArrayList JavaDoc keys = new ArrayList JavaDoc( inventoryProductMap.keySet() );
141         for (Iterator JavaDoc iterMap = keys.iterator(); iterMap.hasNext(); ){
142             String JavaDoc productId = (String JavaDoc) iterMap.next();
143             inventoryProductList = (ArrayList JavaDoc) inventoryProductMap.get(productId);
144             double productAtp = 0;
145             for (iter = inventoryProductList.iterator();iter.hasNext();){
146                 //Acumulate all the InventoryEventPlanned.quantity
147
inventoryTmp = (GenericValue)iter.next();
148                 if (firstOfList){
149                     //Intinial ATP equal to the current product QOH
150
try {
151                         GenericValue product = inventoryTmp.getRelatedOneCache("Product");
152                         productAtp = MrpServices.findProductMrpQoh(product, dispatcher);
153                     } catch (Exception JavaDoc e) {
154                         Debug.logError("Error : getRelatedOneCache Produc with productId="+inventoryTmp.getString("productId")+"--"+e.getMessage(), module);
155                         return null;
156                     }
157                     firstOfList = false;
158                 }
159                 Double JavaDoc doubleTmp = (Double JavaDoc)inventoryTmp.getDouble("eventQuantity");
160                 productAtp += doubleTmp.doubleValue();
161                 cumulativeAtpByEventMap.put( inventoryTmp, new Double JavaDoc(productAtp) );
162             }
163             firstOfList = true;
164         }
165         
166         //construct the return list
167
List JavaDoc eventPlannedAndCumulativeAtp = new LinkedList JavaDoc();
168         iter = inventoryList.iterator();
169         while( iter.hasNext() ){
170             inventoryTmp = (GenericValue)iter.next();
171             Double JavaDoc productAtp = (Double JavaDoc) cumulativeAtpByEventMap.get(inventoryTmp);
172             eventPlannedAndCumulativeAtp.add( productAtp );
173         }
174         return eventPlannedAndCumulativeAtp;
175         
176     }
177 }
178
Popular Tags