1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.enrollment.class2.population.service.impl;
18
19 import org.kuali.rice.core.api.criteria.Predicate;
20 import org.kuali.rice.core.api.criteria.PredicateFactory;
21 import org.kuali.rice.core.api.criteria.QueryByCriteria;
22 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23 import org.kuali.rice.krad.lookup.LookupableImpl;
24 import org.kuali.rice.krad.web.form.LookupForm;
25 import org.kuali.student.enrollment.class2.population.dto.PopulationWrapper;
26 import org.kuali.student.r2.common.dto.ContextInfo;
27 import org.kuali.student.r2.common.util.ContextUtils;
28 import org.kuali.student.r2.core.constants.PopulationServiceConstants;
29 import org.kuali.student.r2.core.population.dto.PopulationInfo;
30 import org.kuali.student.r2.core.population.dto.PopulationRuleInfo;
31 import org.kuali.student.r2.core.population.service.PopulationService;
32
33 import javax.xml.namespace.QName;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37
38
39
40
41
42
43 public class PopulationWrapperLookupableImpl extends LookupableImpl {
44
45 private transient PopulationService populationService;
46
47 protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
48 List<PopulationWrapper> populationWrappers = new ArrayList<PopulationWrapper>();
49
50 ContextInfo context = ContextUtils.createDefaultContextInfo();
51
52 try {
53
54 QueryByCriteria qbc = buildQueryByCriteria(fieldValues);
55 List<PopulationInfo> populationInfoList = getPopulationService().searchForPopulations(qbc, context);
56
57
58 for (PopulationInfo populationInfo: populationInfoList) {
59 PopulationRuleInfo populationRuleInfo = getPopulationService().getPopulationRuleForPopulation(populationInfo.getId(), context);
60 PopulationWrapper wrapper = new PopulationWrapper();
61 wrapper.setPopulationRuleInfo(populationRuleInfo);
62 wrapper.setPopulationInfo(populationInfo);
63 wrapper.setId(populationInfo.getId());
64
65 wrapper.setPopulationRuleTypeKeyName(populationRuleInfo.getTypeKey().substring(populationRuleInfo.getTypeKey().lastIndexOf('.')+1));
66 wrapper.setPopulationStateKeyName(populationInfo.getStateKey().substring(populationInfo.getStateKey().lastIndexOf('.')+1));
67
68 populationWrappers.add(wrapper);
69 }
70 } catch (Exception e) {
71 throw new RuntimeException("PopulationWrapperLookupableImpl exception. ", e);
72 }
73
74 return populationWrappers;
75 }
76
77
78
79
80
81
82
83
84 private QueryByCriteria buildQueryByCriteria(Map<String, String> fieldValues){
85 String keyword = fieldValues.get("keyword");
86 String stateKey = fieldValues.get("populationInfo.stateKey");
87
88 keyword = keyword.isEmpty()?"*":keyword;
89
90 List<Predicate> predicates = new ArrayList<Predicate>();
91 predicates.add(PredicateFactory.or(PredicateFactory.like("name", "%"+keyword+"%"),
92 PredicateFactory.like("descrPlain", "%"+keyword+"%")));
93 if (stateKey.equals(PopulationServiceConstants.POPULATION_ACTIVE_STATE_KEY) ||
94 stateKey.equals(PopulationServiceConstants.POPULATION_INACTIVE_STATE_KEY)) {
95 predicates.add(PredicateFactory.and(PredicateFactory.equal("populationState", stateKey)));
96 }
97
98 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
99 qbcBuilder.setPredicates(predicates.toArray(new Predicate[predicates.size()]));
100
101 return qbcBuilder.build();
102 }
103
104 private PopulationService getPopulationService() {
105 if(populationService == null) {
106 populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationService"));
107 }
108 return this.populationService;
109 }
110
111 }