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

com.codingapi.springboot.fast.jpa.repository.ExampleBuilder Maven / Gradle / Ivy

There is a newer version: 3.3.0.dev1
Show newest version
package com.codingapi.springboot.fast.jpa.repository;

import com.codingapi.springboot.framework.dto.request.Filter;
import com.codingapi.springboot.framework.dto.request.PageRequest;
import com.codingapi.springboot.framework.dto.request.RequestFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;

import java.beans.PropertyDescriptor;

/**
 *  Example组装
 */
class ExampleBuilder {

    private final PageRequest request;
    private final Class clazz;

    public ExampleBuilder(PageRequest request, Class clazz) {
        this.request = request;
        this.clazz = clazz;
    }

    public  Example getExample() {
        RequestFilter requestFilter = request.getRequestFilter();
        if (!requestFilter.hasFilter()) {
            return null;
        }
        Object entity = null;
        try {
            entity = clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz);
        for (PropertyDescriptor descriptor : descriptors) {
            String name = descriptor.getName();
            Filter value = requestFilter.getFilter(name);
            if (value != null) {
                try {
                    descriptor.getWriteMethod().invoke(entity, value.getFilterValue(descriptor.getPropertyType()));
                } catch (Exception e) {
                }
            }
        }
        return (Example) Example.of(entity);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy