KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > store > ProductStatusTracker


1 /*
2  * Created on 20.10.2004
3  */

4 package com.nightlabs.ipanema.store;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import com.nightlabs.ipanema.security.User;
11
12 /**
13  * The ProductStatusTracker keeps track of status changes (manages a history) and knows the
14  * current status of a product. There exists one instance of ProductStatusTracker in the
15  * datastore for one instance of Product.
16  *
17  * @author Marco Schulze - marco at nightlabs dot de
18  *
19  * @jdo.persistence-capable
20  * identity-type = "application"
21  * objectid-class = "com.nightlabs.ipanema.store.id.ProductStatusTrackerID"
22  * detachable = "true"
23  *
24  * @jdo.inheritance strategy = "new-table"
25  */

26 public class ProductStatusTracker
27     implements Serializable JavaDoc
28 {
29     /**
30      * @jdo.field primary-key="true"
31      * @jdo.column length="100"
32      */

33     private String JavaDoc organisationID;
34
35     /**
36      * @jdo.field primary-key="true"
37      * @jdo.column length="100"
38      */

39     private String JavaDoc productID;
40
41     /**
42      * @jdo.field persistence-modifier="persistent" dependent="true"
43      */

44     private Product product;
45
46     private ProductStatus currentStatus;
47
48     /**
49      * If the product is allocated (or sold, means neither available nor blocked)
50      * and a new allocation is tried, the allocation will not fail, if the allocationOwnerKey
51      * is the same who originally allocated.
52      */

53     private String JavaDoc allocationOwnerKey = null;
54
55     /**
56      * List of ProductStatus. The last item is always identical with currentStatus.
57      *
58      * @jdo.field
59      * persistence-modifier="persistent"
60      * collection-type="collection"
61      * element-type="ProductStatus"
62      * mapped-by="productStatusTracker"
63      * dependent="true"
64      */

65     protected List JavaDoc statusHistory = new ArrayList JavaDoc();
66     
67     private int nextStatusID = 0;
68
69     protected ProductStatusTracker() { }
70
71     public ProductStatusTracker(User user, Product product, boolean hollow)
72     {
73         if (product == null)
74             throw new NullPointerException JavaDoc("product must not be null!");
75         if (product.getOrganisationID() == null)
76             throw new NullPointerException JavaDoc("product.organisationID must not be null!");
77         if (product.getProductID() == null)
78             throw new NullPointerException JavaDoc("product.productID must not be null!");
79
80         this.product = product;
81         this.organisationID = product.getOrganisationID();
82         this.productID = product.getProductID();
83         this.setCurrentStatus(
84                 new ProductStatus(this, user, createStatusID(), ProductStatus.STATUS_BLOCKED, hollow));
85     }
86
87     protected synchronized int createStatusID()
88     {
89         int res = nextStatusID++;
90         return res;
91     }
92     
93     /**
94      * @return Returns the organisationID.
95      */

96     public String JavaDoc getOrganisationID()
97     {
98         return organisationID;
99     }
100     /**
101      * @return Returns the productID.
102      */

103     public String JavaDoc getProductID()
104     {
105         return productID;
106     }
107
108     /**
109      * @return Returns the product.
110      */

111     public Product getProduct()
112     {
113         return product;
114     }
115
116     /**
117      * @return Returns the currentStatus.
118      */

119     public ProductStatus getCurrentStatus()
120     {
121         return currentStatus;
122     }
123     
124     protected void setCurrentStatus(ProductStatus newCurrentStatus)
125     {
126         this.currentStatus = newCurrentStatus;
127         statusHistory.add(currentStatus);
128     }
129
130     /**
131      * @return Returns the allocationOwnerKey.
132      */

133     public String JavaDoc getAllocationOwnerKey()
134     {
135         return allocationOwnerKey;
136     }
137     
138     public synchronized void allocate(User user, String JavaDoc allocationOwnerKey)
139         throws NotAvailableException
140     {
141         ProductStatus previousStatus = getCurrentStatus();
142         if (!ProductStatus.STATUS_AVAILABLE.equals(previousStatus.getStatus())) {
143             // the allocation does NOT fail, if it is done for the same owner
144
if (!this.getAllocationOwnerKey().equals(allocationOwnerKey))
145                 throw new NotAvailableException("Product \""+getProduct().getPrimaryKey()+"\" is not available! Cannot allocate!");
146         }
147
148         setCurrentStatus(
149                 new ProductStatus(
150                         this, user, createStatusID(),
151                         ProductStatus.STATUS_ALLOCATED, previousStatus.isHollow()));
152     }
153
154     public synchronized void release(User user)
155         throws NotAllocatedException
156     {
157         ProductStatus previousStatus = getCurrentStatus();
158         if (!ProductStatus.STATUS_ALLOCATED.equals(previousStatus.getStatus()))
159             throw new NotAllocatedException("Product \""+getProduct().getPrimaryKey()+"\" is not allocated! Cannot release!");
160
161         setCurrentStatus(
162                 new ProductStatus(
163                         this, user, createStatusID(),
164                         ProductStatus.STATUS_AVAILABLE, previousStatus.isHollow()));
165     }
166 }
167
Popular Tags