View Javadoc

1   /*
2    * BusinessSales.java Created @ Jun 16, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package org.gscg.singleuser.interactionlayer.business;
15  
16  import java.rmi.RemoteException;
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.SortedSet;
20  import java.util.TreeSet;
21  
22  import nl.tudelft.simulation.event.Event;
23  import nl.tudelft.simulation.event.EventInterface;
24  import nl.tudelft.simulation.event.EventProducer;
25  import nl.tudelft.simulation.event.EventType;
26  import nl.tudelft.simulation.logger.Logger;
27  import nl.tudelft.simulation.supplychain.actor.Trader;
28  
29  import org.gscg.common.interactionlayer.AnnounceInterface;
30  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
31  import org.gscg.singleuser.interactionlayer.dataobjects.BusinessHeaderData;
32  
33  /***
34   * The BusinessStock takes care of firing all the stock product headers.
35   * <p>
36   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
37   * Delft, the Netherlands. All rights reserved.
38   * 
39   * See for project information <a href="http://www.simulation.tudelft.nl/">
40   * www.simulation.tudelft.nl </a>.
41   * 
42   * The source code and binary code of this software is proprietary information
43   * of Delft University of Technology.
44   * 
45   * @author <a
46   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
47   *         van Houten </a>
48   * @version $Revision: 1.1 $ $Date: 2005/08/09 15:43:41 $
49   * @since 1.0.0
50   */
51  public class BusinessStock extends EventProducer implements AnnounceInterface
52  {
53  	/*** the serial version uid */
54  	private static final long serialVersionUID = 11L;
55  
56  	/*** updates the product headers and the number of required columns */
57  	public static final EventType UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS = new EventType(
58  			"UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS");
59  
60  	/*** the owner of the economics */
61  	private SingleUserInteractionLayerInterface owner = null;
62  
63  	/*** the events to subscribe the owner to */
64  	private EventType[] eventsToSubscribeOwnerTo = {BusinessStock.UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS};
65  
66  	/***
67  	 * the cached announce events should be fired after the product headers have
68  	 * been updated
69  	 */
70  	private ArrayList cachedAnnounceEvents = new ArrayList();
71  
72  	/***
73  	 * constructs a new BusinessSales
74  	 * 
75  	 * @param owner the owner
76  	 */
77  	public BusinessStock(final SingleUserInteractionLayerInterface owner)
78  	{
79  		super();
80  		this.owner = owner;
81  
82  		// subscribe the owner to events fired from this object
83  		// also add the necessary announce events
84  		for (int i = 0; i < this.eventsToSubscribeOwnerTo.length; i++)
85  		{
86  			this.addListener(this.owner, this.eventsToSubscribeOwnerTo[i]);
87  			try
88  			{
89  				this.owner.addEventType(this.eventsToSubscribeOwnerTo[i]);
90  				this.owner.addEventTypeToAnnounceList(
91  						this.eventsToSubscribeOwnerTo[i], this);
92  			} catch (RemoteException remoteException)
93  			{
94  				Logger.severe(this, "init", remoteException);
95  			}
96  		}
97  	}
98  
99  	/***
100 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
101 	 *      boolean)
102 	 */
103 	public void announce(final EventType eventType, final boolean announce)
104 	{
105 		if (eventType
106 				.equals(BusinessStock.UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS))
107 		{
108 			try
109 			{
110 				if (announce)
111 				{
112 					this.owner
113 							.notifyAnnounced(new Event(
114 									BusinessStock.UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS,
115 									this, new BusinessHeaderData(this
116 											.getProducts())));
117 					for (int i = 0; i < this.cachedAnnounceEvents.size(); i++)
118 					{
119 						this.owner
120 								.notifyAnnounced((EventInterface) this.cachedAnnounceEvents
121 										.remove(i));
122 					}
123 				} else
124 				{
125 					this
126 							.fireEvent(new Event(
127 									BusinessStock.UPDATE_BUSINESS_STOCK_PRODUCT_HEADERS,
128 									this, new BusinessHeaderData(this
129 											.getProducts())));
130 				}
131 			} catch (RemoteException remoteException)
132 			{
133 				Logger.severe(this, "announce", remoteException);
134 			}
135 			return;
136 		}
137 	}
138 
139 	/***
140 	 * Method getProducts returns a sorted set with products to extract product
141 	 * headers from
142 	 * 
143 	 * @return the sorted set
144 	 */
145 	private SortedSet getProducts()
146 	{
147 		try
148 		{
149 			List products = ((Trader) this.owner.getOwner())
150 					.getProductsOnStock();
151 			TreeSet sortedSet = new TreeSet(new ProductComparator());
152 
153 			for (int i = 0; i < products.size(); i++)
154 			{
155 				sortedSet.add(products.get(i));
156 			}
157 			return sortedSet;
158 		} catch (RemoteException remoteException)
159 		{
160 			Logger.severe(this, "getProducts", remoteException);
161 			return null;
162 		}
163 	}
164 }