001: package org.ofbiz.content.data;
002:
003: import java.io.IOException;
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009: import javax.servlet.http.HttpSession;
010:
011: import org.ofbiz.base.util.Debug;
012: import org.ofbiz.base.util.UtilHttp;
013: import org.ofbiz.entity.GenericDelegator;
014: import org.ofbiz.entity.GenericPK;
015: import org.ofbiz.entity.GenericValue;
016: import org.ofbiz.service.GenericServiceException;
017: import org.ofbiz.entity.GenericEntityException;
018: import org.ofbiz.service.LocalDispatcher;
019:
020: /**
021: * DataEvents Class
022: *
023: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
024: * @version $Revision: 1.7 $
025: * @since 3.0
026: *
027: *
028: */
029: public class DataEvents {
030:
031: public static final String module = DataEvents.class.getName();
032:
033: public static String uploadImage(HttpServletRequest request,
034: HttpServletResponse response) {
035:
036: return DataResourceWorker.uploadAndStoreImage(request,
037: "dataResourceId", "imageData");
038:
039: }
040:
041: /** Streams ImageDataResource data to the output. */
042: public static String serveImage(HttpServletRequest request,
043: HttpServletResponse response) {
044:
045: GenericDelegator delegator = (GenericDelegator) request
046: .getAttribute("delegator");
047: String dataResourceId = request.getParameter("imgId");
048: byte[] b = null;
049: String imageType = null;
050: try {
051: b = DataResourceWorker.acquireImage(delegator,
052: dataResourceId);
053: imageType = DataResourceWorker.getImageType(delegator,
054: dataResourceId);
055: } catch (GenericEntityException e) {
056: String errorMsg = "Error getting image record from db: "
057: + e.toString();
058: Debug.logError(e, errorMsg, module);
059: request.setAttribute("_ERROR_MESSAGE_", errorMsg);
060: return "error";
061: }
062:
063: if (imageType == null || b == null) {
064: String errorMsg = "image(" + b + ") or type(" + imageType
065: + ") is null.";
066: Debug.logVerbose(errorMsg, module);
067: request.setAttribute("_ERROR_MESSAGE_", errorMsg);
068: return "error";
069: } else {
070: try {
071: UtilHttp.streamContentToBrowser(response, b, imageType);
072: } catch (IOException e) {
073: String errorMsg = "Error writing image to OutputStream: "
074: + e.toString();
075: Debug.logError(e, errorMsg, module);
076: request.setAttribute("_ERROR_MESSAGE_", errorMsg);
077: return "error";
078: }
079: }
080:
081: return "success";
082: }
083:
084: /** Dual create and edit event.
085: * Needed to make permission criteria available to services.
086: */
087: public static String persistDataResource(
088: HttpServletRequest request, HttpServletResponse response) {
089:
090: Map result = null;
091: LocalDispatcher dispatcher = (LocalDispatcher) request
092: .getAttribute("dispatcher");
093: GenericDelegator delegator = (GenericDelegator) request
094: .getAttribute("delegator");
095: GenericValue userLogin = (GenericValue) request.getSession()
096: .getAttribute("userLogin");
097: Map paramMap = UtilHttp.getParameterMap(request);
098: String dataResourceId = (String) paramMap.get("dataResourceId");
099: GenericValue dataResource = delegator.makeValue("DataResource",
100: null);
101: dataResource.setPKFields(paramMap);
102: dataResource.setNonPKFields(paramMap);
103: Map serviceInMap = new HashMap(dataResource);
104: serviceInMap.put("userLogin", userLogin);
105: String mode = (String) paramMap.get("mode");
106:
107: if (mode != null && mode.equals("UPDATE")) {
108: try {
109: result = dispatcher.runSync("updateDataResource",
110: serviceInMap);
111: } catch (GenericServiceException e) {
112: String errMsg = "Error calling the updateDataResource service."
113: + e.toString();
114: Debug.logError(e, errMsg, module);
115: request.setAttribute("_ERROR_MESSAGE_", errMsg);
116: return "error";
117: }
118: } else {
119: mode = "CREATE";
120: try {
121: result = dispatcher.runSync("createDataResource",
122: serviceInMap);
123: } catch (GenericServiceException e) {
124: String errMsg = "Error calling the createDataResource service."
125: + e.toString();
126: Debug.logError(e, errMsg, module);
127: request.setAttribute("_ERROR_MESSAGE_", errMsg);
128: return "error";
129: }
130: dataResourceId = (String) result.get("dataResourceId");
131: dataResource.set("dataResourceId", dataResourceId);
132: }
133:
134: // Save the primary key so that it can be used in a "quick pick" list later
135: GenericPK pk = dataResource.getPrimaryKey();
136: HttpSession session = request.getSession();
137: //ContentManagementWorker.mruAdd(session, pk);
138:
139: String returnStr = "success";
140: if (mode.equals("CREATE")) {
141: // Set up return message to guide selection of follow on view
142: request.setAttribute("dataResourceId", result
143: .get("dataResourceId"));
144: String dataResourceTypeId = (String) serviceInMap
145: .get("dataResourceTypeId");
146: if (dataResourceTypeId != null) {
147: if (dataResourceTypeId.equals("ELECTRONIC_TEXT")
148: || dataResourceTypeId.equals("IMAGE_OBJECT")) {
149: returnStr = dataResourceTypeId;
150: }
151: }
152: }
153:
154: return returnStr;
155: }
156:
157: }
|