You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2022/07/01 16:07:35 UTC
[ofbiz-framework] branch release22.01 updated: Improved: Refactoring service resenquence (OFBIZ-12624)
This is an automated email from the ASF dual-hosted git repository.
nmalin pushed a commit to branch release22.01
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/release22.01 by this push:
new 4c0773e531 Improved: Refactoring service resenquence (OFBIZ-12624)
4c0773e531 is described below
commit 4c0773e53172b8fa45d4a8abab7b19fd684650b6
Author: Nicolas Malin <ni...@nereide.fr>
AuthorDate: Fri Jul 1 17:53:38 2022 +0200
Improved: Refactoring service resenquence (OFBIZ-12624)
The service "resequence" compute for a content all sequenceNum for linked content assoc.
The code is very old (before apache) so apply a slim refactor and migrate the service name resequence to resequenceContentAssocChildren.
By the way I kept the service definition resequence to move it as deprecated.
---
applications/content/servicedef/services.xml | 19 ++--
.../ofbiz/content/ContentManagementServices.java | 103 ++++++++++-----------
2 files changed, 61 insertions(+), 61 deletions(-)
diff --git a/applications/content/servicedef/services.xml b/applications/content/servicedef/services.xml
index a812326b9a..f252a75903 100644
--- a/applications/content/servicedef/services.xml
+++ b/applications/content/servicedef/services.xml
@@ -559,20 +559,27 @@
<attribute name="fieldValue0" type="String" mode="IN" optional="true"/>
</service>
- <service name="resequence" auth="true" engine="java" validate="true" transaction-timeout="7200"
- location="org.apache.ofbiz.content.ContentManagementServices" invoke="resequence">
+ <service name="resequence" auth="true" engine="group">
+ <deprecated use-instead="resequenceContentAssocChildren" since="release22.01"/>
+ <group>
+ <invoke name="resequenceContentAssocChildren"/>
+ </group>
+ </service>
+ <service name="resequenceContentAssocChildren" auth="true" engine="java" transaction-timeout="7200"
+ location="org.apache.ofbiz.content.ContentManagementServices" invoke="resequence">
<description>Reorder sequence numbers in ContentAssoc entities for a given parent id</description>
- <attribute mode="IN" name="contentIdTo" optional="false" type="String">
+ <attribute mode="IN" name="contentId" optional="false" type="String">
<type-validate>
- <fail-property resource="ContentErrorUiLabels" property="ContentRequiredFieldMissingContentIdTo"/>
+ <fail-property resource="ContentErrorUiLabels" property="ContentRequiredFieldMissingContentId"/>
</type-validate>
</attribute>
- <attribute mode="IN" name="seqInc" optional="true" type="Integer"/>
+ <attribute mode="IN" name="seqInc" optional="true" type="Integer" default-value="10"/>
<attribute mode="IN" name="typeList" optional="true" type="List"/>
- <attribute mode="IN" name="contentId" optional="true" type="String"/>
+ <attribute mode="IN" name="contentIdTo" optional="true" type="String"/>
<attribute mode="IN" name="contentAssocTypeId" optional="true" type="String"/>
<attribute mode="IN" name="dir" optional="true" type="String"/>
</service>
+
<service name="changeLeafToNode" validate="true" auth="true" engine="java" transaction-timeout="7200"
location="org.apache.ofbiz.content.ContentManagementServices" invoke="changeLeafToNode">
<description>Moves dataResource to separate content associated with current content so that node can have only children, no content of its own</description>
diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java b/applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java
index b273d66c30..7fdaac8529 100644
--- a/applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java
+++ b/applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java
@@ -851,15 +851,23 @@ public class ContentManagementServices {
return results;
}
+ /**
+ * Reorder sequence numbers in ContentAssoc entities for a given parent id
+ * if variable dir contains the string up or down, realize for the given contentIdTo switch
+ * with the previous or the next
+ * @param dctx
+ * @param context
+ * @return
+ * @throws GenericServiceException
+ */
public static Map<String, Object> resequence(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
Map<String, Object> result = new HashMap<>();
Delegator delegator = dctx.getDelegator();
- String contentIdTo = (String) context.get("contentIdTo");
- Integer seqInc = (Integer) context.get("seqInc");
- if (seqInc == null) {
- seqInc = 100;
- }
- int seqIncrement = seqInc;
+ String contentId = (String) context.get("contentId");
+
+ int seqStep = (Integer) context.get("seqInc");
+
+ // Resolve the association type to use for resolve content's child
List<String> typeList = UtilGenerics.cast(context.get("typeList"));
if (typeList == null) {
typeList = new LinkedList<>();
@@ -871,60 +879,45 @@ public class ContentManagementServices {
if (UtilValidate.isEmpty(typeList)) {
typeList = UtilMisc.toList("PUBLISH_LINK", "SUB_CONTENT");
}
- EntityCondition conditionType = EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.IN, typeList);
- EntityCondition conditionMain = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("contentIdTo",
- EntityOperator.EQUALS, contentIdTo), conditionType), EntityOperator.AND);
+
+ // Resolve all content assoc to resequence from the content
try {
- List<GenericValue> listAll = EntityQuery.use(delegator).from("ContentAssoc")
- .where(conditionMain)
+ List<GenericValue> contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
+ .where(List.of(
+ EntityCondition.makeCondition("contentId", contentId),
+ EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.IN, typeList)))
.orderBy("sequenceNum", "fromDate", "createdDate")
- .filterByDate().queryList();
- String contentId = (String) context.get("contentId");
+ .filterByDate()
+ .queryList();
+ String contentIdTo = (String) context.get("contentIdTo");
String dir = (String) context.get("dir");
- int seqNum = seqIncrement;
- String thisContentId = null;
- for (int i = 0; i < listAll.size(); i++) {
- GenericValue contentAssoc = listAll.get(i);
- if (UtilValidate.isNotEmpty(contentId) && UtilValidate.isNotEmpty(dir)) {
- // move targeted entry up or down
- thisContentId = contentAssoc.getString("contentId");
- if (contentId.equals(thisContentId)) {
- if (dir.startsWith("up")) {
- if (i > 0) {
- // Swap with previous entry
- try {
- GenericValue prevValue = listAll.get(i - 1);
- Long prevSeqNum = (Long) prevValue.get("sequenceNum");
- prevValue.put("sequenceNum", (long) seqNum);
- prevValue.store();
- contentAssoc.put("sequenceNum", prevSeqNum);
- contentAssoc.store();
- } catch (GenericEntityException e) {
- return ServiceUtil.returnError(e.toString());
- }
- }
- } else {
- if (i < listAll.size()) {
- // Swap with next entry
- GenericValue nextValue = listAll.get(i + 1);
- nextValue.put("sequenceNum", (long) seqNum);
- nextValue.store();
- seqNum += seqIncrement;
- contentAssoc.put("sequenceNum", (long) seqNum);
- contentAssoc.store();
- i++; // skip next one
- }
- }
- } else {
- contentAssoc.put("sequenceNum", (long) seqNum);
- contentAssoc.store();
- }
- } else {
- contentAssoc.put("sequenceNum", (long) seqNum);
- contentAssoc.store();
+ int seqNum = seqStep;
+ boolean switchSequence = UtilValidate.isNotEmpty(contentIdTo) && UtilValidate.isNotEmpty(dir);
+ boolean switchModeUp = switchSequence && dir.startsWith("up");
+ int changePosition = -1;
+
+ // update the sequence for all element and check if we need switch two element
+ for (int i = 0; i < contentAssocs.size(); i++) {
+ boolean stopLimit = ((i <= 0 && switchModeUp) || (i + 1 >= contentAssocs.size() && !switchModeUp));
+ GenericValue contentAssoc = contentAssocs.get(i);
+ contentAssoc.put("sequenceNum", (long) seqNum);
+ seqNum += seqStep;
+ if (switchSequence
+ && contentIdTo.equals(contentAssoc.getString("contentIdTo"))
+ && !stopLimit) {
+ changePosition = i;
}
- seqNum += seqIncrement;
}
+
+ // We need to switch, do it
+ if (changePosition >= 0) {
+ GenericValue currentContent = contentAssocs.get(changePosition);
+ GenericValue destinationContent = contentAssocs.get(changePosition + (switchModeUp ? -1 : +1));
+ long switchSeqNum = currentContent.getLong("sequenceNum");
+ currentContent.put("sequenceNum", destinationContent.getLong("sequenceNum"));
+ destinationContent.put("sequenceNum", switchSeqNum);
+ }
+ delegator.storeAll(contentAssocs);
} catch (GenericEntityException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.toString());