📄 dataservices.java
字号:
/* * $Id: DataServices.java 6759 2006-02-17 03:23:00Z jonesde $ * * 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.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.StringWriter;import java.io.IOException;import java.io.RandomAccessFile;import java.io.Writer;import java.sql.Timestamp;import java.util.HashMap;import java.util.Locale;import java.util.Map;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;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.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * DataServices Class * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6759 $ * @since 3.0 * * */public class DataServices { public static final String module = DataServices.class.getName(); /** * A top-level service for creating a DataResource and ElectronicText together. */ public static Map createDataResourceAndText(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Map result = new HashMap(); Map thisResult = createDataResourceMethod(dctx, context); if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) { return ServiceUtil.returnError((String) thisResult.get(ModelService.ERROR_MESSAGE)); } result.put("dataResourceId", thisResult.get("dataResourceId")); context.put("dataResourceId", thisResult.get("dataResourceId")); String dataResourceTypeId = (String) context.get("dataResourceTypeId"); if (dataResourceTypeId != null && dataResourceTypeId.equals("ELECTRONIC_TEXT")) { thisResult = createElectronicText(dctx, context); if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) { return ServiceUtil.returnError((String) thisResult.get(ModelService.ERROR_MESSAGE)); } } return result; } /** * A service wrapper for the createDataResourceMethod method. Forces permissions to be checked. */ public static Map createDataResource(DispatchContext dctx, Map context) { Map result = createDataResourceMethod(dctx, context); return result; } public static Map createDataResourceMethod(DispatchContext dctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String userLoginId = (String) userLogin.get("userLoginId"); String createdByUserLogin = userLoginId; String lastModifiedByUserLogin = userLoginId; Timestamp createdDate = UtilDateTime.nowTimestamp(); Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); String dataTemplateTypeId = (String) context.get("dataTemplateTypeId"); if (UtilValidate.isEmpty(dataTemplateTypeId)) { dataTemplateTypeId = "NONE"; context.put("dataTemplateTypeId", dataTemplateTypeId ); } // If textData exists, then create DataResource and return dataResourceId String dataResourceId = (String) context.get("dataResourceId"); if (UtilValidate.isEmpty(dataResourceId)) dataResourceId = delegator.getNextSeqId("DataResource").toString(); if (Debug.infoOn()) Debug.logInfo("in createDataResourceMethod, dataResourceId:" + dataResourceId, module); GenericValue dataResource = delegator.makeValue("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); dataResource.setNonPKFields(context); dataResource.put("createdByUserLogin", createdByUserLogin); dataResource.put("lastModifiedByUserLogin", lastModifiedByUserLogin); dataResource.put("createdDate", createdDate); dataResource.put("lastModifiedDate", lastModifiedDate); try { dataResource.create(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } catch(Exception e2) { return ServiceUtil.returnError(e2.getMessage()); } result.put("dataResourceId", dataResourceId); result.put("dataResource", dataResource); return result; } /** * A service wrapper for the createElectronicTextMethod method. Forces permissions to be checked. */ public static Map createElectronicText(DispatchContext dctx, Map context) { Map result = createElectronicTextMethod(dctx, context); return result; } public static Map createElectronicTextMethod(DispatchContext dctx, Map context) { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String dataResourceId = (String) context.get("dataResourceId"); String textData = (String) context.get("textData"); if (textData != null && textData.length() > 0) { GenericValue electronicText = delegator.makeValue("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId, "textData", textData)); try { electronicText.create(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } } return result; } /** * A service wrapper for the createFileMethod method. Forces permissions to be checked. */ public static Map createFile(DispatchContext dctx, Map context) { return createFileMethod(dctx, context); } public static Map createFileNoPerm(DispatchContext dctx, Map context) { context.put("skipPermissionCheck", "true"); return createFileMethod(dctx, context); } public static Map createFileMethod(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); //GenericValue dataResource = (GenericValue) context.get("dataResource"); String dataResourceTypeId = (String) context.get("dataResourceTypeId"); String objectInfo = (String) context.get("objectInfo"); ByteWrapper binData = (ByteWrapper) context.get("binData"); String textData = (String) context.get("textData"); // a few place holders String prefix = ""; String sep = ""; // extended validation for binary/character data if (UtilValidate.isNotEmpty(textData) && binData != null) { return ServiceUtil.returnError("Cannot process both character and binary data in the same file"); } // obtain a reference to the file File file = null; if (UtilValidate.isEmpty(dataResourceTypeId) || dataResourceTypeId.equals("LOCAL_FILE")) { file = new File(objectInfo); if (!file.isAbsolute()) { return ServiceUtil.returnError("DataResource LOCAL_FILE does not point to an absolute location"); } } else if (dataResourceTypeId.equals("OFBIZ_FILE")) { prefix = System.getProperty("ofbiz.home"); if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } file = new File(prefix + sep + objectInfo); } else if (dataResourceTypeId.equals("CONTEXT_FILE")) { prefix = (String) context.get("rootDir"); if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } file = new File(prefix + sep + objectInfo); } if (file == null) { return ServiceUtil.returnError("Unable to obtain a reference to file - " + objectInfo); } // write the data to the file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -