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