|
| 1 | +package ca.bc.gov.educ.api.institute.service.v1; |
| 2 | + |
| 3 | +import ca.bc.gov.educ.api.institute.exception.InvalidParameterException; |
| 4 | +import ca.bc.gov.educ.api.institute.exception.SchoolAPIRuntimeException; |
| 5 | +import ca.bc.gov.educ.api.institute.filter.DistrictHistoryFilterSpecs; |
| 6 | +import ca.bc.gov.educ.api.institute.filter.FilterOperation; |
| 7 | +import ca.bc.gov.educ.api.institute.model.v1.DistrictHistoryEntity; |
| 8 | +import ca.bc.gov.educ.api.institute.model.v1.SchoolEntity; |
| 9 | +import ca.bc.gov.educ.api.institute.repository.v1.DistrictHistoryRepository; |
| 10 | +import ca.bc.gov.educ.api.institute.struct.v1.Condition; |
| 11 | +import ca.bc.gov.educ.api.institute.struct.v1.Search; |
| 12 | +import ca.bc.gov.educ.api.institute.struct.v1.SearchCriteria; |
| 13 | +import ca.bc.gov.educ.api.institute.struct.v1.ValueType; |
| 14 | +import ca.bc.gov.educ.api.institute.util.RequestUtil; |
| 15 | +import ca.bc.gov.educ.api.institute.util.TransformUtil; |
| 16 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.jboss.threads.EnhancedQueueExecutor; |
| 23 | +import org.springframework.data.domain.Page; |
| 24 | +import org.springframework.data.domain.PageRequest; |
| 25 | +import org.springframework.data.domain.Pageable; |
| 26 | +import org.springframework.data.domain.Sort; |
| 27 | +import org.springframework.data.jpa.domain.Specification; |
| 28 | +import org.springframework.stereotype.Service; |
| 29 | +import org.springframework.transaction.annotation.Propagation; |
| 30 | +import org.springframework.transaction.annotation.Transactional; |
| 31 | + |
| 32 | +import java.time.Duration; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.CompletionException; |
| 36 | +import java.util.concurrent.Executor; |
| 37 | + |
| 38 | +/** |
| 39 | + * The type School search service. |
| 40 | + */ |
| 41 | +@Service |
| 42 | +@Slf4j |
| 43 | +public class DistrictHistorySearchService { |
| 44 | + private final DistrictHistoryFilterSpecs districtHistoryFilterSpecs; |
| 45 | + |
| 46 | + private final DistrictHistoryRepository districtHistoryRepository; |
| 47 | + |
| 48 | + private final Executor paginatedQueryExecutor = new EnhancedQueueExecutor.Builder() |
| 49 | + .setThreadFactory(new ThreadFactoryBuilder().setNameFormat("async-pagination-query-executor-%d").build()) |
| 50 | + .setCorePoolSize(2).setMaximumPoolSize(10).setKeepAliveTime(Duration.ofSeconds(60)).build(); |
| 51 | + |
| 52 | + public DistrictHistorySearchService(DistrictHistoryFilterSpecs districtHistoryFilterSpecs, DistrictHistoryRepository districtHistoryRepository) { |
| 53 | + this.districtHistoryFilterSpecs = districtHistoryFilterSpecs; |
| 54 | + this.districtHistoryRepository = districtHistoryRepository; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets specifications. |
| 59 | + * |
| 60 | + * @param districtSpecs the pen reg batch specs |
| 61 | + * @param i the |
| 62 | + * @param search the search |
| 63 | + * @return the specifications |
| 64 | + */ |
| 65 | + public Specification<DistrictHistoryEntity> getSpecifications(Specification<DistrictHistoryEntity> districtSpecs, int i, Search search) { |
| 66 | + if (i == 0) { |
| 67 | + districtSpecs = getDistrictHistoryEntitySpecification(search.getSearchCriteriaList()); |
| 68 | + } else { |
| 69 | + if (search.getCondition() == Condition.AND) { |
| 70 | + districtSpecs = districtSpecs.and(getDistrictHistoryEntitySpecification(search.getSearchCriteriaList())); |
| 71 | + } else { |
| 72 | + districtSpecs = districtSpecs.or(getDistrictHistoryEntitySpecification(search.getSearchCriteriaList())); |
| 73 | + } |
| 74 | + } |
| 75 | + return districtSpecs; |
| 76 | + } |
| 77 | + |
| 78 | + private Specification<DistrictHistoryEntity> getDistrictHistoryEntitySpecification(List<SearchCriteria> criteriaList) { |
| 79 | + Specification<DistrictHistoryEntity> districtSpecs = null; |
| 80 | + if (!criteriaList.isEmpty()) { |
| 81 | + int i = 0; |
| 82 | + for (SearchCriteria criteria : criteriaList) { |
| 83 | + if (criteria.getKey() != null && criteria.getOperation() != null && criteria.getValueType() != null) { |
| 84 | + var criteriaValue = criteria.getValue(); |
| 85 | + if(StringUtils.isNotBlank(criteria.getValue()) && TransformUtil.isUppercaseField(SchoolEntity.class, criteria.getKey())) { |
| 86 | + criteriaValue = criteriaValue.toUpperCase(); |
| 87 | + } |
| 88 | + Specification<DistrictHistoryEntity> typeSpecification = getTypeSpecification(criteria.getKey(), criteria.getOperation(), criteriaValue, criteria.getValueType()); |
| 89 | + districtSpecs = getSpecificationPerGroup(districtSpecs, i, criteria, typeSpecification); |
| 90 | + i++; |
| 91 | + } else { |
| 92 | + throw new InvalidParameterException("Search Criteria can not contain null values for key, value and operation type"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return districtSpecs; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets specification per group. |
| 101 | + * |
| 102 | + * @param districtHistoryEntitySpecification the pen request batch entity specification |
| 103 | + * @param i the |
| 104 | + * @param criteria the criteria |
| 105 | + * @param typeSpecification the type specification |
| 106 | + * @return the specification per group |
| 107 | + */ |
| 108 | + private Specification<DistrictHistoryEntity> getSpecificationPerGroup(Specification<DistrictHistoryEntity> districtHistoryEntitySpecification, int i, SearchCriteria criteria, Specification<DistrictHistoryEntity> typeSpecification) { |
| 109 | + if (i == 0) { |
| 110 | + districtHistoryEntitySpecification = Specification.where(typeSpecification); |
| 111 | + } else { |
| 112 | + if (criteria.getCondition() == Condition.AND) { |
| 113 | + districtHistoryEntitySpecification = districtHistoryEntitySpecification.and(typeSpecification); |
| 114 | + } else { |
| 115 | + districtHistoryEntitySpecification = districtHistoryEntitySpecification.or(typeSpecification); |
| 116 | + } |
| 117 | + } |
| 118 | + return districtHistoryEntitySpecification; |
| 119 | + } |
| 120 | + |
| 121 | + private Specification<DistrictHistoryEntity> getTypeSpecification(String key, FilterOperation filterOperation, String value, ValueType valueType) { |
| 122 | + Specification<DistrictHistoryEntity> schoolEntitySpecification = null; |
| 123 | + switch (valueType) { |
| 124 | + case STRING: |
| 125 | + schoolEntitySpecification = districtHistoryFilterSpecs.getStringTypeSpecification(key, value, filterOperation); |
| 126 | + break; |
| 127 | + case DATE_TIME: |
| 128 | + schoolEntitySpecification = districtHistoryFilterSpecs.getDateTimeTypeSpecification(key, value, filterOperation); |
| 129 | + break; |
| 130 | + case LONG: |
| 131 | + schoolEntitySpecification = districtHistoryFilterSpecs.getLongTypeSpecification(key, value, filterOperation); |
| 132 | + break; |
| 133 | + case INTEGER: |
| 134 | + schoolEntitySpecification = districtHistoryFilterSpecs.getIntegerTypeSpecification(key, value, filterOperation); |
| 135 | + break; |
| 136 | + case DATE: |
| 137 | + schoolEntitySpecification = districtHistoryFilterSpecs.getDateTypeSpecification(key, value, filterOperation); |
| 138 | + break; |
| 139 | + case UUID: |
| 140 | + schoolEntitySpecification = districtHistoryFilterSpecs.getUUIDTypeSpecification(key, value, filterOperation); |
| 141 | + break; |
| 142 | + case BOOLEAN: |
| 143 | + schoolEntitySpecification = districtHistoryFilterSpecs.getBooleanTypeSpecification(key, value, filterOperation); |
| 144 | + break; |
| 145 | + default: |
| 146 | + break; |
| 147 | + } |
| 148 | + return schoolEntitySpecification; |
| 149 | + } |
| 150 | + |
| 151 | + @Transactional(propagation = Propagation.SUPPORTS) |
| 152 | + public CompletableFuture<Page<DistrictHistoryEntity>> findAll(Specification<DistrictHistoryEntity> districtSpecs, final Integer pageNumber, final Integer pageSize, final List<Sort.Order> sorts) { |
| 153 | + log.trace("In find all query: {}", districtSpecs); |
| 154 | + return CompletableFuture.supplyAsync(() -> { |
| 155 | + Pageable paging = PageRequest.of(pageNumber, pageSize, Sort.by(sorts)); |
| 156 | + try { |
| 157 | + log.trace("Running paginated query: {}", districtSpecs); |
| 158 | + var results = this.districtHistoryRepository.findAll(districtSpecs, paging); |
| 159 | + log.trace("Paginated query returned with results: {}", results); |
| 160 | + return results; |
| 161 | + } catch (final Throwable ex) { |
| 162 | + log.error("Failure querying for paginated schools: {}", ex.getMessage()); |
| 163 | + throw new CompletionException(ex); |
| 164 | + } |
| 165 | + }, paginatedQueryExecutor); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Sets specification and sort criteria. |
| 171 | + * |
| 172 | + * @param sortCriteriaJson the sort criteria json |
| 173 | + * @param searchCriteriaListJson the search criteria list json |
| 174 | + * @param objectMapper the object mapper |
| 175 | + * @param sorts the sorts |
| 176 | + * @return the specification and sort criteria |
| 177 | + */ |
| 178 | + public Specification<DistrictHistoryEntity> setSpecificationAndSortCriteria(String sortCriteriaJson, String searchCriteriaListJson, ObjectMapper objectMapper, List<Sort.Order> sorts) { |
| 179 | + Specification<DistrictHistoryEntity> districtSpecs = null; |
| 180 | + try { |
| 181 | + RequestUtil.getSortCriteria(sortCriteriaJson, objectMapper, sorts); |
| 182 | + if (StringUtils.isNotBlank(searchCriteriaListJson)) { |
| 183 | + List<Search> searches = objectMapper.readValue(searchCriteriaListJson, new TypeReference<>() { |
| 184 | + }); |
| 185 | + int i = 0; |
| 186 | + for (var search : searches) { |
| 187 | + districtSpecs = getSpecifications(districtSpecs, i, search); |
| 188 | + i++; |
| 189 | + } |
| 190 | + } |
| 191 | + } catch (JsonProcessingException e) { |
| 192 | + throw new SchoolAPIRuntimeException(e.getMessage()); |
| 193 | + } |
| 194 | + return districtSpecs; |
| 195 | + } |
| 196 | +} |
0 commit comments