Handle null in 'optional' Ingredient field

version2
Tim Holloway 2 years ago
parent 5ef369743a
commit e5984e5d6f
  1. 52
      src/main/java/com/mousetech/gourmetj/IngredientUI.java

@ -1,6 +1,5 @@
package com.mousetech.gourmetj;
import com.mousetech.gourmetj.persistence.model.Ingredient;
import com.mousetech.gourmetj.persistence.model.IngredientIF;
import com.mousetech.gourmetj.persistence.model.Recipe;
@ -9,8 +8,8 @@ import com.mousetech.gourmetj.IngredientDigester.IngredientAmountFormat;
/**
* JSF-friendly decorator for @see Ingredient. Formats amount
* with fractions and supports checkboxes. Primary use
* is as a JSF TableModel wrapped content in @see RecipeDetailBean.
* with fractions and supports checkboxes. Primary use is as a
* JSF TableModel wrapped content in @see RecipeDetailBean.
*
* TestedBy @see IngredientUITest
*
@ -22,6 +21,7 @@ public class IngredientUI implements IngredientIF {
/**
* Constructor.
*
* @param ingredient
*/
public IngredientUI(Ingredient ingredient) {
@ -31,8 +31,9 @@ public class IngredientUI implements IngredientIF {
public Ingredient getIngredient() {
return this.ingredient;
}
private boolean ingGroup;
/**
* @param ingGroup the ingGroup to set
*/
@ -41,25 +42,24 @@ public class IngredientUI implements IngredientIF {
}
/**
* Ingredient groups are rendered visually as synthetic
* rows. Actual group IDs are part of line-item ingredients,
* so when building the model, we create a group row when
* the line item inggroup value changes.
* Ingredient groups are rendered visually as synthetic rows.
* Actual group IDs are part of line-item ingredients, so
* when building the model, we create a group row when the
* line item inggroup value changes.
*
* @return <code>true</code> for an Ingredient Group
* header row.
* @return <code>true</code> for an Ingredient Group header
* row.
*/
public boolean isIngGroup() {
return this.ingGroup;
}
/**
* Row selection checkbox (UI only)
*/
private boolean selected;
/**
* @return the selected status
*/
@ -74,7 +74,6 @@ public class IngredientUI implements IngredientIF {
this.selected = selected;
}
/**
* @return
* @see com.mousetech.gourmetj.persistence.model.Ingredient#getAmount()
@ -93,25 +92,28 @@ public class IngredientUI implements IngredientIF {
/**
* Get amount display-friendly
*
* @see #getAmount
*/
public String getDisplayAmount() {
// TODO
Double amt = ingredient.getAmount();
if ( amt == null ) {
if (amt == null) {
return "";
}
String amt1 = IngredientDigester.displayAmount(
IngredientAmountFormat.IA_SYMBOLS, amt, ingredient.getRangeamount());
IngredientAmountFormat.IA_SYMBOLS, amt,
ingredient.getRangeamount());
return amt1;
}
/**
* Set amount display-friendly
*
* @see #setAmount
*/
public void setDisplayAmount(String amount) {
if ( amount.isBlank() ) {
if (amount.isBlank()) {
ingredient.setAmount(null);
}
IngredientDigester digester = new IngredientDigester();
@ -119,6 +121,7 @@ public class IngredientUI implements IngredientIF {
ingredient.setAmount(amt[0]);
ingredient.setRangeamount(amt[1]);
}
/**
* @return
* @see com.mousetech.gourmetj.persistence.model.Ingredient#getDeleted()
@ -225,10 +228,15 @@ public class IngredientUI implements IngredientIF {
/**
* Get optional value in boolean Checkbox friendly form
*
* @return
*/
public boolean getOptionalCB() {
return ingredient.getOptional() != 0;
Integer optional = ingredient.getOptional();
if (optional == null) {
return false;
}
return optional != 0;
}
public void setOptionalCB(boolean value) {
@ -328,7 +336,7 @@ public class IngredientUI implements IngredientIF {
* @see com.mousetech.gourmetj.persistence.model.Ingredient#setShopoptional(java.lang.Integer)
*/
public void setShopoptionalCB(boolean shopoptional) {
ingredient.setShopoptional(shopoptional? 1:0);
ingredient.setShopoptional(shopoptional ? 1 : 0);
}
/**
@ -347,8 +355,8 @@ public class IngredientUI implements IngredientIF {
ingredient.setUnit(unit);
}
// This goes to the shopCats table via ManyToOne at save time.
// This goes to the shopCats table via ManyToOne at save
// time.
private String shopCat;
/**

Loading…
Cancel
Save