1 41 package com.tonbeller.wcf.convert; 42 43 import java.lang.reflect.InvocationTargetException ; 44 import java.util.Map ; 45 46 import org.apache.commons.beanutils.PropertyUtils; 47 import org.apache.commons.fileupload.FileItem; 48 import org.apache.log4j.Logger; 49 import org.w3c.dom.Element ; 50 51 import com.tonbeller.wcf.format.Formatter; 52 import com.tonbeller.wcf.ui.FileUploadCtrl; 53 import com.tonbeller.wcf.utils.XoplonNS; 54 55 public class FileUploadConverter extends NodeConverterBase { 56 57 private static Logger logger = Logger.getLogger(FileUploadConverter.class); 58 59 public void convert(Formatter fmt, Map param, Map fileSource, Element element, Object bean) 60 throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 61 62 if (FileUploadCtrl.isDisabled(element)) 63 return; 64 65 String id = FileUploadCtrl.getId(element); 66 67 FileItem [] fileItems = (FileItem[]) fileSource.get(id); 68 69 if (fileItems != null && fileItems.length>0) { 71 FileItem fileItem = fileItems[0]; 72 try { 73 XoplonNS.removeAttribute(element, "error"); 74 75 FileUploadCtrl.setFileName(element, fileItem.getName()); 76 77 String model = FileUploadCtrl.getModelReference(element); 78 if (bean != null && model.length() > 0) { 79 PropertyUtils.setProperty(bean, model, fileItem); 80 } 81 } catch (IllegalAccessException e) { 82 logger.info("exception caught", e); 83 XoplonNS.setAttribute(element, "error", e.getMessage()); 84 XoplonNS.setAttribute(element, "value", fileItem.getName()); 85 throw e; 86 } catch (NoSuchMethodException e) { 87 logger.info("exception caught", e); 88 XoplonNS.setAttribute(element, "error", e.getMessage()); 89 XoplonNS.setAttribute(element, "value", fileItem.getName()); 90 throw e; 91 } catch (InvocationTargetException e) { 92 logger.info("exception caught", e); 93 XoplonNS.setAttribute(element, "error", e.getMessage()); 94 XoplonNS.setAttribute(element, "value", fileItem.getName()); 95 throw e; 96 } 97 } 98 } 99 100 public void convert(Formatter fmt, Object bean, Element element) throws ConvertException, 101 IllegalAccessException , NoSuchMethodException , InvocationTargetException { 102 try { 103 String model = FileUploadCtrl.getModelReference(element); 104 if (model.length() == 0) 105 return; 106 107 FileItem value = (FileItem) PropertyUtils.getProperty(bean, model); 108 if(value!=null) 109 FileUploadCtrl.setFileName(element, value.getName()); 110 else 111 FileUploadCtrl.setFileName(element, ""); 112 113 } catch (IllegalAccessException e) { 114 XoplonNS.setAttribute(element, "error", e.getMessage()); 115 throw e; 116 } catch (NoSuchMethodException e) { 117 XoplonNS.setAttribute(element, "error", e.getMessage()); 118 throw e; 119 } catch (InvocationTargetException e) { 120 XoplonNS.setAttribute(element, "error", e.getMessage()); 121 throw e; 122 } 123 } 124 125 } | Popular Tags |