📄 dataresourceworker.java
字号:
* callDataResourcePermissionCheck Formats data for a call to the checkContentPermission service. */ public static String callDataResourcePermissionCheck(GenericDelegator delegator, LocalDispatcher dispatcher, Map context) { Map permResults = callDataResourcePermissionCheckResult(delegator, dispatcher, context); String permissionStatus = (String) permResults.get("permissionStatus"); return permissionStatus; } /** * callDataResourcePermissionCheck Formats data for a call to the checkContentPermission service. */ public static Map callDataResourcePermissionCheckResult(GenericDelegator delegator, LocalDispatcher dispatcher, Map context) { Map permResults = new HashMap(); String skipPermissionCheck = (String) context.get("skipPermissionCheck"); if (Debug.infoOn()) Debug.logInfo("in callDataResourcePermissionCheckResult, skipPermissionCheck:" + skipPermissionCheck,""); if (skipPermissionCheck == null || skipPermissionCheck.length() == 0 || (!skipPermissionCheck.equalsIgnoreCase("true") && !skipPermissionCheck.equalsIgnoreCase("granted"))) { GenericValue userLogin = (GenericValue) context.get("userLogin"); Map serviceInMap = new HashMap(); serviceInMap.put("userLogin", userLogin); serviceInMap.put("targetOperationList", context.get("targetOperationList")); serviceInMap.put("contentPurposeList", context.get("contentPurposeList")); serviceInMap.put("entityOperation", context.get("entityOperation")); // It is possible that permission to work with DataResources will be controlled // by an external Content entity. String ownerContentId = (String) context.get("ownerContentId"); if (ownerContentId != null && ownerContentId.length() > 0) { try { GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", ownerContentId)); if (content != null) serviceInMap.put("currentContent", content); } catch (GenericEntityException e) { Debug.logError(e, "e.getMessage()", "ContentServices"); } } try { permResults = dispatcher.runSync("checkContentPermission", serviceInMap); } catch (GenericServiceException e) { Debug.logError(e, "Problem checking permissions", "ContentServices"); } } else { permResults.put("permissionStatus", "granted"); } return permResults; } /** * Gets image data from ImageDataResource and returns it as a byte array. */ public static byte[] acquireImage(GenericDelegator delegator, String dataResourceId) throws GenericEntityException { byte[] b = null; GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); if (dataResource == null) return b; b = acquireImage(delegator, dataResource); return b; } public static byte[] acquireImage(GenericDelegator delegator, GenericValue dataResource) throws GenericEntityException { byte[] b = null; String dataResourceTypeId = dataResource.getString("dataResourceTypeId"); String dataResourceId = dataResource.getString("dataResourceId"); GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); if (imageDataResource != null) { //b = (byte[]) imageDataResource.get("imageData"); b = imageDataResource.getBytes("imageData"); } return b; } /** * Returns the image type. */ public static String getImageType(GenericDelegator delegator, String dataResourceId) throws GenericEntityException { GenericValue dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); String imageType = getImageType(delegator, dataResource); return imageType; } public static String getMimeType(GenericValue dataResource) { String mimeTypeId = null; if (dataResource != null) { mimeTypeId = (String) dataResource.get("mimeTypeId"); if (UtilValidate.isEmpty(mimeTypeId)) { String fileName = (String) dataResource.get("objectInfo"); if (fileName != null && fileName.indexOf('.') > -1) { String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1); if (UtilValidate.isNotEmpty(fileExtension)) { GenericValue ext = null; try { ext = dataResource.getDelegator().findByPrimaryKey("FileExtension", UtilMisc.toMap("fileExtensionId", fileExtension)); } catch (GenericEntityException e) { Debug.logError(e, module); } if (ext != null) { mimeTypeId = ext.getString("mimeTypeId"); } } } // check one last time if (UtilValidate.isEmpty(mimeTypeId)) { // use a default mime type mimeTypeId = "application/octet-stream"; } } } return mimeTypeId; } /** @deprecated */ public static String getImageType(GenericDelegator delegator, GenericValue dataResource) { String imageType = null; if (dataResource != null) { imageType = (String) dataResource.get("mimeTypeId"); if (UtilValidate.isEmpty(imageType)) { String imageFileNameExt = null; String imageFileName = (String)dataResource.get("objectInfo"); if (UtilValidate.isNotEmpty(imageFileName)) { int pos = imageFileName.lastIndexOf("."); if (pos >= 0) imageFileNameExt = imageFileName.substring(pos + 1); } imageType = "image/" + imageFileNameExt; } } return imageType; } public static String renderDataResourceAsText(GenericDelegator delegator, String dataResourceId, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { Writer outWriter = new StringWriter(); renderDataResourceAsText(delegator, dataResourceId, outWriter, templateContext, view, locale, mimeTypeId); return outWriter.toString(); } public static void renderDataResourceAsText(GenericDelegator delegator, String dataResourceId, Writer out, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { if (templateContext == null) { templateContext = new HashMap(); }// Map context = (Map) templateContext.get("context");// if (context == null) {// context = new HashMap();// } if (UtilValidate.isEmpty(mimeTypeId)) { mimeTypeId = "text/html"; } // if the target mimeTypeId is not a text type, throw an exception if (!mimeTypeId.startsWith("text/")) { throw new GeneralException("The desired mime-type is not a text type, cannot render as text: " + mimeTypeId); } GenericValue dataResource = null; if (view != null) { String entityName = view.getEntityName(); dataResource = delegator.makeValue("DataResource", null); if ("DataResource".equals(entityName)) { dataResource.setAllFields(view, true, null, null); } else { dataResource.setAllFields(view, true, "dr", null); } dataResourceId = dataResource.getString("dataResourceId"); if (UtilValidate.isEmpty(dataResourceId)) { throw new GeneralException("The dataResourceId [" + dataResourceId + "] is empty."); } } if (dataResource == null || dataResource.isEmpty()) { if (dataResourceId == null) { throw new GeneralException("DataResourceId is null"); } dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); } if (dataResource == null || dataResource.isEmpty()) { throw new GeneralException("DataResource not found with id=" + dataResourceId); } String drMimeTypeId = dataResource.getString("mimeTypeId"); if (UtilValidate.isEmpty(drMimeTypeId)) { drMimeTypeId = "text/plain"; } String dataTemplateTypeId = dataResource.getString("dataTemplateTypeId"); // if this is a template, we need to get the full template text and interpret it, otherwise we should just write a bit at a time to the writer to better support large text if (UtilValidate.isEmpty(dataTemplateTypeId) || "NONE".equals(dataTemplateTypeId)) { writeDataResourceText(dataResource, mimeTypeId, locale, templateContext, delegator, out); } else { String subContentId = (String)templateContext.get("subContentId"); //String subContentId = (String)context.get("subContentId"); // TODO: the reason why I did this (and I can't remember) may not be valid or it can be done better if (UtilValidate.isNotEmpty(subContentId)) { //context.put("contentId", subContentId); //context.put("subContentId", null); templateContext.put("contentId", subContentId); templateContext.put("subContentId", null); } // get the full text of the DataResource String templateText = getDataResourceText(dataResource, mimeTypeId, locale, templateContext, delegator); //String subContentId3 = (String)context.get("subContentId");// context.put("mimeTypeId", null); templateContext.put("mimeTypeId", null);// templateContext.put("context", context); if ("FTL".equals(dataTemplateTypeId)) { try { FreeMarkerWorker.renderTemplate("DataResource:" + dataResourceId, templateText, templateContext, out); } catch (TemplateException e) { throw new GeneralException("Error rendering FTL template", e); } } else { throw new GeneralException("The dataTemplateTypeId [" + dataTemplateTypeId + "] is not yet supported"); } } } public static String renderDataResourceAsTextCache(GenericDelegator delegator, String dataResourceId, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { Writer outWriter = new StringWriter(); renderDataResourceAsTextCache(delegator, dataResourceId, outWriter, templateContext, view, locale, mimeTypeId); return outWriter.toString(); } public static void renderDataResourceAsTextCache(GenericDelegator delegator, String dataResourceId, Writer out, Map templateRoot, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { if (templateRoot == null) { templateRoot = new HashMap(); } //Map context = (Map) templateRoot.get("context"); //if (context == null) { //context = new HashMap(); //} String disableCache = UtilProperties.getPropertyValue("content", "disable.ftl.template.cache"); if (disableCache == null || !disableCache.equalsIgnoreCase("true")) { Template cachedTemplate = FreeMarkerWorker.getTemplateCached(dataResourceId); if (cachedTemplate != null) { try { String subContentId = (String)templateRoot.get("subContentId"); if (UtilValidate.isNotEmpty(subContentId)) { templateRoot.put("contentId", subContentId); templateRoot.put("subContentId", null); templateRoot.put("globalNodeTrail", null); // Force getCurrentContent to query for subContent } FreeMarkerWorker.renderTemplateCached(cachedTemplate, templateRoot, out); } catch (TemplateException e) { Debug.logError("Error rendering FTL template. " + e.getMessage(), module); throw new GeneralException("Error rendering FTL template", e); } return; } } if (UtilValidate.isEmpty(mimeTypeId)) { mimeTypeId = "text/html"; } // if the target mimeTypeId is not a text type, throw an exception if (!mimeTypeId.startsWith("text/")) { throw new GeneralException("The desired mime-type is not a text type, cannot render as text: " + mimeTypeId); } GenericValue dataResource = null; if (view != null) { String entityName = view.getEntityName(); dataResource = delegator.makeValue("DataResource", null); if ("DataResource".equals(entityName)) { dataResource.setAllFields(view, true, null, null); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -