📄 dataservices.java
字号:
if (UtilValidate.isNotEmpty(textData)) { try { FileWriter out = new FileWriter(file); out.write(textData); out.close(); } catch (IOException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("Unable to write character data to: " + file.getAbsolutePath()); } } else if (binData != null) { try { RandomAccessFile out = new RandomAccessFile(file, "rw"); out.write(binData.getBytes()); out.close(); } catch (FileNotFoundException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to open file for writing: " + file.getAbsolutePath()); } catch (IOException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to write binary data to: " + file.getAbsolutePath()); } } else { return ServiceUtil.returnError("No file content passed for: " + file.getAbsolutePath()); } Map result = ServiceUtil.returnSuccess(); return result; } /** * A top-level service for updating a DataResource and ElectronicText together. */ public static Map updateDataResourceAndText(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Map thisResult = updateDataResourceMethod(dctx, context); if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) { return ServiceUtil.returnError((String) thisResult.get(ModelService.ERROR_MESSAGE)); } String dataResourceTypeId = (String) context.get("dataResourceTypeId"); if (dataResourceTypeId != null && dataResourceTypeId.equals("ELECTRONIC_TEXT")) { thisResult = updateElectronicText(dctx, context); if (thisResult.get(ModelService.RESPONSE_MESSAGE) != null) { return ServiceUtil.returnError((String) thisResult.get(ModelService.ERROR_MESSAGE)); } } return ServiceUtil.returnSuccess(); } /** * A service wrapper for the updateDataResourceMethod method. Forces permissions to be checked. */ public static Map updateDataResource(DispatchContext dctx, Map context) { //context.put("skipPermissionCheck", null); Map result = updateDataResourceMethod(dctx, context); return result; } public static Map updateDataResourceMethod(DispatchContext dctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue dataResource = null; //Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); String userLoginId = (String) userLogin.get("userLoginId"); String lastModifiedByUserLogin = userLoginId; Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); // If textData exists, then create DataResource and return dataResourceId String dataResourceId = (String) context.get("dataResourceId"); try { dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); } catch (GenericEntityException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("dataResource.update.read_failure" + e.getMessage()); } dataResource.setNonPKFields(context); dataResource.put("lastModifiedByUserLogin", lastModifiedByUserLogin); dataResource.put("lastModifiedDate", lastModifiedDate); try { dataResource.store(); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } result.put("dataResource", dataResource); return result; } /** * A service wrapper for the updateElectronicTextMethod method. Forces permissions to be checked. */ public static Map updateElectronicText(DispatchContext dctx, Map context) { Map result = updateElectronicTextMethod(dctx, context); return result; } /** * Because sometimes a DataResource will exist, but no ElectronicText has been created, * this method will create an ElectronicText if it does not exist. * @param dctx * @param context * @return */ public static Map updateElectronicTextMethod(DispatchContext dctx, Map context) { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue electronicText = null; //Locale locale = (Locale) context.get("locale"); String dataResourceId = (String) context.get("dataResourceId"); result.put("dataResourceId",dataResourceId); String contentId = (String) context.get("contentId"); result.put("contentId",contentId); if (UtilValidate.isEmpty(dataResourceId)) { String errMsg = "dataResourceId is null."; Debug.logError(errMsg, module); return ServiceUtil.returnError(errMsg); } String textData = (String) context.get("textData"); if (Debug.infoOn()) Debug.logInfo("in updateElectronicText, textData:" + textData, module); try { electronicText = delegator.findByPrimaryKey("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId)); if (electronicText != null) { electronicText.put("textData", textData); electronicText.store(); } else { electronicText = delegator.makeValue("ElectronicText", null); electronicText.put("dataResourceId", dataResourceId); electronicText.put("textData", textData); electronicText.create(); } } catch (GenericEntityException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("electronicText.update.read_failure" + e.getMessage()); } return result; } /** * A service wrapper for the updateFileMethod method. Forces permissions to be checked. */ public static Map updateFile(DispatchContext dctx, Map context) { Map result = null; try { result = updateFileMethod(dctx, context); } catch (GenericServiceException e) { return ServiceUtil.returnError(e.getMessage()); } return result; } public static Map updateFileMethod(DispatchContext dctx, Map context) throws GenericServiceException { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); //GenericValue fileText = null; //Locale locale = (Locale) context.get("locale"); //String dataResourceId = (String) dataResource.get("dataResourceId"); String dataResourceTypeId = (String) context.get("dataResourceTypeId"); String objectInfo = (String) context.get("objectInfo"); String textData = (String) context.get("textData"); ByteWrapper binData = (ByteWrapper) context.get("binData"); String prefix = ""; File file = null; String fileName = ""; String sep = ""; try { if (UtilValidate.isEmpty(dataResourceTypeId) || dataResourceTypeId.equals("LOCAL_FILE")) { fileName = prefix + sep + objectInfo; file = new File(fileName); if (file == null) { throw new GenericServiceException("File: " + fileName + " is null."); } if (!file.isAbsolute()) { throw new GenericServiceException("File: " + fileName + " is not absolute."); } } 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) { throw new IOException("File: " + file + " is null"); } // write the data to the file if (UtilValidate.isNotEmpty(textData)) { try { FileWriter out = new FileWriter(file); out.write(textData); out.close(); } catch (IOException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("Unable to write character data to: " + file.getAbsolutePath()); } } else if (binData != null) { try { RandomAccessFile out = new RandomAccessFile(file, "rw"); out.write(binData.getBytes()); out.close(); } catch (FileNotFoundException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to open file for writing: " + file.getAbsolutePath()); } catch (IOException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to write binary data to: " + file.getAbsolutePath());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -