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 mharmath on 7/17/12
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   * This class performs lookups on Populations.
40   *
41   * @author Kuali Student Team
42   */
43  public class PopulationWrapperLookupableImpl extends LookupableImpl {
44      private static final long serialVersionUID = 1L;
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              //perform the lookup using the service
54              QueryByCriteria qbc = buildQueryByCriteria(fieldValues);
55              List<PopulationInfo> populationInfoList = getPopulationService().searchForPopulations(qbc, context);
56  
57              //Transform each PopulationInfo to the wrapper class
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                  //set display names
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       * Builds a QueryByCriteria based on the KRAD field values passed in.
79       * Performs fuzzy searching on the keyword field against the name and description fields on PopulationEntity
80       *
81       * @param fieldValues map of field names and values
82       * @return a criteria query
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; //search for all if empty
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     /**
112      * Determines if given data object has associated maintenance document that allows delete maintenance
113      * actions.
114      *
115      * @return boolean true if the maintenance delete action is allowed for the data object instance, false otherwise
116      */
117 
118     @Override
119     public boolean allowsMaintenanceDeleteAction(Object dataObject) {
120         // maintenance delete action not allowed
121         return false;
122     }
123 
124 }