1 38 39 40 package org.apache.cocoon.faces.samples.carstore; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 import javax.faces.application.Application; 46 import javax.faces.application.FacesMessage; 47 import javax.faces.component.UIComponent; 48 import javax.faces.component.UIInput; 49 import javax.faces.component.UISelectItems; 50 import javax.faces.component.ValueHolder; 51 import javax.faces.context.FacesContext; 52 import javax.faces.convert.Converter; 53 import javax.faces.model.SelectItem; 54 55 import java.util.ArrayList ; 56 import java.util.Enumeration ; 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Map ; 60 import java.util.MissingResourceException ; 61 import java.util.ResourceBundle ; 62 import java.util.StringTokenizer ; 63 64 136 137 public class CarBean extends Object { 138 139 protected static final Log log = LogFactory.getLog(CarBean.class); 140 141 147 public static final String CONVERTER_ERROR_MESSAGE_ID = 148 "carstore.Converter_Error"; 149 150 151 155 158 159 private ResourceBundle resources = null; 160 161 164 private ResourceBundle priceData = null; 165 166 170 171 private Map components = null; 172 173 177 178 private Map attributes = null; 179 180 184 public CarBean() { 185 this.init(CarStore.DEFAULT_MODEL_PROPERTIES); 186 } 187 188 189 public CarBean(String bundleName) { 190 this.init(bundleName); 191 } 192 193 194 202 203 private void init(String bundleName) { 204 FacesContext context = FacesContext.getCurrentInstance(); 205 ResourceBundle data = null; 206 Enumeration keys = null; 207 components = new HashMap (); 208 209 resources = 211 ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + 212 ".bundles.Resources", 213 context.getViewRoot().getLocale()); 214 215 priceData = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + 217 ".bundles.OptionPrices"); 218 219 if (log.isDebugEnabled()) { 221 log.debug("Loading bundle: " + bundleName + "."); 222 } 223 data = ResourceBundle.getBundle(bundleName, 224 context.getViewRoot().getLocale()); 225 if (log.isDebugEnabled()) { 226 log.debug("Bundle " + bundleName + 227 " loaded. Reading properties..."); 228 } 229 initComponentsFromProperties(context, data); 230 if (log.isDebugEnabled()) { 231 log.debug("done."); 232 } 233 234 if (log.isDebugEnabled()) { 236 log.debug("Loading bundle: Common_options."); 237 } 238 data = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + 239 ".bundles.Common_options"); 240 if (log.isDebugEnabled()) { 241 log.debug("Bundle Common_options loaded. Reading properties..."); 242 } 243 initComponentsFromProperties(context, data); 244 if (log.isDebugEnabled()) { 245 log.debug("done."); 246 } 247 248 if (log.isDebugEnabled()) { 250 log.debug("Loading bundle: " + bundleName + "_options."); 251 } 252 data = ResourceBundle.getBundle(bundleName + "_options"); 253 if (log.isDebugEnabled()) { 254 log.debug("Bundle " + bundleName + 255 "_options loaded. Reading properties..."); 256 } 257 initComponentsFromProperties(context, data); 258 if (log.isDebugEnabled()) { 259 log.debug("done."); 260 } 261 262 attributes = 265 new Map () { 266 public void clear() { 267 CarBean.this.components.clear(); 268 } 269 270 271 public boolean containsKey(Object key) { 272 return CarBean.this.components.containsKey(key); 273 } 274 275 276 public boolean containsValue(Object value) { 277 throw new UnsupportedOperationException (); 278 } 279 280 281 public java.util.Set entrySet() { 282 throw new UnsupportedOperationException (); 283 } 284 285 286 public boolean equals(Object o) { 287 throw new UnsupportedOperationException (); 288 } 289 290 291 public Object get(Object key) { 292 UIComponent component = null; 293 Converter converter = null; 294 Object result = null; 295 if (null == key) { 296 return null; 297 } 298 if (null != (component = (UIComponent) 299 CarBean.this.components.get(key))) { 300 if (component instanceof ValueHolder) { 302 converter = ((ValueHolder) component). 304 getConverter(); 305 result = ((ValueHolder) component).getValue(); 306 } 307 308 if (null != result) { 310 if (null != converter) { 312 result = converter. 314 getAsString(FacesContext. 315 getCurrentInstance(), 316 component, result); 317 } 318 } 319 } 320 return result; 321 } 322 323 324 public int hashCode() { 325 return CarBean.this.components.hashCode(); 326 } 327 328 329 public boolean isEmpty() { 330 return CarBean.this.components.isEmpty(); 331 } 332 333 334 public java.util.Set keySet() { 335 return CarBean.this.components.keySet(); 336 } 337 338 339 public Object put(Object k, Object v) { 340 throw new UnsupportedOperationException (); 341 } 342 343 344 public void putAll(Map t) { 345 throw new UnsupportedOperationException (); 346 } 347 348 349 public Object remove(Object k) { 350 throw new UnsupportedOperationException (); 351 } 352 353 354 public int size() { 355 return CarBean.this.components.size(); 356 } 357 358 359 public java.util.Collection values() { 360 ArrayList result = new ArrayList (); 361 Iterator keys = keySet().iterator(); 362 while (keys.hasNext()) { 363 result.add(get(keys.next())); 364 } 365 return result; 366 } 367 }; 368 369 } 370 371 372 376 377 private void initComponentsFromProperties(FacesContext context, 378 ResourceBundle data) { 379 Application application = context.getApplication(); 380 Enumeration keys = data.getKeys(); 381 String 382 key = null, 383 value = null, 384 componentType = null, 385 valueType = null; 386 UIComponent component = null; 387 388 while (keys.hasMoreElements()) { 390 key = (String ) keys.nextElement(); 391 if (key == null) { 392 continue; 393 } 394 if (-1 != key.indexOf("_")) { 396 continue; 397 } 398 value = data.getString(key); 399 componentType = data.getString(key + "_componentType"); 400 valueType = data.getString(key + "_valueType"); 401 if (log.isDebugEnabled()) { 402 log.debug("populating map for " + key + "\n" + 403 "\n\tvalue: " + value + 404 "\n\tcomponentType: " + componentType + 405 "\n\tvalueType: " + valueType); 406 } 407 component = application.createComponent(componentType); 409 populateComponentWithValue(context, component, componentType, 410 value, valueType); 411 components.put(key, component); 412 } 413 } 414 415 416 421 422 private void populateComponentWithValue(FacesContext context, 423 UIComponent component, 424 String componentType, 425 String value, String valueType) { 426 Application application = context.getApplication(); 427 Converter converter = null; 428 UISelectItems items = null; 429 430 if (!valueType.equals("java.lang.String") && 432 component instanceof ValueHolder) { 433 try { 435 converter = 436 application.createConverter(CarStore.loadClass(valueType, 437 this)); 438 } catch (ClassNotFoundException cne) { 439 FacesMessage errMsg = MessageFactory.getMessage( 440 CONVERTER_ERROR_MESSAGE_ID, 441 (new Object []{valueType})); 442 throw new IllegalStateException (errMsg.getSummary()); 443 } 444 ((ValueHolder) component).setConverter(converter); 446 } 447 448 if (isMultiValue(componentType)) { 450 items = new UISelectItems(); 452 items.setValue(parseStringIntoArrayList(context, component, 453 value, valueType, 454 converter)); 455 component.getChildren().add(items); 457 } else { 458 if (null != converter) { 460 component.getAttributes().put("value", 461 converter.getAsObject(context, 462 component, 463 value)); 464 } else { 465 component.getAttributes().put("value", value); 466 } 467 } 468 } 469 470 471 474 private boolean isMultiValue(String componentType) { 475 if (null == componentType) { 476 return false; 477 } 478 return (componentType.startsWith("javax.faces.SelectMany") || 479 componentType.startsWith("javax.faces.SelectOne")); 480 } 481 482 483 491 public ArrayList parseStringIntoArrayList(FacesContext context, 492 UIComponent component, 493 String value, 494 String valueType, 495 Converter converter) { 496 ArrayList optionsList = null; 497 int i = 0; 498 Object optionValue = null; 499 String 500 optionKey = null, 501 optionLabel = null; 502 Map nonLocalizedOptionValues = null; 503 504 if (value == null) { 505 return null; 506 } 507 StringTokenizer st = new StringTokenizer (value, ","); 508 optionsList = new ArrayList ((st.countTokens()) + 1); 509 while (st.hasMoreTokens()) { 510 optionKey = st.nextToken(); 511 try { 512 optionLabel = resources.getString(optionKey); 513 } catch (MissingResourceException e) { 514 optionLabel = optionKey; 516 } 517 518 if (null != converter) { 519 } else { 521 optionsList.add(new SelectItem(optionKey, optionLabel)); 522 } 523 } 524 return optionsList; 525 } 526 527 528 public String updatePricing() { 529 getCurrentPrice(); 530 return null; 531 } 532 533 534 public Integer getCurrentPrice() { 535 int sum = ((Integer ) ((ValueHolder) getComponents().get("basePrice")). 537 getValue()).intValue(); 538 Iterator iter = getComponents().keySet().iterator(); 539 String key = null; 540 Object value = null; 541 UIComponent component = null; 542 while (iter.hasNext()) { 543 key = (String ) iter.next(); 544 component = (UIComponent) getComponents().get(key); 545 value = component.getAttributes().get("value"); 546 if (null == value || (!(component instanceof UIInput))) { 547 continue; 548 } 549 550 if (value instanceof String ) { 552 try { 553 sum += 554 Integer.valueOf(priceData.getString((String ) value)) 555 .intValue(); 556 } catch (NumberFormatException e) { 557 } 558 } 559 else if (value instanceof Boolean && 561 ((Boolean ) value).booleanValue()) { 562 try { 563 sum += 564 Integer.valueOf(priceData.getString(key)).intValue(); 565 } catch (NumberFormatException e) { 566 } 567 } else if (value instanceof Number ) { 568 sum += ((Number ) value).intValue(); 569 } 570 } 571 Integer result = new Integer (sum); 572 ((ValueHolder) getComponents().get("currentPrice")). 574 setValue(result); 575 return result; 576 } 577 578 579 public Map getComponents() { 580 return components; 581 } 582 583 584 public Map getAttributes() { 585 return attributes; 586 } 587 588 589 } 590 | Popular Tags |