View Javadoc

1   /**
2    * Copyright 2011-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  
16  package org.kuali.mobility.library.service;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.log4j.Logger;
24  import org.kuali.mobility.library.dao.LibraryDao;
25  import org.kuali.mobility.library.entity.Library;
26  import org.kuali.mobility.library.entity.LibraryHour;
27  import org.kuali.mobility.library.entity.LibraryHourPeriod;
28  import org.kuali.mobility.library.entity.LibraryHourSet;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.beans.factory.annotation.Qualifier;
31  import org.springframework.stereotype.Service;
32  
33  /**
34   * Implementation of the <code>LibraryService</code>
35   * @author Kuali Mobility Team (mobility.collab@kuali.org)
36   * @since 2.3.0
37   */
38  @Service
39  public class LibraryServiceImpl implements LibraryService {
40  
41  	/** A reference to a logger */
42  	private static final Logger LOG = Logger.getLogger(LibraryServiceImpl.class);
43  
44  	/**
45  	 * A reference to the <code>CacheManager</code>.
46  	 */
47  	//@Autowired
48  	//private CacheManager cacheManager;
49  
50  	/**
51  	 * Data Access Object for Libraries.
52  	 */
53  	@Autowired
54  	@Qualifier("libraryDao")
55  	private LibraryDao dao;
56  
57  
58  	/*
59  	 * (non-Javadoc)
60  	 * @see org.kuali.mobility.library.service.LibraryService#getLibraries()
61  	 */
62  	@Override
63  	//@Cacheable(value="libraryCache")
64  	public List<Library> getLibraries() {
65  		return dao.getLibraries();
66  	}
67  
68  	/*
69  	 * (non-Javadoc)
70  	 * @see org.kuali.mobility.library.service.LibraryService#getLibrary(long)
71  	 */
72  	@Override
73  	//@Cacheable(value="libraryCache", key="#libraryId")  //TODO: Sort out this caching issue with optimistic locking.
74  	public Library getLibrary(long libraryId) {
75  		Library lib = dao.getLibrary(libraryId);
76  		// TODO sort the hoursets and hours
77  		return lib;
78  	}
79  
80  	/*
81  	 * (non-Javadoc)
82  	 * @see org.kuali.mobility.library.service.LibraryService#saveLibrary(org.kuali.mobility.library.entity.Library)
83  	 */
84  	@Override
85  	public Library saveLibrary(Library library) {
86  		this.clearLibraryCache();
87  		return this.dao.saveLibrary(library);
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * @see org.kuali.mobility.library.service.LibraryService#getDisplayableHoursSet(org.kuali.mobility.library.entity.LibraryHourSet)
93  	 */
94  	@Override
95  	//@Cacheable(value="libraryCache", key="#hourSet")
96  	public LibraryHourSet getDisplayableHoursSet(LibraryHourSet hourSet) {
97  		List<LibraryHour> returnHours = new ArrayList<LibraryHour>(); // LinkedHashSet for we need to order preserved
98  		List<LibraryHour> hours = new ArrayList<LibraryHour>(hourSet.getHours());
99  		LibraryHour lh;
100 		int weekSpan = dayOfWeekSpan(hours);
101 		int index = 0;
102 		if (weekSpan >= 4){ // At least till Thursday
103 			lh = new LibraryHour();
104 			lh.setDisplayLabel("library.hoursSpan." + weekSpan);
105 			lh.setFromTime(hours.get(0).getFromTime());
106 			lh.setToTime(hours.get(0).getToTime());
107 			lh.setDayOfWeek(hours.get(0).getDayOfWeek());
108 			returnHours.add(lh);
109 			index = weekSpan;
110 		}else {
111 			index = 0;
112 		}
113 		for ( ; index < hours.size() ; index ++){
114 			lh = new LibraryHour();
115 			lh.setDisplayLabel("library.hours." + (index+1));
116 			lh.setFromTime(hours.get(index).getFromTime());
117 			lh.setToTime(hours.get(index).getToTime());
118 			lh.setDayOfWeek(hours.get(index).getDayOfWeek());
119 			returnHours.add(lh);
120 		}
121 		LibraryHourSet lhs = new LibraryHourSet();
122 		lhs.setPeriod(hourSet.getPeriod());
123 		lhs.setHours(returnHours);
124 		return lhs;
125 	}
126 
127 	/**
128 	 * Figures out whether the opening and closing time is the same spanning a
129 	 * few days so it does not have to happen in the jsp
130 	 * Just check Monday (0) to Friday (4) if the from and to are the same
131 	 * returning the range of how many it share the same times.
132 	 * @param libraryHour
133 	 * @return The index of the last day in the week - 6 = Sunday
134 	 */
135 	private final int dayOfWeekSpan(List<LibraryHour> libraryHours) {
136 		int count = 0;
137 		LibraryHour lh = new LibraryHour();
138 		if(libraryHours != null) {
139 			for (LibraryHour libHour : libraryHours) {
140 				if (count > 0) {
141 					if ((libHour.getFromTime() == null || libHour.getToTime() == null)){
142 						return count;
143 					}
144 					if ((libHour.getFromTime() != null && libHour.getToTime() != null)){
145 						if(lh.getFromTime() == null || lh.getToTime() == null){
146 							return count;
147 						}
148 						if (lh.getFromTime().compareTo(libHour.getFromTime()) != 0
149 								|| (lh.getToTime().compareTo(libHour.getToTime()) != 0)) {
150 							return count;
151 						}
152 					}
153 				}
154 				count++;
155 				lh = libHour;
156 				if(count == 7){
157 					break; // Don't go past Sunday
158 				}
159 			}
160 		}
161 		return count; // Day of week to index
162 	}
163 
164 	/*
165 	 * (non-Javadoc)
166 	 * @see org.kuali.mobility.library.service.LibraryService#getLibrariesByCampus()
167 	 */
168 	@Override
169 	//@Cacheable(value="libraryCache", key="#campusCode")
170 	public Map<String,List<Library>> getLibrariesByCampus(){
171 
172 		Map<String,List<Library>> libraryMap = new HashMap<String, List<Library>>();
173 
174 		// Get campuses with libraries
175 		List<String> campuses = this.dao.getCampusWithLibraries();
176 		if (campuses == null){
177 			return libraryMap; // Map is still empty
178 		}
179 
180 		// Loop through each campus and add their libraries to a map
181 		for(String campus : campuses){
182 			// The list should not be null, since we queried for campuses with libraries
183 			List<Library> libraries = this.dao.getLibariesForCampus(campus);
184 			libraryMap.put(campus, libraries);
185 		}
186 		return libraryMap;
187 	}
188 
189 	// evicts the cache is called from the save method.
190 	//@CacheEvict(value = "libraryCache", allEntries=true)
191 	public boolean clearLibraryCache(){
192 		return true;
193 	}
194 
195 	/*
196 	 * (non-Javadoc)
197 	 * @see org.kuali.mobility.library.service.LibraryService#getLibraryHourSets(long)
198 	 */
199 	@Override
200 	public List<LibraryHourSet> getLibraryHourSets(long libraryId) {
201 		return this.dao.getLibraryHourSets(libraryId);
202 	}
203 
204 
205 	/*
206 	 * (non-Javadoc)
207 	 * @see org.kuali.mobility.library.service.LibraryService#saveLibraryHourSets(org.kuali.mobility.library.entity.LibraryHourSet)
208 	 */
209 	public LibraryHourSet saveLibraryHourSets(LibraryHourSet lhs) {
210 		return this.dao.saveLibraryHourSets(lhs);
211 	}
212 
213 
214 	/**
215 	 * Sets the reference to the <code>LibraryDao</code>.
216 	 * @param dao The reference to the <code>LibraryDao</code>.
217 	 */
218 	public void setDao(LibraryDao dao) {
219 		this.dao = dao;
220 	}
221 
222 	/**
223 	 * Gets the reference to the <code>LibraryDao</code>.
224 	 * @returns The reference to the <code>LibraryDao</code>.
225 	 */
226 	public LibraryDao getDao() {
227 		return dao;
228 	}
229 
230 	/* (non-Javadoc)
231 	 * @see org.kuali.mobility.library.service.LibraryService#getCampusWithLibraries()
232 	 */
233 	@Override
234 	public List<String> getCampusWithLibraries() {
235 		return this.dao.getCampusWithLibraries();
236 	}
237 
238 	/* (non-Javadoc)
239 	 * @see org.kuali.mobility.library.service.LibraryService#saveLibraryHourPeriod(org.kuali.mobility.library.entity.LibraryHourPeriod)
240 	 */
241 	@Override
242 	public LibraryHourPeriod saveLibraryHourPeriod(LibraryHourPeriod libraryHourPeriod) {
243 		return this.dao.saveLibraryHourPeriod(libraryHourPeriod);
244 	}
245 }