3d6ce38a5d2c2b1bf7792e450bca78378d14e504
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.repo;
2
3 import org.springframework.data.domain.Example;
4 import org.springframework.data.domain.ExampleMatcher;
5 import org.springframework.data.domain.Page;
6 import org.springframework.data.domain.PageRequest;
7 import org.springframework.stereotype.Repository;
8
9 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
10 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
11 import com.supwisdom.institute.backend.system.domain.entity.Config;
12
13 import java.util.Map;
14 import java.util.Optional;
15
16 /**
17  * @author loie
18  */
19 @Repository
20 public interface ConfigRepository extends BaseJpaRepository<Config> {
21   
22   public default Page<Config> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
23     Config probe = new Config();
24     if (mapBean != null) {
25       probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
26       probe.setCategoryCode(MapBeanUtils.getString(mapBean, "categoryCode"));
27       probe.setCategoryName(MapBeanUtils.getString(mapBean, "categoryName"));
28       probe.setName(MapBeanUtils.getString(mapBean, "name"));
29       probe.setDescription(MapBeanUtils.getString(mapBean, "description"));
30       probe.setConfigKey(MapBeanUtils.getString(mapBean, "configKey"));
31       probe.setEditable(MapBeanUtils.getBoolean(mapBean, "editable"));
32     }
33     
34     ExampleMatcher matcher = ExampleMatcher.matching()
35         .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
36         .withMatcher("categoryCode", ExampleMatcher.GenericPropertyMatchers.exact())
37         .withMatcher("categoryName", ExampleMatcher.GenericPropertyMatchers.contains())
38         .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
39         .withMatcher("description", ExampleMatcher.GenericPropertyMatchers.contains())
40         .withMatcher("configKey", ExampleMatcher.GenericPropertyMatchers.exact())
41         .withMatcher("editable", ExampleMatcher.GenericPropertyMatchers.exact())
42     ;
43     
44     if (loadAll) {
45       pageIndex = 0;
46       pageSize = Integer.MAX_VALUE;
47     }
48     
49     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
50     Example<Config> example = Example.of(probe, matcher);
51     
52     Page<Config> page = this.findAll(example, pageRequest);
53     
54     return page;
55   }
56
57   public default Config selectByCategoryKey(String categoryCode, String configKey) {
58     Config probe = new Config();
59     
60     probe.setDeleted(false);
61     probe.setCategoryCode(categoryCode);
62     probe.setConfigKey(configKey);
63     
64     ExampleMatcher matcher = ExampleMatcher.matching()
65         .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
66         .withMatcher("categoryCode", ExampleMatcher.GenericPropertyMatchers.exact())
67         .withMatcher("configKey", ExampleMatcher.GenericPropertyMatchers.exact())
68     ;
69     
70     Example<Config> example = Example.of(probe, matcher);
71     
72     Optional<Config> config = this.findOne(example);
73     
74     return config.isPresent() ? config.get() : null;
75   }
76
77 }