📄 dataservices.java
字号:
} } else { return ServiceUtil.returnError("No file content passed for: " + file.getAbsolutePath()); } } catch (IOException e) { Debug.logWarning(e, module); throw new GenericServiceException(e.getMessage()); } return result; } public static Map renderDataResourceAsText(DispatchContext dctx, Map context) throws GeneralException, IOException { Map results = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); //LocalDispatcher dispatcher = dctx.getDispatcher(); Writer out = (Writer) context.get("outWriter"); Map templateContext = (Map) context.get("templateContext"); //GenericValue userLogin = (GenericValue) context.get("userLogin"); String dataResourceId = (String) context.get("dataResourceId"); if (templateContext != null && UtilValidate.isEmpty(dataResourceId)) { dataResourceId = (String) templateContext.get("dataResourceId"); } String mimeTypeId = (String) context.get("mimeTypeId"); if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) { mimeTypeId = (String) templateContext.get("mimeTypeId"); } Locale locale = (Locale) context.get("locale"); if (templateContext == null) { templateContext = new HashMap(); } GenericValue view = (GenericValue) context.get("subContentDataResourceView"); Writer outWriter = new StringWriter(); DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, outWriter, templateContext, view, locale, mimeTypeId); try { out.write(outWriter.toString()); results.put("textData", outWriter.toString()); } catch (IOException e) { Debug.logError(e, "Error rendering sub-content text", module); return ServiceUtil.returnError(e.toString()); } return results; } /** * A service wrapper for the updateImageMethod method. Forces permissions to be checked. */ public static Map updateImage(DispatchContext dctx, Map context) { Map result = updateImageMethod(dctx, context); return result; } public static Map updateImageMethod(DispatchContext dctx, Map context) { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue image = null; //Locale locale = (Locale) context.get("locale"); String dataResourceId = (String) context.get("dataResourceId"); ByteWrapper byteWrapper = (ByteWrapper)context.get("imageData"); if (byteWrapper != null) { byte[] imageBytes = byteWrapper.getBytes(); try { GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); if (Debug.infoOn()) Debug.logInfo("imageDataResource(U):" + imageDataResource, module); if (Debug.infoOn()) Debug.logInfo("imageBytes(U):" + imageBytes, module); if (imageDataResource == null) { return createImageMethod(dctx, context); } else { imageDataResource.setBytes("imageData", imageBytes); imageDataResource.store(); } } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } } return result; } /** * A service wrapper for the createImageMethod method. Forces permissions to be checked. */ public static Map createImage(DispatchContext dctx, Map context) { Map result = createImageMethod(dctx, context); return result; } public static Map createImageMethod(DispatchContext dctx, Map context) { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String dataResourceId = (String) context.get("dataResourceId"); ByteWrapper byteWrapper = (ByteWrapper)context.get("imageData"); if (byteWrapper != null) { byte[] imageBytes = byteWrapper.getBytes(); try { GenericValue imageDataResource = delegator.makeValue("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); //imageDataResource.set("imageData", imageBytes); imageDataResource.setBytes("imageData", imageBytes); if (Debug.infoOn()) Debug.logInfo("imageDataResource(C):" + imageDataResource, module); imageDataResource.create(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } } return result; } /** * A service wrapper for the createBinaryFileMethod method. Forces permissions to be checked. */ public static Map createBinaryFile(DispatchContext dctx, Map context) { Map result = null; try { result = createBinaryFileMethod(dctx, context); } catch (GenericServiceException e) { return ServiceUtil.returnError(e.getMessage()); } return result; } public static Map createBinaryFileMethod(DispatchContext dctx, Map context) throws GenericServiceException { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue dataResource = (GenericValue) context.get("dataResource"); //String dataResourceId = (String) dataResource.get("dataResourceId"); String dataResourceTypeId = (String) dataResource.get("dataResourceTypeId"); String objectInfo = (String) dataResource.get("objectInfo"); byte [] imageData = (byte []) context.get("imageData"); String rootDir = (String)context.get("rootDir"); String prefix = ""; File file = null; if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, dataResourceTypeId:" + dataResourceTypeId, module); if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, objectInfo:" + objectInfo, module); if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, rootDir:" + rootDir, module); try { file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, rootDir); } catch (FileNotFoundException e) { Debug.logWarning(e, module); throw new GenericServiceException(e.getMessage()); } catch (GeneralException e2) { Debug.logWarning(e2, module); throw new GenericServiceException(e2.getMessage()); } if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, file:" + file, module); if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, imageData:" + imageData.length, module); if (imageData != null && imageData.length > 0) { try { FileOutputStream out = new FileOutputStream(file); out.write(imageData); if (Debug.infoOn()) Debug.logInfo("in createBinaryFileMethod, length:" + file.length(), module); out.close(); } catch (IOException e) { Debug.logWarning(e, module); throw new GenericServiceException(e.getMessage()); } } return result; } /** * A service wrapper for the createBinaryFileMethod method. Forces permissions to be checked. */ public static Map updateBinaryFile(DispatchContext dctx, Map context) { Map result = null; try { result = updateBinaryFileMethod(dctx, context); } catch (GenericServiceException e) { return ServiceUtil.returnError(e.getMessage()); } return result; } public static Map updateBinaryFileMethod(DispatchContext dctx, Map context) throws GenericServiceException { HashMap result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue dataResource = (GenericValue) context.get("dataResource"); //String dataResourceId = (String) dataResource.get("dataResourceId"); String dataResourceTypeId = (String) dataResource.get("dataResourceTypeId"); String objectInfo = (String) dataResource.get("objectInfo"); byte [] imageData = (byte []) context.get("imageData"); String rootDir = (String)context.get("rootDir"); String prefix = ""; File file = null; if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, dataResourceTypeId:" + dataResourceTypeId, module); if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, objectInfo:" + objectInfo, module); if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, rootDir:" + rootDir, module); try { file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, rootDir); } catch (FileNotFoundException e) { Debug.logWarning(e, module); throw new GenericServiceException(e.getMessage()); } catch (GeneralException e2) { Debug.logWarning(e2, module); throw new GenericServiceException(e2.getMessage()); } if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, file:" + file, module); if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, imageData:" + imageData, module); if (imageData != null && imageData.length > 0) { try { FileOutputStream out = new FileOutputStream(file); out.write(imageData); if (Debug.infoOn()) Debug.logInfo("in updateBinaryFileMethod, length:" + file.length(), module); out.close(); } catch (IOException e) { Debug.logWarning(e, module); throw new GenericServiceException(e.getMessage()); } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -