View Javadoc
1   /**
2    * Copyright 2011 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  package org.kuali.mobility.campus.service;
16  
17  import org.apache.commons.lang.StringUtils;
18  import org.kuali.mobility.campus.entity.Campus;
19  import org.springframework.stereotype.Service;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import javax.ws.rs.GET;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.QueryParam;
29  
30  
31  /**
32   * A service for doing the actual work of interacting with Campus objects.
33   * @author Kuali Mobility Team (mobility.collab@kuali.org)
34   * @since 1.0.0
35   */
36  @Service
37  public class CampusServiceImpl implements CampusService {
38  
39  	/**
40  	 * List of configured campuses
41  	 */
42  	private List<Campus> campuses;
43  	
44  	/**
45  	 * Map of campus code to campus object
46  	 */
47  	private Map<String, Campus> campusCodeMap = new HashMap<String, Campus>();
48  
49  	/*
50  	 * (non-Javadoc)
51  	 * @see org.kuali.mobility.campus.service.CampusService#findCampusesByTool(java.lang.String)
52  	 */
53  	public List<Campus> findCampusesByTool(String toolName) {
54  		List<Campus> toolCampuses = new ArrayList<Campus>();
55  
56  		for (Iterator<Campus> iterator = campuses.iterator(); iterator.hasNext();) {
57  			Campus campus = iterator.next();
58  			if (campus.getTools().contains(toolName)) {
59  				toolCampuses.add(campus);
60  			}
61  		}
62  		return toolCampuses;
63  	}
64  
65  	/*
66  	 * (non-Javadoc)
67  	 * @see org.kuali.mobility.campus.service.CampusService#needToSelectDifferentCampusForTool(java.lang.String, java.lang.String)
68  	 */
69  	public boolean needToSelectDifferentCampusForTool(String tool, String campus) {
70  		List<Campus> campuses = findCampusesByTool(tool);
71  		boolean needDifferentCampus = true;
72  		if (campuses != null && !campuses.isEmpty()) {
73  			for (Campus foundCampus : campuses) {
74  				if (foundCampus.getCode().equals(campus)) {
75  					needDifferentCampus = false;
76  				}
77  			}
78  		}
79  		return needDifferentCampus;
80  	}
81  
82  	/*
83  	 * (non-Javadoc)
84  	 * @see org.kuali.mobility.campus.service.CampusService#setCampuses(java.util.List)
85  	 */
86  	public void setCampuses(List<Campus> campuses) {
87  		this.campuses = campuses;
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * @see org.kuali.mobility.campus.service.CampusService#getCampuses()
93  	 */
94  	@GET
95      @Path("/campuses")
96  	public List<Campus> getCampuses() {
97  		return this.campuses;
98  	}
99  
100 	/* (non-Javadoc)
101 	 * @see org.kuali.mobility.campus.service.CampusService#getCampusByCode(java.lang.String)
102 	 */
103 	@Override
104 	public Campus getCampusByCode(String campusCode) {
105 		
106 		// If empty campus code, return null
107 		// TODO or PUBLIC campus??
108 		if(StringUtils.isEmpty(campusCode)){
109 			return null;
110 		}
111 		
112 		// If there is no campuses
113 		if (this.campuses == null || this.campuses.size() == 0){
114 			return null;
115 		}
116 		
117 		// Find in a cached map first
118 		if (campusCodeMap.containsKey(campusCode)){
119 			return campusCodeMap.get(campusCode);
120 		}
121 		
122 		// Find the entry in the list and cache in a map
123 		for(Campus campus : this.campuses){
124 			// ! We already checked campusCode is not null
125 			if (campusCode.equals(campus.getCode())){
126 				this.campusCodeMap.put(campusCode, campus);
127 				return campus;
128 			}
129 		}
130 		// We could not find such a campus
131 		return null;
132 	}
133 }