|
@@ -7,10 +7,18 @@ import com.mooctest.crowd.domain.exception.FieldNoExistException;
|
|
|
import com.mooctest.crowd.domain.exception.TestTypeNoExistException;
|
|
|
import com.mooctest.crowd.domain.model.*;
|
|
|
import com.mooctest.crowd.domain.util.Converter;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.data.jpa.domain.Specifications;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -45,6 +53,9 @@ public class CommonRepo {
|
|
|
@Autowired
|
|
|
private PartnerDao partnerDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ExpertDao expertDao;
|
|
|
+
|
|
|
public List<TestType> getAllTestType(){
|
|
|
return testTypeDao.findAll().stream().map(testTypePO -> Converter.convert(TestType.class, testTypePO)).collect(Collectors.toList());
|
|
|
}
|
|
@@ -122,4 +133,43 @@ public class CommonRepo {
|
|
|
return testTypeList;
|
|
|
}
|
|
|
|
|
|
+ public Page<Resource> findAllResourceByPage(Pageable pageable, String keyword){
|
|
|
+ Specifications<ResourcePO> where = Specifications.where(getResource(keyword));
|
|
|
+ return resourceDao.findAll(where, pageable).map(resourcePO -> Converter.convert(Resource.class, resourcePO));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Specification<ResourcePO> getResource(String keyword) {
|
|
|
+ return new Specification<ResourcePO>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<ResourcePO> a, CriteriaQuery<?> q, CriteriaBuilder cb) {
|
|
|
+ Predicate predicate = cb.conjunction();
|
|
|
+ if(keyword != null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.like(a.<String>get("name"), "%" + StringUtils.trim(keyword) + "%")
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return predicate;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<Expert> findAllExpertByPage(Pageable pageable, String keyword){
|
|
|
+ Specifications<ExpertPO> where = Specifications.where(getExpert(keyword));
|
|
|
+ return expertDao.findAll(where, pageable).map(expertPO -> Converter.convert(Expert.class, expertPO));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Specification<ExpertPO> getExpert(String keyword) {
|
|
|
+ return new Specification<ExpertPO>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<ExpertPO> a, CriteriaQuery<?> q, CriteriaBuilder cb) {
|
|
|
+ Predicate predicate = cb.conjunction();
|
|
|
+ if(keyword != null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.like(a.<String>get("name"), "%" + StringUtils.trim(keyword) + "%")
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return predicate;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|