1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
37
38 @Service
39 public class LibraryServiceImpl implements LibraryService {
40
41
42 private static final Logger LOG = Logger.getLogger(LibraryServiceImpl.class);
43
44
45
46
47
48
49
50
51
52
53 @Autowired
54 @Qualifier("libraryDao")
55 private LibraryDao dao;
56
57
58
59
60
61
62 @Override
63
64 public List<Library> getLibraries() {
65 return dao.getLibraries();
66 }
67
68
69
70
71
72 @Override
73
74 public Library getLibrary(long libraryId) {
75 Library lib = dao.getLibrary(libraryId);
76
77 return lib;
78 }
79
80
81
82
83
84 @Override
85 public Library saveLibrary(Library library) {
86 this.clearLibraryCache();
87 return this.dao.saveLibrary(library);
88 }
89
90
91
92
93
94 @Override
95
96 public LibraryHourSet getDisplayableHoursSet(LibraryHourSet hourSet) {
97 List<LibraryHour> returnHours = new ArrayList<LibraryHour>();
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){
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
129
130
131
132
133
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;
158 }
159 }
160 }
161 return count;
162 }
163
164
165
166
167
168 @Override
169
170 public Map<String,List<Library>> getLibrariesByCampus(){
171
172 Map<String,List<Library>> libraryMap = new HashMap<String, List<Library>>();
173
174
175 List<String> campuses = this.dao.getCampusWithLibraries();
176 if (campuses == null){
177 return libraryMap;
178 }
179
180
181 for(String campus : campuses){
182
183 List<Library> libraries = this.dao.getLibariesForCampus(campus);
184 libraryMap.put(campus, libraries);
185 }
186 return libraryMap;
187 }
188
189
190
191 public boolean clearLibraryCache(){
192 return true;
193 }
194
195
196
197
198
199 @Override
200 public List<LibraryHourSet> getLibraryHourSets(long libraryId) {
201 return this.dao.getLibraryHourSets(libraryId);
202 }
203
204
205
206
207
208
209 public LibraryHourSet saveLibraryHourSets(LibraryHourSet lhs) {
210 return this.dao.saveLibraryHourSets(lhs);
211 }
212
213
214
215
216
217
218 public void setDao(LibraryDao dao) {
219 this.dao = dao;
220 }
221
222
223
224
225
226 public LibraryDao getDao() {
227 return dao;
228 }
229
230
231
232
233 @Override
234 public List<String> getCampusWithLibraries() {
235 return this.dao.getCampusWithLibraries();
236 }
237
238
239
240
241 @Override
242 public LibraryHourPeriod saveLibraryHourPeriod(LibraryHourPeriod libraryHourPeriod) {
243 return this.dao.saveLibraryHourPeriod(libraryHourPeriod);
244 }
245 }