All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.croz.nrich.search.converter.DefaultStringToEntityPropertyMapConverter Maven / Gradle / Ivy

Go to download

Provides an easy way of querying of JPA entities through automatic query creation from passed in data (either strings or classes that hold restriction values)

The newest version!
/*
 *  Copyright 2020-2023 CROZ d.o.o, the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package net.croz.nrich.search.converter;

import lombok.RequiredArgsConstructor;
import net.croz.nrich.search.api.converter.StringToEntityPropertyMapConverter;
import net.croz.nrich.search.api.converter.StringToTypeConverter;
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 {

    private final List> converterList;

    @Override
    public Map convert(String value, List propertyToSearchList, ManagedType managedType, SearchPropertyConfiguration searchPropertyConfiguration) {
        if (value == null || CollectionUtils.isEmpty(propertyToSearchList)) {
            return Collections.emptyMap();
        }

        Assert.notNull(managedType, "Managed type cannot be null!");

        JpaEntityAttributeResolver attributeResolver = new JpaEntityAttributeResolver(managedType);

        Map resultMap = new HashMap<>();

        propertyToSearchList.forEach(property -> {
            AttributeHolder attributeHolder = attributeResolver.resolveAttributeByPath(property);

            if (!attributeHolder.isFound()) {
                String propertyWithoutSuffix = PropertyNameUtil.propertyNameWithoutSuffix(property, searchPropertyConfiguration);

                attributeHolder = attributeResolver.resolveAttributeByPath(propertyWithoutSuffix);
            }

            if (!attributeHolder.isFound()) {
                return;
            }

            Attribute attribute = attributeHolder.attribute();

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

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

                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;
    }

    private Object doConversion(String searchTerm, Class attributeType) {
        if (String.class.isAssignableFrom(attributeType)) {
            return searchTerm;
        }

        StringToTypeConverter converter = converterList.stream()
            .filter(value -> value.supports(attributeType))
            .findFirst()
            .orElseThrow(() -> new IllegalArgumentException(String.format("No converter found for attribute type %s", attributeType.getName())));

        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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy