Specification Builder Snippet In Java

    0

    1

    Kishan Kumar

    This class is used to build a specification that tests a given class.

    import com.solucionis.porta.specifications.SpecificationBuilder;
    import org.springframework.data.jpa.domain.Specification;
    import org.slf4j.LoggerFactory;
    import java.util.ArrayList;
    import java.time.format.DateTimeFormatter;
    import java.time.LocalDate;
    import org.slf4j.Logger;
    import static com.solucionis.porta.specifications.SpecificationBuilder.isEqual;
    import java.util.List;
    
    public class TestingSpecificationBuilder {
    
      private TestingSpecificationBuilder() {
      throw new IllegalStateException("TestingSpecificationBuilder class");
      }
      
      public static Specification<TestingClass> searchSpecification(TestingClass search) {
    
      List<Specification<TestingClass>> searchSpecification = new ArrayList<>();
    
      if (StringUtils.isNotBlank(search.getUserId())) {
        searchSpecification.add(SpecificationBuilder.isEqual("userId", search.getUserId()));
      }
      if (CommonUtil.isNotEmpty(search.getRelationId())) {
        searchSpecification.add(SpecificationBuilder.isEqual("relationId", search.getRelationId()));
      }
      if (CommonUtil.isNotEmpty(search.getAccountIds())) {
        searchSpecification.add(SpecificationBuilder.isFieldIn("accountNumber", search.getAccountIds()));
      }
      if (CommonUtil.isNotEmpty(search.getAccountId())) {
        searchSpecification.add(SpecificationBuilder.isEqual("accountNumber", search.getAccountId()));
      }
      if (CommonUtil.isNotEmpty(search.getAccountName())) {
        searchSpecification.add(SpecificationBuilder.isLike("accountname", search.getAccountName()));
      }
    
      return SpecificationBuilder.combineWithAnd(searchSpecification);
      }
    
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.