View Javadoc
1   /**
2    * Copyright 2005-2014 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.QueryByCriteria;
22  import org.kuali.rice.core.api.criteria.QueryResults;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.kuali.rice.krad.data.DataObjectService;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.location.api.campus.Campus;
28  import org.kuali.rice.location.api.campus.CampusQueryResults;
29  import org.kuali.rice.location.api.campus.CampusService;
30  import org.kuali.rice.location.api.campus.CampusType;
31  import org.kuali.rice.location.api.campus.CampusTypeQueryResults;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  
38  import static java.util.Collections.singletonMap;
39  
40  public class CampusServiceImpl implements CampusService {
41      private DataObjectService dataObjectService;
42  	
43  	/**
44       * @see org.kuali.rice.location.api.campus.CampusService#getCampus(String code)
45       */
46  	@Override
47  	public Campus getCampus(String code) {
48  		if (StringUtils.isBlank(code)) {
49              throw new RiceIllegalArgumentException("code is blank");
50          }
51          CampusBo campusBo = getDataObjectService().find(CampusBo.class, code);
52          return CampusBo.to(campusBo);
53  	}
54  
55  	/**
56       * @see org.kuali.rice.location.api.campus.CampusService#findAllCampuses
57       */
58  	@Override
59  	public List<Campus> findAllCampuses() {
60          QueryByCriteria qbc = QueryByCriteria.Builder.forAttribute("active", true).build();
61          QueryResults<CampusBo> campusBos = getDataObjectService().findMatching(CampusBo.class,qbc);
62          return this.convertListOfCampusBosToImmutables(campusBos.getResults());
63  	}
64  
65  	/**
66       * @see org.kuali.rice.location.api.campus.CampusService#getCampusType(String code)
67       */
68  	@Override
69  	public CampusType getCampusType(String code) {
70  		if (StringUtils.isBlank(code)) {
71              throw new RiceIllegalArgumentException("code is blank");
72          }
73          CampusTypeBo campusTypeBo = dataObjectService.find(CampusTypeBo.class, code);
74          return CampusTypeBo.to(campusTypeBo);
75  	}
76  
77  	/**
78       * @see org.kuali.rice.location.api.campus.CampusService#findAllCampusTypes
79       */
80  	@Override
81  	public List<CampusType> findAllCampusTypes() {
82          QueryByCriteria qbc = QueryByCriteria.Builder.forAttribute("active", true).build();
83          QueryResults<CampusTypeBo> campusTypeBos = dataObjectService.findMatching(CampusTypeBo.class,qbc);
84  		return this.convertListOfCampusTypesBosToImmutables(campusTypeBos.getResults());
85  	}
86  
87      @Override
88      public CampusQueryResults findCampuses(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
89          incomingParamCheck(queryByCriteria, "queryByCriteria");
90  
91          QueryResults<CampusBo> results = dataObjectService.findMatching(CampusBo.class, queryByCriteria);
92  
93          CampusQueryResults.Builder builder = CampusQueryResults.Builder.create();
94          builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
95          builder.setTotalRowCount(results.getTotalRowCount());
96  
97          final List<Campus.Builder> ims = new ArrayList<Campus.Builder>();
98          for (CampusBo bo : results.getResults()) {
99              ims.add(Campus.Builder.create(bo));
100         }
101 
102         builder.setResults(ims);
103         return builder.build();
104     }
105 
106     @Override
107     public CampusTypeQueryResults findCampusTypes(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
108         incomingParamCheck(queryByCriteria, "query");
109 
110         QueryResults<CampusTypeBo> results = dataObjectService.findMatching(CampusTypeBo.class, queryByCriteria);
111 
112         CampusTypeQueryResults.Builder builder = CampusTypeQueryResults.Builder.create();
113         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
114         builder.setTotalRowCount(results.getTotalRowCount());
115 
116         final List<CampusType.Builder> ims = new ArrayList<CampusType.Builder>();
117         for (CampusTypeBo bo : results.getResults()) {
118             ims.add(CampusType.Builder.create(bo));
119         }
120 
121         builder.setResults(ims);
122         return builder.build();
123     }
124 
125     private List<Campus> convertListOfCampusBosToImmutables(List<CampusBo> campusBos) {
126         ArrayList<Campus> campuses = new ArrayList<Campus>();
127         for (CampusBo bo : campusBos) {
128             Campus campus = CampusBo.to(bo);
129             campuses.add(campus) ;
130         }
131         return Collections.unmodifiableList(campuses);
132     }
133     
134     private List<CampusType> convertListOfCampusTypesBosToImmutables(List<CampusTypeBo> campusTypeBos) {
135         ArrayList<CampusType> campusTypes = new ArrayList<CampusType>();
136         for (CampusTypeBo bo : campusTypeBos) {
137             CampusType campusType = CampusTypeBo.to(bo);
138             campusTypes.add(campusType) ;
139         }
140         return Collections.unmodifiableList(campusTypes);
141     }
142 
143     private void incomingParamCheck(Object object, String name) {
144         if (object == null) {
145             throw new RiceIllegalArgumentException(name + " was null");
146         } else if (object instanceof String
147                 && StringUtils.isBlank((String) object)) {
148             throw new RiceIllegalArgumentException(name + " was blank");
149         }
150     }
151 
152     public DataObjectService getDataObjectService() {
153         return dataObjectService;
154     }
155 
156     @Required
157     public void setDataObjectService(DataObjectService dataObjectService) {
158         this.dataObjectService = dataObjectService;
159     }
160 }