View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.inventory.model.facade;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import net.sf.oness.common.all.BaseObject;
27  import net.sf.oness.common.model.dao.Dao;
28  import net.sf.oness.common.model.util.CollectionUtils;
29  import net.sf.oness.common.model.util.PaginatedList;
30  import net.sf.oness.inventory.model.product.bo.Color;
31  import net.sf.oness.inventory.model.product.bo.Fare;
32  import net.sf.oness.inventory.model.product.bo.Model;
33  import net.sf.oness.inventory.model.product.bo.Price;
34  import net.sf.oness.inventory.model.product.bo.Product;
35  import net.sf.oness.inventory.model.product.bo.Size;
36  import net.sf.oness.inventory.model.product.bo.SizeGroup;
37  import net.sf.oness.inventory.model.warehouse.bo.Stock;
38  import net.sf.oness.inventory.model.warehouse.bo.Warehouse;
39  
40  /***
41   * @author Carlos Sanchez
42   * @version $Revision: 1.21 $
43   */
44  public class InventorySpringFacadeDelegate extends BaseObject implements
45          InventoryFacadeDelegate {
46  
47      private Dao modelDao, sizeGroupDao, colorDao, productDao, stockDao,
48              priceDao, warehouseDao, fareDao;
49  
50      /***
51       * @param modelDao
52       *            The modelDao to set.
53       */
54      public void setModelDao(Dao modelDao) {
55          this.modelDao = modelDao;
56      }
57  
58      /***
59       * @return Returns the modelDao.
60       */
61      public Dao getModelDao() {
62          return modelDao;
63      }
64  
65      /***
66       * @param sizeGroupDao
67       *            The sizeGroupDao to set.
68       */
69      public void setSizeGroupDao(Dao sizeGroupDao) {
70          this.sizeGroupDao = sizeGroupDao;
71      }
72  
73      /***
74       * @return Returns the sizeGroupDao.
75       */
76      public Dao getSizeGroupDao() {
77          return sizeGroupDao;
78      }
79  
80      /***
81       * @param colorDao
82       *            The colorDao to set.
83       */
84      public void setColorDao(Dao colorDao) {
85          this.colorDao = colorDao;
86      }
87  
88      /***
89       * @return Returns the colorDao.
90       */
91      public Dao getColorDao() {
92          return colorDao;
93      }
94  
95      /***
96       * @param productDao
97       *            The productDao to set.
98       */
99      public void setProductDao(Dao productDao) {
100         this.productDao = productDao;
101     }
102 
103     /***
104      * @return Returns the productDao.
105      */
106     public Dao getProductDao() {
107         return productDao;
108     }
109 
110     /***
111      * @param stockDao
112      *            The stockDao to set.
113      */
114     public void setStockDao(Dao stockDao) {
115         this.stockDao = stockDao;
116     }
117 
118     /***
119      * @return Returns the stockDao.
120      */
121     public Dao getStockDao() {
122         return stockDao;
123     }
124 
125     /***
126      * @param priceDao
127      *            The priceDao to set.
128      */
129     public void setPriceDao(Dao priceDao) {
130         this.priceDao = priceDao;
131     }
132 
133     /***
134      * @return Returns the priceDao.
135      */
136     public Dao getPriceDao() {
137         return priceDao;
138     }
139 
140     /***
141      * @param warehouseDao
142      *            The warehouseDao to set.
143      */
144     public void setWarehouseDao(Dao warehouseDao) {
145         this.warehouseDao = warehouseDao;
146     }
147 
148     /***
149      * @return Returns the warehouseDao.
150      */
151     public Dao getWarehouseDao() {
152         return warehouseDao;
153     }
154 
155     /***
156      * @param fareDao
157      *            The fareDao to set.
158      */
159     public void setFareDao(Dao fareDao) {
160         this.fareDao = fareDao;
161     }
162 
163     /***
164      * @return Returns the fareDao.
165      */
166     public Dao getFareDao() {
167         return fareDao;
168     }
169 
170     public Model createModel(Model model, Long sizeGroupId, Collection colorIds) {
171 
172         SizeGroup sizeGroup = (SizeGroup) getSizeGroupDao().findById(
173                 sizeGroupId);
174         model.setSizeGroups(CollectionUtils.newList(sizeGroup));
175 
176         Collection colors = new ArrayList(getColorDao().findById(colorIds));
177         model.setColors(colors);
178 
179         //Model newModel = (Model) getModelDao().create(model);
180 
181         Collection products = new ArrayList();
182         Iterator colorIter = model.getColors().iterator();
183         while (colorIter.hasNext()) {
184             Color color = (Color) colorIter.next();
185             Iterator sizeIter = sizeGroup.getSizes().iterator();
186             while (sizeIter.hasNext()) {
187 
188                 /* products */
189                 Size size = (Size) sizeIter.next();
190                 Product product = new Product();
191                 product.setStockKeepingUnit(model.getName() + size.getId()
192                         + color.getId());
193                 product.setColor(color);
194                 product.setSize(size);
195                 product.setModels(CollectionUtils.newList(model));
196                 product.setPrices(new ArrayList());
197                 product = (Product) getProductDao().create(product);
198                 products.add(product);
199 
200                 /* stocks */
201                 Collection stocks = new ArrayList();
202                 Iterator warehouseIter = getAllWarehouses().iterator();
203                 while (warehouseIter.hasNext()) {
204                     Warehouse warehouse = (Warehouse) warehouseIter.next();
205                     Stock stock = new Stock();
206                     stock.setWarehouses(CollectionUtils.newList(warehouse));
207                     stock.setProducts(CollectionUtils.newList(product));
208                     stock.setQuantity(new BigDecimal("0"));
209                     stocks.add(getStockDao().create(stock));
210                 }
211                 product.setStocks(stocks);
212             }
213         }
214         model.setProducts(products);
215         model = (Model) getModelDao().create(model);
216         return model;
217     }
218 
219     /***
220      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#updateModel(net.sf.oness.inventory.model.product.bo.Model)
221      */
222     public Model updateModel(Model model) {
223         Model oldModel = (Model) getModelDao().findWithDetails(model.getId());
224         model.setProducts(oldModel.getProducts());
225         model.setColors(oldModel.getColors());
226         model.setSizeGroups(CollectionUtils.newList(oldModel.getSizeGroup()));
227         Model updated = (Model) getModelDao().update(model);
228         return findModelWithDetails(updated.getId());
229     }
230 
231     /***
232      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#deleteModel(java.lang.Long)
233      */
234     public void deleteModel(Long id) {
235         getModelDao().delete(id);
236     }
237 
238     /***
239      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#findModel(java.lang.Long)
240      */
241     public Model findModel(Long id) {
242         return (Model) getModelDao().findById(id);
243     }
244 
245     /***
246      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#findModelWithDetails(java.lang.Long)
247      */
248     public Model findModelWithDetails(Long id) {
249         Model model = (Model) getModelDao().findWithDetails(id);
250 
251         /* products */
252         Iterator iter = model.getProducts().iterator();
253         Collection products = new ArrayList(model.getProducts().size());
254         while (iter.hasNext()) {
255             Product product = (Product) iter.next();
256             products.add(findProductWithDetails(product.getId()));
257         }
258         model.setProducts(products);
259 
260         /* sizegroup */
261         SizeGroup sizeGroup = model.getSizeGroup();
262         model.setSizeGroups(CollectionUtils.newList(getSizeGroupDao()
263                 .findWithDetails(sizeGroup.getId())));
264 
265         return model;
266     }
267 
268     /***
269      * 
270      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#findProductWithDetails(java.lang.Long)
271      */
272     public Product findProductWithDetails(Long id) {
273         Product product = (Product) getProductDao().findWithDetails(id);
274 
275         /* prices */
276         Collection prices = new ArrayList(product.getPrices().size());
277         Iterator iterator = product.getPrices().iterator();
278         while (iterator.hasNext()) {
279             Price price = (Price) iterator.next();
280             prices.add(getPriceDao().findWithDetails(price.getId()));
281         }
282         product.setPrices(prices);
283 
284         /* stocks */
285         Collection stocks = new ArrayList(product.getStocks().size());
286         iterator = product.getStocks().iterator();
287         while (iterator.hasNext()) {
288             Stock stock = (Stock) iterator.next();
289             stocks.add(getStockDao().findWithDetails(stock.getId()));
290         }
291         product.setStocks(stocks);
292 
293         return product;
294     }
295 
296     /***
297      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#findModel(net.sf.oness.inventory.model.product.bo.Model,
298      *      int, int)
299      */
300     public PaginatedList findModel(Model model, int firstElement,
301             int maxElements) {
302         PaginatedList list = getModelDao().find(model, firstElement,
303                 maxElements);
304         Iterator iter = list.getList().iterator();
305         while (iter.hasNext()) {
306             Model m = (Model) iter.next();
307             m.setSizeGroups(CollectionUtils.newList(getSizeGroupDao().findById(
308                     m.getSizeGroup().getId())));
309         }
310         return list;
311     }
312 
313     /***
314      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#getAllSizeGroups()
315      */
316     public List getAllSizeGroups() {
317         Collection c = getSizeGroupDao().findAll();
318         Iterator iter = c.iterator();
319         List sizeGroups = new ArrayList(c.size());
320         while (iter.hasNext()) {
321             SizeGroup sizeGroup = (SizeGroup) iter.next();
322             sizeGroups
323                     .add(getSizeGroupDao().findWithDetails(sizeGroup.getId()));
324         }
325         return sizeGroups;
326     }
327 
328     /***
329      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#getAllColors()
330      */
331     public List getAllColors() {
332         return getColorDao().findAll();
333     }
334 
335     /***
336      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#getAllFares()
337      */
338     public List getAllFares() {
339         return getFareDao().findAll();
340     }
341 
342     /***
343      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#getAllWarehouses()
344      */
345     public List getAllWarehouses() {
346         return getWarehouseDao().findAll();
347     }
348 
349     /***
350      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#editPrices(java.lang.Long)
351      */
352     public Model editPrices(Long modelId) {
353         return findModelWithDetails(modelId);
354     }
355 
356     /***
357      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#updatePrices(java.util.Map)
358      */
359     public Model updatePrices(Map productPrices) {
360         Iterator iter = productPrices.entrySet().iterator();
361         Product oldProduct = null;
362         while (iter.hasNext()) {
363             Map.Entry entry = (Map.Entry) iter.next();
364             Long productId = (Long) entry.getKey();
365             Map prices = (Map) entry.getValue();
366             oldProduct = (Product) getProductDao().findWithDetails(productId);
367 
368             /* map (fareId, price) */
369             Map oldPrices = new HashMap();
370             Iterator oldPricesIter = oldProduct.getPrices().iterator();
371             while (oldPricesIter.hasNext()) {
372                 Price price = (Price) oldPricesIter.next();
373                 price = (Price) getPriceDao().findWithDetails(price.getId());
374                 oldPrices.put(price.getFare().getId(), price);
375             }
376 
377             Iterator pricesIter = prices.entrySet().iterator();
378             while (pricesIter.hasNext()) {
379                 entry = (Map.Entry) pricesIter.next();
380                 Long fareId = (Long) entry.getKey();
381                 BigDecimal amount = (BigDecimal) entry.getValue();
382 
383                 Price oldPrice = (Price) oldPrices.get(fareId);
384 
385                 /* if it is a new price create it */
386                 if (oldPrice == null) {
387                     if (amount != null) {
388                         Price newPrice = new Price();
389                         newPrice.setAmount(amount);
390                         newPrice.setProducts(CollectionUtils
391                                 .newList(oldProduct));
392                         Fare fare = (Fare) getFareDao().findById(fareId);
393                         newPrice.setFares(CollectionUtils.newList(fare));
394                         newPrice = (Price) getPriceDao().create(newPrice);
395                         oldProduct.getPrices().add(newPrice);
396 
397                         if (log.isDebugEnabled())
398                             log.debug("Created new price (fare=" + fareId
399                                     + ", amount=" + amount);
400                     }
401                 }
402                 /*
403                  * else delete old when amount is null or update if amount
404                  * changed
405                  */
406                 else {
407                     if (amount == null) {
408                         getPriceDao().delete(oldPrice.getId());
409 
410                         if (log.isDebugEnabled())
411                             log.debug("Deleted price (fare="
412                                     + oldPrice.getFare().getId() + ", amount="
413                                     + oldPrice.getAmount());
414                     } else {
415                         if (!amount.equals(oldPrice.getAmount())) {
416                             oldPrice.setAmount(amount);
417                             oldPrice = (Price) getPriceDao().update(oldPrice);
418                             oldProduct.getPrices().add(oldPrice);
419 
420                             if (log.isDebugEnabled())
421                                 log.debug("Updated price (fare=" + fareId
422                                         + ", amount=" + amount);
423                         }
424                     }
425                 }
426             }
427 
428         }
429         return findModelWithDetails(oldProduct.getModel().getId());
430     }
431 
432     /***
433      * @see net.sf.oness.inventory.model.facade.InventoryFacadeDelegate#findProducts(java.util.Collection)
434      */
435     public Map findProducts(Collection productIds) {
436         Map map = new HashMap();
437         if (productIds.isEmpty())
438             return map;
439         Iterator iter = productIds.iterator();
440         while (iter.hasNext()) {
441             Long id = (Long) iter.next();
442             map.put(id, findProductWithDetails(id));
443         }
444         return map;
445     }
446 
447 }