Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acs/damsel/srvr/group/GroupMgr.java


1   package org.acs.damsel.srvr.group;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import org.acs.damsel.srvr.db.*;
7   import org.apache.log4j.*;
8   
9   /**
10   * <p>Title: GroupMgr</p>
11   * <p>Description: The GroupMgr class provides access to user groups.  It can be
12   * used to create and remove groups in the database, as well as add and delete
13   * users from groups, get a list of all of the groups in the data base, and
14   * retrieve the permissions associated with a particular group.</p>
15   * @version 1.0
16   */
17  public class GroupMgr {
18    private AssetDB assetDB = null;
19    private static Logger log = Logger.getLogger(GroupMgr.class);
20  
21    /**
22     * Default constructor, throws a SQLException if the UserMgr is not created.
23     */
24    public GroupMgr() {
25      try {
26        assetDB = AssetDB.instance();
27      }
28      catch (SQLException ex) {
29        log.warn("SQLException caught while constructing the UserMgr");
30      }
31    }
32  
33    /**
34     * Method returns true if the group exists in the database, false otherwise.
35     * @param ug Group to search the database for
36     * @return boolean
37     */
38    public boolean isGroupInDB(Group ug) {
39      try {
40        return assetDB.isGroupInDB(ug);
41      }
42      catch (SQLException ex) {
43        log.warn("Unexpected SQLException caught in GroupMgr.isGroupInDB " +
44                 ex.getMessage());
45        return false;
46      }
47    }
48  
49    /**
50     * Method adds the specified group to the database.
51     * @param group Group to be added
52     * @return int, number of rows affected in the database (1 if sucessful,
53     * 0 otherwise).
54     */
55    public int addGroup(Group group) {
56      try {
57        return assetDB.addGroup(group);
58      }
59      catch (SQLException ex) {
60        log.warn("Unexpected SQLException caught in GroupMgr.addGroup " +
61                 ex.getMessage());
62        return -1;
63      }
64    }
65  
66    /**
67     * Method returns a vector of all groups stored in the groupstable of the
68     * database.
69     * @return Vector of group names
70     */
71    public Vector groupNames() {
72      try {
73        return AssetDB.instance().getGroupNames();
74      }
75      catch (SQLException ex) {
76        log.warn("Caught unexpected SQLException in groupNames");
77        ex.printStackTrace();
78        return null;
79      }
80    }
81  
82    /**
83     * Method updates all of the fields in the updateNames with all of the
84     * values in the updateValues for the group specified by the groupName.
85     * @param updateNames Vector of fields in the group table to be updated
86     * @param updateValues Vector of values to be updated
87     * @param groupName String containing the name of the group to be updated
88     */
89    public void updateGroup(Vector updateNames, Vector updateValues,
90                            String groupName) {
91      try {
92        MidAssetDB midAssetDB = MidAssetDB.instance();
93        Vector groupVec = new Vector();
94        groupVec.add("GroupName");
95        Vector groupValue = new Vector();
96        groupValue.add(groupName);
97        midAssetDB.update("GroupsTable", updateNames, updateValues, groupVec,
98                          groupValue);
99      }
100     catch (SQLException ex) {
101       log.warn("Caught unexpected SQLException in updateGroup");
102       ex.printStackTrace();
103     }
104   }
105 
106   /**
107    * Gets all the users in a specified group.
108    * @param groupName String containing the name of a group
109    * @return Vector of user names
110    */
111   public Vector getUsersInGroup(String groupName) {
112     try {
113       return AssetDB.instance().getUsersInGroup(groupName);
114     }
115     catch (SQLException ex) {
116       log.error("SQL Exception in getUsersInGroup");
117       return null;
118     }
119   }
120 
121   /**
122    * Method returns a Group object representing the group specified by the
123    * input string containing the group name
124    * @param groupName String containing the group name
125    * @return Group
126    * @throws SQLException
127    */
128   public Group getGroup(String groupName) throws SQLException {
129     return assetDB.getGroup(groupName);
130   }
131 
132   /**
133    * Method deletes all instances of the group name sent from the
134    * usersgroupstable.  The group will still exist in the groupstable.  The
135    * group will not exist in the usersgroupstable.  This function is used when
136    * you add and remove users from a group.  To make sure you get all users
137    * removed from a group you first must delete everyone in that group and then
138    * add only the ones that you want to be in that group
139    * @param groupName String containing the groupName
140    */
141   public void deleteMultipleUsersFromGroup(String groupName) {
142     try {
143       assetDB.deleteMultipleUsersFromGroup(groupName);
144     }
145     catch (SQLException ex) {
146       log.error("An SQLExcpetion occured in GroupMgr.addMultipleUsersToGroup");
147     }
148   }
149 
150   /**
151    * Method adds the specified user to the specified group.  Throws an
152    * exception if the user is not added successfully.
153    * @param userName String containing the name of the user to be added
154    * @param groupName String containing the name of the group to add to
155    * @throws SQLException
156    */
157   public void addUserToGroup(String userName, String groupName) throws
158       SQLException {
159     assetDB.addUserToGroup(userName, groupName);
160   }
161 
162   /**
163    * Method deletes the group with the specified name.
164    * @param groupName String containing the name of the group to be deleted
165    * @return int, the number of rows affected in the groupstable
166    * @throws SQLException
167    */
168   public int deleteGroup(String groupName) throws SQLException {
169     return assetDB.deleteGroup(groupName);
170   }
171 
172   /**
173    * Method returns the permissions associated with the specified group
174    * @param groupName String containing the group name
175    * @return Vector of permission strings
176    */
177   public Vector getGroupPermissions(String groupName) {
178     return assetDB.getGroupPermissions(groupName);
179   }
180 } // end of class