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.PredicateFactory;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24  import org.kuali.rice.krad.lookup.LookupableImpl;
25  import org.kuali.rice.krad.web.form.LookupForm;
26  import org.kuali.student.r2.common.constants.CommonServiceConstants;
27  import org.kuali.student.r2.common.dto.ContextInfo;
28  import org.kuali.student.r2.core.constants.HoldServiceConstants;
29  import org.kuali.student.r2.common.util.constants.OrganizationServiceConstants;
30  import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
31  import org.kuali.student.r2.core.hold.service.HoldService;
32  import org.kuali.student.r2.core.organization.dto.OrgInfo;
33  import org.kuali.student.r2.core.organization.service.OrganizationService;
34  
35  import javax.xml.namespace.QName;
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  
40  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
41  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
42  import static org.kuali.rice.core.api.criteria.PredicateFactory.like;
43  
44  
45  /**
46   * This class //TODO ...
47   *
48   * @author Kuali Student Team
49   */
50  public class HoldIssueInfoLookupableImpl extends LookupableImpl {
51      private HoldService holdService;
52      ContextInfo contextInfo = new ContextInfo();
53  
54      @Override
55      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
56          List<HoldIssueInfo> results = new ArrayList<HoldIssueInfo>();
57          QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
58          List<Predicate> pList = new ArrayList<Predicate>();
59          Predicate p;
60  
61          String type = fieldValues.get("typeKey");
62          String name = fieldValues.get("name");
63          String state = fieldValues.get("stateKey");
64          String orgId = fieldValues.get("organizationId");
65          String descr = fieldValues.get("descr.plain");
66          qBuilder.setPredicates();
67          if (StringUtils.isNotBlank(name)){
68              p = like("name", "%" + name + "%");
69              pList.add(p);
70          }
71  
72          if (StringUtils.isNotBlank(type)){
73              p = like("holdIssueType", "%" + type + "%");
74              pList.add(p);
75          }
76  
77          if (StringUtils.isNotBlank(state)){
78              p = equal("holdIssueState", state);
79              pList.add(p);
80          }
81  
82          if (StringUtils.isNotBlank(orgId)){
83              p = equal("organizationId", orgId);
84              pList.add(p);
85          }
86  
87          if (StringUtils.isNotBlank(descr)){
88              p = like("descrPlain", "%" + descr + "%");
89              pList.add(p);
90          }
91  
92          if (!pList.isEmpty()){
93              Predicate[] preds = new Predicate[pList.size()];
94              pList.toArray(preds);
95              qBuilder.setPredicates(and(preds));
96          }
97  
98          try {
99              QueryByCriteria query = qBuilder.build();
100 
101             HoldService holdService = getHoldService();
102 
103 
104             List<HoldIssueInfo> holdIssueInfos = holdService.searchForHoldIssues(query, getContextInfo());
105             if (!holdIssueInfos.isEmpty()){
106                 results.addAll(holdIssueInfos);
107             }
108         } catch (Exception e) {
109             e.printStackTrace();
110             throw new RuntimeException("Error Performing Search",e); //To change body of catch statement use File | Settings | File Templates.
111         }
112         return results;
113     }
114 
115 
116     protected HoldService getHoldService(){
117         if(holdService == null) {
118             holdService = (HoldService) GlobalResourceLoader.getService(new QName(HoldServiceConstants.NAMESPACE, HoldServiceConstants.SERVICE_NAME_LOCAL_PART));
119         }
120         return holdService;
121     }
122 
123     public ContextInfo getContextInfo() {
124         if (contextInfo == null){
125             contextInfo =  org.kuali.student.enrollment.common.util.ContextBuilder.loadContextInfo();
126         }
127         return contextInfo;
128     }
129 }