1 5 package com.nightlabs.ipanema.store; 6 7 import java.io.Serializable ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 import javax.jdo.JDOHelper; 15 import javax.jdo.PersistenceManager; 16 import javax.jdo.StoreCallback; 17 18 import com.nightlabs.inheritance.FieldInheriter; 19 import com.nightlabs.inheritance.Inheritable; 20 import com.nightlabs.inheritance.InheritanceCallbacks; 21 import com.nightlabs.inheritance.MapFieldInheriter; 22 import com.nightlabs.inheritance.jdo.JDOSimpleFieldInheriter; 23 import com.nightlabs.ipanema.organisation.LocalOrganisation; 24 import com.nightlabs.jdo.BaseObjectID; 25 26 37 public class Product 38 implements 39 StoreCallback, 40 Inheritable, InheritanceCallbacks, 41 Serializable 42 { 43 50 private String organisationID = null; 51 52 56 private String productID; 57 58 61 private Product extendedProduct; 62 63 public static final String NATURE_TYPE = "type"; 64 public static final String NATURE_INSTANCE = "instance"; 65 66 72 private String nature; 73 74 89 protected Map fieldMetaDataMap = new HashMap (); 90 91 112 130 147 private Map packagedProducts = new HashMap (); 148 149 152 private boolean productFactoryAssigned; 153 154 157 private ProductFactory productFactory; 158 159 166 private boolean internal; 167 168 protected static String EMPTYSTRING = ""; 169 170 protected Product() {} 171 172 181 public Product( 182 String organisationID, String productID, Product extendedProduct, 183 String nature, ProductFactory productFactory, boolean internal) 184 { 185 if (organisationID != null) 186 setOrganisationID(organisationID); 187 188 setProductID(productID); 189 setNature(nature); 190 setExtendedProduct(extendedProduct); 191 192 this.setProductFactory(productFactory); 193 this.internal = internal; 194 195 } 197 198 201 public String getOrganisationID() 202 { 203 return organisationID; 204 } 205 208 protected void setOrganisationID(String organisationID) 209 { 210 BaseObjectID.assertValidIDString(organisationID, "organisationID"); 211 this.organisationID = organisationID; 212 } 213 216 protected void setProductID(String productID) 217 { 218 BaseObjectID.assertValidIDString(productID, "productID"); 219 this.productID = productID; 220 } 221 public String getProductID() 222 { 223 return productID; 224 } 225 226 public static String getPrimaryKey(String organisationID, String productID) 227 { 228 if (organisationID == null) 229 throw new NullPointerException ("organisationID must not be null!"); 230 if (productID == null) 231 throw new NullPointerException ("productID must not be null!"); 232 return organisationID + '/' + productID; 233 } 234 public String getPrimaryKey() 235 { 236 return organisationID + '/' + productID; 237 } 238 239 242 public Product getExtendedProduct() 243 { 244 return extendedProduct; 245 } 246 249 protected void setExtendedProduct(Product extendedProduct) 250 { 251 if (extendedProduct != null) { 252 if (!NATURE_TYPE.equals(extendedProduct.getNature())) 253 throw new IllegalArgumentException ("extendedProduct is not of nature NATURE_TYPE. Cannot extend!"); 254 255 if (!(extendedProduct.getClass().isInstance(this))) 256 throw new IllegalArgumentException ("This product (\""+this.getPrimaryKey()+"\") is of type " + this.getClass().getName() + " and cannot extend the product \""+extendedProduct.getPrimaryKey()+"\" which is of type "+extendedProduct.getClass().getName()); 257 } 258 259 this.extendedProduct = extendedProduct; 260 } 261 262 265 public String getNature() 266 { 267 return nature; 268 } 269 272 protected void setNature(String nature) 273 { 274 if (nature == null) 275 throw new NullPointerException ("nature must not be null!"); 276 if (!NATURE_INSTANCE.equals(nature) && !NATURE_TYPE.equals(nature)) 277 throw new IllegalArgumentException ("nature must be one of: NATURE_TYPE, NATURE_INSTANCE"); 278 this.nature = nature; 279 } 280 281 303 335 public void addPackagedProduct(Product product) 337 { 338 if (!this.getNature().equals(product.getNature())) 339 throw new IllegalArgumentException ("Nature of product to package (\""+product.getNature()+"\") and this nature (\""+this.getNature()+"\") does not match!"); 340 341 packagedProducts.put(product.getPrimaryKey(), product); 342 } 343 346 public Collection getPackagedProducts() 347 { 348 return packagedProducts.values(); 349 } 350 355 public Product getPackagedProduct(String organisationID, String productID, boolean throwExceptionIfNotExistent) 356 { 357 Product res = (Product)packagedProducts.get(getPrimaryKey(organisationID, productID)); 358 if (res == null && throwExceptionIfNotExistent) 359 throw new IllegalArgumentException ("No packaged Product existing with organisationID=\""+organisationID+"\", productID=\""+productID+"\"!"); 360 return res; 361 } 362 367 public void removePackagedProduct(String organisationID, String productID) 368 { 369 packagedProducts.remove(getPrimaryKey(organisationID, productID)); 370 } 371 373 380 public ProductFactory getProductFactory() 381 { 382 return productFactory; 383 } 384 391 public void setProductFactory(ProductFactory productFactory) 392 { 393 this.productFactory = productFactory; 394 this.productFactoryAssigned = productFactory != null; 395 } 396 403 public boolean isProductFactoryAssigned() 404 { 405 return productFactoryAssigned; 406 } 407 408 private static Set nonInheritableFields = new HashSet (); 410 411 414 public com.nightlabs.inheritance.FieldMetaData getFieldMetaData(String fieldName) 415 { 416 if (fieldName.startsWith("jdo")) 417 return null; 418 419 synchronized (nonInheritableFields) { 420 if (nonInheritableFields.isEmpty()) { 421 nonInheritableFields.add("organisationID"); 422 nonInheritableFields.add("productID"); 423 nonInheritableFields.add("extendedProduct"); 424 nonInheritableFields.add("nature"); 425 nonInheritableFields.add("fieldMetaDataMap"); 426 nonInheritableFields.add("creationSupported"); 428 } 429 if (nonInheritableFields.contains(fieldName)) 430 return null; 431 } 432 433 ProductFieldMetaData fmd = (ProductFieldMetaData)fieldMetaDataMap.get(fieldName); 434 if (fmd == null) { 435 if ("properties".equals(fieldName) || "packagedProducts".equals(fieldName)) 436 fmd = new MapFieldMetaData(this, fieldName); 437 else 438 fmd = new ProductFieldMetaData(this, fieldName); 439 440 fieldMetaDataMap.put(fieldName, fmd); 441 } 443 return fmd; 444 } 445 446 449 public FieldInheriter getFieldInheriter(String fieldName) 450 { 451 if ("packagedProducts".equals(fieldName)) { 452 return new MapFieldInheriter(); 453 } 454 455 return new JDOSimpleFieldInheriter(); 456 } 457 458 461 public void preInherit() 462 { 463 packagedProducts.size(); 466 } 467 468 471 public void postInherit() 472 { 473 } 474 475 478 481 public void jdoPreStore() 482 { 483 if (organisationID != null) 484 return; 485 486 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 487 this.organisationID = LocalOrganisation.getLocalOrganisation(pm).getOrganisationID(); 488 } 489 490 } 491 | Popular Tags |