001    /*-----------------------------------------------------------------------
002     *  Copyright (C) 2006
003     *  Associated Universities, Inc. Washington DC, USA.
004     *  This program is free software; you can redistribute it and/or
005     *  modify it under the terms of the GNU General Public License as
006     *  published by the Free Software Foundation; either version 2 of
007     *  the License, or (at your option) any later version.
008     *
009     *  This program is distributed in the hope that it will be useful,
010     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *  GNU General Public License for more details.
013     *
014     *  Correspondence concerning this software should be addressed as follows:
015     *         Internet email: switz@nrao.edu
016     *         Postal address: User Database
017     *                         National Radio Astronomy Observatory
018     *                         Post Office Box 0
019     *                         Socorro, NM 87801  USA
020     *-----------------------------------------------------------------------*/
021    package edu.nrao.sss.model.user;
022    
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    import edu.nrao.sss.xml.XMLTransform;
027    
028    public class UserGroup extends ObjectId implements XMLable {
029    
030            public final static String OP_NAME = "open";
031    
032            public final static short OP_TYPE = 1;
033    
034            public static final String PDB_ADMIN = "pdb-admin";
035    
036            public final static String RH_NAME = "restricted/hidden";
037    
038            public final static short RH_TYPE = 3;
039    
040            public final static String RV_NAME = "restricted/visible";
041    
042            public final static short RV_TYPE = 2;
043    
044            private static final long serialVersionUID = 1L;
045    
046            private Set<User> users;
047            
048            private String userGroupDescription;
049    
050            private String userGroupName;
051    
052            private short userGroupType;
053    
054            public UserGroup() {
055                    super();
056            }
057    
058            public UserGroup(UserGroup userGroup) {
059                    super(userGroup);
060                    this.setUserGroupType(userGroup.getUserGroupType());
061                    this.setUserGroupName(userGroup.getUserGroupName());
062                    this.setUserGroupDescription(userGroup.getUserGroupDescription());
063            }
064    
065            public void addToUsers(User user) throws InfrastructureException {
066                    if (user == null)
067                            return;
068                    else if (user.isNew())
069                            throw new InfrastructureException(
070                                            "unknown (new) user added to users set");
071    
072                    if (users == null)
073                            users = new HashSet<User>();
074                    users.add(user);
075            }
076    
077            public boolean equals(Object obj) {
078                    if (null == obj)
079                            return false;
080                    if (!(obj instanceof UserGroup))
081                            return false;
082                    else {
083                            UserGroup mObj = (UserGroup) obj;
084                            return (this.getId() == mObj.getId());
085                    }
086            }
087            
088            public String getUserGroupDescription() {
089                    return userGroupDescription;
090            }
091            
092            public String getUserGroupName() {
093                    return userGroupName;
094            }
095            
096            public short getUserGroupType() {
097                    return userGroupType;
098            }
099    
100            public Set<User> getUsers() {
101                    if (users == null)
102                            users = new HashSet<User>();
103                    return users;
104            }
105    
106            public void setUserGroupDescription(String userGroupDescription) {
107                    this.userGroupDescription = userGroupDescription;
108            }
109    
110            public void setUserGroupName(String userGroupName) {
111                    this.userGroupName = userGroupName;
112            }
113    
114            public void setUserGroupType(short userGroupType) {
115                    this.userGroupType = userGroupType;
116            }
117    
118            public void setUsers(Set<User> users) {
119                    this.users = users;
120            }
121    
122            public String toXML() {
123                    StringBuffer result = new StringBuffer();
124                    result.append("<user-group>\n");
125                    result.append("<group-name>" + XMLTransform.encode(getUserGroupName())
126                                    + "</group-name>\n");
127                    result.append("<type>"
128                                    + XMLTransform.encode(userGroupTypeToName(getUserGroupType()))
129                                    + "</type>\n");
130                    result.append("<description>"
131                                    + XMLTransform.encode(getUserGroupDescription())
132                                    + "</description>\n");
133                    result.append("</user-group>\n");
134                    return result.toString();
135            }
136    
137            private String userGroupTypeToName(int i) {
138                    String gType;
139                    switch (i) {
140                    case OP_TYPE:
141                            gType = OP_NAME;
142                            break;
143                    case RV_TYPE:
144                            gType = RV_NAME;
145                            break;
146                    case RH_TYPE:
147                            gType = RH_NAME;
148                            break;
149                    default:
150                            gType = "unknown-type";
151                            break;
152                    }
153                    return gType;
154            }
155    
156    }