View Javadoc
1   /******************************************************************************
2    * Library.java - domain object for the entire library.
3    * $Id: Library.java,v 1.16 2015/05/11 18:02:01 dick Exp $
4    * 
5    * BuckoVidLib - The BuckoSoft Video Library
6    * Copyright(c) 2014 - Dick Balaska
7    * 
8    * $Log: Library.java,v $
9    * Revision 1.16  2015/05/11 18:02:01  dick
10   * Fix the maxRecentVideos test.
11   *
12   * Revision 1.15  2015/05/11 17:50:01  dick
13   * Bump up maxNewestVideos to 100.
14   *
15   * Revision 1.14  2015/04/30 06:34:27  dick
16   * Getting lists of VideoBase is always protected by session.
17   *
18   * Revision 1.13  2015/04/30 03:18:04  dick
19   * Deal mostly with VideoBase. Only fetch full Video when we really need it.
20   * Protect all (most) calls to the restricted section of the library by session.
21   *
22   * Revision 1.12  2015/04/28 16:16:21  dick
23   * Keep a list of the restricted LibrarySection names handy.
24   *
25   * Revision 1.11  2015/04/27 14:59:12  dick
26   * Support having restricted LibrarySections that you must have access to get to.
27   *
28   * Revision 1.10  2015/04/02 23:24:37  dick
29   * Handle Writers.  Store the hashKey with the Video.
30   *
31   * Revision 1.9  2015/04/01 02:41:22  dick
32   * Library keeps a list of Actors, Directors and Genres.
33   *
34   * Revision 1.8  2015/03/21 09:17:32  dick
35   * getLibrarySection(key) returns one LibrarySection.
36   *
37   * Revision 1.7  2014/11/01 08:05:05  dick
38   * Count TV episodes.
39   *
40   * Revision 1.6  2014/10/25 22:51:49  dick
41   * Keep a list of newest videos, in case we want more than just the most recent.
42   *
43   * Revision 1.5  2014/10/20 01:19:02  dick
44   * Track the newestVideo in the library.  Track TVSeasons.
45   *
46   * Revision 1.4  2014/10/09 08:34:35  dick
47   * Use the Video as a key for the client, not just the image url.
48   *
49   * Revision 1.3  2014/10/08 04:24:40  dick
50   * Resurrect Library to respresent the entire library.
51   *
52   */
53  package com.buckosoft.BuckoVidLib.domain;
54  
55  import java.util.ArrayList;
56  import java.util.HashMap;
57  import java.util.HashSet;
58  import java.util.LinkedList;
59  import java.util.List;
60  import java.util.ListIterator;
61  
62  /** Domain object for the entire library.
63   * Library has no knowledge of how to deal with resticted sections, other than to keep a list of them.
64   * That job is LibraryManager's.
65   * @author dick
66   * @since 2014-10-07
67   */
68  public class Library {
69  	private List<LibrarySection>	librarySections = new ArrayList<LibrarySection>();
70  	private List<LibrarySection>	unrestrictedLibrarySections = new ArrayList<LibrarySection>();
71  	private	List<String>			restrictedSectionNames = new ArrayList<String>();
72  
73  	private	HashMap<Integer, VideoBase> videoMap = new HashMap<Integer, VideoBase>();
74  	private	HashMap<Integer, Integer>	mapVideoIdtoHashKey = new HashMap<Integer, Integer>();
75  	private	HashSet<Actor>		actors = new HashSet<Actor>();
76  	private	HashSet<Director>	directors = new HashSet<Director>();
77  	private	HashSet<Genre>		genres = new HashSet<Genre>();
78  	private	HashSet<Writer>		writers = new HashSet<Writer>();
79  	
80  	private	LinkedList<VideoBase>	newestVideos = new LinkedList<VideoBase>();
81  	private	final int			maxNewestVideos = 100;
82  	
83  //	private	Video	newestVideo;	// or TV Show
84  
85  	
86  	/** Fetch the max length of the recent video list.
87  	 * @return the maxNewestVideos
88  	 */
89  	public int getMaxNewestVideos() {
90  		return maxNewestVideos;
91  	}
92  	
93  	/**
94  	 * @return the librarySections
95  	 */
96  	public List<LibrarySection> getLibrarySections() {
97  		return librarySections;
98  	}
99  	public List<LibrarySection> getUnrestrictedLibrarySections() {
100 		return(unrestrictedLibrarySections);
101 	}
102 	public LibrarySection getLibrarySection(int key) {
103 		for (LibrarySection ls : librarySections) {
104 			if (ls.getKey() == key)
105 				return(ls);
106 		}
107 		return(null);
108 	}
109 	/**
110 	 * @param librarySections the librarySections to set
111 	 */
112 	public void setLibrarySections(List<LibrarySection> librarySections) {
113 		this.librarySections = librarySections;
114 		this.unrestrictedLibrarySections.clear();
115 		for (LibrarySection ls : librarySections)
116 			if (!ls.isRestricted())
117 				this.unrestrictedLibrarySections.add(ls);
118 	}
119 
120 	public void addLibrarySection(LibrarySection librarySection) {
121 		this.librarySections.add(librarySection);
122 		if (!librarySection.isRestricted())
123 			this.unrestrictedLibrarySections.add(librarySection);
124 	}
125 
126 	/**
127 	 * @return the restrictedSectionNames
128 	 */
129 	public List<String> getRestrictedSectionNames() {
130 		return restrictedSectionNames;
131 	}
132 	/**
133 	 * @param restrictedSectionNames the restrictedSectionNames to set
134 	 */
135 	public void setRestrictedSectionNames(List<String> restrictedSectionNames) {
136 		this.restrictedSectionNames = restrictedSectionNames;
137 	}
138 
139 	/**
140 	 * @return the videoMap
141 	 */
142 	public HashMap<Integer, VideoBase> getVideoMap() {
143 		return videoMap;
144 	}
145 
146 	/**
147 	 * @param videoMap the videoMap to set
148 	 */
149 //	public void setVideoMap(HashMap<Integer, Video> videoMap) {
150 //		this.videoMap = videoMap;
151 //	}
152 	
153 	public void addVideo(int key, VideoBase video) {
154 		videoMap.put(key, video);
155 		this.mapVideoIdtoHashKey.put(video.getId(), key);
156 /*		if (video instanceof TVShow) {
157 			TVShow tv = (TVShow)video;
158 			for (TVSeason season : tv.getSeasons()) {
159 				if (season.getAddedAt() > newestVideo.getAddedAt()) {
160 					video = season;
161 				}
162 			}
163 		}
164 */
165 		/* head is newest.  head: 100, tail: 50 */
166 		if (newestVideos.isEmpty())	{
167 			newestVideos.add(video);
168 		} else if (video.getAddedAt() > newestVideos.getLast().getAddedAt()
169 				|| newestVideos.size() < maxNewestVideos) {
170 			ListIterator<VideoBase> iter = newestVideos.listIterator();
171 			boolean added = false;
172 			while (iter.hasNext()) {
173 				VideoBase v = iter.next();
174 				if (video.getAddedAt() > v.getAddedAt()) {
175 					iter.previous();
176 					iter.add(video);
177 					added = true;
178 					break;
179 				}
180 			}
181 			if (!added)
182 				newestVideos.addLast(video);
183 			if (newestVideos.size() > maxNewestVideos)
184 				newestVideos.removeLast();
185 		}
186 	}
187 	
188 	public VideoBase getVideoBase(int key) {
189 		return(videoMap.get(key));
190 	}
191 	
192 	public VideoBase getVideoById(int videoId) {
193 		return(videoMap.get(mapVideoIdtoHashKey.get(videoId)));
194 	}
195 
196 	public VideoBase getNewestVideo() {
197 		if (!newestVideos.isEmpty())
198 			return(newestVideos.getFirst());
199 		return(null);
200 	}
201 
202 	public List<VideoBase> getNewestVideos() {
203 		return(newestVideos);
204 	}
205 
206 	/**
207 	 * @return the actors
208 	 */
209 	public HashSet<Actor> getActors() {
210 		return actors;
211 	}
212 
213 	/**
214 	 * @param actors the actors to set
215 	 */
216 	public void setActors(HashSet<Actor> actors) {
217 		this.actors = actors;
218 	}
219 
220 	/**
221 	 * @return the genres
222 	 */
223 	public HashSet<Genre> getGenres() {
224 		return genres;
225 	}
226 
227 	/**
228 	 * @param genres the genres to set
229 	 */
230 	public void setGenres(HashSet<Genre> genres) {
231 		this.genres = genres;
232 	}
233 
234 	/**
235 	 * @return the directors
236 	 */
237 	public HashSet<Director> getDirectors() {
238 		return directors;
239 	}
240 
241 	/**
242 	 * @param directors the directors to set
243 	 */
244 	public void setDirectors(HashSet<Director> directors) {
245 		this.directors = directors;
246 	}
247 
248 	/**
249 	 * @return the writers
250 	 */
251 	public HashSet<Writer> getWriters() {
252 		return writers;
253 	}
254 
255 	/**
256 	 * @param writers the writers to set
257 	 */
258 	public void setWriters(HashSet<Writer> writers) {
259 		this.writers = writers;
260 	}
261 
262 
263 }