in applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java [144:427]
public static Map<String, Object> persistContentAndAssoc(DispatchContext dctx, Map<String, ? extends Object> rcontext)
throws GenericServiceException {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Map<String, Object> context = UtilMisc.makeMapWritable(rcontext);
Locale locale = (Locale) context.get("locale");
// Check if a webshell is not uploaded
String textData = (String) context.get("textData");
if (UtilValidate.isNotEmpty(textData)) {
try {
if (!SecuredUpload.isValidText(textData, Collections.emptyList())) {
Debug.logError("================== Not saved for security reason ==================", MODULE);
return ServiceUtil.returnError("================== Not saved for security reason ==================");
}
} catch (IOException e) {
Debug.logError("================== Not saved for security reason ==================", MODULE);
return ServiceUtil.returnError("================== Not saved for security reason ==================");
}
}
// Knowing why a request fails permission check is one of the more difficult
// aspects of content management. Setting "displayFailCond" to true will
// put an html table in result.errorMessage that will show what tests were performed
Boolean bDisplayFailCond = (Boolean) context.get("displayFailCond");
String mapKey = (String) context.get("mapKey");
// If "deactivateExisting" is set, other Contents that are tied to the same
// contentIdTo will be deactivated (thruDate set to now)
String deactivateString = (String) context.get("deactivateExisting");
boolean deactivateExisting = "true".equalsIgnoreCase(deactivateString);
if (Debug.infoOn()) {
Debug.logInfo("in persist... mapKey(0):" + mapKey, MODULE);
}
// ContentPurposes can get passed in as a delimited string or a list. Combine.
List<String> contentPurposeList = UtilGenerics.cast(context.get("contentPurposeList"));
if (contentPurposeList == null) {
contentPurposeList = new LinkedList<>();
}
String contentPurposeString = (String) context.get("contentPurposeString");
if (UtilValidate.isNotEmpty(contentPurposeString)) {
List<String> tmpPurposes = StringUtil.split(contentPurposeString, "|");
contentPurposeList.addAll(tmpPurposes);
}
context.put("contentPurposeList", contentPurposeList);
context.put("contentPurposeString", null);
if (Debug.infoOn()) {
Debug.logInfo("in persist... contentPurposeList(0):" + contentPurposeList, MODULE);
Debug.logInfo("in persist... textData(0):" + textData, MODULE);
}
GenericValue content = delegator.makeValue("Content");
content.setPKFields(context);
content.setNonPKFields(context);
String contentId = (String) content.get("contentId");
String contentTypeId = (String) content.get("contentTypeId");
String origContentId = (String) content.get("contentId");
String origDataResourceId = (String) content.get("dataResourceId");
if (Debug.infoOn()) {
Debug.logInfo("in persist... contentId(0):" + contentId, MODULE);
}
GenericValue dataResource = delegator.makeValue("DataResource");
dataResource.setPKFields(context);
dataResource.setNonPKFields(context);
dataResource.setAllFields(context, false, "dr", null);
String isPublic = (String) context.get("isPublic");
if (UtilValidate.isEmpty(isPublic)) {
dataResource.set("isPublic", "N");
}
context.putAll(dataResource);
String dataResourceId = (String) dataResource.get("dataResourceId");
String dataResourceTypeId = (String) dataResource.get("dataResourceTypeId");
if (Debug.infoOn()) {
Debug.logInfo("in persist... dataResourceId(0):" + dataResourceId, MODULE);
}
GenericValue contentAssoc = delegator.makeValue("ContentAssoc");
String contentAssocTypeId = (String) context.get("contentAssocTypeId");
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
context.put("caContentAssocTypeId", contentAssocTypeId);
}
contentAssocTypeId = (String) context.get("caContentAssocTypeId");
contentAssoc.setAllFields(context, false, "ca", null);
contentAssoc.put("contentId", context.get("caContentId"));
context.putAll(contentAssoc);
GenericValue electronicText = delegator.makeValue("ElectronicText");
electronicText.setPKFields(context);
electronicText.setNonPKFields(context);
// save expected primary keys on result now in case there is no operation that uses them
Map<String, Object> results = ServiceUtil.returnSuccess();
results.put("contentId", content.get("contentId"));
results.put("dataResourceId", dataResource.get("dataResourceId"));
results.put("drDataResourceId", dataResource.get("dataResourceId"));
results.put("drDataResourceId", dataResource.get("dataResourceId"));
results.put("caContentIdTo", contentAssoc.get("contentIdTo"));
results.put("caContentId", contentAssoc.get("contentId"));
results.put("caFromDate", contentAssoc.get("fromDate"));
results.put("caContentAssocTypeId", contentAssoc.get("contentAssocTypeId"));
// get user info for multiple use
GenericValue userLogin = (GenericValue) context.get("userLogin");
boolean dataResourceExists = true;
if (Debug.infoOn()) {
Debug.logInfo("in persist... dataResourceTypeId(0):" + dataResourceTypeId, MODULE);
}
if (UtilValidate.isNotEmpty(dataResourceTypeId)) {
Map<String, Object> dataResourceResult;
try {
dataResourceResult = persistDataResourceAndDataMethod(dctx, context);
} catch (GenericEntityException | GenericServiceException e) {
Debug.logError(e, e.toString(), MODULE);
return ServiceUtil.returnError(e.toString());
}
String errorMsg = ServiceUtil.getErrorMessage(dataResourceResult);
if (UtilValidate.isNotEmpty(errorMsg)) {
return ServiceUtil.returnError(errorMsg);
}
dataResourceId = (String) dataResourceResult.get("dataResourceId");
results.put("dataResourceId", dataResourceId);
results.put("drDataResourceId", dataResourceId);
context.put("dataResourceId", dataResourceId);
content.put("dataResourceId", dataResourceId);
context.put("drDataResourceId", dataResourceId);
}
// Do update and create permission checks on Content if warranted.
context.put("skipPermissionCheck", null); // Force check here
boolean contentExists = true;
if (Debug.infoOn()) {
Debug.logInfo("in persist... contentTypeId:" + contentTypeId + " dataResourceTypeId:" + dataResourceTypeId + " contentId:"
+ contentId + " dataResourceId:" + dataResourceId, MODULE);
}
if (UtilValidate.isNotEmpty(contentTypeId)) {
if (UtilValidate.isEmpty(contentId)) {
contentExists = false;
} else {
try {
GenericValue val = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
if (val == null) {
dataResourceExists = false;
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
}
}
context.putAll(content);
if (contentExists) {
Map<String, Object> contentContext = new HashMap<>();
ModelService contentModel = dispatcher.getDispatchContext().getModelService("updateContent");
contentContext.putAll(contentModel.makeValid(content, ModelService.IN_PARAM));
contentContext.put("userLogin", userLogin);
contentContext.put("displayFailCond", bDisplayFailCond);
contentContext.put("skipPermissionCheck", context.get("skipPermissionCheck"));
Debug.logInfo("In persistContentAndAssoc calling updateContent with content: " + contentContext, MODULE);
Map<String, Object> thisResult = dispatcher.runSync("updateContent", contentContext);
if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "ContentContentUpdatingError", UtilMisc.toMap("serviceName",
"persistContentAndAssoc"), locale), null, null, thisResult);
}
} else {
Map<String, Object> contentContext = new HashMap<>();
ModelService contentModel = dispatcher.getDispatchContext().getModelService("createContent");
contentContext.putAll(contentModel.makeValid(content, ModelService.IN_PARAM));
contentContext.put("userLogin", userLogin);
contentContext.put("displayFailCond", bDisplayFailCond);
contentContext.put("skipPermissionCheck", context.get("skipPermissionCheck"));
Debug.logInfo("In persistContentAndAssoc calling createContent with content: " + contentContext, MODULE);
Map<String, Object> thisResult = dispatcher.runSync("createContent", contentContext);
if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "ContentContentCreatingError", UtilMisc.toMap("serviceName",
"persistContentAndAssoc"), locale), null, null, thisResult);
}
contentId = (String) thisResult.get("contentId");
}
results.put("contentId", contentId);
context.put("contentId", contentId);
context.put("caContentIdTo", contentId);
// Add ContentPurposes if this is a create operation
if (contentId != null && !contentExists) {
try {
Set<String> contentPurposeSet = new LinkedHashSet<>(contentPurposeList);
for (String contentPurposeTypeId : contentPurposeSet) {
GenericValue contentPurpose = delegator.makeValue("ContentPurpose", UtilMisc.toMap("contentId", contentId,
"contentPurposeTypeId", contentPurposeTypeId));
contentPurpose.create();
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
}
}
} else if (UtilValidate.isNotEmpty(dataResourceTypeId) && UtilValidate.isNotEmpty(contentId)) {
// If dataResource was not previously existing, then update the associated content with its id
if (UtilValidate.isNotEmpty(dataResourceId) && !dataResourceExists) {
Map<String, Object> map = new HashMap<>();
map.put("userLogin", userLogin);
map.put("dataResourceId", dataResourceId);
map.put("contentId", contentId);
if (Debug.infoOn()) {
Debug.logInfo("in persist... context:" + context, MODULE);
}
Map<String, Object> r = dispatcher.runSync("updateContent", map);
boolean isError = ModelService.RESPOND_ERROR.equals(r.get(ModelService.RESPONSE_MESSAGE));
if (isError) {
return ServiceUtil.returnError((String) r.get(ModelService.ERROR_MESSAGE));
}
}
}
// Put contentId
if (UtilValidate.isNotEmpty(contentId)) {
contentAssoc.put("contentIdTo", contentId);
}
// If parentContentIdTo or parentContentIdFrom exists, create association with newly created content
if (Debug.infoOn()) {
Debug.logInfo("CREATING contentASSOC contentAssocTypeId:" + contentAssocTypeId, MODULE);
}
// create content assoc if the key values are present....
if (Debug.infoOn()) {
Debug.logInfo("contentAssoc: " + contentAssoc.toString(), MODULE);
}
if (UtilValidate.isNotEmpty(contentAssocTypeId) && contentAssoc.get("contentId") != null && contentAssoc.get("contentIdTo") != null) {
if (Debug.infoOn()) {
Debug.logInfo("in persistContentAndAssoc, deactivateExisting:" + deactivateExisting, MODULE);
}
Map<String, Object> contentAssocContext = new HashMap<>();
contentAssocContext.put("userLogin", userLogin);
contentAssocContext.put("displayFailCond", bDisplayFailCond);
contentAssocContext.put("skipPermissionCheck", context.get("skipPermissionCheck"));
Map<String, Object> thisResult = null;
try {
GenericValue contentAssocExisting = EntityQuery.use(delegator).from("ContentAssoc").where(contentAssoc.getPrimaryKey()).queryOne();
if (contentAssocExisting == null) {
ModelService contentAssocModel = dispatcher.getDispatchContext().getModelService("createContentAssoc");
Map<String, Object> ctx = contentAssocModel.makeValid(contentAssoc, ModelService.IN_PARAM);
contentAssocContext.putAll(ctx);
thisResult = dispatcher.runSync("createContentAssoc", contentAssocContext);
if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(thisResult));
}
results.put("caContentIdTo", thisResult.get("contentIdTo"));
results.put("caContentId", thisResult.get("contentIdFrom"));
results.put("caContentAssocTypeId", thisResult.get("contentAssocTypeId"));
results.put("caFromDate", thisResult.get("fromDate"));
results.put("caSequenceNum", thisResult.get("sequenceNum"));
} else {
if (deactivateExisting) {
contentAssocExisting.put("thruDate", UtilDateTime.nowTimestamp());
} else if (UtilValidate.isNotEmpty(context.get("thruDate"))) {
contentAssocExisting.put("thruDate", context.get("thruDate"));
}
ModelService contentAssocModel = dispatcher.getDispatchContext().getModelService("updateContentAssoc");
Map<String, Object> ctx = contentAssocModel.makeValid(contentAssocExisting, ModelService.IN_PARAM);
contentAssocContext.putAll(ctx);
thisResult = dispatcher.runSync("updateContentAssoc", contentAssocContext);
if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(thisResult));
}
}
} catch (GenericEntityException | GenericServiceException e) {
throw new GenericServiceException(e.toString());
}
String errMsg = ServiceUtil.getErrorMessage(thisResult);
if (UtilValidate.isNotEmpty(errMsg)) {
return ServiceUtil.returnError(errMsg);
}
}
context.remove("skipPermissionCheck");
context.put("contentId", origContentId);
context.put("dataResourceId", origDataResourceId);
context.remove("dataResource");
Debug.logInfo("results:" + results, MODULE);
return results;
}