1 13 package com.tonbeller.wcf.convert; 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.apache.commons.beanutils.PropertyUtils; 22 import org.apache.log4j.Logger; 23 import org.w3c.dom.Element ; 24 25 import com.tonbeller.wcf.format.FormatException; 26 import com.tonbeller.wcf.format.FormatHandler; 27 import com.tonbeller.wcf.format.Formatter; 28 import com.tonbeller.wcf.ui.Item; 29 import com.tonbeller.wcf.ui.Select; 30 import com.tonbeller.wcf.utils.DomUtils; 31 32 35 public abstract class SelectConverterBase extends NodeConverterBase { 36 private static Logger logger = Logger.getLogger(SelectConverterBase.class); 37 38 42 public void convert(Formatter fmt, Map params, Map fileSource, Element elem, Object bean) 43 throws ConvertException, FormatException, IllegalAccessException , NoSuchMethodException , InvocationTargetException { 44 45 if (Select.isDisabled(elem)) 47 return; 48 49 String id = Select.getId(elem); 50 51 Object inputAvail = params.get(id + ".valid"); 53 if (inputAvail == null) 54 return; 55 56 String [] values = (String []) params.get(id); 58 if (values == null) 59 values = new String [0]; 60 61 DomUtils.removeAttribute(elem, "error"); 62 63 List items = Select.getItems(elem); 65 for (Iterator it = items.iterator(); it.hasNext();) 66 Item.setSelected((Element ) it.next(), false); 67 68 List selected = selectListItems(elem, values, items); 70 71 updateModelReference(fmt, elem, bean); 72 } 73 74 77 List selectListItems(Element elem, String [] values, List items) throws ConvertException { 78 List selected = new ArrayList (); 79 for (int i = 0; i < values.length; i++) { 80 String itemId = values[i]; 81 boolean found = false; 82 search : for (Iterator it = items.iterator(); it.hasNext();) { 83 Element item = (Element ) it.next(); 84 if (Item.getId(item).equals(itemId)) { 85 Item.setSelected(item, true); 86 selected.add(item); 87 found = true; 88 break search; 89 } 90 } 91 if (!found) { 92 String mesg = "Item with id=\"" + itemId + "\" not found in ListBox"; 93 showMissing(elem, mesg); 94 } 95 } 96 return selected; 97 } 98 99 100 101 protected abstract void updateModelReference(Formatter fmt, Element elem, Object bean) 102 throws FormatException, IllegalAccessException , NoSuchMethodException , InvocationTargetException ; 103 104 105 108 public void convert(Formatter fmt, Object bean, Element elem) 109 throws ConvertException, IllegalAccessException , NoSuchMethodException , InvocationTargetException { 110 111 String modelReference = Select.getModelReference(elem); 113 if (modelReference.length() == 0) 114 return; 115 116 String type = Select.getType(elem); 118 String formatString = Select.getFormatString(elem); 119 FormatHandler handler = fmt.getHandler(type); 120 if (handler == null) 121 throw new FormatException("no handler found for type: " + type); 122 123 Object o = PropertyUtils.getProperty(bean, modelReference); 125 Object [] values = handler.toObjectArray(o); 126 if (values == null) 127 values = new Object [0]; 128 129 List items = Select.getItems(elem); 131 for (Iterator it = items.iterator(); it.hasNext(); ) 132 Item.setSelected((Element )it.next(), false); 133 134 for (int i = 0; i < values.length; i++) { 136 Object beanValue = values[i]; 137 boolean found = false; 138 search: for (Iterator it = items.iterator(); it.hasNext(); ) { 139 Element item = (Element )it.next(); 140 Object itemValue = handler.parse(Item.getValue(item), formatString); 141 if (itemValue.equals(beanValue)) { 142 Item.setSelected(item, true); 143 found = true; 144 break search; 145 } 146 } 147 if (!found) { 148 String mesg = "No item has a value of \"" + beanValue + "\"."; 149 showMissing(elem, mesg); 150 } 151 } 152 } 153 154 private void showMissing(Element elem, String mesg) { 155 boolean soft = "true".equals(elem.getAttribute("ignore-missing")); 156 if (soft) 157 logger.warn(mesg); 158 else 159 throw new ConvertException(mesg); 160 } 161 162 } 163 | Popular Tags |