View Javadoc

1   /*
2    * InteractiveOrderConfirmationFineHandler.java Created @ Mar 4, 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.handlers;
15  
16  import java.io.Serializable;
17  import java.text.DateFormat;
18  import java.util.Calendar;
19  
20  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
21  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
22  import nl.tudelft.simulation.logger.Logger;
23  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
24  import nl.tudelft.simulation.supplychain.content.OrderConfirmation;
25  import nl.tudelft.simulation.supplychain.content.Shipment;
26  import nl.tudelft.simulation.supplychain.handlers.SupplyChainHandler;
27  
28  import org.gscg.gameactors.GameDistributor;
29  
30  /***
31   * The InteractiveOrderConfirmationFineHandler is a simple implementation of the
32   * business logic for a OrderConfirmation that comes in. When the confirmation
33   * is positive: just ignore it. When it is negative: it is more difficult. The
34   * easiest is to go to the 'next' option, e.g. to the next Quote when there were
35   * quotes. It is also possible to redo the entire ordering process from scratch.
36   * The latter strategy is implemented in this version of the handler. <br>
37   * <br>
38   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
39   * Delft, the Netherlands. All rights reserved.
40   * 
41   * See for project information <a href="http://www.simulation.tudelft.nl/">
42   * www.simulation.tudelft.nl </a>.
43   * 
44   * The source code and binary code of this software is proprietary information
45   * of Delft University of Technology.
46   * 
47   * @author <a
48   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
49   *         Verbraeck </a>
50   * @version $Revision: 1.2 $ $Date: 2005/08/03 08:52:50 $
51   * @since 1.0.0
52   */
53  public class InteractiveOrderConfirmationFineHandler extends SupplyChainHandler
54  {
55  	/*** the serial version uid */
56  	private static final long serialVersionUID = 11L;
57  
58  	/*** the maximum time out for a shipment */
59  	private double maximumTimeOut = 0.0;
60  
61  	/*** the margin for the fine */
62  	private double fineMargin = 0.0;
63  
64  	/*** the fixed fine */
65  	private double fixedFine = 0.0;
66  
67  	/*** true for debug mode */
68  	private boolean debug = false;
69  
70  	/***
71  	 * constructs a new InteractiveOrderConfirmationFineHandler
72  	 * 
73  	 * @param owner the owner
74  	 * @param maximumTimeOut the time out
75  	 * @param fineMargin the margin
76  	 * @param fixedFine the fixed fine
77  	 */
78  	public InteractiveOrderConfirmationFineHandler(
79  			final SupplyChainActor owner, final double maximumTimeOut,
80  			final double fineMargin, final double fixedFine)
81  	{
82  		super(owner);
83  		this.maximumTimeOut = maximumTimeOut;
84  		this.fineMargin = fineMargin;
85  		this.fixedFine = fixedFine;
86  	}
87  
88  	/***
89  	 * For the moment, the handler will just reorder the products from the start
90  	 * of the process, in case the confirmation is negative.
91  	 * 
92  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
93  	 */
94  	public boolean handleContent(final Serializable content)
95  	{
96  		OrderConfirmation orderConfirmation = (OrderConfirmation) checkContent(content);
97  		if (!isValidContent(orderConfirmation))
98  		{
99  			return false;
100 		}
101 		if (orderConfirmation.isAccepted())
102 		{
103 			// let's claim some stock
104 			// tell the stock that we claimed some amount
105 			((GameDistributor) this.owner).getStock().changeOrderedAmount(
106 					orderConfirmation.getProduct(),
107 					orderConfirmation.getOrder().getAmount());
108 
109 			// schedule for possible fine
110 			try
111 			{
112 				orderConfirmation.getSender().getSimulator().scheduleEvent(
113 						orderConfirmation.getOrder().getDeliveryDate()
114 								- this.owner.getSimulatorTime()
115 								+ this.maximumTimeOut, this, this,
116 						"checkShipment", new Object[]{orderConfirmation});
117 
118 				if (this.debug)
119 				{
120 					long startTime = orderConfirmation.getSender()
121 							.getSimulator().getReplication().getRunControl()
122 							.getTreatment().getStartTime();
123 
124 					TimeUnitInterface timeUnit = orderConfirmation.getSender()
125 							.getSimulator().getReplication().getRunControl()
126 							.getTreatment().getTimeUnit();
127 
128 					// convert the time to milliseconds
129 					double timeInMilliSeconds = TimeUnit
130 							.convert(
131 									(orderConfirmation.getOrder()
132 											.getDeliveryDate()
133 											- this.owner.getSimulatorTime() + this.maximumTimeOut),
134 									timeUnit, TimeUnitInterface.MILLISECOND);
135 
136 					Calendar calendar = Calendar.getInstance();
137 					calendar.setTimeInMillis(startTime
138 							+ (long) timeInMilliSeconds);
139 					String scheduledDate = DateFormat.getDateInstance().format(
140 							calendar.getTime());
141 					System.out
142 							.println("DEBUG -- InteractiveOrderConfirmationHandler scheduled shipment fine check for: "
143 									+ scheduledDate);
144 				}
145 
146 			} catch (Exception exception)
147 			{
148 				Logger.severe(this, "handleContent", exception);
149 			}
150 		} else
151 		{
152 			System.out.println("!orderConfirmation.isAccepted()");
153 		}
154 		return true;
155 	}
156 
157 	/***
158 	 * @param orderConfirmation the order confirmation
159 	 */
160 	protected void checkShipment(final OrderConfirmation orderConfirmation)
161 	{
162 		if (this.owner.getContentStore().getContentList(
163 				orderConfirmation.getInternalDemandID(), Shipment.class)
164 				.isEmpty())
165 		{
166 			// there is still an order, but no shipment... we fine!
167 			double fine = this.fixedFine + this.fineMargin
168 					* orderConfirmation.getOrder().getPrice();
169 
170 			// send the bill for the fine
171 			/*
172 			 * Bill bill = new Bill(super.owner, orderConfirmation.getSender(),
173 			 * orderConfirmation .getInternalDemandID(), orderConfirmation
174 			 * .getOrder(), super.owner.getSimulatorTime() + 0.0, fine, "FINE");
175 			 * super.owner.sendContent(bill, 0.0);
176 			 */
177 			orderConfirmation.getSender().getBankAccount().withdrawFromBalance(
178 					fine);
179 			orderConfirmation.getReceiver().getBankAccount().addToBalance(fine);
180 		}
181 	}
182 
183 	/***
184 	 * @see nl.tudelft.simulation.supplychain.handlers.SupplyChainHandler#checkContentClass(java.io.Serializable)
185 	 */
186 	protected boolean checkContentClass(final Serializable content)
187 	{
188 		return (content instanceof OrderConfirmation);
189 	}
190 }