001 package market; 002 003 import java.util.Iterator; 004 005 import data.Catalog; 006 import data.CatalogItem; 007 import data.DataBasket; 008 import data.events.VetoException; 009 import data.ooimpl.CatalogImpl; 010 import data.ooimpl.CountingStockImpl; 011 012 /** 013 * CountingStockImpl that always contains the same keys as its {@linkplain CArticleCatalog} source catalog, 014 * even if no StockItems have been added or some have been removed completely. 015 * Normally the keys of StockItems are removed when their number drops to zero. But this prevents 016 * StockItems with an amount of zero to be displayed in tables. 017 */ 018 public class CSOffer extends CountingStockImpl { 019 020 private Integer iNull = new Integer(0); 021 022 /** 023 * @param sName the Stock's name. 024 * @param cat the source catalog. 025 */ 026 public CSOffer(String sName, CatalogImpl cat) { 027 super(sName, cat); 028 addZeros(null); 029 } 030 031 /** 032 * Removes StockItems from the Stock. If the item has been removed completely, its 033 * index is re-added and its amount is set to 0. 034 * 035 * @param sKey the ID of the StockItem to be removed. 036 * @param nCount the number of Items to be removed. 037 * @param db the belonging DataBasket. 038 */ 039 public void remove(String sKey, int nCount, DataBasket db){ 040 try { 041 super.remove(sKey, nCount, db); 042 } 043 catch (VetoException ve) { 044 System.err.println(ve.getMessage()); 045 } 046 if (!contains(sKey, db)) { 047 getItemsContainer().put(sKey, iNull); 048 } 049 } 050 051 /** 052 * Checks, which CatalogItems have no appropriate StockItems. 053 * If one is found the key of the item is added to the stock. 054 * The number of StockItems is set to 0. 055 * 056 * @param db the DataBasket to be used. 057 */ 058 public void addZeros(DataBasket db) { 059 Iterator it = m_ciCatalog.iterator(null, false); 060 while (it.hasNext()) { 061 String sKey = ((CatalogItem)it.next()).getName(); 062 if (!contains(sKey, db)) { 063 getItemsContainer().put(sKey, iNull); 064 } 065 } 066 } 067 068 069 /** 070 * Changes the StockItems of this stock according to a new Catalog<br><br> 071 * All elements that appear in both the old and the new Catalog will be kept.<br> 072 * All elements that do not appear in the new Catalog will be deleted.<br> 073 * All elements that have not appeared in the old Catalog will be added 074 * and their number is set to 0.<br> 075 * This method is used by the filters that filter Stocks by their item's category. 076 * 077 * @param cat the new Catalog to be set. 078 */ 079 public void changeArticleCatalog(Catalog cat) throws VetoException { 080 Iterator it = null; 081 //convert the given catalog to a CatalogImpl and make it the new base 082 Catalog oldBase = m_ciCatalog; 083 if (cat instanceof CatalogImpl) { //Catalog instance of CatalogImpl => leave it 084 m_ciCatalog = (CatalogImpl)cat; 085 } else { //Catalog not instance of CatalogImpl (e.g. Filter) 086 CatalogImpl c = new CatalogImpl(m_ciCatalog.getName()); 087 it = cat.keySet(null).iterator(); 088 while (it.hasNext()) { //copy all CatalogItems from cat to new 089 String nextKey = (String)it.next(); //created CatalogImpl 090 CatalogItem ci = oldBase.get(nextKey, null, false); 091 c.add(ci, null); 092 } 093 m_ciCatalog = c; //make new CatalogImpl the new base 094 } 095 //iterate through new base and remove all unneeded StockItems from this stock 096 it = this.keySet(null).iterator(); 097 int nrOfItems = 0; 098 while (it.hasNext()) { 099 String sKey = (String)it.next(); 100 nrOfItems = countItems(sKey, null); 101 if (!contains(sKey, null)) { 102 if (nrOfItems > 0) { 103 try { 104 super.remove(sKey, nrOfItems, null); 105 } 106 catch (VetoException ve) { 107 ve.printStackTrace(); 108 throw ve; 109 } 110 } else { 111 getItemsContainer().remove(sKey); 112 } 113 } 114 addZeros(null); 115 } 116 } 117 118 /** 119 * @return a clone of CSOffer with its StockItems. 120 */ 121 public Object clone() { 122 CSOffer cso = new CSOffer(getName(), m_ciCatalog); 123 Iterator it = this.keySet(null).iterator(); 124 while (it.hasNext()) { 125 String sKey = (String)it.next(); 126 int amount = countItems(sKey, null); 127 if (amount > 0) { //add all items with one or more elements 128 cso.add(sKey, amount, null); //(adding 0 elements is not allowed by the Framework) 129 } 130 addZeros(null); 131 } 132 return cso; 133 } 134 }