View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.location.impl.campus;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
20  import org.kuali.rice.core.api.criteria.GenericQueryResults;
21  import org.kuali.rice.core.api.criteria.LookupCustomizer;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.kuali.rice.kim.api.group.GroupQueryResults;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.location.api.campus.Campus;
27  import org.kuali.rice.location.api.campus.CampusQueryResults;
28  import org.kuali.rice.location.api.campus.CampusService;
29  import org.kuali.rice.location.api.campus.CampusType;
30  import org.kuali.rice.location.api.campus.CampusTypeQueryResults;
31  
32  import javax.jws.WebParam;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
36  
37  import static java.util.Collections.singletonMap;
38  
39  public class CampusServiceImpl implements CampusService {
40  
41  	private BusinessObjectService boService;
42      private CriteriaLookupService criteriaLookupService;
43  	
44  	/**
45       * @see org.kuali.rice.location.api.campus.CampusService#getCampus(String code)
46       */
47  	@Override
48  	public Campus getCampus(String code) {
49  		if (StringUtils.isBlank(code)) {
50              throw new RiceIllegalArgumentException("code is blank");
51          }
52  
53          CampusBo campusBo = boService.findByPrimaryKey(CampusBo.class, singletonMap("code", code));
54  
55          return CampusBo.to(campusBo);
56  	}
57  
58  	/**
59       * @see org.kuali.rice.location.api.campus.CampusService#findAllCampuses
60       */
61  	@Override
62  	public List<Campus> findAllCampuses() {
63  		List<CampusBo> campusBos = (List<CampusBo>) boService.findMatching(CampusBo.class, singletonMap("active", "true"));
64  
65          return this.convertListOfCampusBosToImmutables(campusBos);
66  	}
67  
68  	/**
69       * @see org.kuali.rice.location.api.campus.CampusService#getCampusType(String code)
70       */
71  	@Override
72  	public CampusType getCampusType(String code) {
73  		if (StringUtils.isBlank(code)) {
74              throw new RiceIllegalArgumentException("code is blank");
75          }
76  
77  		CampusTypeBo campusTypeBo = boService.findByPrimaryKey(CampusTypeBo.class, singletonMap("code", code));
78  
79          return CampusTypeBo.to(campusTypeBo);
80  	}
81  
82  	/**
83       * @see org.kuali.rice.location.api.campus.CampusService#findAllCampusTypes
84       */
85  	@Override
86  	public List<CampusType> findAllCampusTypes() {
87  		List<CampusTypeBo> campusTypeBos = (List<CampusTypeBo>)boService.findMatching(CampusTypeBo.class, singletonMap("active", "true"));
88  		return this.convertListOfCampusTypesBosToImmutables(campusTypeBos);
89  	}
90  
91      @Override
92      public CampusQueryResults findCampuses(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
93          incomingParamCheck(queryByCriteria, "queryByCriteria");
94  
95          GenericQueryResults<CampusBo> results = criteriaLookupService.lookup(CampusBo.class, queryByCriteria);
96  
97          CampusQueryResults.Builder builder = CampusQueryResults.Builder.create();
98          builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
99          builder.setTotalRowCount(results.getTotalRowCount());
100 
101         final List<Campus.Builder> ims = new ArrayList<Campus.Builder>();
102         for (CampusBo bo : results.getResults()) {
103             ims.add(Campus.Builder.create(bo));
104         }
105 
106         builder.setResults(ims);
107         return builder.build();
108     }
109 
110     @Override
111     public CampusTypeQueryResults findCampusTypes(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
112         incomingParamCheck(queryByCriteria, "query");
113 
114         GenericQueryResults<CampusTypeBo> results = criteriaLookupService.lookup(CampusTypeBo.class, queryByCriteria);
115 
116         CampusTypeQueryResults.Builder builder = CampusTypeQueryResults.Builder.create();
117         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
118         builder.setTotalRowCount(results.getTotalRowCount());
119 
120         final List<CampusType.Builder> ims = new ArrayList<CampusType.Builder>();
121         for (CampusTypeBo bo : results.getResults()) {
122             ims.add(CampusType.Builder.create(bo));
123         }
124 
125         builder.setResults(ims);
126         return builder.build();
127     }
128 
129     public void setBusinessObjectService(BusinessObjectService boService) {
130         this.boService = boService;
131     }
132 
133     private List<Campus> convertListOfCampusBosToImmutables(List<CampusBo> campusBos) {
134         ArrayList<Campus> campuses = new ArrayList<Campus>();
135         for (CampusBo bo : campusBos) {
136             Campus campus = CampusBo.to(bo);
137             campuses.add(campus) ;
138         }
139         return Collections.unmodifiableList(campuses);
140     }
141     
142     private List<CampusType> convertListOfCampusTypesBosToImmutables(List<CampusTypeBo> campusTypeBos) {
143         ArrayList<CampusType> campusTypes = new ArrayList<CampusType>();
144         for (CampusTypeBo bo : campusTypeBos) {
145             CampusType campusType = CampusTypeBo.to(bo);
146             campusTypes.add(campusType) ;
147         }
148         return Collections.unmodifiableList(campusTypes);
149     }
150 
151     private void incomingParamCheck(Object object, String name) {
152         if (object == null) {
153             throw new RiceIllegalArgumentException(name + " was null");
154         } else if (object instanceof String
155                 && StringUtils.isBlank((String) object)) {
156             throw new RiceIllegalArgumentException(name + " was blank");
157         }
158     }
159 
160     /**
161      * Sets the criteriaLookupService attribute value.
162      *
163      * @param criteriaLookupService The criteriaLookupService to set.
164      */
165     public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
166         this.criteriaLookupService = criteriaLookupService;
167     }
168 }