Skip to content

Commit

Permalink
Fallback to searching by association id when association name has bee…
Browse files Browse the repository at this point in the history
…n sent as property name
  • Loading branch information
agrancaric committed Jun 17, 2024
1 parent 2dc9620 commit d50af68
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 265 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createAndSaveRegistryTestEmbeddedUserGroup;
import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createBulkListRegistryRequest;
import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createEmbeddedIdSearchRequest;
import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createListRegistryRequest;
import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createRegistryTestEmbeddedUserGroup;
import static net.croz.nrich.registry.data.testutil.RegistryDataGeneratingUtil.createRegistryTestEmbeddedUserGroupId;
Expand Down Expand Up @@ -395,4 +396,18 @@ void shouldCreateRegistryEntityWithAssociationFromRequestClass() {
assertThat(loaded.getName()).isEqualTo("name 1");
assertThat(loaded.getParent()).isNotNull();
}

@Test
void shouldSearchByEmbeddedId() {
// given
createAndSaveRegistryTestEmbeddedUserGroup(entityManager);
RegistryTestEmbeddedUserGroup entity = createAndSaveRegistryTestEmbeddedUserGroup(entityManager);
ListRegistryRequest request = createEmbeddedIdSearchRequest(entity);

// when
Page<RegistryTestEmbeddedUserGroup> result = registryDataService.list(request);

// then
assertThat(result).hasSize(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ public static UpdateRegistryTestEntityRequest createUpdateRegistryTestEntityRequ
return request;
}

public static ListRegistryRequest createEmbeddedIdSearchRequest(RegistryTestEmbeddedUserGroup entity) {
SearchParameter searchParameter = new SearchParameter();

searchParameter.setPropertyNameList(List.of("userGroupId.user"));
searchParameter.setQuery(entity.getUserGroupId().getGroup().getId().toString());

ListRegistryRequest request = new ListRegistryRequest();

request.setClassFullName(RegistryTestEmbeddedUserGroup.class.getName());
request.setPageNumber(0);
request.setPageSize(10);
request.setSearchParameter(searchParameter);

return request;
}

private static RegistryTestEntity createRegistryTestEntity(String name, Integer age, RegistryTestEntity parent) {
RegistryTestEntity entity = new RegistryTestEntity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@
import net.croz.nrich.search.api.model.property.SearchPropertyConfiguration;
import net.croz.nrich.search.model.AttributeHolder;
import net.croz.nrich.search.support.JpaEntityAttributeResolver;
import net.croz.nrich.search.util.PathResolvingUtil;
import net.croz.nrich.search.util.PropertyNameUtil;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.IdentifiableType;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SingularAttribute;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequiredArgsConstructor
public class DefaultStringToEntityPropertyMapConverter implements StringToEntityPropertyMapConverter {

Expand Down Expand Up @@ -63,10 +69,26 @@ public Map<String, Object> convert(String value, List<String> propertyToSearchLi
return;
}

Object convertedValue = doConversion(value, attributeHolder.getAttribute().getJavaType());
Attribute<?, ?> attribute = attributeHolder.getAttribute();

Object convertedValue = doConversion(value, attribute.getJavaType());

if (convertedValue == null && attribute.isAssociation()) {
IdentifiableType<?> identifiableType = asIdentifiableType(attribute);
if (identifiableType == null || !identifiableType.hasSingleIdAttribute()) {
return;
}

resultMap.put(property, convertedValue);
Class<?> idType = identifiableType.getIdType().getJavaType();
String idName = identifiableType.getId(idType).getName();

convertedValue = doConversion(value, idType);

resultMap.put(PathResolvingUtil.joinPath(property, idName), convertedValue);
}
else {
resultMap.put(property, convertedValue);
}
});

return resultMap;
Expand All @@ -84,4 +106,15 @@ private Object doConversion(String searchTerm, Class<?> attributeType) {

return converter.convert(searchTerm, attributeType);
}

private IdentifiableType<?> asIdentifiableType(Attribute<?, ?> attribute) {
if (attribute instanceof SingularAttribute<?, ?> singularAttribute && singularAttribute.getType() instanceof IdentifiableType<?> identifiableType) {
return identifiableType;
}
if (attribute instanceof PluralAttribute<?, ?, ?> pluralAttribute && pluralAttribute.getElementType() instanceof IdentifiableType<?> identifiableType) {
return identifiableType;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ void shouldConvertPropertyWithSuffix() {
assertThat(result).containsKeys(propertyToSearchList.toArray(new String[0])).containsValue(dateOf(value));
}

@Test
void shouldSupportSearchingAssociationsById() {
// given
String value = "1";
List<String> propertyToSearchList = List.of("nestedEntity", "nestedEntityList");

// when
Map<String, Object> result = stringToEntityPropertyMapConverter.convert(value, propertyToSearchList, managedTypeOfTestEntity(), PROPERTY_CONFIGURATION);

// then
assertThat(result).containsKeys("nestedEntity.id", "nestedEntityList.id").containsValue(Long.parseLong(value));
}

private ManagedType<?> managedTypeOfTestEntity() {
return entityManager.getMetamodel().managedType((Class<?>) DefaultStringToEntityPropertyMapConverterTestEntity.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import java.util.Date;
import java.util.List;

@Setter
@Getter
Expand All @@ -42,4 +44,7 @@ public class DefaultStringToEntityPropertyMapConverterTestEntity {
@OneToOne
private DefaultStringToEntityPropertyMapConverterTestNestedEntity nestedEntity;

@OneToMany
private List<DefaultStringToEntityPropertyMapConverterTestNestedEntity> nestedEntityList;

}

0 comments on commit d50af68

Please sign in to comment.