📄 dataresourceworker.java
字号:
/* * $Id: DataResourceWorker.java 7125 2006-03-30 10:19:10Z byersa $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.content.data;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.StringWriter;import java.io.Writer;import java.net.URL;import java.util.ArrayList;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.TreeMap;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.xml.parsers.ParserConfigurationException;import org.apache.commons.fileupload.DiskFileUpload;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.collections.MapStack;import org.ofbiz.base.util.template.FreeMarkerWorker;import org.ofbiz.content.content.UploadContentAndImage;import org.ofbiz.content.email.NotificationServices;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.ByteWrapper;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.webapp.view.ViewHandlerException;import org.ofbiz.widget.html.HtmlScreenRenderer;import org.ofbiz.widget.screen.ModelScreen;import org.ofbiz.widget.screen.ScreenFactory;import org.ofbiz.widget.screen.ScreenRenderer;import org.ofbiz.widget.screen.ScreenStringRenderer;import org.xml.sax.SAXException;import freemarker.template.Template;import freemarker.template.TemplateException;//import com.clarkware.profiler.Profiler;/** * DataResourceWorker Class * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7125 $ * @since 3.0 */public class DataResourceWorker { public static final String module = DataResourceWorker.class.getName(); public static final String err_resource = "ContentErrorUiLabel"; /** * Traverses the DataCategory parent/child structure and put it in categoryNode. Returns non-null error string if there is an error. * @param depth The place on the categoryTypesIds to start collecting. * @param getAll Indicates that all descendants are to be gotten. Used as "true" to populate an * indented select list. */ public static String getDataCategoryMap(GenericDelegator delegator, int depth, Map categoryNode, List categoryTypeIds, boolean getAll) throws GenericEntityException { String errorMsg = null; String parentCategoryId = (String) categoryNode.get("id"); String currentDataCategoryId = null; int sz = categoryTypeIds.size(); // The categoryTypeIds has the most senior types at the end, so it is necessary to // work backwards. As "depth" is incremented, that is the effect. // The convention for the topmost type is "ROOT". if (depth >= 0 && (sz - depth) > 0) { currentDataCategoryId = (String) categoryTypeIds.get(sz - depth - 1); } // Find all the categoryTypes that are children of the categoryNode. String matchValue = null; if (parentCategoryId != null) { matchValue = parentCategoryId; } else { matchValue = null; } List categoryValues = delegator.findByAndCache("DataCategory", UtilMisc.toMap("parentCategoryId", matchValue)); categoryNode.put("count", new Integer(categoryValues.size())); List subCategoryIds = new ArrayList(); for (int i = 0; i < categoryValues.size(); i++) { GenericValue category = (GenericValue) categoryValues.get(i); String id = (String) category.get("dataCategoryId"); String categoryName = (String) category.get("categoryName"); Map newNode = new HashMap(); newNode.put("id", id); newNode.put("name", categoryName); errorMsg = getDataCategoryMap(delegator, depth + 1, newNode, categoryTypeIds, getAll); if (errorMsg != null) break; subCategoryIds.add(newNode); } // The first two parentCategoryId test just make sure that the first level of children // is gotten. This is a hack to make them available for display, but a more correct // approach should be formulated. // The "getAll" switch makes sure all descendants make it into the tree, if true. // The other test is to only get all the children if the "leaf" node where all the // children of the leaf are wanted for expansion. if (parentCategoryId == null || parentCategoryId.equals("ROOT") || (currentDataCategoryId != null && currentDataCategoryId.equals(parentCategoryId)) || getAll) { categoryNode.put("kids", subCategoryIds); } return errorMsg; } /** * Finds the parents of DataCategory entity and puts them in a list, the start entity at the top. */ public static void getDataCategoryAncestry(GenericDelegator delegator, String dataCategoryId, List categoryTypeIds) throws GenericEntityException { categoryTypeIds.add(dataCategoryId); GenericValue dataCategoryValue = delegator.findByPrimaryKey("DataCategory", UtilMisc.toMap("dataCategoryId", dataCategoryId)); if (dataCategoryValue == null) return; String parentCategoryId = (String) dataCategoryValue.get("parentCategoryId"); if (parentCategoryId != null) { getDataCategoryAncestry(delegator, parentCategoryId, categoryTypeIds); } return; } /** * Takes a DataCategory structure and builds a list of maps, one value (id) is the dataCategoryId value and the other is an indented string suitable for * use in a drop-down pick list. */ public static void buildList(HashMap nd, List lst, int depth) { String id = (String) nd.get("id"); String nm = (String) nd.get("name"); String spc = ""; for (int i = 0; i < depth; i++) spc += " "; HashMap map = new HashMap(); map.put("dataCategoryId", id); map.put("categoryName", spc + nm); if (id != null && !id.equals("ROOT") && !id.equals("")) { lst.add(map); } List kids = (List) nd.get("kids"); int sz = kids.size(); for (int i = 0; i < sz; i++) { HashMap kidNode = (HashMap) kids.get(i); buildList(kidNode, lst, depth + 1); } } /** * Uploads image data from a form and stores it in ImageDataResource. Expects key data in a field identitified by the "idField" value and the binary data * to be in a field id'd by uploadField. */ // TODO: This method is not used and should be removed. amb public static String uploadAndStoreImage(HttpServletRequest request, String idField, String uploadField) { //GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); //String idFieldValue = null; DiskFileUpload fu = new DiskFileUpload(); List lst = null; Locale locale = UtilHttp.getLocale(request); try { lst = fu.parseRequest(request); } catch (FileUploadException e) { request.setAttribute("_ERROR_MESSAGE_", e.toString()); return "error"; } if (lst.size() == 0) { String errMsg = UtilProperties.getMessage(DataResourceWorker.err_resource, "dataResourceWorker.no_files_uploaded", locale); request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module); return "error"; } // This code finds the idField and the upload FileItems FileItem fi = null; FileItem imageFi = null; String imageFileName = null; Map passedParams = new HashMap(); HttpSession session = request.getSession(); GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); passedParams.put("userLogin", userLogin); byte[] imageBytes = null; for (int i = 0; i < lst.size(); i++) { fi = (FileItem) lst.get(i); //String fn = fi.getName(); String fieldName = fi.getFieldName(); if (fi.isFormField()) { String fieldStr = fi.getString(); passedParams.put(fieldName, fieldStr); } else if (fieldName.startsWith("imageData")) { imageFi = fi; imageBytes = imageFi.get(); passedParams.put(fieldName, imageBytes); imageFileName = imageFi.getName(); passedParams.put("drObjectInfo", imageFileName); if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]imageData: " + imageBytes.length, module); } } if (imageBytes != null && imageBytes.length > 0) { String mimeType = getMimeTypeFromImageFileName(imageFileName); if (UtilValidate.isNotEmpty(mimeType)) { passedParams.put("drMimeTypeId", mimeType); try { String returnMsg = UploadContentAndImage.processContentUpload(passedParams, "", request); if (returnMsg.equals("error")) { return "error"; } } catch(GenericServiceException e) { request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); return "error"; } } else { request.setAttribute("_ERROR_MESSAGE_", "mimeType is empty."); return "error"; } } return "success"; } public static String getMimeTypeFromImageFileName(String imageFileName) { String mimeType = null; if (UtilValidate.isEmpty(imageFileName)) return mimeType; int pos = imageFileName.lastIndexOf("."); if (pos < 0) return mimeType; String suffix = imageFileName.substring(pos + 1); String suffixLC = suffix.toLowerCase(); if (suffixLC.equals("jpg")) mimeType = "image/jpeg"; else mimeType = "image/" + suffixLC; return mimeType; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -