📄 dataresourceworker.java
字号:
/*
* $Id: DataResourceWorker.java,v 1.17 2004/01/07 19:30:11 byersa Exp $
*
* Copyright (c) 2001, 2002 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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.content.email.NotificationServices;
import org.ofbiz.content.webapp.ftl.FreeMarkerWorker;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import freemarker.template.TemplateException;
/**
* DataResourceWorker Class
*
* @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.17 $
* @since 3.0
*/
public class DataResourceWorker {
public static final String module = DataResourceWorker.class.getName();
/**
* 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.
*/
public static String uploadAndStoreImage(HttpServletRequest request, String idField, String uploadField) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
if (Debug.verboseOn()) Debug.logVerbose("in uploadAndStoreImage, idField:" + idField, "");
String idFieldValue = null;
DiskFileUpload fu = new DiskFileUpload();
java.util.List lst = null;
try {
lst = fu.parseRequest(request);
} catch (FileUploadException e4) {
request.setAttribute("_ERROR_MESSAGE_", e4.getMessage());
return "error";
}
if (lst.size() == 0) {
request.setAttribute("_ERROR_MESSAGE_", "No files uploaded");
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;
for (int i = 0; i < lst.size(); i++) {
fi = (FileItem) lst.get(i);
//String fn = fi.getName();
String fieldName = fi.getFieldName();
String fieldStr = fi.getString();
if (Debug.verboseOn()) Debug.logVerbose("in uploadAndStoreImage, fieldName:" + fieldName, "");
if (fieldName.equals(idField)) {
idFieldValue = fieldStr;
}
if (fieldName.equals(uploadField)) {
imageFi = fi;
}
}
if (imageFi == null || idFieldValue == null) {
request.setAttribute("_ERROR_MESSAGE_", "imageFi(" + imageFi + " or idFieldValue(" + idFieldValue + " is null");
Debug.logWarning("[DataEvents.uploadImage] imageFi(" + imageFi + " or idFieldValue(" + idFieldValue + " is null", module);
return "error";
}
if (Debug.verboseOn()) Debug.logVerbose("in uploadAndStoreImage, idFieldValue:" + idFieldValue, "");
byte[] imageBytes = imageFi.get();
try {
GenericValue dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", idFieldValue));
// Use objectInfo field to store the name of the file, since there is no
// place in ImageDataResource for it.
if (dataResource != null) {
dataResource.set("objectInfo", imageFi.getName());
dataResource.store();
}
// See if this needs to be a create or an update procedure
GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource", UtilMisc.toMap("dataResourceId", idFieldValue));
if (imageDataResource == null) {
imageDataResource = delegator.makeValue("ImageDataResource", UtilMisc.toMap("dataResourceId", idFieldValue));
imageDataResource.set("imageData", imageBytes);
imageDataResource.create();
} else {
imageDataResource.set("imageData", imageBytes);
imageDataResource.store();
}
} catch (GenericEntityException e3) {
request.setAttribute("_ERROR_MESSAGE_", e3.getMessage());
return "error";
}
request.setAttribute("dataResourceId", idFieldValue);
request.setAttribute(idField, idFieldValue);
return "success";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -