| 1 4 package com.nightlabs.ipanema.store; 5 6 import java.io.Serializable ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import com.nightlabs.ipanema.security.User; 11 12 26 public class ProductStatusTracker 27 implements Serializable  28 { 29 33 private String organisationID; 34 35 39 private String productID; 40 41 44 private Product product; 45 46 private ProductStatus currentStatus; 47 48 53 private String allocationOwnerKey = null; 54 55 65 protected List statusHistory = new ArrayList (); 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 ("product must not be null!"); 75 if (product.getOrganisationID() == null) 76 throw new NullPointerException ("product.organisationID must not be null!"); 77 if (product.getProductID() == null) 78 throw new NullPointerException ("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 96 public String getOrganisationID() 97 { 98 return organisationID; 99 } 100 103 public String getProductID() 104 { 105 return productID; 106 } 107 108 111 public Product getProduct() 112 { 113 return product; 114 } 115 116 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 133 public String getAllocationOwnerKey() 134 { 135 return allocationOwnerKey; 136 } 137 138 public synchronized void allocate(User user, String allocationOwnerKey) 139 throws NotAvailableException 140 { 141 ProductStatus previousStatus = getCurrentStatus(); 142 if (!ProductStatus.STATUS_AVAILABLE.equals(previousStatus.getStatus())) { 143 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 |