1 4 package com.nightlabs.ipanema.store; 5 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import javax.jdo.JDOHelper; 12 import javax.jdo.PersistenceManager; 13 14 import com.nightlabs.ipanema.organisation.LocalOrganisation; 15 import com.nightlabs.ipanema.security.User; 16 import com.nightlabs.ipanema.transfer.Anchor; 17 import com.nightlabs.ipanema.transfer.Transfer; 18 import com.nightlabs.ipanema.transfer.TransferRegistry; 19 20 32 public class Store 33 implements TransferRegistry 34 { 35 42 public static Store getStore(PersistenceManager pm) 43 { 44 Iterator it = pm.getExtent(Store.class).iterator(); 45 if (it.hasNext()) 46 return (Store)it.next(); 47 48 Store store = new Store(); 49 50 it = pm.getExtent(LocalOrganisation.class).iterator(); 52 if (!it.hasNext()) 53 throw new IllegalStateException ("LocalOrganisation undefined in datastore!"); 54 LocalOrganisation localOrganisation = (LocalOrganisation) it.next(); 55 store.organisationID = localOrganisation.getOrganisation().getOrganisationID(); 56 57 pm.makePersistent(store); 58 return store; 59 } 60 61 65 private String organisationID; 66 67 private long nextProductID = 0; 68 69 protected Store() { } 70 71 86 protected Map productTypes = new HashMap (); 87 88 103 protected Map productTypeStatusTrackers = new HashMap (); 104 105 120 protected Map products = new HashMap (); 121 122 137 protected Map productStatusTrackers = new HashMap (); 138 139 154 protected Map productTransferTrackers = new HashMap (); 155 156 159 public String getOrganisationID() 160 { 161 return organisationID; 162 } 163 166 protected void setOrganisationID(String organisationID) 167 { 168 this.organisationID = organisationID; 169 } 170 171 protected synchronized long createProductID() 172 { 173 long res = nextProductID++; 174 return res; 175 } 176 177 182 public void addProductType(User user, Product productType) 183 { 184 if (productType.getProductID() == null) { 185 if (productType.getOrganisationID() != null && 186 !this.getOrganisationID().equals(productType.getOrganisationID())) 187 throw new IllegalArgumentException ("productType.productID is null, but productType.organisationID is not null and a foreign organisation! Cannot generate an ID for a foreign organisation!"); 188 189 productType.setOrganisationID(getOrganisationID()); 190 productType.setProductID(Long.toHexString(createProductID())); 191 }; 192 193 if (!Product.NATURE_TYPE.equals(productType.getNature())) 194 throw new IllegalArgumentException ("productType has not nature Product.NATURE_TYPE!"); 195 196 String key = productType.getPrimaryKey(); 197 if (productTypes.containsKey(key)) 198 throw new IllegalStateException ("Cannot add productType, because a productType with the same key already exists!"); 199 200 205 boolean hollow = !this.getOrganisationID().equals(productType.getOrganisationID()); 206 207 ProductStatusTracker productStatusTracker = new ProductStatusTracker(user, productType, hollow); 208 productTypes.put(key, productType); 209 productTypeStatusTrackers.put(key, productStatusTracker); 210 } 211 212 public Product getProductType(String organisationID, String productTypeID) 213 { 214 return (Product)productTypes.get(Product.getPrimaryKey(organisationID, productTypeID)); 215 } 216 public ProductStatusTracker getProductTypeStatusTracker(String organisationID, String productTypeID) 217 { 218 return (ProductStatusTracker)productTypeStatusTrackers.get(Product.getPrimaryKey(organisationID, productTypeID)); 219 } 220 public ProductStatusTracker getProductTypeStatusTracker(Product productType) 221 { 222 if (productType == null) 223 throw new NullPointerException ("productType must not be null!"); 224 return (ProductStatusTracker)productTypeStatusTrackers.get(productType.getPrimaryKey()); 225 } 226 227 public void addProduct(User user, Product product) 228 { 229 if (product.getProductID() == null) { 230 if (product.getOrganisationID() != null && 231 !this.getOrganisationID().equals(product.getOrganisationID())) 232 throw new IllegalArgumentException ("product.productID is null, but product.organisationID is not null and a foreign organisation! Cannot generate an ID for a foreign organisation!"); 233 234 product.setOrganisationID(getOrganisationID()); 235 product.setProductID(Long.toHexString(createProductID())); 236 }; 237 238 if (!Product.NATURE_INSTANCE.equals(product.getNature())) 239 throw new IllegalArgumentException ("product has not nature Product.NATURE_INSTANCE!"); 240 241 String key = product.getPrimaryKey(); 242 if (products.containsKey(key)) 243 throw new IllegalStateException ("Cannot add product, because a product with the same key already exists!"); 244 245 250 boolean hollow = !this.getOrganisationID().equals(product.getOrganisationID()); 251 252 ProductStatusTracker productStatusTracker = new ProductStatusTracker(user, product, hollow); 253 ProductTransferTracker productTransferTracker = new ProductTransferTracker(product); 254 products.put(key, product); 255 productStatusTrackers.put(key, productStatusTracker); 256 productTransferTrackers.put(key, productTransferTracker); 257 } 258 259 public Product getProduct(String organisationID, String productID) 260 { 261 return (Product)products.get(Product.getPrimaryKey(organisationID, productID)); 262 } 263 public ProductStatusTracker getProductStatusRegistry(String organisationID, String productID) 264 { 265 return (ProductStatusTracker)productStatusTrackers.get(Product.getPrimaryKey(organisationID, productID)); 266 } 267 public ProductStatusTracker getProductStatusTracker(Product product) 268 { 269 if (product == null) 270 throw new NullPointerException ("product must not be null!"); 271 return (ProductStatusTracker)productStatusTrackers.get(product.getPrimaryKey()); 272 } 273 public ProductTransferTracker getProductTransferTracker(String organisationID, String productID) 274 { 275 return (ProductTransferTracker)productTransferTrackers.get(Product.getPrimaryKey(organisationID, productID)); 276 } 277 public ProductTransferTracker getProductTransferTracker(Product product) 278 { 279 return (ProductTransferTracker)productTransferTrackers.get(product.getPrimaryKey()); 280 } 281 282 protected ProductTransfer transferProducts(ProductTransfer container, User initiator, Anchor from, Anchor to, Collection products) 283 { 284 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 285 if (pm == null) 286 throw new IllegalStateException ("This store is not persistent! I don't have a PersistenceManager!"); 287 288 ProductTransfer productTransfer = new ProductTransfer(this, null, initiator, from, to, products); 289 for (Iterator it = products.iterator(); it.hasNext(); ) { 290 Product product = (Product)it.next(); 291 getProductTransferTracker(product).addProductTransfer(productTransfer); 292 } 293 return productTransfer; 296 } 297 298 public ProductTransfer transferProducts(User initiator, Anchor from, Anchor to, ProductTransfer container) 299 { 300 return transferProducts(container, initiator, from, to, container.products.values()); 301 } 302 303 public ProductTransfer transferProducts(User initiator, Anchor from, Anchor to, Collection products) 304 { 305 return transferProducts(null, initiator, from, to, products); 306 } 307 308 309 private long nextTransferID = 0; 311 312 327 protected Map transfers = new HashMap (); 328 329 335 public void addTransfer(Transfer transfer) 336 { 337 if (transfer == null) 338 throw new NullPointerException ("transfer is null!"); 339 340 if (transfer.getOrganisationID() == null) 341 throw new NullPointerException ("transfer.organisationID is null!"); 342 343 if (transfer.getTransferID() < 0) 344 throw new NullPointerException ("transfer.transferID < 0!"); 345 346 String key = transfer.getPrimaryKey(); 347 if (transfers.containsKey(key)) 348 throw new IllegalStateException ("Cannot add transfer, because a transfer with the same key already exists!"); 349 350 transfers.put(transfer.getPrimaryKey(), transfer); 351 } 352 353 public synchronized long createTransferID(String organisationID, String transferTypeID) 354 { 355 if (organisationID.equals(getOrganisationID())) 356 throw new IllegalArgumentException ("This instance of Store can only create transferIDs for the organisation \""+getOrganisationID()+"\"!"); 357 358 if (!ProductTransfer.TRANSFERTYPEID.equals(transferTypeID)) 359 throw new IllegalArgumentException ("This implementation of TransferRegistry manages only Transfers with transferTypeID=\""+ProductTransfer.TRANSFERTYPEID+"\"!"); 360 long res = nextTransferID++; 361 return res; 362 } 363 } 365 | Popular Tags |