View Javadoc

1   /*
2    * @(#)InteractiveBillTimeOutHandler.java Jul 11, 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  package org.gscg.singleuser.handlers;
14  
15  import java.io.Serializable;
16  
17  import nl.tudelft.simulation.logger.Logger;
18  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
19  import nl.tudelft.simulation.supplychain.content.Bill;
20  import nl.tudelft.simulation.supplychain.content.Payment;
21  import nl.tudelft.simulation.supplychain.handlers.SupplyChainHandler;
22  
23  /***
24   * The InteractiveBillTimeOutHandler implements the most basic rules to deal
25   * with bills. In this case it is only used as a placeholder to prevent
26   * exceptions. Later versions may include rules, which are customized by a
27   * human.
28   * <p>
29   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
30   * Delft, the Netherlands. All rights reserved.
31   * 
32   * See for project information <a href="http://www.simulation.tudelft.nl/">
33   * www.simulation.tudelft.nl </a>.
34   * 
35   * The source code and binary code of this software is proprietary information
36   * of Delft University of Technology.
37   * 
38   * @author <a
39   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
40   *         van Houten </a>
41   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:56 $
42   * @since 1.0.0
43   */
44  public class InteractiveBillTimeOutHandler extends SupplyChainHandler
45  {
46  	/*** the serial version uid */
47  	private static final long serialVersionUID = 11L;
48  
49  	/*** the maximum time out for a shipment */
50  	private double maximumTimeOut = 0.0;
51  
52  	/*** true for debug */
53  	private boolean debug = true;
54  
55  	/***
56  	 * constructs a new InteractiveBillTimeOutHandler
57  	 * 
58  	 * @param owner the owner
59  	 * @param maximumTimeOut the time out to use
60  	 */
61  	public InteractiveBillTimeOutHandler(final SupplyChainActor owner,
62  			final double maximumTimeOut)
63  	{
64  		super(owner);
65  		this.maximumTimeOut = maximumTimeOut;
66  	}
67  
68  	/***
69  	 * @see nl.tudelft.simulation.supplychain.handlers.SupplyChainHandler#checkContentClass(java.io.Serializable)
70  	 */
71  	protected boolean checkContentClass(final Serializable content)
72  	{
73  		return (content instanceof Bill);
74  	}
75  
76  	/***
77  	 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
78  	 */
79  	public boolean handleContent(final Serializable content)
80  	{
81  		if (this.checkContentClass(content))
82  		{
83  			Bill bill = (Bill) content;
84  			try
85  			{
86  				bill.getSender().getSimulator().scheduleEvent(
87  						bill.getFinalPaymentDate()
88  								- bill.getSender().getSimulatorTime()
89  								+ this.maximumTimeOut, this, this,
90  						"checkPayment", new Object[]{bill});
91  			} catch (Exception exception)
92  			{
93  				Logger.severe(this, "handleContent", exception);
94  			}
95  
96  			return true;
97  		}
98  		return false;
99  	}
100 
101 	/***
102 	 * @param bill the bill
103 	 */
104 	protected void checkPayment(final Bill bill)
105 	{
106 		if (!bill.isPaid())
107 		{
108 			// sad moment, we have to pay...
109 			this.forcedPay(bill);
110 		}
111 	}
112 
113 	/***
114 	 * Pay.
115 	 * 
116 	 * @param bill the bill to pay.
117 	 */
118 	private void forcedPay(final Bill bill)
119 	{
120 		// make a payment to send out
121 		this.owner.getBankAccount().withdrawFromBalance(bill.getPrice());
122 		Payment payment = new Payment(super.owner, bill.getSender(), bill
123 				.getInternalDemandID(), bill, bill.getPrice());
124 		super.owner.sendContent(payment, 0.0);
125 		if (this.debug)
126 		{
127 			System.out
128 					.println("DEBUG -- INTERACTIVEBILLTIMEOUTHANDLER: FORCED PAYMENT IMPOSED: ");
129 		}
130 	}
131 }