KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > layout > LayoutWorker


1 package org.ofbiz.content.layout;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Locale JavaDoc;
5 import java.util.Map JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7
8 import org.ofbiz.base.util.UtilHttp;
9 import org.ofbiz.base.util.UtilMisc;
10 import org.ofbiz.base.util.UtilProperties;
11 import org.ofbiz.entity.GenericDelegator;
12 import org.ofbiz.entity.util.ByteWrapper;
13 import org.ofbiz.service.ServiceUtil;
14
15 import org.apache.commons.fileupload.DiskFileUpload;
16 import org.apache.commons.fileupload.FileItem;
17 import org.apache.commons.fileupload.FileUploadException;
18
19 /**
20  * LayoutWorker Class
21  *
22  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
23  * @version $Rev: 5462 $
24  * @since 3.0
25  *
26  *
27  */

28 public class LayoutWorker {
29
30     public static final String JavaDoc module = LayoutWorker.class.getName();
31     public static final String JavaDoc err_resource = "ContentErrorUiLabel";
32
33     /**
34      * Uploads image data from a form and stores it in ImageDataResource.
35      * Expects key data in a field identitified by the "idField" value
36      * and the binary data to be in a field id'd by uploadField.
37      */

38     public static Map JavaDoc uploadImageAndParameters(HttpServletRequest JavaDoc request, String JavaDoc uploadField) {
39
40         //Debug.logVerbose("in uploadAndStoreImage", "");
41
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
42         Locale JavaDoc locale = UtilHttp.getLocale(request);
43         
44         HashMap JavaDoc results = new HashMap JavaDoc();
45         HashMap JavaDoc formInput = new HashMap JavaDoc();
46         results.put("formInput", formInput);
47         DiskFileUpload fu = new DiskFileUpload();
48         java.util.List JavaDoc lst = null;
49         try {
50            lst = fu.parseRequest(request);
51         } catch (FileUploadException e4) {
52             return ServiceUtil.returnError(e4.getMessage());
53         }
54
55         if (lst.size() == 0) {
56             String JavaDoc errMsg = UtilProperties.getMessage(LayoutWorker.err_resource, "layoutEvents.no_files_uploaded", locale);
57             request.setAttribute("_ERROR_MESSAGE_", errMsg);
58             //Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module);
59
return ServiceUtil.returnError("No files uploaded.");
60         }
61
62
63         // This code finds the idField and the upload FileItems
64
FileItem fi = null;
65         FileItem imageFi = null;
66         for (int i=0; i < lst.size(); i++) {
67             fi = (FileItem)lst.get(i);
68         String JavaDoc fn = fi.getName();
69         String JavaDoc fieldName = fi.getFieldName();
70         String JavaDoc fieldStr = fi.getString();
71             if (fi.isFormField()) {
72                 formInput.put(fieldName, fieldStr);
73             //Debug.logVerbose("in uploadAndStoreImage, fieldName:" + fieldName + " fieldStr:" + fieldStr, "");
74
}
75             if (fieldName.equals(uploadField)) imageFi = fi;
76         }
77
78         if (imageFi == null ) {
79             Map JavaDoc messageMap = UtilMisc.toMap("imageFi", imageFi);
80             String JavaDoc errMsg = UtilProperties.getMessage(LayoutWorker.err_resource, "layoutEvents.image_null", messageMap, locale);
81             request.setAttribute("_ERROR_MESSAGE_", errMsg);
82             //Debug.logWarning("[DataEvents.uploadImage] imageFi(" + imageFi + ") is null", module);
83
return null;
84         }
85
86         byte[] imageBytes = imageFi.get();
87         ByteWrapper byteWrap = new ByteWrapper(imageBytes);
88         results.put("imageData", byteWrap);
89         results.put("imageFileName", imageFi.getName());
90       
91         //Debug.logVerbose("in uploadAndStoreImage, results:" + results, "");
92
return results;
93
94     }
95
96
97     public static ByteWrapper returnByteWrapper(Map JavaDoc map) {
98
99         ByteWrapper byteWrap = (ByteWrapper)map.get("imageData");
100         return byteWrap;
101     }
102
103 }
104
Popular Tags