Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPD-37787 Filtering using r_<relationshipName>_c_<objectName>ERC is not working properly #158839

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
64f3a8d
LPD-37787 Use correct objectDefinition in _getValue
dannielraposo Jan 17, 2025
97c1ee6
LPD-37787 Change entityfield for RelationshipERC field
dannielraposo Jan 28, 2025
8102ff1
LPD-37787 Handle relatedERC filtering from child and parent objects
dannielraposo Jan 28, 2025
d80230b
LPD-37787 Complete test to extend coverage
dannielraposo Jan 13, 2025
ee5f48e
LPD-37787 Add test filtering by RelationshipERC field
dannielraposo Jan 13, 2025
68b79e9
LPD-37787 Fix sorting by RelationshipERC field
dannielraposo Jan 29, 2025
72445df
LPD-37787 Make test consistent. Was not failing before because the ER…
dannielraposo Jan 29, 2025
bada04f
LPD-37787 Simplify and refactor
dannielraposo Jan 30, 2025
3f5b3f5
LPD-37787 Simplify and refactor. With this, we remove all the changes…
ccorreagg Feb 3, 2025
2d6cab3
LPD-37787 Increase coverage of tests for all the relationship types
ccorreagg Feb 5, 2025
811b2cc
LPD-37787 Constants and then free form text
brianchandotcom Feb 14, 2025
e057af0
LPD-37787 Rename
brianchandotcom Feb 14, 2025
0be8b8b
LPD-37787 Match class name
brianchandotcom Feb 14, 2025
3211e3f
LPD-37787 SF
brianchandotcom Feb 14, 2025
965b8a9
LPD-37787 Buy space
brianchandotcom Feb 14, 2025
09987ce
LPD-37787 Rename
brianchandotcom Feb 14, 2025
86a3431
LPD-37787 Rename. I am reusing the substring as the number of charact…
ccorreagg Feb 14, 2025
5336dd0
LPD-37787 Rename. We use the value given by the Object Field Settings…
ccorreagg Feb 14, 2025
0a2e4f1
LPD-37787 Rename test
ccorreagg Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* SPDX-FileCopyrightText: (c) 2025 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.object.rest.internal.odata.entity;

import com.liferay.portal.odata.entity.StringEntityField;

import java.util.Locale;
import java.util.function.Function;

/**
* @author Carlos Correa
*/
public class ReferenceStringEntityField extends StringEntityField {

public ReferenceStringEntityField(
String fieldName, Function<Locale, String> filterableFunction,
String referenceFieldName) {

super(fieldName, locale -> referenceFieldName, filterableFunction);

_referenceFieldName = referenceFieldName;
}

public String getReferenceFieldName() {
return _referenceFieldName;
}

private final String _referenceFieldName;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.liferay.object.model.ObjectField;
import com.liferay.object.model.ObjectRelationship;
import com.liferay.object.relationship.util.ObjectRelationshipUtil;
import com.liferay.object.rest.internal.odata.entity.ReferenceStringEntityField;
import com.liferay.object.service.ObjectFieldLocalServiceUtil;
import com.liferay.object.service.ObjectRelationshipLocalServiceUtil;
import com.liferay.petra.string.StringPool;
Expand All @@ -33,9 +34,11 @@

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import javax.ws.rs.BadRequestException;

Expand Down Expand Up @@ -157,6 +160,10 @@ else if (Objects.equals(
"Unable to get entity field for object field " + objectField);
}

private Function<Locale, String> _getExternalReferenceCodeFunction() {
return locale -> "externalReferenceCode";
}

private Map<String, EntityField> _getObjectDefinitionEntityFieldsMap(
ObjectDefinition objectDefinition) {

Expand Down Expand Up @@ -211,7 +218,8 @@ private Map<String, EntityField> _getStringEntityFieldsMap(
).put(
"externalReferenceCode",
() -> new StringEntityField(
"externalReferenceCode", locale -> "externalReferenceCode")
"externalReferenceCode",
_getExternalReferenceCodeFunction())
).put(
"id", new IdEntityField("id", locale -> "id", String::valueOf)
).put(
Expand Down Expand Up @@ -279,9 +287,11 @@ private Map<String, EntityField> _getStringEntityFieldsMap(

entityFieldsMap.put(
objectRelationshipERCObjectFieldName,
new StringEntityField(
new ReferenceStringEntityField(
objectRelationshipERCObjectFieldName,
locale -> objectFieldName));
_getExternalReferenceCodeFunction(),
objectFieldName.split(StringPool.UNDERLINE)[1] +
"/externalReferenceCode"));

String relationshipIdName = objectFieldName.substring(
objectFieldName.lastIndexOf(StringPool.UNDERLINE) + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.liferay.object.related.models.ObjectRelatedModelsPredicateProvider;
import com.liferay.object.related.models.ObjectRelatedModelsPredicateProviderRegistry;
import com.liferay.object.relationship.util.ObjectRelationshipUtil;
import com.liferay.object.rest.internal.odata.entity.ReferenceStringEntityField;
import com.liferay.object.rest.internal.util.BinaryExpressionConverterUtil;
import com.liferay.object.rest.odata.entity.v1_0.provider.EntityModelProvider;
import com.liferay.object.service.ObjectFieldLocalService;
Expand Down Expand Up @@ -161,6 +162,21 @@ public Object visitComplexPropertyExpression(
});
}

if (propertyExpression instanceof PrimitivePropertyExpression) {
PrimitivePropertyExpression primitivePropertyExpression =
(PrimitivePropertyExpression)propertyExpression;

Object value = _visitPrimitivePropertyExpression(
_getEntityField(
complexPropertyExpression.getName() + StringPool.SLASH +
primitivePropertyExpression.getName(),
_objectDefinition),
primitivePropertyExpression);

return complexPropertyExpression.getName() + StringPool.SLASH +
value;
}

return complexPropertyExpression.toString();
}

Expand Down Expand Up @@ -318,7 +334,10 @@ else if (type == MethodExpression.Type.STARTS_WITH) {
public Object visitPrimitivePropertyExpression(
PrimitivePropertyExpression primitivePropertyExpression) {

return primitivePropertyExpression.getName();
return _visitPrimitivePropertyExpression(
_getEntityField(
primitivePropertyExpression.getName(), _objectDefinition),
primitivePropertyExpression);
}

@Override
Expand Down Expand Up @@ -415,20 +434,32 @@ private ObjectRelationship _fetchObjectRelationship(
private Column<?, Object> _getColumn(
Object fieldName, ObjectDefinition objectDefinition) {

EntityField entityField = _getEntityField(fieldName, objectDefinition);
EntityField entityField = _getEntityField(
(String)fieldName, objectDefinition);

return (Column<?, Object>)_objectFieldLocalService.getColumn(
objectDefinition.getObjectDefinitionId(),
entityField.getFilterableName(null));
}

private EntityField _getEntityField(
Object fieldName, ObjectDefinition objectDefinition) {
String fieldName, ObjectDefinition objectDefinition) {

Map<String, EntityField> entityFieldsMap = _getEntityFieldsMap(
objectDefinition);

return entityFieldsMap.get(GetterUtil.getString(fieldName));
int index = fieldName.indexOf(StringPool.SLASH);

if (index == -1) {
return entityFieldsMap.get(fieldName);
}

return _getEntityField(
fieldName.substring(index + 1),
ObjectRelationshipUtil.getRelatedObjectDefinition(
objectDefinition,
_fetchObjectRelationship(
objectDefinition, fieldName.substring(0, index))));
}

private Map<String, EntityField> _getEntityFieldsMap(
Expand Down Expand Up @@ -645,7 +676,8 @@ else if (Objects.equals(BinaryExpression.Operation.OR, operation)) {
private Object _getValue(
Object left, ObjectDefinition objectDefinition, Object right) {

EntityField entityField = _getEntityField(left, objectDefinition);
EntityField entityField = _getEntityField(
(String)left, objectDefinition);

EntityField.Type entityType = entityField.getType();

Expand Down Expand Up @@ -696,7 +728,7 @@ private Object _getValue(

try {
ObjectField objectField = _objectFieldLocalService.getObjectField(
_objectDefinition.getObjectDefinitionId(),
objectDefinition.getObjectDefinitionId(),
entityFieldFilterableName);

ObjectFieldBusinessType objectFieldBusinessType =
Expand Down Expand Up @@ -760,6 +792,22 @@ else if (propertyExpression instanceof ComplexPropertyExpression) {
complexPropertyExpression.getPropertyExpression(),
relationshipsNames);
}
else if (propertyExpression instanceof PrimitivePropertyExpression) {
PrimitivePropertyExpression primitivePropertyExpression =
(PrimitivePropertyExpression)propertyExpression;

String relationshipsNamesString = StringUtil.merge(
relationshipsNames, StringPool.SLASH);

Object value = _visitPrimitivePropertyExpression(
_getEntityField(
relationshipsNamesString + StringPool.SLASH +
primitivePropertyExpression.getName(),
_objectDefinition),
primitivePropertyExpression);

return relationshipsNamesString + StringPool.SLASH + value;
}

relationshipsNames.add(propertyExpression.toString());

Expand Down Expand Up @@ -835,6 +883,20 @@ private Predicate _visitCollectionPropertyExpression(
_serviceTrackerMap));
}

private Object _visitPrimitivePropertyExpression(
EntityField entityField,
PrimitivePropertyExpression primitivePropertyExpression) {

if (entityField instanceof ReferenceStringEntityField) {
ReferenceStringEntityField referenceStringEntityField =
(ReferenceStringEntityField)entityField;

return referenceStringEntityField.getReferenceFieldName();
}

return primitivePropertyExpression.getName();
}

private static final Log _log = LogFactoryUtil.getLog(
PredicateExpressionVisitorImpl.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,22 @@ public void testGetObjectEntries() throws Exception {

// Contains expression

testGetObjectEntries(
HashMapBuilder.put(
"filter",
_buildContainsExpressionFilterString(
_objectRelationshipERCObjectFieldName,
parentObjectEntry1.getExternalReferenceCode())
).build(),
childObjectEntry1);
testGetObjectEntries(
HashMapBuilder.put(
"filter",
_buildContainsExpressionFilterString(
_objectRelationshipERCObjectFieldName,
parentObjectEntry2.getExternalReferenceCode())
).build(),
childObjectEntry2);
testGetObjectEntries(
HashMapBuilder.put(
"filter",
Expand Down Expand Up @@ -3370,6 +3386,22 @@ public void testGetObjectEntries() throws Exception {

// "Starts with" expression

testGetObjectEntries(
HashMapBuilder.put(
"filter",
_buildStartsWithExpressionFilterString(
_objectRelationshipERCObjectFieldName,
parentObjectEntry1.getExternalReferenceCode())
).build(),
childObjectEntry1);
testGetObjectEntries(
HashMapBuilder.put(
"filter",
_buildStartsWithExpressionFilterString(
_objectRelationshipERCObjectFieldName,
parentObjectEntry2.getExternalReferenceCode())
).build(),
childObjectEntry2);
testGetObjectEntries(
HashMapBuilder.put(
"filter",
Expand Down
Loading