KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > transfer > Transfer


1 /*
2  * Created on 20.10.2004
3  */

4 package com.nightlabs.ipanema.transfer;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Date JavaDoc;
8
9 import com.nightlabs.ipanema.security.User;
10
11 /**
12  * @author Marco Schulze - marco at nightlabs dot de
13  *
14  * @jdo.persistence-capable
15  * identity-type = "application"
16  * objectid-class = "com.nightlabs.ipanema.transfer.id.TransferID"
17  * detachable = "true"
18  *
19  * @jdo.inheritance strategy = "new-table"
20  */

21 public abstract class Transfer
22     implements Serializable JavaDoc
23 {
24     /**
25      * @jdo.field primary-key="true"
26      * @jdo.column length="100"
27      */

28     private String JavaDoc organisationID;
29
30     /**
31      * @jdo.field primary-key="true"
32      * @jdo.column length="100"
33      */

34     private String JavaDoc transferTypeID;
35
36     /**
37      * @jdo.field primary-key="true"
38      */

39     private long transferID;
40
41     private Transfer container;
42
43     private Anchor from;
44     private Anchor to;
45
46     public User initiator;
47     private Date JavaDoc timestamp;
48
49     protected Transfer() { }
50
51     /**
52      * This constructor creates an instance of Transfer and automatically registers it in the TransferRegistry.
53      *
54      * @param transferRegistry The transferRegistry is used to obtain a primary key and to register this instance automatically there.
55      * @param container Can be null. If not null, container defines the enclosing Transfer of which this Transfer is a part.
56      * @param initiator The user which is responsible for this transfer. Must not be null.
57      * @param from The source of this transfer. Must not be null.
58      * @param to The destination of this transfer. Must not be null.
59      */

60     public Transfer(TransferRegistry transferRegistry, String JavaDoc transferTypeID, Transfer container, User initiator, Anchor from, Anchor to)
61     {
62         this.organisationID = transferRegistry.getOrganisationID();
63         this.transferTypeID = transferTypeID;
64         this.transferID = transferRegistry.createTransferID(this.organisationID, transferTypeID);
65         this.container = container;
66         if (initiator == null)
67             throw new NullPointerException JavaDoc("initiator must not be null! Someone must be responsible!");
68         this.initiator = initiator;
69         if (from == null)
70             throw new NullPointerException JavaDoc("from must not be null! Nothing can come out of nirvana!");
71         this.from = from;
72         if (to == null)
73             throw new NullPointerException JavaDoc("to must not be null! Even nirvana must be known as Anchor!");
74         this.to = to;
75         this.timestamp = new Date JavaDoc();
76         transferRegistry.addTransfer(this);
77     }
78
79     public static String JavaDoc getPrimaryKey(String JavaDoc organisationID, String JavaDoc transferTypeID, long transferID)
80     {
81         return organisationID + '/' + transferTypeID + '/' + Long.toHexString(transferID);
82     }
83
84     public String JavaDoc getPrimaryKey()
85     {
86         return getPrimaryKey(organisationID, transferTypeID, transferID);
87     }
88
89     /**
90      * @return Returns the organisationID.
91      */

92     public String JavaDoc getOrganisationID()
93     {
94         return organisationID;
95     }
96
97     /**
98      * @return Returns the transferID.
99      */

100     public long getTransferID()
101     {
102         return transferID;
103     }
104
105     /**
106      * One Transfer can be part of an enclosing Tranfer. E.g. if a product needs to be shipped, it may first
107      * need to be checked out of a certain storage hall, brought to a test center (to assure the product is in
108      * good order) and finally leaves the source and reaches the customer. Thus, in this example, we have the
109      * enclosing transfer: Organisation A transfers object to Organisation B. This transfer contains two transfers:
110      * First the one from the storage hall to the test center and second from test center to customer.
111      *
112      * @return Returns the container, which may be null.
113      */

114     public Transfer getContainer()
115     {
116         return container;
117     }
118
119     /**
120      * The initiator is the user who is responsible for this transfer.
121      *
122      * @return Returns the initiator, which is never null.
123      */

124     public User getInitiator()
125     {
126         return initiator;
127     }
128     /**
129      * @return Returns the from. Never null.
130      */

131     public Anchor getFrom()
132     {
133         return from;
134     }
135     /**
136      * @return Returns the to. Never null.
137      */

138     public Anchor getTo()
139     {
140         return to;
141     }
142
143     public static final int ANCHORTYPE_UNKNOWN = 0;
144     public static final int ANCHORTYPE_FROM = 1;
145     public static final int ANCHORTYPE_TO = 2;
146     public int getAnchorType(Anchor anchor)
147     {
148         if (anchor == null)
149             throw new NullPointerException JavaDoc("anchor must not be null!");
150
151         if (from.getPrimaryKey().equals(anchor.getPrimaryKey()))
152             return ANCHORTYPE_FROM;
153         
154         if (to.getPrimaryKey().equals(anchor.getPrimaryKey()))
155             return ANCHORTYPE_TO;
156         
157         return ANCHORTYPE_UNKNOWN;
158     }
159     
160     /**
161      * When did the transfer happen.
162      *
163      * @return Returns the timestamp. Never null.
164      */

165     public Date JavaDoc getTimestamp()
166     {
167         return timestamp;
168     }
169
170 }
171
Popular Tags