1 13 package com.tonbeller.wcf.convert; 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.Map ; 17 18 import org.apache.commons.beanutils.PropertyUtils; 19 import org.apache.log4j.Logger; 20 import org.w3c.dom.Element ; 21 22 import com.tonbeller.wcf.format.FormatException; 23 import com.tonbeller.wcf.format.FormatHandler; 24 import com.tonbeller.wcf.format.Formatter; 25 import com.tonbeller.wcf.ui.EditCtrl; 26 import com.tonbeller.wcf.ui.TypedCtrl; 27 import com.tonbeller.wcf.utils.XoplonNS; 28 29 34 public class EditCtrlConverter extends NodeConverterBase { 35 private static Logger logger = Logger.getLogger(EditCtrlConverter.class); 36 37 40 public void convert(Formatter formatter, Map param, Map fileSource, Element element, Object bean) 41 throws FormatException, IllegalAccessException , NoSuchMethodException , InvocationTargetException { 42 43 if (TypedCtrl.isDisabled(element)) 45 return; 46 47 String id = EditCtrl.getId(element); 48 String [] inputValue = (String []) param.get(id); 49 50 if (inputValue != null && inputValue.length > 0) { 52 53 XoplonNS.removeAttribute(element, "error"); 54 55 String formatString = EditCtrl.getFormatString(element); 57 try { 58 59 checkRequired(formatter.getLocale(), element, inputValue[0].trim().length() == 0); 60 61 String type = EditCtrl.getType(element); 62 FormatHandler handler = formatter.getHandler(type); 63 if (handler == null) 64 throw new FormatException("no handler found for type: " + type); 65 66 Object newValue = handler.parse(inputValue[0], formatString); 67 String strValue = handler.format(newValue, formatString); 68 EditCtrl.setValue(element, strValue); 69 70 String model = EditCtrl.getModelReference(element); 71 if (bean != null && model.length() > 0) { 72 PropertyUtils.setProperty(bean, model, newValue); 73 } 74 75 } catch (IllegalAccessException e) { 76 logger.info("exception caught", e); 77 XoplonNS.setAttribute(element, "error", e.getMessage()); 78 XoplonNS.setAttribute(element, "value", inputValue[0]); 79 throw e; 80 } catch (NoSuchMethodException e) { 81 logger.info("exception caught", e); 82 XoplonNS.setAttribute(element, "error", e.getMessage()); 83 XoplonNS.setAttribute(element, "value", inputValue[0]); 84 throw e; 85 } catch (InvocationTargetException e) { 86 logger.info("exception caught", e); 87 XoplonNS.setAttribute(element, "error", e.getMessage()); 88 XoplonNS.setAttribute(element, "value", inputValue[0]); 89 throw e; 90 } catch (FormatException e) { 91 logger.info("invalid user input: " + e.getMessage()); 92 XoplonNS.setAttribute(element, "error", e.getMessage()); 93 XoplonNS.setAttribute(element, "value", inputValue[0]); 94 throw e; 95 } 96 } 97 } 98 99 102 public void convert(Formatter formatter, Object bean, Element element) 103 throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 104 105 try { 106 String model = EditCtrl.getModelReference(element); 107 if (model.length() == 0) 108 return; 109 110 String type = EditCtrl.getType(element); 111 FormatHandler handler = formatter.getHandler(type); 112 if (handler == null) 113 throw new FormatException("no handler found for type: " + type); 114 115 Object value = PropertyUtils.getProperty(bean, model); 116 String pattern = EditCtrl.getFormatString(element); 117 String strValue = handler.format(value, pattern); 118 EditCtrl.setValue(element, strValue); 119 120 } catch (IllegalAccessException e) { 121 XoplonNS.setAttribute(element, "error", e.getMessage()); 122 throw e; 123 } catch (NoSuchMethodException e) { 124 XoplonNS.setAttribute(element, "error", e.getMessage()); 125 throw e; 126 } catch (InvocationTargetException e) { 127 XoplonNS.setAttribute(element, "error", e.getMessage()); 128 throw e; 129 } catch (FormatException e) { 130 XoplonNS.setAttribute(element, "error", e.getMessage()); 131 throw e; 132 } 133 } 134 135 } 136 | Popular Tags |