package com.din.din.webapp.beans.interfaces.wrappers;
import java.math.BigDecimal;
import java.util.List;
import com.din.din.model.entities.KeyEntity;
import com.din.din.model.entities.Project;
import com.din.din.model.entities.QuantityType;
import com.din.din.model.entities.QuantityType.MeasurementType;
import com.din.din.model.entities.RecipeItem;
import com.din.din.model.util.EntityCachingManager;
import com.din.din.model.webapp.KeyEntityWrapper;
import com.din.din.webapp.beans.util.BeanUtil;
public class RecipeItemWrapper implements KeyEntityWrapper {
private RecipeItem recipeItem;
private EntityCachingManager cache;
private Project project;
public RecipeItemWrapper(Project project) {
this(project, null);
recipeItem = new RecipeItem();
loadDefaults();
}
public RecipeItemWrapper(Project project, RecipeItem recipeItem) {
this.project = project;
cache = (EntityCachingManager)BeanUtil.getBean(EntityCachingManager.class, "#{cacheBean.cache}");
this.recipeItem = recipeItem;
}
/**
* Intelligently attempt to fill in the blanks on some values
*/
private void loadDefaults() {
QuantityType piecesType = null;
try {
List<KeyEntity> quantityTypes = cache.getListBy(QuantityType.class, "type", MeasurementType.Quantity, "project", project);
for(KeyEntity entity : quantityTypes) {
QuantityType qt = (QuantityType)entity;
if(qt.isRemoveable()) {
piecesType = qt;
}
}
if(piecesType != null) {
recipeItem.setQuantityTypeKey(piecesType.getKey());
}
} catch (Exception e) {
}
}
public RecipeItem getRecipeItem() {
return recipeItem;
}
public RecipeItem getEntity() {
return recipeItem;
}
public QuantityType getQuantityType() {
return (QuantityType) (recipeItem.getQuantityTypeKey() != null ? cache.get(QuantityType.class, recipeItem.getQuantityTypeKey(), true) : null);
}
public void setQuantityType(QuantityType type) {
recipeItem.setQuantityTypeKey(type.getKey());
}
public String getDescription() {
return recipeItem.getDescription();
}
public void setDescription(String description) {
recipeItem.setDescription(description);
}
public BigDecimal getQuantity() {
return recipeItem.getQuantity();
}
public void setQuantity(BigDecimal quantity) {
recipeItem.setQuantity(quantity);
}
@Override
public int hashCode() {
return recipeItem.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RecipeItemWrapper other = (RecipeItemWrapper) obj;
if (recipeItem == null) {
if (other.recipeItem != null)
return false;
} else if (!recipeItem.equals(other.recipeItem))
return false;
return true;
}
}