# ChoiceController.jpf Sample

This topic inludes the source code for the ChoiceController.jpf Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebApp/tagSamples/netui_databinding/choice/

## Sample Source Code

`001 package tagSamples.netui_databinding.choice; 002 003 import com.bea.wlw.netui.pageflow.Forward; 004 import tagSamples.netui_databinding.choice.ItemsDBControl; 005 import weblogic.jws.control.DatabaseFilter; 006 import javax.sql.RowSet; 007 import java.sql.SQLException; 008 import com.bea.wlw.netui.databinding.form.RowSetForm; 009 import com.bea.wlw.netui.pageflow.FormData; 010 import com.bea.wlw.netui.tags.databinding.SortFilterService; 011 import java.util.ArrayList; 012 import java.util.List; 013 import java.io.Serializable; 014 015 /** 016 * The page flow demonstrates the use of the netui:choice tag. 017 * See items.jsp and shipping.jsp for syntax details. 018 * 019 * @jpf:view-properties view-properties:: 020 * <!-- This data is auto-generated. Hand-editing this section is not recommended. --> 021 * <view-properties> 022 * <pageflow-object id="pageflow:/tagSamples/netui_databinding/choice/choiceController.jpf"/> 023 * <pageflow-object id="action:begin.do"> 024 * <property value="120" name="x"/> 025 * <property value="120" name="y"/> 026 * </pageflow-object> 027 * <pageflow-object id="page:index.jsp"> 028 * <property value="320" name="x"/> 029 * <property value="120" name="y"/> 030 * </pageflow-object> 031 * <pageflow-object id="page:/error.jsp"> 032 * <property value="240" name="x"/> 033 * <property value="860" name="y"/> 034 * </pageflow-object> 035 * <pageflow-object id="page:items.jsp"> 036 * <property value="320" name="x"/> 037 * <property value="220" name="y"/> 038 * </pageflow-object> 039 * <pageflow-object id="page:shipping.jsp"> 040 * <property value="320" name="x"/> 041 * <property value="320" name="y"/> 042 * </pageflow-object> 043 * <pageflow-object id="forward:path#start#index.jsp#@action:begin.do@"> 044 * <property value="112,112,112,112" name="elbowsY"/> 045 * <property value="156,220,220,284" name="elbowsX"/> 046 * <property value="West_1" name="toPort"/> 047 * <property value="East_1" name="fromPort"/> 048 * <property value="start" name="label"/> 049 * </pageflow-object> 050 * <pageflow-object id="control:tagSamples.netui_databinding.choice.ItemsDBControl#myControl"> 051 * <property value="28" name="x"/> 052 * <property value="34" name="y"/> 053 * </pageflow-object> 054 * <pageflow-object id="formbeanprop:tagSamples.netui_databinding.choice.choiceController.DatabaseForm#itemnumber#int"/> 055 * <pageflow-object id="formbeanprop:tagSamples.netui_databinding.choice.choiceController.DatabaseForm#itemname#java.lang.String"/> 056 * <pageflow-object id="formbeanprop:tagSamples.netui_databinding.choice.choiceController.DatabaseForm#quantityavailable#int"/> 057 * <pageflow-object id="formbeanprop:tagSamples.netui_databinding.choice.choiceController.DatabaseForm#price#java.lang.Double"/> 058 * <pageflow-object id="formbean:tagSamples.netui_databinding.choice.choiceController.DatabaseForm"/> 059 * </view-properties> 060 * :: 061 * 062 */ 063 public class choiceController extends com.bea.wlw.netui.pageflow.PageFlowController 064 { 065 /** 066 * The Items database control. 067 * @common:control 068 */ 069 private ItemsDBControl myControl; 070 071 private transient RowSet allRows; 072 private transient SortFilterService sortFilterService; 073 private transient String gridName = "choiceTagControllerGridName"; 074 private Cart cart = null; 075 076 public Cart getCart() 077 { 078 return cart; 079 } 080 081 public Double sumCartItems(List items) 082 { 083 if(items == null) return new Double(0); 084 085 double sum = 0; 086 for(int i = 0; i < items.size(); i++) 087 { 088 LineItem item = (LineItem)items.get(i); 089 sum += item.getQuantity() * item.getPrice(); 090 } 091 092 return new Double(sum); 093 } 094 095 public String getShippingState(int shipState) 096 { 097 if(shipState == LineItem.IN_TRANSIT) 098 return "inTransit"; 099 else if(shipState == LineItem.NOT_SHIPPED) 100 return "notShipped"; 101 else if(shipState == LineItem.ARRIVED) 102 return "arrived"; 103 else return "unknown"; 104 } 105 106 107 public String isItemOnSale(int quantityavailable) 108 { 109 if(quantityavailable < 10) 110 return "true"; 111 else 112 return "false"; 113 } 114 115 116 /** 117 * This method represents the point of entry into the page group 118 * 119 * @jpf:action 120 * @jpf:forward name="start" path="index.jsp" 121 * @jpf:catch method="sqlExceptionHandler" type="SQLException" 122 */ 123 public Forward begin() 124 throws Exception 125 { 126 // Initiate the allRows object, displayed on items.jsp. 127 sortFilterService = SortFilterService.getInstance( getRequest () ); 128 allRows = myControl.getAllItems( getSortFilterService ().getDatabaseFilter ( gridName )); 129 130 // Initiate the cart object, displayed on shipping.jsp. 131 if(cart == null) 132 cart = initCart(); 133 134 return new Forward( "start" ); 135 } 136 137 138 /** 139 * @jpf:exception-handler 140 * @jpf:forward name="errorPage" path="/error.jsp" 141 */ 142 protected Forward sqlExceptionHandler( SQLException aSQLException, String aString, String aString2, FormData aFormData3 ) 143 { 144 // log sql exception here 145 return new Forward("errorPage"); 146 } 147 148 149 public SortFilterService getSortFilterService() 150 { 151 return sortFilterService; 152 } 153 154 155 public RowSet getAllRows() 156 { 157 return allRows; 158 } 159 160 private Cart initCart() 161 { 162 Cart c = new Cart(); 163 c.addItem(new LineItem("Product A", 3, 9.95, 1)); 164 c.addItem(new LineItem("Product C", 2, 19.95, 2)); 165 c.addItem(new LineItem("Product B", 5, 29.95, 3)); 166 c.addItem(new LineItem("Product D", 1, 39.95, 4)); 167 c.addItem(new LineItem("Product E", 3, 59.95, 3)); 168 c.addItem(new LineItem("Product F", 2, 1.95, 2)); 169 c.addItem(new LineItem("Product G", 2, 4.95, 2)); 170 171 return c; 172 } 173 174 public static class Cart 175 implements Serializable 176 { 177 private List items = null; 178 179 public void addItem(LineItem item) 180 { 181 if(items == null) items = new ArrayList(); 182 183 items.add(item); 184 } 185 186 public List getLineItemList() 187 { 188 return items; 189 } 190 191 public LineItem[] getLineItemArray() 192 { 193 if(items == null) return null; 194 else return (LineItem[])items.toArray(); 195 } 196 } 197 198 public static class LineItem 199 implements Serializable 200 { 201 public static final int NOT_SHIPPED = 1; 202 public static final int IN_TRANSIT = 2; 203 public static final int ARRIVED = 3; 204 public static final int UNKNOWN = 4; 205 206 private String name = null; 207 private int quantity = 0; 208 private double price = 0.0; 209 private int shipState = NOT_SHIPPED; 210 211 public LineItem(String name, int quantity, double price, int shipState) 212 { 213 this.name = name; 214 this.quantity = quantity; 215 this.price = price; 216 this.shipState = shipState; 217 } 218 219 public String getName() {return name;} 220 public double getPrice() {return price;} 221 public int getQuantity() {return quantity;} 222 public int getShipState() {return shipState;} 223 } 224 225 226 public static class DatabaseForm extends RowSetForm 227 { 228 229 public DatabaseForm() 230 { 231 super (); 232 } 233 234 private int itemnumber; 235 private String itemname; 236 private int quantityavailable; 237 private Double price; 238 239 public int getItemnumber() 240 { 241 return itemnumber; 242 } 243 244 public void setItemnumber( int newOne ) 245 { 246 this.itemnumber = newOne; 247 } 248 249 public String getItemname() 250 { 251 return itemname; 252 } 253 254 public void setItemname( String newOne ) 255 { 256 this.itemname = newOne; 257 registerChange ( "itemname" ); 258 } 259 260 public int getQuantityavailable() 261 { 262 return quantityavailable; 263 } 264 265 public void setQuantityavailable( int newOne ) 266 { 267 this.quantityavailable = newOne; 268 registerChange ( "quantityavailable" ); 269 } 270 271 public Double getPrice() 272 { 273 return price; 274 } 275 276 public void setPrice( Double newOne ) 277 { 278 this.price = newOne; 279 registerChange ( "price" ); 280 } 281 } 282 }`