View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Adi Rajesh on 6/7/12
16   */
17  package org.kuali.student.enrollment.class1.hold.service;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.criteria.Predicate;
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.common.util.ContextBuilder;
26  import org.kuali.student.r2.common.dto.ContextInfo;
27  import org.kuali.student.r2.core.constants.HoldServiceConstants;
28  import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
29  import org.kuali.student.r2.core.hold.service.HoldService;
30  
31  import javax.xml.namespace.QName;
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Map;
35  
36  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
37  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
38  import static org.kuali.rice.core.api.criteria.PredicateFactory.like;
39  
40  
41  /**
42   * This class //TODO ...
43   *
44   * @author Kuali Student Team
45   */
46  public class HoldIssueInfoLookupableImpl extends LookupableImpl {
47      private HoldService holdService;
48      ContextInfo contextInfo = new ContextInfo();
49  
50      @Override
51      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
52          List<HoldIssueInfo> results = new ArrayList<HoldIssueInfo>();
53          QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
54          List<Predicate> pList = new ArrayList<Predicate>();
55          Predicate p;
56  
57          String type = fieldValues.get("typeKey");
58          String name = fieldValues.get("name");
59          String state = fieldValues.get("stateKey");
60          String orgId = fieldValues.get("organizationId");
61          String descr = fieldValues.get("descr.plain");
62          qBuilder.setPredicates();
63          if (StringUtils.isNotBlank(name)){
64              p = like("name", "%" + name + "%");
65              pList.add(p);
66          }
67  
68          if (StringUtils.isNotBlank(type)){
69              p = like("holdIssueType", "%" + type + "%");
70              pList.add(p);
71          }
72  
73          if (StringUtils.isNotBlank(state)){
74              p = equal("holdIssueState", state);
75              pList.add(p);
76          }
77  
78          if (StringUtils.isNotBlank(orgId)){
79              p = equal("organizationId", orgId);
80              pList.add(p);
81          }
82  
83          if (StringUtils.isNotBlank(descr)){
84              p = like("descrPlain", "%" + descr + "%");
85              pList.add(p);
86          }
87  
88          if (!pList.isEmpty()){
89              Predicate[] preds = new Predicate[pList.size()];
90              pList.toArray(preds);
91              qBuilder.setPredicates(and(preds));
92          }
93  
94          try {
95              QueryByCriteria query = qBuilder.build();
96  
97              HoldService holdService = getHoldService();
98  
99  
100             List<HoldIssueInfo> holdIssueInfos = holdService.searchForHoldIssues(query, getContextInfo());
101             if (!holdIssueInfos.isEmpty()){
102                 results.addAll(holdIssueInfos);
103             }
104         } catch (Exception e) {
105             e.printStackTrace();
106             throw new RuntimeException("Error Performing Search",e); //To change body of catch statement use File | Settings | File Templates.
107         }
108         return results;
109     }
110 
111 
112     protected HoldService getHoldService(){
113         if(holdService == null) {
114             holdService = (HoldService) GlobalResourceLoader.getService(new QName(HoldServiceConstants.NAMESPACE, HoldServiceConstants.SERVICE_NAME_LOCAL_PART));
115         }
116         return holdService;
117     }
118 
119     public ContextInfo getContextInfo() {
120         if (contextInfo == null){
121             contextInfo =  ContextBuilder.loadContextInfo();
122         }
123         return contextInfo;
124     }
125 }