View Javadoc

1   /*
2    * @(#) LatLonOrderHandlerStock.java May 21, 2005
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 are proprietary information
11   * of Delft University of Technology.
12   */
13  package org.gscg.singleuser.handlers;
14  
15  import java.io.Serializable;
16  
17  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
18  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
19  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
20  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
21  import nl.tudelft.simulation.logger.Logger;
22  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
23  import nl.tudelft.simulation.supplychain.content.Bill;
24  import nl.tudelft.simulation.supplychain.content.Order;
25  import nl.tudelft.simulation.supplychain.content.OrderBasedOnQuote;
26  import nl.tudelft.simulation.supplychain.content.OrderConfirmation;
27  import nl.tudelft.simulation.supplychain.content.Shipment;
28  import nl.tudelft.simulation.supplychain.handlers.OrderHandler;
29  import nl.tudelft.simulation.supplychain.product.Cargo;
30  import nl.tudelft.simulation.supplychain.product.Product;
31  import nl.tudelft.simulation.supplychain.stock.StockInterface;
32  import nl.tudelft.simulation.supplychain.transport.TransportMode;
33  
34  import org.gscg.common.geo.CalculateLatLonDistance;
35  
36  /***
37   * <p>
38   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
39   * University of Technology </a>, the Netherlands. <br>
40   * See for project information <a
41   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
42   * 
43   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
44   * Delft, the Netherlands. All rights reserved.
45   * 
46   * See for project information <a href="http://www.simulation.tudelft.nl/">
47   * www.simulation.tudelft.nl </a>.
48   * 
49   * The source code and binary code of this software are proprietary information
50   * of Delft University of Technology.
51   * 
52   * @author <a
53   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
54   *         van Houten </a>
55   * @version $Revision: 1.1 $ $Date: 2005/08/03 08:52:50 $
56   * @since 1.1.10
57   */
58  public class LatLonOrderHandlerStock extends OrderHandler
59  {
60  	/*** the serial version uid */
61  	private static final long serialVersionUID = 11L;
62  
63  	/*** for debugging */
64  	private static final boolean DEBUG = false;
65  
66  	/*** the lat lon distance calculator to use */
67  	private CalculateLatLonDistance latLonDistanceCalculator = null;
68  
69  	/***
70  	 * constructs a new LatLonOrderHandlerStock
71  	 * 
72  	 * @param owner
73  	 * @param stock
74  	 */
75  	public LatLonOrderHandlerStock(SupplyChainActor owner, StockInterface stock)
76  	{
77  		super(owner, stock);
78  		this.latLonDistanceCalculator = new CalculateLatLonDistance();
79  	}
80  
81  	/***
82  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
83  	 */
84  	public boolean handleContent(final Serializable content)
85  	{
86  		// get the order
87  		Order order = (Order) content;
88  		// send out the confirmation
89  		OrderConfirmation orderConfirmation = new OrderConfirmation(
90  				super.owner, order.getSender(), order.getInternalDemandID(),
91  				order, OrderConfirmation.CONFIRMED);
92  		super.owner.sendContent(orderConfirmation, 0.00001);
93  		// tell the stock that we claimed some amount
94  		this.stock.changeClaimedAmount(order.getProduct(), order.getAmount());
95  		// wait till the right time to start shipping
96  		try
97  		{
98  			// TODO: get shipping times into all orders
99  			double shippingTime = Math.max(super.owner.getSimulatorTime(),
100 					((OrderBasedOnQuote) order).getQuote()
101 							.getProposedShippingDate());
102 			Object[] args = new Object[]{order};
103 			SimEventInterface simEvent = new SimEvent(shippingTime, this, this,
104 					"ship", args);
105 			super.owner.getSimulator().scheduleEvent(simEvent);
106 		} catch (Exception e)
107 		{
108 			Logger.severe(this, "handleContent", e);
109 			return false;
110 		}
111 		return true;
112 	}
113 
114 	/***
115 	 * Pick and ship the goods.
116 	 * 
117 	 * @param order the order that should be handled
118 	 */
119 	protected void ship(final Order order)
120 	{
121 		Product product = order.getProduct();
122 		double amount = order.getAmount();
123 		try
124 		{
125 			double day = TimeUnit.convert(1.0, TimeUnitInterface.DAY,
126 					super.owner.getSimulator());
127 			if (this.stock.getActualAmount(product) < amount)
128 			{
129 				// try again in one day
130 				Object[] args = new Object[]{order};
131 				SimEventInterface simEvent = new SimEvent(super.owner
132 						.getSimulatorTime()
133 						+ day, this, this, "ship", args);
134 				super.owner.getSimulator().scheduleEvent(simEvent);
135 			} else
136 			{
137 				// tell the stock that we got the claimed amount
138 				this.stock.changeClaimedAmount(order.getProduct(), -order
139 						.getAmount());
140 				// available: make shipment and ship to customer
141 				Cargo cargo = this.stock.getStock(product, amount);
142 				Shipment shipment = new Shipment(super.owner,
143 						order.getSender(), order.getInternalDemandID(), order,
144 						cargo);
145 				shipment.setInTransit(true);
146 
147 				// for now: handle it as a message and "fax" or "email" it
148 				// delay is of course the shipment time
149 				// TODO get the transportation mode from the shipment?
150 				/*
151 				 * super.owner.sendContent(shipment, ((OrderBasedOnQuote) order)
152 				 * .getQuote().getProposedDeliveryDate() - ((OrderBasedOnQuote)
153 				 * order).getQuote() .getProposedShippingDate());
154 				 */
155 				super.owner.sendContent(shipment, TransportMode.PLANE
156 						.transportTime(shipment.getSender(), shipment
157 								.getReceiver()));
158 
159 				//
160 				// we substract the transportation costs
161 				double value = cargo.getValue();
162 				double weight = shipment.getOrder().getAmount()
163 						* shipment.getOrder().getProduct()
164 								.getAverageUnitWeight();
165 
166 				// calculate the costs based on the the lat lon distance!
167 				double transportCosts = TransportMode.PLANE.transportCosts(
168 						this.latLonDistanceCalculator.getDistance(shipment
169 								.getSender(), shipment.getReceiver()), weight);
170 				// the transportation costs are substracted from the bill since
171 				// these costs are paid by the customer to the tranportation
172 				// company, and not to the distributor! However, in the offer
173 				// made to a customer, the costs were taken into account, which
174 				// is common practice
175 				value -= transportCosts;
176 
177 				if (LatLonOrderHandlerStock.DEBUG)
178 				{
179 					System.out
180 							.println("LatLonOrderHandlerStock: transport costs substracted from order: "
181 									+ transportCosts
182 									+ ", value of bill: "
183 									+ value);
184 				}
185 
186 				//
187 				// send a bill when the shipment leaves...
188 				Bill bill = new Bill(super.owner, order.getSender(), order
189 						.getInternalDemandID(), order, super.owner
190 						.getSimulatorTime()
191 						+ (14.0 * day), /* cargo.getValue() */value, "SALE");
192 				super.owner.sendContent(bill, 0.002);
193 			}
194 		} catch (Exception e)
195 		{
196 			Logger.severe(this, "ship", e);
197 			return;
198 		}
199 	}
200 }