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.class2.courseoffering.service.impl;
18  
19  import org.apache.commons.lang.StringUtils;
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.r2.common.constants.CommonServiceConstants;
26  import org.kuali.student.r2.common.util.ContextUtils;
27  import org.kuali.student.r2.core.organization.dto.OrgInfo;
28  import org.kuali.student.r2.core.organization.service.OrganizationService;
29  
30  import javax.xml.namespace.QName;
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.Map;
34  
35  
36  /**
37   * This class //TODO ...
38   *
39   * @author Kuali Student Team
40   */
41  public class OrganizationInfoLookupableImpl extends LookupableImpl {
42      private OrganizationService organizationService;
43  
44      @Override
45      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
46          List<OrgInfo> results = new ArrayList<OrgInfo>();
47  
48  
49          String shortName = fieldValues.get("shortName");
50          String longName = fieldValues.get("longName");
51  
52          QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
53          if (StringUtils.isNotBlank(longName) && !longName.isEmpty()) {
54              if (StringUtils.isNotBlank(shortName) && !shortName.isEmpty()){
55                  qBuilder.setPredicates(PredicateFactory.or(
56                          PredicateFactory.like("longName","*"+ fieldValues.get("longName")+ "*"),
57                          PredicateFactory.like("shortName","*" + fieldValues.get("shortName") + "*")));
58              }
59              else {
60                  qBuilder.setPredicates(PredicateFactory.like("longName","*"+ fieldValues.get("longName")+ "*"));
61              }
62          }else if (StringUtils.isNotBlank(shortName) && !shortName.isEmpty()){
63              qBuilder.setPredicates(PredicateFactory.like("shortName","*" + fieldValues.get("shortName") + "*"));
64          }
65          try {
66              QueryByCriteria query = qBuilder.build();
67  
68              OrganizationService  organizationService = getOrganizationService();
69  
70              List<OrgInfo> orgInfos = organizationService.searchForOrgs(query, ContextUtils.createDefaultContextInfo());
71              if (!orgInfos.isEmpty()){
72                  results.addAll(orgInfos);
73              }
74          } catch (Exception e) {
75              throw new RuntimeException("Error Performing Search", e); //To change body of catch statement use File | Settings | File Templates.
76          }
77          return results;
78      }
79  
80      private OrganizationService getOrganizationService(){
81          if(organizationService == null) {
82              organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "organization", "OrganizationService"));
83  
84          }
85          return organizationService;
86  
87      }
88  }