Initial commit from SVN.
This commit is contained in:
147
Foundation Test/src/com/foundation/model/test/DbPart.java
Normal file
147
Foundation Test/src/com/foundation/model/test/DbPart.java
Normal file
@@ -0,0 +1,147 @@
|
||||
package com.foundation.model.test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.foundation.common.AttributeBinding;
|
||||
import com.foundation.event.model.ModelListener;
|
||||
import com.foundation.metadata.Attribute;
|
||||
|
||||
/**
|
||||
* Copyright Wynne Crisman 1999,2006<p>
|
||||
*/
|
||||
public class DbPart extends AbstractModel {
|
||||
public static final Attribute ID = registerAttribute(DbPart.class, "id");
|
||||
public static final Attribute NAME = registerAttribute(DbPart.class, "name");
|
||||
public static final Attribute PRODUCT = registerAttribute(DbPart.class, "product", DbProduct.class);
|
||||
public static final Attribute COST = registerAttribute(DbPart.class, "cost", AO_LAZY);
|
||||
public static final Attribute PRICE = registerAttribute(DbPart.class, "price", AO_CALCULATED);
|
||||
/**
|
||||
* DbPart constructor.
|
||||
*/
|
||||
public DbPart() {
|
||||
super();
|
||||
}//DbPart()//
|
||||
/**
|
||||
* DbPart constructor.
|
||||
*/
|
||||
public DbPart(String name, BigDecimal cost) {
|
||||
super();
|
||||
setName(name);
|
||||
setAttributeValue(PRICE, new BigDecimal(0));
|
||||
setCost(cost);
|
||||
}//DbPart()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.foundation.common.Entity#lazyLoadAttribute(com.foundation.metadata.Attribute)
|
||||
*/
|
||||
protected Object lazyLoadAttribute(Attribute attribute) {
|
||||
Object result;
|
||||
|
||||
if(attribute == COST) {
|
||||
result = new BigDecimal(1.50);
|
||||
}//if//
|
||||
else {
|
||||
result = super.lazyLoadAttribute(attribute);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//lazyLoadAttribute()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.foundation.common.Entity#calculatedAttributeRegister(com.foundation.metadata.Attribute, com.foundation.common.Entity.ICalculatedAttributeListener)
|
||||
*/
|
||||
protected ModelListener calculatedAttributeRegister(Attribute attribute, ICalculatedAttributeListener listener) {
|
||||
if(attribute == PRICE) {
|
||||
ModelListener priceListener = new ModelListener(this);
|
||||
priceListener.addBinding(new AttributeBinding(DbPart.class, COST, 0, listener, null));
|
||||
priceListener.addBinding(new AttributeBinding(DbPart.class, PRODUCT));
|
||||
priceListener.addBinding(new AttributeBinding(DbProduct.class, DbProduct.MARKUP, 0, listener, null));
|
||||
priceListener.initialize();
|
||||
return priceListener;
|
||||
}//if//
|
||||
else {
|
||||
return super.calculatedAttributeRegister(attribute, listener);
|
||||
}//else//
|
||||
}//calculatedAttributeRegister()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.foundation.common.Entity#calculatedAttributeUpdate(com.foundation.metadata.Attribute)
|
||||
*/
|
||||
protected Object calculatedAttributeUpdate(Attribute attribute) {
|
||||
Object result;
|
||||
|
||||
if(attribute == PRICE) {
|
||||
result = calculatePrice();
|
||||
}//if//
|
||||
else {
|
||||
result = super.calculatedAttributeUpdate(attribute);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//calculatedAttributeUpdate()//
|
||||
/**
|
||||
* Gets the id for the part.
|
||||
* @return
|
||||
*/
|
||||
public Integer getId() {
|
||||
return (Integer) getAttributeValue(ID);
|
||||
}//getId()//
|
||||
/**
|
||||
* Gets the name for the part.
|
||||
* @return
|
||||
*/
|
||||
public String getName() {
|
||||
return (String) getAttributeValue(NAME);
|
||||
}//getName()//
|
||||
/**
|
||||
* Sets the name for the part.
|
||||
* @param name
|
||||
*/
|
||||
public void setName(String name) {
|
||||
setAttributeValue(NAME, name);
|
||||
}//setName()//
|
||||
/**
|
||||
* Gets the cost for the part.
|
||||
* @return
|
||||
*/
|
||||
public BigDecimal getCost() {
|
||||
return (BigDecimal) getAttributeValue(COST);
|
||||
}//getCost()//
|
||||
/**
|
||||
* Sets the cost for the part.
|
||||
* @param cost
|
||||
*/
|
||||
public void setCost(BigDecimal cost) {
|
||||
if(cost == null) {
|
||||
cost = new BigDecimal(0.00);
|
||||
}//if//
|
||||
|
||||
setAttributeValue(COST, cost);
|
||||
}//setCost()//
|
||||
/**
|
||||
* Gets the part's containing product.
|
||||
* @return
|
||||
*/
|
||||
public DbProduct getProduct() {
|
||||
return (DbProduct) getAttributeValue(PRODUCT);
|
||||
}//getProduct()//
|
||||
/**
|
||||
* Calculates the price for the item based on the product's markup.
|
||||
* @return
|
||||
*/
|
||||
private BigDecimal calculatePrice() {
|
||||
BigDecimal result = getCost();
|
||||
|
||||
if(getProduct() != null) {
|
||||
result = result.multiply(getProduct().getMarkup());
|
||||
}//if//
|
||||
|
||||
result.setScale(2, BigDecimal.ROUND_HALF_UP);
|
||||
|
||||
return result;
|
||||
}//calculatePrice()//
|
||||
/**
|
||||
* Gets the price for the part which is a function of the product's markup and the cost of this item.
|
||||
* @return
|
||||
*/
|
||||
public BigDecimal getPrice() {
|
||||
return (BigDecimal) getAttributeValue(PRICE);
|
||||
}//getPrice()//
|
||||
}//Part//
|
||||
Reference in New Issue
Block a user