001: package org.ofbiz.content.layout;
002:
003: import java.sql.Timestamp;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Locale;
010:
011: import org.ofbiz.base.util.Debug;
012: import org.ofbiz.base.util.UtilDateTime;
013: import org.ofbiz.base.util.UtilMisc;
014: import org.ofbiz.base.util.UtilProperties;
015: import org.ofbiz.entity.GenericDelegator;
016: import org.ofbiz.entity.GenericEntityException;
017: import org.ofbiz.entity.GenericValue;
018: import org.ofbiz.entity.condition.EntityCondition;
019: import org.ofbiz.entity.condition.EntityConditionList;
020: import org.ofbiz.entity.condition.EntityExpr;
021: import org.ofbiz.entity.condition.EntityOperator;
022: import org.ofbiz.entity.util.ByteWrapper;
023: import org.ofbiz.security.Security;
024: import org.ofbiz.service.DispatchContext;
025: import org.ofbiz.service.ServiceUtil;
026: import org.ofbiz.service.GenericServiceException;
027: import org.ofbiz.service.LocalDispatcher;
028: import org.ofbiz.service.ModelService;
029:
030: import org.apache.commons.fileupload.*;
031: import javax.servlet.http.HttpServletRequest;
032:
033: /**
034: * LayoutWorker Class
035: *
036: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
037: * @version $Revision: 1.2 $
038: * @since 3.0
039: *
040: *
041: */
042: public class LayoutWorker {
043:
044: public static final String module = LayoutWorker.class.getName();
045:
046: /**
047: * Uploads image data from a form and stores it in ImageDataResource.
048: * Expects key data in a field identitified by the "idField" value
049: * and the binary data to be in a field id'd by uploadField.
050: */
051: public static Map uploadImageAndParameters(
052: HttpServletRequest request, String uploadField) {
053:
054: //Debug.logVerbose("in uploadAndStoreImage", "");
055: GenericDelegator delegator = (GenericDelegator) request
056: .getAttribute("delegator");
057:
058: HashMap results = new HashMap();
059: HashMap formInput = new HashMap();
060: results.put("formInput", formInput);
061: DiskFileUpload fu = new DiskFileUpload();
062: java.util.List lst = null;
063: try {
064: lst = fu.parseRequest(request);
065: } catch (FileUploadException e4) {
066: return ServiceUtil.returnError(e4.getMessage());
067: }
068:
069: if (lst.size() == 0) {
070: request
071: .setAttribute("_ERROR_MESSAGE_",
072: "No files uploaded");
073: //Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module);
074: return ServiceUtil.returnError("No files uploaded.");
075: }
076:
077: // This code finds the idField and the upload FileItems
078: FileItem fi = null;
079: FileItem imageFi = null;
080: for (int i = 0; i < lst.size(); i++) {
081: fi = (FileItem) lst.get(i);
082: String fn = fi.getName();
083: String fieldName = fi.getFieldName();
084: String fieldStr = fi.getString();
085: if (fi.isFormField()) {
086: formInput.put(fieldName, fieldStr);
087: //Debug.logVerbose("in uploadAndStoreImage, fieldName:" + fieldName + " fieldStr:" + fieldStr, "");
088: }
089: if (fieldName.equals(uploadField))
090: imageFi = fi;
091: }
092:
093: if (imageFi == null) {
094: request.setAttribute("_ERROR_MESSAGE_", "imageFi("
095: + imageFi + ") is null");
096: //Debug.logWarning("[DataEvents.uploadImage] imageFi(" + imageFi + ") is null", module);
097: return null;
098: }
099:
100: byte[] imageBytes = imageFi.get();
101: ByteWrapper byteWrap = new ByteWrapper(imageBytes);
102: results.put("imageData", byteWrap);
103: results.put("imageFileName", imageFi.getName());
104:
105: //Debug.logVerbose("in uploadAndStoreImage, results:" + results, "");
106: return results;
107:
108: }
109:
110: public static ByteWrapper returnByteWrapper(Map map) {
111:
112: ByteWrapper byteWrap = (ByteWrapper) map.get("imageData");
113: return byteWrap;
114: }
115:
116: }
|