View Javadoc
1   /******************************************************************************
2    * User.java - Extend BSAccountUser with BuckoVidLib specific attributes
3    * $Id: User.java,v 1.8 2015/05/10 19:03:31 dick Exp $
4    * 
5    * BuckoVidLib - The BuckoSoft Video Library
6    * Copyright(c) 2015 - Dick Balaska
7    * 
8    * $Log: User.java,v $
9    * Revision 1.8  2015/05/10 19:03:31  dick
10   * Let's see if add a default to the switch will cover all the branches of it.
11   *
12   * Revision 1.7  2015/05/07 00:15:38  dick
13   * findMaxResults is a user config option.
14   *
15   * Revision 1.6  2015/05/06 03:36:53  dick
16   * hideRestricted allows a user with restricted access to temporarily turn it off.
17   *
18   * Revision 1.5  2015/05/05 04:26:00  dick
19   * Persist backgroundDelay and artShowDelay.
20   *
21   * Revision 1.4  2015/05/04 23:38:17  dick
22   * Add backgroundDelay and artShowDelay
23   *
24   * Revision 1.3  2015/05/04 20:36:08  dick
25   * getBackgroundFit() needs to know if we are running the ArtShow, which may be different
26   * than the normal backgroundFit.
27   *
28   * Revision 1.2  2015/05/03 18:01:03  dick
29   * Locally we keep attributes as normal bools/ints.  But we persist them as a list of key/value pairs. Manage the in and out of the User attributes.
30   *
31   * Revision 1.1  2015/04/27 02:25:14  dick
32   * Extend BSAccountUser with BuckoVidLib specific attributes.
33   *
34   */
35  package com.buckosoft.BuckoVidLib.domain;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import com.buckosoft.BSAccount.domain.BSAccount;
44  import com.buckosoft.BSAccount.domain.BSAccountUser;
45  
46  /** Extend BSAccountUser with BuckoVidLib specific attributes
47   * @author dick
48   * @since 2015-04-26
49   * @version $Revision: 1.8 $ <br/> $Date: 2015/05/10 19:03:31 $  
50   */
51  public class User extends BSAccountUser {
52  	private static final Log log = LogFactory.getLog(User.class);
53  	private static final long serialVersionUID = 42;
54  
55  	private	boolean	admin = false;
56  	private	boolean	unrestricted = false;
57  	private	int		backgroundFit = 1;
58  	private	int		artShowFit = 1;
59  	private	int		backgroundDelay = 30;
60  	private	int		artShowDelay = 10;
61  	private	boolean	hideRestricted = false;
62  	private	int		findMaxResults = 40;
63  
64  	private	List<UserAttribute>	userAttributes;
65  
66  	public User(BSAccount account) {
67  		super(account);
68  	}
69  
70  	/** Is this user an admin?
71  	 * @return the admin
72  	 */
73  	public boolean isAdmin() {
74  		return admin;
75  	}
76  
77  	/** Declare this user to be an admin
78  	 * @param admin the admin to set
79  	 */
80  	public void setAdmin(boolean admin) {
81  		this.admin = admin;
82  	}
83  
84  	/** Is this user allowed access to the restricted areas of the library?
85  	 * @return default = false, true = allow access
86  	 */
87  	public boolean isUnrestricted() {
88  		return unrestricted;
89  	}
90  
91  	/** Declare this user's access to the restricted areas.
92  	 * @param unrestricted the unrestricted to set
93  	 */
94  	public void setUnrestricted(boolean unrestricted) {
95  		this.unrestricted = unrestricted;
96  	}
97  
98  	/** Perhaps the User wants to hide the restricted sections, even though he's authorized.
99  	 * @return the hideRestricted
100 	 */
101 	public boolean isHideRestricted() {
102 		return hideRestricted;
103 	}
104 
105 	/**
106 	 * @param hideRestricted the hideRestricted to set
107 	 */
108 	public void setHideRestricted(boolean hideRestricted) {
109 		this.hideRestricted = hideRestricted;
110 	}
111 
112 	/** Get the User's preference for background type
113 	 * 0 = fit the image to the screen
114 	 * 1 = fill the screen with the image (default)
115 	 * 2 = no background image
116 	 * @return the backgroundFit
117 	 */
118 	public int getBackgroundFit() {
119 		return backgroundFit;
120 	}
121 
122 	/**
123 	 * @param backgroundFit the backgroundFit to set
124 	 */
125 	public void setBackgroundFit(int backgroundFit) {
126 		this.backgroundFit = backgroundFit;
127 	}
128 
129 	/**
130 	 * @return the artShowFit
131 	 */
132 	public int getArtShowFit() {
133 		return artShowFit;
134 	}
135 
136 	/** User's preference for the ArtShow background type.
137 	 * If the user is running the grey background, then he may still want to style the ArtShow.
138 	 * @param artShowFit the artShowFit to set
139 	 */
140 	public void setArtShowFit(int artShowFit) {
141 		this.artShowFit = artShowFit;
142 	}
143 
144 	/**
145 	 * @return the backgroundDelay
146 	 */
147 	public int getBackgroundDelay() {
148 		return backgroundDelay;
149 	}
150 
151 	/**
152 	 * @param backgroundDelay the backgroundDelay to set
153 	 */
154 	public void setBackgroundDelay(int backgroundDelay) {
155 		this.backgroundDelay = backgroundDelay;
156 	}
157 
158 	/**
159 	 * @return the artShowDelay
160 	 */
161 	public int getArtShowDelay() {
162 		return artShowDelay;
163 	}
164 
165 	/**
166 	 * @param artShowDelay the artShowDelay to set
167 	 */
168 	public void setArtShowDelay(int artShowDelay) {
169 		this.artShowDelay = artShowDelay;
170 	}
171 
172 	/**
173 	 * @return the findMaxResults
174 	 */
175 	public int getFindMaxResults() {
176 		return findMaxResults;
177 	}
178 
179 	/**
180 	 * @param findMaxResults the findMaxResults to set
181 	 */
182 	public void setFindMaxResults(int findMaxResults) {
183 		this.findMaxResults = findMaxResults;
184 	}
185 
186 	/** Get the attributes as a List of UserAttribute.
187 	 * These should not be directly accessed by the api. They are internal for hibernate to persist a User. <br>
188 	 * Key/value pairs are used so that the User object can grow without having to redefine the User in the database table.
189 	 * @return the attributes
190 	 */
191 	public List<UserAttribute> getUserAttributes() {
192 		if (userAttributes == null)
193 			userAttributes = new ArrayList<UserAttribute>();
194 		setAttribute("admin", "" + admin);
195 		setAttribute("unrestricted", "" + unrestricted);
196 		setAttribute("hideRestricted", "" + hideRestricted);
197 		setAttribute("backgroundFit", "" + backgroundFit);
198 		setAttribute("artShowFit", "" + artShowFit);
199 		setAttribute("backgroundDelay", "" + backgroundDelay);
200 		setAttribute("artShowDelay", "" + artShowDelay);
201 		setAttribute("findMaxResults", "" + findMaxResults);
202 		return userAttributes;
203 	}
204 
205 	/**
206 	 * @param attributes the attributes to set
207 	 */
208 	public void setUserAttributes(List<UserAttribute> userAttributes) {
209 		this.userAttributes = userAttributes;
210 		if (userAttributes == null)
211 			return;
212 		for (UserAttribute ua : userAttributes) {
213 			switch (ua.getKey()) {
214 			case "admin":
215 				this.admin = Boolean.parseBoolean(ua.getValue());
216 				break;
217 			case "unrestricted":
218 				this.unrestricted = Boolean.parseBoolean(ua.getValue());
219 				break;
220 			case "hideRestricted":
221 				this.hideRestricted = Boolean.parseBoolean(ua.getValue());
222 				break;
223 			case "backgroundFit":
224 				this.backgroundFit = Integer.parseInt(ua.getValue());
225 				break;
226 			case "artShowFit":
227 				this.artShowFit = Integer.parseInt(ua.getValue());
228 				break;
229 			case "backgroundDelay":
230 				this.backgroundDelay = Integer.parseInt(ua.getValue());
231 				break;
232 			case "artShowDelay":
233 				this.artShowDelay = Integer.parseInt(ua.getValue());
234 				break;
235 			case "findMaxResults":
236 				this.findMaxResults = Integer.parseInt(ua.getValue());
237 				break;
238 			default:
239 				log.error("Unknown UserAttribute" + ua.getKey());
240 				break;
241 			}
242 		}
243 	}
244 	
245 	private void setAttribute(String key, String value) {
246 		for (UserAttribute ua : userAttributes) {
247 			if (ua.getKey().equals(key)) {
248 				ua.setValue(value);
249 				return;
250 			}
251 		}
252 		UserAttribute ua = new UserAttribute();
253 		ua.setUserId(this.getUserId());
254 		ua.setKey(key);
255 		ua.setValue(value);
256 		userAttributes.add(ua);
257 	}
258 }