Skip to content

Commit 60e4f25

Browse files
authored
Replace generic updateOrRemove service with ProductFeatureDataResource-specific service (#1227)
Replaced the ambiguous updateOrRemove service with a dedicated createOrRemoveProductFeatureDataResource service for managing CMS product feature data resource links. - Added explicit productFeatureId and dataResourceId service parameters - Removed generic entityName, pkFieldCount, fieldName, and fieldValue handling - Simplified ProductFeatureDataResource create/remove logic - Updated CMSContentEdit.ftl to submit defined service parameters - Updated updateFeatures controller event to invoke the new service
1 parent 9d87a3f commit 60e4f25

4 files changed

Lines changed: 32 additions & 73 deletions

File tree

applications/content/servicedef/services.xml

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -500,28 +500,12 @@
500500
<attribute mode="IN" name="contentId" optional="true" type="String"/>
501501
</service>
502502

503-
<service name="updateOrRemove" engine="java"
504-
location="org.apache.ofbiz.content.ContentManagementServices" invoke="updateOrRemove" auth="true" validate="false">
505-
<description>Update or remove a child entity based on value of "action"</description>
506-
<attribute name="entityName" type="String" mode="IN" optional="false">
507-
<type-validate>
508-
<fail-property resource="ContentErrorUiLabels" property="ContentRequiredFieldMissingEntityName"/>
509-
</type-validate>
510-
</attribute>
503+
<service name="createOrRemoveProductFeatureDataResource" engine="java"
504+
location="org.apache.ofbiz.content.ContentManagementServices" invoke="createOrRemoveProductFeatureDataResource" auth="true" validate="false">
505+
<description>Create or remove ProductFeatureDataResource based on value of "action"</description>
511506
<attribute name="action" type="String" mode="IN" optional="true"/>
512-
<attribute name="pkFieldCount" type="String" mode="IN" optional="false">
513-
<type-validate>
514-
<fail-property resource="ContentErrorUiLabels" property="ContentRequiredFieldMissingPkFieldCount"/>
515-
</type-validate>
516-
</attribute>
517-
<attribute name="fieldName0" type="String" mode="IN" optional="true"/>
518-
<attribute name="fieldValue1" type="String" mode="IN" optional="true"/>
519-
<attribute name="fieldName2" type="String" mode="IN" optional="true"/>
520-
<attribute name="fieldValue2" type="String" mode="IN" optional="true"/>
521-
<attribute name="fieldName3" type="String" mode="IN" optional="true"/>
522-
<attribute name="fieldValue3" type="String" mode="IN" optional="true"/>
523-
<attribute name="fieldName1" type="String" mode="IN" optional="true"/>
524-
<attribute name="fieldValue0" type="String" mode="IN" optional="true"/>
507+
<attribute name="productFeatureId" type="String" mode="IN"/>
508+
<attribute name="dataResourceId" type="String" mode="IN"/>
525509
</service>
526510

527511
<service name="resequence" auth="true" engine="group">

applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -797,58 +797,41 @@ public static Map<String, Object> updateSiteRolesDyn(DispatchContext dctx, Map<S
797797
return results;
798798
}
799799

800-
public static Map<String, Object> updateOrRemove(DispatchContext dctx, Map<String, ? extends Object> context) {
801-
Map<String, Object> results = new HashMap<>();
800+
/**
801+
* Creates or removes a link between a product feature and a data resource based on the provided action.
802+
*
803+
* @param dctx The DispatchContext instance providing access to the delegator and other utilities.
804+
* @param context A map containing the following keys:
805+
* - productFeatureId (String): The ID of the product feature.
806+
* - dataResourceId (String): The ID of the data resource.
807+
* - action (String): Indicates whether to create or remove the link. Use "Y" for create, otherwise the link is removed.
808+
* @return A map containing the results of the operation. In case of an error, returns a map with an error message.
809+
*/
810+
public static Map<String, Object> createOrRemoveProductFeatureDataResource(DispatchContext dctx, Map<String, ? extends Object> context) {
802811
Delegator delegator = dctx.getDelegator();
803-
String entityName = (String) context.get("entityName");
812+
String productFeatureId = (String) context.get("productFeatureId");
813+
String dataResourceId = (String) context.get("dataResourceId");
804814
String action = (String) context.get("action");
805-
String pkFieldCount = (String) context.get("pkFieldCount");
806-
Map<String, String> pkFields = new HashMap<>();
807-
int fieldCount = Integer.parseInt(pkFieldCount);
808-
for (int i = 0; i < fieldCount; i++) {
809-
String fieldName = (String) context.get("fieldName" + i);
810-
String fieldValue = (String) context.get("fieldValue" + i);
811-
if (UtilValidate.isEmpty(fieldValue)) {
812-
// It may be the case that the last row in a form is "empty" waiting for
813-
// someone to enter a value, in which case we do not want to throw an
814-
// error, we just want to ignore it.
815-
return results;
816-
}
817-
pkFields.put(fieldName, fieldValue);
818-
}
819815
boolean doLink = "Y".equalsIgnoreCase(action);
820-
if (Debug.infoOn()) {
821-
Debug.logInfo("in updateOrRemove, context:" + context, MODULE);
822-
}
823816
try {
824-
GenericValue entityValuePK = delegator.makeValue(entityName, pkFields);
825-
if (Debug.infoOn()) {
826-
Debug.logInfo("in updateOrRemove, entityValuePK:" + entityValuePK, MODULE);
827-
}
828-
GenericValue entityValueExisting = EntityQuery.use(delegator).from(entityName).where(entityValuePK).cache().queryOne();
829-
if (Debug.infoOn()) {
830-
Debug.logInfo("in updateOrRemove, entityValueExisting:" + entityValueExisting, MODULE);
831-
}
817+
GenericValue entityValuePK = delegator.makeValue("ProductFeatureDataResource",
818+
UtilMisc.toMap("productFeatureId", productFeatureId, "dataResourceId", dataResourceId));
819+
820+
GenericValue entityValueExisting = EntityQuery.use(delegator).from("ProductFeatureDataResource")
821+
.where("productFeatureId", productFeatureId, "dataResourceId", dataResourceId).cache().queryOne();
822+
832823
if (entityValueExisting == null) {
833824
if (doLink) {
834825
entityValuePK.create();
835-
if (Debug.infoOn()) {
836-
Debug.logInfo("in updateOrRemove, entityValuePK: CREATED", MODULE);
837-
}
838-
}
839-
} else {
840-
if (!doLink) {
841-
entityValueExisting.remove();
842-
if (Debug.infoOn()) {
843-
Debug.logInfo("in updateOrRemove, entityValueExisting: REMOVED", MODULE);
844-
}
845826
}
827+
} else if (!doLink) {
828+
entityValueExisting.remove();
846829
}
847830
} catch (GenericEntityException e) {
848831
Debug.logError(e, MODULE);
849832
return ServiceUtil.returnError(e.toString());
850833
}
851-
return results;
834+
return ServiceUtil.returnSuccess();
852835
}
853836

854837
/**

applications/content/template/cms/CMSContentEdit.ftl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,8 @@ under the License.
111111
<tr>
112112
<td class="">[${feature.productFeatureId}] - ${feature.description}</td>
113113
<td class=""><input type="checkbox" name="action_o_${rowCount}" value="Y" ${checked}/></td>
114-
<input type="hidden" name="fieldName0_o_${rowCount}" value="productFeatureId"/>
115-
<input type="hidden" name="fieldValue0_o_${rowCount}" value="${feature.productFeatureId}"/>
116-
<input type="hidden" name="fieldName1_o_${rowCount}" value="dataResourceId"/>
117-
<input type="hidden" name="fieldValue1_o_${rowCount}" value="${feature.dataResourceId}"/>
118-
<input type="hidden" name="entityName_o_${rowCount}" value="ProductFeatureDataResource"/>
119-
<input type="hidden" name="pkFieldCount_o_${rowCount}" value="2"/>
114+
<input type="hidden" name="productFeatureId_o_${rowCount}" value="${feature.productFeatureId}"/>
115+
<input type="hidden" name="dataResourceId_o_${rowCount}" value="${feature.dataResourceId}"/>
120116
</tr>
121117
<#assign rowCount=rowCount + 1/>
122118
</#list>
@@ -126,12 +122,8 @@ under the License.
126122
<@htmlTemplate.lookupField formName="updatefeatures" name="fieldValue0_o_${rowCount}" id="fieldValue0_o_${rowCount}" fieldFormName="LookupProductFeature"/>
127123
</div>
128124
</td>
129-
<input type="hidden" name="fieldName0_o_${rowCount}" value="productFeatureId"/>
130-
<input type="hidden" name="fieldValue0_o_${rowCount}" value=""/>
131-
<input type="hidden" name="fieldName1_o_${rowCount}" value="dataResourceId"/>
132-
<input type="hidden" name="fieldValue1_o_${rowCount}" value="${dataResourceId}"/>
133-
<input type="hidden" name="entityName_o_${rowCount}" value="ProductFeatureDataResource"/>
134-
<input type="hidden" name="pkFieldCount_o_${rowCount}" value="2"/>
125+
<input type="hidden" name="productFeatureId_o_${rowCount}" value=""/>
126+
<input type="hidden" name="dataResourceId_o_${rowCount}" value="${dataResourceId}"/>
135127
<#assign rowCount=rowCount + 1/>
136128
</tr>
137129
<tr>

applications/content/webapp/content/WEB-INF/controller.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ under the License.
15611561
</request-map>
15621562
<request-map uri="updateFeatures">
15631563
<security https="true" auth="true"/>
1564-
<event type="service-multi" invoke="updateOrRemove"/>
1564+
<event type="service-multi" invoke="createOrRemoveProductFeatureDataResource"/>
15651565
<response name="success" type="view" value="CMSContentEdit"/>
15661566
<response name="error" type="view" value="CMSContentEdit"/>
15671567
</request-map>

0 commit comments

Comments
 (0)