📄 dataresourceworker.java
字号:
/**
* callDataResourcePermissionCheck Formats data for a call to the checkContentPermission service.
*/
public static String callDataResourcePermissionCheck(GenericDelegator delegator, LocalDispatcher dispatcher, Map context) {
String permissionStatus = "granted";
String skipPermissionCheck = (String) context.get("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.findByPrimaryKey("Content", UtilMisc.toMap("contentId", ownerContentId));
if (content != null)
serviceInMap.put("currentContent", content);
} catch (GenericEntityException e) {
Debug.logError(e, "e.getMessage()", "ContentServices");
}
}
try {
Map permResults = dispatcher.runSync("checkContentPermission", serviceInMap);
permissionStatus = (String) permResults.get("permissionStatus");
} catch (GenericServiceException e) {
Debug.logError(e, "Problem checking permissions", "ContentServices");
}
}
return permissionStatus;
}
/**
* Gets image data from ImageDataResource and returns it as a byte array.
*/
public static byte[] acquireImage(GenericDelegator delegator, String dataResourceId) throws GenericEntityException {
GenericValue imageDataResource = delegator.findByPrimaryKey("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
byte[] b = null;
if (imageDataResource != null) {
b = (byte[]) imageDataResource.get("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 = (String) dataResource.get("mimeTypeId");
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 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 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 (Debug.verboseOn()) Debug.logVerbose(" in renderDataResourceAsHtml, mimeTypeId:" + mimeTypeId, module);
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);
}
//if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), view:" + view, "");
//if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), dataResource:" + dataResource, "");
//if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), dataResourceMap:" + dataResourceMap, "");
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)context.get("subContentId");
if (Debug.verboseOn()) Debug.logVerbose(" in renderDataResourceAsHtml, subContentId:" + subContentId, module);
if (UtilValidate.isNotEmpty(subContentId)) {
context.put("contentId", subContentId);
context.put("subContentId", null);
}
//String subContentId2 = (String)context.get("subContentId");
// 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("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 void renderDataResourceAsTextCache(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 (Debug.verboseOn()) Debug.logVerbose(" in renderDataResourceAsHtml, mimeTypeId:" + mimeTypeId, module);
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);
}
String thisDataResourceId = null;
try {
thisDataResourceId = (String) view.get("drDataResourceId");
} catch (Exception e) {
thisDataResourceId = (String) view.get("dataResourceId");
}
if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), view:" + view, "");
if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), dataResource:" + dataResource, "");
if (Debug.verboseOn()) Debug.logVerbose("in renderDAtaResource(work), thisDataResourceId:" + thisDataResourceId, "");
if (UtilValidate.isEmpty(thisDataResourceId)) {
if (UtilValidate.isNotEmpty(dataResourceId))
view = null; // causes lookup of DataResource
else
throw new GeneralException("The dataResourceId [" + dataResourceId + "] is empty.");
}
}
if (dataResource == null || dataResource.isEmpty()) {
if (dataResourceId == null) {
throw new GeneralException("DataResourceId is null");
}
dataResource = delegator.findByPrimaryKeyCache("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)) {
writeDataResourceTextCache(dataResource, mimeTypeId, locale, templateContext, delegator, out);
} else {
String subContentId = (String)context.get("subContentId");
if (Debug.verboseOn()) Debug.logVerbose(" in renderDataResourceAsHtml, subContentId:" + subContentId, module);
if (UtilValidate.isNotEmpty(subContentId)) {
context.put("contentId", subContentId);
context.put("subContentId", null);
}
//String subContentId2 = (String)context.get("subContentId");
// get the full text of the DataResource
String templateText = getDataResourceTextCache(dataResource, mimeTypeId, locale, templateContext, delegator);
//String subContentId3 = (String)context.get("subContentId");
context.put("mimeTypeId", null);
templateContext.put("context", context);
if ("FTL".equals(dataTemplateTypeId)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -