1 package org.splat.dal.bo.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.security.Principal;
10 import org.splat.dal.bo.kernel.Persistent;
11 import org.splat.kernel.InvalidPropertyException;
12 import org.splat.kernel.MissedPropertyException;
13 import org.splat.kernel.MultiplyDefinedException;
14 import org.splat.kernel.Name;
17 public class User extends Persistent implements Principal, Name {
20 @SuppressWarnings("unused")
21 private String password; // Property without getter function
23 private String username; // Unique in the user directory
26 private String display; // Optional
27 private Role role; // Roles as list (as stored into the database)
29 private String organid;
31 // Fields initialization class
32 public static class Properties extends Persistent.Properties {
33 // ------------------------------------------------------------
34 private String username = null;
35 private String password = null;;
36 private String first = null;
37 private String last = null;
38 private String display = null;
39 private Role role = null;
40 private String email = null;
41 private String organid = null;
45 public void clear () {
56 public String getOrganizationName () {
59 public String getPassword () {
62 public String getUsername () {
67 public Properties addRole (String role) throws InvalidPropertyException
69 if (role.length() == 0) throw new InvalidPropertyException("role");
70 if (this.role == null) {
71 this.role = new Role(username, role);
73 Role[] curole = this.role.toArray();
74 for (int i=0; i<curole.length; i++) if (curole[i].is(role)) return this;
75 this.role.addRole(role);
79 public Properties setDisplayName (String display) throws InvalidPropertyException
81 if (display.length() == 0) throw new InvalidPropertyException("displayname");
82 this.display = display;
85 public Properties setFirstName (String first) throws InvalidPropertyException
87 if (first.length() == 0) throw new InvalidPropertyException("firstname");
91 public Properties setMailAddress (String address) throws InvalidPropertyException
93 String[] term = address.split("@"); // Must be of the form x@y
94 if (term.length != 2) throw new InvalidPropertyException("address");
95 term = term[1].split("\\x2E"); // Must be of the form x@y.z
96 if (term.length != 2) throw new InvalidPropertyException("address");
100 public Properties setName (String last) throws InvalidPropertyException
102 if (last.length() == 0) throw new InvalidPropertyException("lastname");
106 public Properties setOrganizationName (String organization) throws InvalidPropertyException
108 if (organization.length() == 0) throw new InvalidPropertyException("organization");
109 this.organid = organization;
112 public Properties setPassword (String password) throws InvalidPropertyException
114 if (password != null) {
115 if (password.length() < 1) throw new InvalidPropertyException("password");
116 this.password = String.valueOf(password.hashCode());
120 public Properties setUsername (String username) throws InvalidPropertyException
122 if (username.length() == 0) throw new InvalidPropertyException("username");
123 this.username = username;
126 // - Global validity check
128 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
130 if (username == null) throw new MissedPropertyException("username");
131 if (first == null) throw new MissedPropertyException("firstname");
132 if (last == null) throw new MissedPropertyException("lastname");
133 if (role == null) throw new MissedPropertyException("role");
134 if (email == null) throw new MissedPropertyException("email");
135 //TODO: Check if username exists
139 // ==============================================================================================================================
141 // ==============================================================================================================================
143 // Database fetch constructor
147 // Anonymous user supposed not to be saved
148 public User (String name) {
149 // -------------------------
160 public User (Properties uprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
161 // ------------------------------
162 super(uprop); // Throws one of the above exception if not valid
163 this.username = uprop.username;
164 this.password = uprop.password;
165 this.first = uprop.first;
166 this.last = uprop.last;
167 this.display = uprop.display;
168 this.role = uprop.role;
169 this.email = uprop.email;
170 this.organid = uprop.organid;
173 // ==============================================================================================================================
174 // Public member functions
175 // ==============================================================================================================================
177 public boolean equals (Object item) {
178 // -----------------------------------
179 if (item == null) return false;
180 if (item instanceof String) {
181 return this.username.equals((String)item); // Usernames are unique
183 else if (item instanceof User) {
184 User given = (User)item;
185 if (isSaved()) return (this.getIndex() == given.getIndex());
186 else return (this.username.equals(given.username)); // Usernames are unique
192 public String getDisplayName () {
193 // -------------------------------
194 if (display == null) return last + " " + first;
198 public String getFirstName () {
199 // -----------------------------
203 public String getMailAddress () {
204 // -------------------------------
208 public String getName () {
209 // ------------------------
213 public String getOrganizationName () {
214 // ------------------------------------
218 public String getRoleNames () {
219 // -----------------------------
220 return role.getName();
223 public Role[] getRoles () {
224 // -------------------------
225 return role.toArray();
228 public String getUsername () {
229 // ----------------------------
233 public String toString () {
234 // -------------------------
235 return last + " " + first;