1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 package com.buckosoft.BuckoVidLib.business;
107
108 import java.util.ArrayList;
109 import java.util.List;
110 import java.util.regex.Pattern;
111
112 import javax.servlet.http.HttpServletRequest;
113
114 import org.apache.commons.logging.Log;
115 import org.apache.commons.logging.LogFactory;
116 import org.springframework.beans.factory.annotation.Autowired;
117 import org.springframework.stereotype.Component;
118
119 import com.buckosoft.BSAccount.BSAccountMan;
120 import com.buckosoft.BuckoVidLib.db.Database;
121 import com.buckosoft.BuckoVidLib.domain.Actor;
122 import com.buckosoft.BuckoVidLib.domain.Director;
123 import com.buckosoft.BuckoVidLib.domain.FailedToRip;
124 import com.buckosoft.BuckoVidLib.domain.LibrarySection;
125 import com.buckosoft.BuckoVidLib.domain.TVSeason;
126 import com.buckosoft.BuckoVidLib.domain.User;
127 import com.buckosoft.BuckoVidLib.domain.UserAttribute;
128 import com.buckosoft.BuckoVidLib.domain.UserWebSession;
129 import com.buckosoft.BuckoVidLib.domain.Video;
130 import com.buckosoft.BuckoVidLib.domain.VideoBase;
131 import com.buckosoft.BuckoVidLib.domain.WishList;
132 import com.buckosoft.BuckoVidLib.domain.Writer;
133 import com.buckosoft.BuckoVidLib.domain.rest.RestVideo;
134 import com.buckosoft.BuckoVidLib.domain.rest.admin.RestRefreshStatus;
135 import com.buckosoft.BuckoVidLib.util.ConfigManager;
136 import com.buckosoft.BuckoVidLib.util.DomainConverter;
137 import com.buckosoft.BuckoVidLib.web.ImageController;
138 import com.buckosoft.BuckoVidLib.web.plex.client.RestClientConstants;
139
140
141
142
143
144 @Component
145 public class BuckoVidLibImpl implements BuckoVidLib {
146 private final Log log = LogFactory.getLog(getClass());
147
148 static String plexUrl = null;
149
150 @Autowired
151 private Database db;
152
153 @Autowired
154 private LibraryManager libraryManager;
155
156 @Autowired
157 BSAccountMan bsAccountMan;
158
159
160 BuckoVidLibImpl() {
161 log.info("Boot...");
162 }
163 @Override
164 public List<LibrarySection> getSectionList() {
165 return(libraryManager.getSectionList());
166 }
167 @Override
168 public List<LibrarySection> getSectionList(boolean allowRestricted) {
169 return(libraryManager.getSectionList(allowRestricted));
170 }
171
172 @Override
173 public RestVideo getRandomMovie(boolean allowRestricted) {
174 VideoBase v = libraryManager.getRandomMovie(allowRestricted);
175 if (v == null)
176 return(null);
177 return(DomainConverter.toRestVideo(v));
178
179 }
180
181
182
183
184 @Override
185 public VideoBase getVideoBaseFromKey(int key, boolean allowRestricted) {
186 return(libraryManager.getVideoBaseFromKey(key, allowRestricted));
187 }
188
189
190
191 @Override
192 public Video getVideoFromKey(int key, boolean allowRestricted) {
193 return(libraryManager.getVideoFromKey(key, allowRestricted));
194 }
195 @Override
196 public List<FailedToRip> getFailedToRip() {
197 return(db.getFailedToRip());
198 }
199
200
201
202
203 @Override
204 public List<WishList> getWishList() {
205 return(db.getWishList());
206 }
207
208
209
210
211 @Override
212 public List<VideoBase> getRecentlyAdded(int count, boolean allowRestricted) {
213 return(libraryManager.getNewestVideos(count, allowRestricted));
214 }
215
216
217
218
219 @Override
220 public List<RestVideo> findVideosAndThings(String key, boolean allowRestricted, int maxReturned) {
221 Pattern p = Pattern.compile("(?i).*"+key + ".*");
222 List<RestVideo> rvl = new ArrayList<RestVideo>();
223 List<VideoBase> vbl = libraryManager.findVideos(p, allowRestricted, maxReturned);
224 for (VideoBase vb : vbl) {
225 rvl.add(DomainConverter.toRestVideo(vb));
226 }
227 int remaining = maxReturned - rvl.size();
228 if (remaining > 1) {
229 List<Actor> la = libraryManager.findActors(key, remaining-1);
230 if (!la.isEmpty()) {
231 remaining--;
232 if (la.size() == 1)
233 rvl.add(new RestVideo("Actor", RestVideo.STYPE_MARKER, RestVideo.STYPE_ACTOR));
234 else
235 rvl.add(new RestVideo("Actors", RestVideo.STYPE_MARKER, RestVideo.STYPE_ACTOR));
236 for (Actor a : la) {
237 rvl.add(new RestVideo(a.getName(), RestVideo.STYPE_ACTOR, a.getId()));
238 }
239 remaining -= la.size();
240 }
241 }
242 if (remaining > 1) {
243 List<Director> ld = libraryManager.findDirectors(key, remaining-1);
244 if (!ld.isEmpty()) {
245 remaining--;
246 if (ld.size() == 1)
247 rvl.add(new RestVideo("Director", RestVideo.STYPE_MARKER, RestVideo.STYPE_DIRECTOR));
248 else
249 rvl.add(new RestVideo("Directors", RestVideo.STYPE_MARKER, RestVideo.STYPE_DIRECTOR));
250 for (Director d : ld) {
251 rvl.add(new RestVideo(d.getName(), RestVideo.STYPE_DIRECTOR, d.getId()));
252 }
253 remaining -= ld.size();
254 }
255 }
256 if (remaining > 1) {
257 List<Writer> lw = libraryManager.findWriters(key, remaining-1);
258 if (!lw.isEmpty()) {
259 remaining--;
260 if (lw.size() == 1)
261 rvl.add(new RestVideo("Writer", RestVideo.STYPE_MARKER, RestVideo.STYPE_WRITER));
262 else
263 rvl.add(new RestVideo("Writers", RestVideo.STYPE_MARKER, RestVideo.STYPE_WRITER));
264 for (Writer w : lw) {
265 rvl.add(new RestVideo(w.getName(), RestVideo.STYPE_WRITER, w.getId()));
266 }
267 remaining -= lw.size();
268 }
269 }
270 return(rvl);
271 }
272
273
274
275 @Override
276 public List<VideoBase> getLibraryListVideos(int key) {
277 log.info("getLibraryListVideos key = " + key);
278 return(libraryManager.getLibrarySectionVideos(key));
279 }
280
281
282
283
284 @Override
285 public List<VideoBase> getDirectorVideos(int key) {
286 log.info("getDirectorVideos key = " + key);
287 return(libraryManager.getDirectorVideos(key));
288 }
289
290
291
292
293 @Override
294 public List<VideoBase> getWriterVideos(int key) {
295 log.info("getWriterVideos key = " + key);
296 return(libraryManager.getWriterVideos(key));
297 }
298
299
300
301 @Override
302 public List<VideoBase> getActorVideos(int key) {
303 log.info("getActorVideos key = " + key);
304 return(libraryManager.getActorVideos(key));
305 }
306
307
308
309 @Override
310 public long getLastUpdateTime() {
311 return(libraryManager.getLastUpdateTime());
312 }
313
314
315
316 @Override
317 public int getTVEpisodeCount(boolean allowRestricted) {
318 return(libraryManager.getTVEpisodeCount(allowRestricted));
319 }
320
321
322
323 @Override
324 public int getMovieCount(boolean allowRestricted) {
325 return(libraryManager.getMovieCount(allowRestricted));
326 }
327
328
329
330 @Override
331 public int getTVShowCount(boolean allowRestricted) {
332 return(libraryManager.getTVShowCount(allowRestricted));
333 }
334
335
336
337
338 @Override
339 public TVSeason getTVSeasonFromHashKey(int key) {
340 return(libraryManager.getTVSeasonFromHashKey(key));
341 }
342
343
344
345 @Override
346 public boolean isAdmin(HttpServletRequest request) {
347 if (!this.bsAccountMan.isUserWebSession(request))
348 return(false);
349 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
350 log.debug("userWebSession = " + userWebSession);
351 boolean admin = false;
352 if (userWebSession.isLoggedIn()) {
353 User u = (User)userWebSession.getUser();
354 if (u.isAdmin())
355 admin = true;
356 }
357 return(admin);
358 }
359
360
361
362
363 @Override
364 public boolean isAllowRestricted(HttpServletRequest request) {
365 if (!this.bsAccountMan.isUserWebSession(request))
366 return(false);
367 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
368 log.debug("userWebSession = " + userWebSession);
369 boolean allowRestricted = false;
370 if (userWebSession.isLoggedIn()) {
371 User u = (User)userWebSession.getUser();
372 if (u.isUnrestricted() && !u.isHideRestricted())
373 allowRestricted = true;
374 }
375 return(allowRestricted);
376 }
377
378
379
380
381 @Override
382 public boolean isHideRestricted(HttpServletRequest request) {
383 if (!this.bsAccountMan.isUserWebSession(request))
384 return(true);
385 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
386 log.debug("userWebSession = " + userWebSession);
387 if (userWebSession.isLoggedIn()) {
388 User u = (User)userWebSession.getUser();
389 return(u.isHideRestricted());
390 }
391 return(true);
392 }
393
394
395
396 @Override
397 public void setHideRestricted(HttpServletRequest request, boolean hideRestricted) {
398 if (!this.bsAccountMan.isUserWebSession(request))
399 return;
400 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
401 log.debug("userWebSession = " + userWebSession);
402 if (!userWebSession.isLoggedIn())
403 return;
404 User u = (User)userWebSession.getUser();
405 u.setHideRestricted(hideRestricted);
406 db.saveUserAttributes(u.getUserAttributes());
407
408 }
409
410
411
412 @Override
413 public int getBackgroundFit(HttpServletRequest request, boolean artShow) {
414 if (!this.bsAccountMan.isUserWebSession(request))
415 return(2);
416 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
417 log.debug("userWebSession = " + userWebSession);
418 User u = (User)userWebSession.getUser();
419 if (artShow && u.getBackgroundFit() == ImageController.bgGrey)
420 return(u.getArtShowFit());
421 return(u.getBackgroundFit());
422 }
423
424
425
426
427 @Override
428 public void setBackgroundFit(HttpServletRequest request, int backgroundFit) {
429 if (!this.bsAccountMan.isUserWebSession(request))
430 return;
431 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
432 log.debug("userWebSession = " + userWebSession);
433 User u = (User)userWebSession.getUser();
434 if (backgroundFit == ImageController.bgGrey && u.getBackgroundFit() != ImageController.bgGrey)
435 u.setArtShowFit(u.getBackgroundFit());
436 u.setBackgroundFit(backgroundFit);
437 if (userWebSession.isLoggedIn())
438 db.saveUserAttributes(u.getUserAttributes());
439 }
440
441
442
443
444
445 @Override
446 public void setBackgroundDelays(HttpServletRequest request,
447 int backgroundDelay, int artShowDelay) {
448 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
449 log.debug("userWebSession = " + userWebSession);
450 User u = (User)userWebSession.getUser();
451 u.setBackgroundDelay(backgroundDelay);
452 u.setArtShowDelay(artShowDelay);
453 if (userWebSession.isLoggedIn())
454 db.saveUserAttributes(u.getUserAttributes());
455 }
456
457
458
459
460
461 @Override
462 public int getFindMaxResults(HttpServletRequest request) {
463 if (!this.bsAccountMan.isUserWebSession(request))
464 return(10);
465 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
466 log.debug("userWebSession = " + userWebSession);
467 User u = (User)userWebSession.getUser();
468 return(u.getFindMaxResults());
469 }
470
471
472
473 @Override
474 public void setFindMaxResults(HttpServletRequest request, int findMaxResults) {
475 UserWebSession userWebSession = (UserWebSession)this.bsAccountMan.getUserWebSession(request);
476 log.debug("userWebSession = " + userWebSession);
477 User u = (User)userWebSession.getUser();
478 if (!userWebSession.isLoggedIn() && findMaxResults > 200)
479 findMaxResults = 200;
480 u.setFindMaxResults(findMaxResults);
481 if (userWebSession.isLoggedIn())
482 db.saveUserAttributes(u.getUserAttributes());
483 }
484
485
486
487 @Override
488 public List<UserAttribute> getUserAttributes(int userId) {
489 return(db.getUserAttributes(userId));
490 }
491
492
493
494 @Override
495 public void saveUserAttributes(List<UserAttribute> list) {
496 db.saveUserAttributes(list);
497 }
498
499
500
501
502 @Override
503 public void startRefreshLibraryFromPlex() {
504 libraryManager.startRefreshLibraryFromPlex();
505 }
506 @Override
507 public RestRefreshStatus getRefreshLibraryFromPlexStatus() {
508 return(libraryManager.getRefreshLibraryFromPlexStatus());
509 }
510
511
512
513 @Override
514 public String getPlexUrl() {
515 if (plexUrl == null) {
516 plexUrl = ConfigManager.getString(RestClientConstants.REST_SERVER_URL_PROP_KEY, RestClientConstants.DEFAULT_REST_SERVER_LOCATION);
517 if (!plexUrl.endsWith("/"))
518 plexUrl += "/";
519 }
520 return(plexUrl);
521 }
522
523
524 }