View Javadoc

1   /*
2    * BusinessProduction.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.HashSet;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  import nl.tudelft.simulation.event.Event;
26  import nl.tudelft.simulation.event.EventInterface;
27  import nl.tudelft.simulation.event.EventProducer;
28  import nl.tudelft.simulation.event.EventType;
29  import nl.tudelft.simulation.logger.Logger;
30  import nl.tudelft.simulation.supplychain.actor.Trader;
31  import nl.tudelft.simulation.supplychain.product.Product;
32  
33  import org.gscg.common.interactionlayer.AnnounceInterface;
34  import org.gscg.common.interactionlayer.timecontrol.GlobalRowOrColumnNumber;
35  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
36  import org.gscg.singleuser.interactionlayer.dataobjects.BusinessData;
37  import org.gscg.singleuser.interactionlayer.dataobjects.BusinessHeaderData;
38  
39  /***
40   * The BusinessProduction manages all the server-side actions related to the
41   * production decisions of a player. It manages initializing and updating the
42   * production panel.
43   * 
44   * <p>
45   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
46   * Delft, the Netherlands. All rights reserved.
47   * 
48   * See for project information <a href="http://www.simulation.tudelft.nl/">
49   * www.simulation.tudelft.nl </a>.
50   * 
51   * The source code and binary code of this software is proprietary information
52   * of Delft University of Technology.
53   * 
54   * @author <a
55   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
56   *         van Houten </a>
57   * @version $Revision: 1.1 $ $Date: 2005/08/09 15:43:41 $
58   * @since 1.0.0
59   */
60  public class BusinessProduction extends EventProducer implements
61  		AnnounceInterface
62  {
63  	/*** the serial version uid */
64  	private static final long serialVersionUID = 11L;
65  
66  	/*** updates the rows in a table */
67  	public static final EventType UPDATE_BUSINESS_PRODUCTION_PANEL_DAY = new EventType(
68  			"UPDATE_BUSINESS_PRODUCTION_PANEL_DAY");
69  
70  	/*** updates the rows in a table */
71  	public static final EventType UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK = new EventType(
72  			"UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK");
73  
74  	/*** updates the rows in a table */
75  	public static final EventType UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH = new EventType(
76  			"UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH");
77  
78  	/*** updates the product headers and the number of required columns */
79  	public static final EventType UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS = new EventType(
80  			"UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS");
81  
82  	/*** the owner of the economics */
83  	private SingleUserInteractionLayerInterface owner = null;
84  
85  	/*** the events to subscribe the owner to */
86  	private EventType[] eventsToSubscribeOwnerTo = {
87  			BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_DAY,
88  			BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK,
89  			BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH,
90  			BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS};
91  
92  	/*** indicates whether the product headers are already updated */
93  	private boolean productHeadersUpdated = false;
94  
95  	/***
96  	 * the cached announce events should be fired after the product headers have
97  	 * been updated
98  	 */
99  	private ArrayList cachedAnnounceEvents = new ArrayList();
100 
101 	/***
102 	 * constructs a new BusinessProduction
103 	 * 
104 	 * @param owner the owner
105 	 */
106 	public BusinessProduction(final SingleUserInteractionLayerInterface owner)
107 	{
108 		super();
109 		this.owner = owner;
110 
111 		// subscribe the owner to events fired from this object
112 		// also add the necessary announce events
113 		for (int i = 0; i < this.eventsToSubscribeOwnerTo.length; i++)
114 		{
115 			this.addListener(this.owner, this.eventsToSubscribeOwnerTo[i]);
116 			try
117 			{
118 				this.owner.addEventType(this.eventsToSubscribeOwnerTo[i]);
119 				this.owner.addEventTypeToAnnounceList(
120 						this.eventsToSubscribeOwnerTo[i], this);
121 			} catch (RemoteException remoteException)
122 			{
123 				Logger.severe(this, "<init>", remoteException);
124 			}
125 		}
126 	}
127 
128 	/***
129 	 * Method calculateNumberOfRows.
130 	 * 
131 	 * @param announce indicates whether this is an announce
132 	 */
133 	private void calculateNumberOfRowsDays(final boolean announce)
134 	{
135 		ArrayList dayHeaderValues = GlobalRowOrColumnNumber
136 				.getDayHeaderValues();
137 
138 		List dataList = new ArrayList();
139 		for (int i = 0; i < dayHeaderValues.size(); i++)
140 		{
141 			dataList.add(new BusinessData(0, i, "", (String) dayHeaderValues
142 					.get(i)));
143 		}
144 		if (announce)
145 		{
146 			if (this.productHeadersUpdated)
147 			{
148 				try
149 				{
150 					this.owner
151 							.notifyAnnounced(new Event(
152 									BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_DAY,
153 									this, dataList));
154 				} catch (RemoteException remoteException)
155 				{
156 					Logger.severe(this, "calculateNumberOfDays",
157 							remoteException);
158 				}
159 			} else
160 			{
161 				this.cachedAnnounceEvents
162 						.add(new Event(
163 								BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_DAY,
164 								this, dataList));
165 			}
166 		} else
167 		{
168 			this.fireEvent(new Event(
169 					BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_DAY,
170 					this, dataList));
171 		}
172 	}
173 
174 	/***
175 	 * Method calculateNumberOfRowsWeeks.
176 	 * 
177 	 * @param announce indicates whether this is an announce
178 	 */
179 	private void calculateNumberOfRowsWeeks(final boolean announce)
180 	{
181 		ArrayList weekHeaderValues = GlobalRowOrColumnNumber
182 				.getWeekHeaderValues();
183 
184 		List dataList = new ArrayList();
185 		for (int i = 0; i < weekHeaderValues.size(); i++)
186 		{
187 			dataList.add(new BusinessData(0, i, "", (String) weekHeaderValues
188 					.get(i)));
189 		}
190 		if (announce)
191 		{
192 			if (this.productHeadersUpdated)
193 			{
194 				try
195 				{
196 					this.owner
197 							.notifyAnnounced(new Event(
198 									BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK,
199 									this, dataList));
200 				} catch (RemoteException remoteException)
201 				{
202 					Logger.severe(this, "calculateNumberOfRowsWeeks",
203 							remoteException);
204 				}
205 			} else
206 			{
207 				this.cachedAnnounceEvents
208 						.add(new Event(
209 								BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK,
210 								this, dataList));
211 			}
212 		} else
213 		{
214 			this.fireEvent(new Event(
215 					BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK,
216 					this, dataList));
217 		}
218 	}
219 
220 	/***
221 	 * Method calculateNumberOfRowsMonths.
222 	 * 
223 	 * @param announce indicates whether this is an announce
224 	 */
225 	private void calculateNumberOfRowsMonths(final boolean announce)
226 	{
227 		ArrayList monthHeaderValues = GlobalRowOrColumnNumber
228 				.getMonthHeaderValues();
229 
230 		List dataList = new ArrayList();
231 		for (int i = 0; i < monthHeaderValues.size(); i++)
232 		{
233 			dataList.add(new BusinessData(0, i, "", (String) monthHeaderValues
234 					.get(i)));
235 		}
236 		if (announce)
237 		{
238 			if (this.productHeadersUpdated)
239 			{
240 				try
241 				{
242 					this.owner
243 							.notifyAnnounced(new Event(
244 									BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH,
245 									this, dataList));
246 				} catch (RemoteException remoteException)
247 				{
248 					Logger.severe(this, "calculateNumberOfRowsMonths",
249 							remoteException);
250 				}
251 			} else
252 			{
253 				this.cachedAnnounceEvents
254 						.add(new Event(
255 								BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH,
256 								this, dataList));
257 			}
258 		} else
259 		{
260 			this.fireEvent(new Event(
261 					BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH,
262 					this, dataList));
263 		}
264 	}
265 
266 	/***
267 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
268 	 *      boolean)
269 	 */
270 	public void announce(final EventType eventType, final boolean announce)
271 	{
272 		if (eventType
273 				.equals(BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_DAY))
274 		{
275 			this.calculateNumberOfRowsDays(announce);
276 			return;
277 		}
278 		if (eventType
279 				.equals(BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_WEEK))
280 		{
281 			this.calculateNumberOfRowsWeeks(announce);
282 			return;
283 		}
284 		if (eventType
285 				.equals(BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PANEL_MONTH))
286 		{
287 			this.calculateNumberOfRowsMonths(announce);
288 			return;
289 		}
290 		if (eventType
291 				.equals(BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS))
292 		{
293 			try
294 			{
295 				if (announce)
296 				{
297 					this.owner
298 							.notifyAnnounced(new Event(
299 									BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS,
300 									this, new BusinessHeaderData(this
301 											.getProducts())));
302 
303 					this.productHeadersUpdated = true;
304 					for (int i = 0; i < this.cachedAnnounceEvents.size(); i++)
305 					{
306 						this.owner
307 								.notifyAnnounced((EventInterface) this.cachedAnnounceEvents
308 										.remove(i));
309 					}
310 				} else
311 				{
312 					this
313 							.fireEvent(new Event(
314 									BusinessProduction.UPDATE_BUSINESS_PRODUCTION_PRODUCT_HEADERS,
315 									this, new BusinessHeaderData(this
316 											.getProducts())));
317 				}
318 			} catch (RemoteException remoteException)
319 			{
320 				Logger.severe(this, "announce", remoteException);
321 			}
322 			return;
323 		}
324 	}
325 
326 	/***
327 	 * Method getProducts returns a sorted set with products to extract product
328 	 * headers from
329 	 * 
330 	 * @return the sorted set
331 	 */
332 	private SortedSet getProducts()
333 	{
334 		try
335 		{
336 			List products = ((Trader) this.owner.getOwner())
337 					.getProductsOnStock();
338 			TreeSet sortedSet = new TreeSet(new ProductComparator());
339 			Set checkList = new HashSet();
340 			for (int i = 0; i < products.size(); i++)
341 			{
342 				Product product = (Product) products.get(i);
343 				Map materials = product.getBillOfMaterials().getMaterials();
344 
345 				if (!materials.keySet().isEmpty())
346 				{
347 					// a bom, we may add
348 					if (!checkList.contains(product.getName()))
349 					{
350 						sortedSet.add(product);
351 						checkList.add(product.getName());
352 					}
353 				}
354 			}
355 			return sortedSet;
356 		} catch (RemoteException remoteException)
357 		{
358 			Logger.severe(this, "getProducts", remoteException);
359 			return null;
360 		}
361 	}
362 }