Source code: org/acs/damsel/srvr/user/UserMgr.java
1 package org.acs.damsel.srvr.user;
2
3 import java.sql.*;
4 import java.util.*;
5
6 import org.acs.damsel.srvr.asset.*;
7 import org.acs.damsel.srvr.collection.*;
8 import org.acs.damsel.srvr.db.*;
9 import org.apache.log4j.*;
10
11 /**
12 * <p>Class Name: UserMgr</p>
13 * <p>Description: This class contains methods to manage the UserTable. It can
14 * be used to set or retrieve users in the DB, as well as update and retrieve
15 * specific fields of the UserTable. </p>
16 * @version 1.1
17 */
18
19 public class UserMgr {
20
21 private AssetDB assetDB = null;
22 private static Logger log = Logger.getLogger(UserMgr.class);
23 public UserMgr() {
24 try {
25 assetDB = AssetDB.instance();
26 }
27 catch (SQLException ex) {
28 log.warn("SQLException caught while constructing the UserMgr");
29 }
30 }
31
32 /**
33 * Method to return a table containing all the columns in the UsersTable
34 * except for the password column.
35 * @return Table
36 */
37 public Table getUserInfo() {
38 return assetDB.getUserTable();
39 }
40
41 /**
42 * Method to return a vector of Strings containing all the user names stored
43 * in the UserTable of the db.
44 * @return Vector
45 */
46 public Vector getAllUserNames() {
47 Vector userNames = new Vector();
48 Table resultsTable = this.getUserInfo();
49 try {
50 resultsTable.orderBy("UserName");
51 }
52 catch (TagNameNotFoundException ex) {
53 log.warn("Unexpected SQL exception in OrderBy.");
54 }
55 Vector results = resultsTable.getResults();
56 /*pull out a user name from each row and throw it in our userNames vector */
57 for (int i = 0; i < results.size(); i++) {
58 userNames.add( ( (Vector) results.elementAt(i)).elementAt(0));
59 }
60 return userNames;
61 }
62
63 /**
64 * Method to add the specified User object to the UserTable of the db.
65 * @param user User
66 * @return int, number of rows affected by add in UserTable
67 * @throws SQLException
68 */
69 public int addUser(User user) throws SQLException {
70 return assetDB.addUser(user);
71 }
72
73 /**
74 * Method to determine if specified User exists in the db.
75 * @param user User
76 * @return boolean
77 * @throws SQLException
78 */
79 public boolean isUserInDB(User user) throws SQLException {
80 return assetDB.isUserInDB(user);
81 }
82
83 /**
84 * Method to return a vector of strings containing the names of all of the
85 * collections that can be viewed by the specified user.
86 * @param user User
87 * @return Vector, vector of strings containing names of viewable collecitons.
88 */
89 public Vector getUsersCollections(User user) {
90 try {
91 return DBUtils.instance().getUsersCollections(user);
92 }
93 catch (SQLException ex) {
94 log.warn("Unexpected SQLException caught in UserMgr.getUsersCollections");
95 return null;
96 }
97 }
98
99 /**
100 * Method will take in a user object and return the associated assets
101 * stored in the usersfavorites table in the db as a CollectionView.
102 * @param user User
103 * @return CollectionView containing the user's favorite assets
104 */
105 public CollectionView getUsersFavorites(User user) {
106 try {
107 return DBUtils.instance().getUsersFavorites(user);
108 }
109 catch (SQLException ex) {
110 log.warn(
111 "Unexpected SQLException caught in UserMgr.getUsersFavorites() : " +
112 ex.getMessage());
113 return null;
114 }
115 }
116
117 /**
118 * Method will take the specified asset and add it to the usersfavorites
119 * table in the database along with the username.
120 * @param asset Asset
121 * @param user User
122 * @return int, number of rows affected by the insert
123 */
124 public int addAssetToFavorites(Asset asset, User user) {
125 try {
126 return AssetDB.instance().addAssetToFavorites(asset, user);
127 }
128 catch (SQLException ex) {
129 log.warn(
130 "Unexpected SQLException caught in UserMgr.addAssetToFavorites() : " +
131 ex.getMessage());
132 return -1;
133 }
134 }
135
136 /**
137 * Method will remove the specified asset belonging to the specified user
138 * from the userscollections table in the database.
139 * @param asset Asset to be removed
140 * @param user User
141 * @return int, number of rows affected by the removed
142 */
143 public int removeAssetFromFavorites(Asset asset, User user) {
144 try {
145 return AssetDB.instance().removeAssetFromFavorites(asset, user);
146 }
147 catch (SQLException ex) {
148 log.warn(
149 "Unexpected SQLException caught in UserMgr.removeAssetFromFavorites() : " +
150 ex.getMessage());
151 return -1;
152 }
153 }
154
155 public int removeSlideShowAssetFromDB(String slideShowName, String assetFileName, String owner) {
156 try {
157 return AssetDB.instance().removeSlideShowAssetFromDB(slideShowName,
158 assetFileName, owner);
159 }
160 catch (SQLException ex) {
161 log.warn("Unexpected SQLException caught in UserMgr.removeSlideShowAssetFromDB() " + ex.getMessage());
162 return 0;
163 }
164 }
165
166 /**
167 * Method will take in a slideShowName and ownerName and fetch a slideShow
168 * from the database matching slideShowName.
169 * @param slideShowName the name of the desired slideShow
170 * @param ownerName the name of the slideShow's owner
171 * @return SlideShow containing all assets in the slideShow, null otherwise
172 */
173 public SlideShow getSlideShow (String ownerName, String slideShowName) {
174 try {
175 return AssetDB.instance().getSlideShow (ownerName, slideShowName);
176 }
177 catch (SQLException ex) {
178 log.warn("Unexpected SQLException caught in UserMgr.getUsersSlideShow() : " + ex.getMessage());
179 return null;
180 }
181 }
182
183 /**
184 * Method returns a vector of strings containing all of the slide shows in the
185 * SlideShowTable for the specified User.
186 * @param user User
187 * @return Vector of all the slideshows for the specified user.
188 */
189 public Vector getSlideShowNames(User user) {
190 try {
191 return assetDB.getSlideShowNames(user);
192 }
193 catch (SQLException ex) {
194 log.warn("Unexpected SQLException caught in UserMgr.getUsersSlideShow() : " + ex.getMessage());
195 return null;
196 }
197 }
198
199 /**
200 * This method returns a vector of strings containing all of the names of the
201 * assets in the specified slideshow
202 * @return Vector, vector of strings containing all of the names of the
203 * assets in the specified slideshow
204 */
205 public Vector getSlideShowAssetFileNames(String slideShowName) {
206 try {
207 return assetDB.getSlideShowAssetFileNames(slideShowName);
208 }
209 catch (SQLException ex) {
210 log.warn("Unexpected SQLException caught in UserMgr.getSlideShowAssetFileNames() : " + ex.getMessage());
211 return null;
212 }
213 }
214
215
216 /**
217 * Method will check if the asset is in the user's myFavorites.
218 * @param asset the asset to be checked
219 * @param user the user to be checked
220 * @return true if asset is in user's myFavorites, false otherwise
221 */
222 public boolean isAssetInFavorites (Asset asset, User user) {
223 try {
224 return AssetDB.instance().isAssetInFavorites(asset, user);
225 }
226 catch (SQLException ex) {
227 log.warn(
228 "Unexpected SQLException caught in UserMgr.isAssetInFavorites() : " +
229 ex.getMessage());
230 return false;
231 }
232 }
233
234 /**
235 * Method to return a User object from the specified username String
236 * @param userName String
237 * @return a user object with the specified user name. A null user is returned if no
238 * user with userName was found.
239 * @throws SQLException
240 * */
241 public User getUserFromName(String userName) throws SQLException {
242 return assetDB.getUserFromName(userName);
243 }
244
245 /**
246 * Method to update all of the field in the userstable for the specified
247 * user
248 * @param user User to be update
249 * @return int, number of rows affected by update
250 * @throws SQLException
251 */
252 public int updateUser(User user) throws SQLException {
253 return assetDB.updateUser(user);
254 }
255
256 /**
257 * Method to retrieve all of the groups to which the specified user belongs.
258 * @param userName String
259 * @return Vector of user groups
260 */
261 public Vector getUsersGroups(String userName) {
262 return assetDB.getUsersGroups(userName);
263 }
264 } // end of class