View Javadoc

1   /*
2    * Economics.java Created @ Jun 13, 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.economics;
15  
16  import java.rmi.RemoteException;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import nl.tudelft.simulation.event.Event;
21  import nl.tudelft.simulation.event.EventProducer;
22  import nl.tudelft.simulation.event.EventType;
23  import nl.tudelft.simulation.logger.Logger;
24  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
25  
26  import org.gscg.common.interactionlayer.AnnounceInterface;
27  import org.gscg.common.interactionlayer.timecontrol.GlobalRowOrColumnNumber;
28  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
29  import org.gscg.singleuser.interactionlayer.dataobjects.EconomicsData;
30  
31  /***
32   * The Economics manages all the server-side actions related to the economic
33   * state of a player. It manages initializing and updating the economics panel.
34   * <p>
35   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
36   * Delft, the Netherlands. All rights reserved.
37   * 
38   * See for project information <a href="http://www.simulation.tudelft.nl/">
39   * www.simulation.tudelft.nl </a>.
40   * 
41   * The source code and binary code of this software is proprietary information
42   * of Delft University of Technology.
43   * 
44   * @author <a
45   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
46   *         van Houten </a>
47   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
48   *         </a>
49   * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
50   * @since 1.0.0
51   */
52  public class Economics extends EventProducer implements AnnounceInterface
53  {
54  	/*** the serial version uid */
55  	private static final long serialVersionUID = 11L;
56  
57  	/*** updates the whole economics table */
58  	public static final EventType UPDATE_ECONOMICS_PANEL = new EventType(
59  			"UPDATE_ECONOMICS_PANEL");
60  
61  	/*** updates the whole economics day table */
62  	public static final EventType UPDATE_ECONOMICS_PANEL_DAY = new EventType(
63  			"UPDATE_ECONOMICS_PANEL_DAY");
64  
65  	/*** updates the whole economics week table */
66  	public static final EventType UPDATE_ECONOMICS_PANEL_WEEK = new EventType(
67  			"UPDATE_ECONOMICS_PANEL_WEEK");
68  
69  	/*** updates the whole economics month table */
70  	public static final EventType UPDATE_ECONOMICS_PANEL_MONTH = new EventType(
71  			"UPDATE_ECONOMICS_PANEL_MONTH");
72  
73  	/*** updates the whole economics table */
74  	public static final EventType UPDATE_ECONOMICS_BALANCE = new EventType(
75  			"UPDATE_ECONOMICS_BALANCE");
76  
77  	/*** the owner of the economics */
78  	private SingleUserInteractionLayerInterface owner = null;
79  
80  	/*** the supply chain actor to use */
81  	private SupplyChainActor actor = null;
82  
83  	/*** the economics financial object */
84  	private EconomicsFinancial economicsFinancial = null;
85  
86  	/*** the events to subscribe the owner to */
87  	private EventType[] eventsToSubscribeOwnerTo = {
88  			Economics.UPDATE_ECONOMICS_PANEL_DAY,
89  			Economics.UPDATE_ECONOMICS_PANEL_WEEK,
90  			Economics.UPDATE_ECONOMICS_PANEL_MONTH};
91  
92  	/***
93  	 * constructs a new Economics
94  	 * 
95  	 * @param owner the owner of the economics object
96  	 */
97  	public Economics(final SingleUserInteractionLayerInterface owner)
98  	{
99  		super();
100 		this.owner = owner;
101 		try
102 		{
103 			this.actor = this.owner.getOwner();
104 			this.economicsFinancial = new EconomicsFinancial(this.owner,
105 					this.actor);
106 
107 			// subscribe the owner to events fired from this object
108 			for (int i = 0; i < this.eventsToSubscribeOwnerTo.length; i++)
109 			{
110 				this.addListener(this.owner, this.eventsToSubscribeOwnerTo[i]);
111 				this.owner.addEventType(this.eventsToSubscribeOwnerTo[i]);
112 			}
113 
114 			// add the announce events
115 			this.owner.addEventTypeToAnnounceList(
116 					Economics.UPDATE_ECONOMICS_BALANCE, this);
117 			this.owner.addEventTypeToAnnounceList(
118 					Economics.UPDATE_ECONOMICS_PANEL, this);
119 		} catch (RemoteException remoteException)
120 		{
121 			Logger.severe(this, "<init>", remoteException);
122 		}
123 	}
124 
125 	/***
126 	 * Method calculateNumberOfColumns.
127 	 * 
128 	 * @param announce indicates whether this is an announce
129 	 */
130 	private void calculateNumberOfColumnsDays(final boolean announce)
131 	{
132 		ArrayList dayHeaderValues = GlobalRowOrColumnNumber
133 				.getDayHeaderValues();
134 
135 		List dataList = new ArrayList();
136 		for (int i = 0; i < GlobalRowOrColumnNumber.getDayHeaderValues().size(); i++)
137 		{
138 			dataList.add(new EconomicsData(i, (String) dayHeaderValues.get(i),
139 					null));
140 		}
141 		if (announce)
142 		{
143 			try
144 			{
145 				this.owner.notifyAnnounced(new Event(
146 						Economics.UPDATE_ECONOMICS_PANEL_DAY, this, dataList));
147 			} catch (RemoteException remoteException)
148 			{
149 				Logger.severe(this, "calculateNumberOfColumnsDays",
150 						remoteException);
151 			}
152 		} else
153 		{
154 			this.fireEvent(new Event(Economics.UPDATE_ECONOMICS_PANEL_DAY,
155 					this, dataList));
156 		}
157 	}
158 
159 	/***
160 	 * Method calculateNumberOfRowsWeeks.
161 	 * 
162 	 * @param announce indicates whether this is an announce
163 	 */
164 	private void calculateNumberOfColumnsWeeks(final boolean announce)
165 	{
166 		ArrayList weekHeaderValues = GlobalRowOrColumnNumber
167 				.getWeekHeaderValues();
168 
169 		List dataList = new ArrayList();
170 		for (int i = 0; i < weekHeaderValues.size(); i++)
171 		{
172 			dataList.add(new EconomicsData(i, (String) weekHeaderValues.get(i),
173 					null));
174 		}
175 		if (announce)
176 		{
177 			try
178 			{
179 				this.owner.notifyAnnounced(new Event(
180 						Economics.UPDATE_ECONOMICS_PANEL_WEEK, this, dataList));
181 			} catch (RemoteException remoteException)
182 			{
183 				Logger.severe(this, "calculateNumberOfColumnsWeeks",
184 						remoteException);
185 			}
186 		} else
187 		{
188 
189 			this.fireEvent(new Event(Economics.UPDATE_ECONOMICS_PANEL_WEEK,
190 					this, dataList));
191 		}
192 	}
193 
194 	/***
195 	 * Method calculateNumberOfRowsWeeks.
196 	 * 
197 	 * @param announce indicates whether this is an announce
198 	 */
199 	private void calculateNumberOfColumnsMonths(final boolean announce)
200 	{
201 		ArrayList monthHeaderValues = GlobalRowOrColumnNumber
202 				.getMonthHeaderValues();
203 
204 		List dataList = new ArrayList();
205 		for (int i = 0; i < monthHeaderValues.size(); i++)
206 		{
207 			dataList.add(new EconomicsData(i,
208 					(String) monthHeaderValues.get(i), null));
209 		}
210 		if (announce)
211 		{
212 			try
213 			{
214 				this.owner
215 						.notifyAnnounced(new Event(
216 								Economics.UPDATE_ECONOMICS_PANEL_MONTH, this,
217 								dataList));
218 			} catch (RemoteException remoteException)
219 			{
220 				Logger.severe(this, "calculateNumberOfColumnsMonths",
221 						remoteException);
222 			}
223 		} else
224 		{
225 			this.fireEvent(new Event(Economics.UPDATE_ECONOMICS_PANEL_MONTH,
226 					this, dataList));
227 		}
228 	}
229 
230 	/***
231 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
232 	 *      boolean)
233 	 */
234 	public void announce(final EventType eventType, final boolean announce)
235 	{
236 		if (eventType.toString().equalsIgnoreCase("UPDATE_ECONOMICS_BALANCE"))
237 		{
238 			this.economicsFinancial.announce(true);
239 		} else
240 		{
241 			this.calculateNumberOfColumnsDays(announce);
242 			this.calculateNumberOfColumnsWeeks(announce);
243 			this.calculateNumberOfColumnsMonths(announce);
244 		}
245 	}
246 
247 	/***
248 	 * @return Returns the economicsFinancial.
249 	 */
250 	public EconomicsFinancial getEconomicsFinancial()
251 	{
252 		return this.economicsFinancial;
253 	}
254 }