Salome HOME
StepServiceImpl doesn't use Database class anymore.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / User.java
1 package org.splat.dal.bo.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.security.Principal;
9
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;
15
16
17 public class User extends Persistent implements Principal, Name {
18
19 //  Persistent fields
20         @SuppressWarnings("unused")
21         private String  password;   // Property without getter function
22
23     private String  username;   // Unique in the user directory
24     private String  first;
25     private String  last;
26     private String  display;    // Optional
27     private Role    role;       // Roles as list (as stored into the database)
28     private String  email;
29     private String  organid;
30
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;
42
43 //  - Public services
44
45       public void clear () {
46         super.clear();
47         username = null;
48         password = null;;
49         first    = null;
50         last     = null;
51         display  = null;
52         role     = null;
53         email    = null;
54         organid  = null;
55       }
56           public String getOrganizationName () {
57                 return organid;
58           }
59           public String getPassword () {
60                 return password;
61           }
62           public String getUsername () {
63                 return username;
64           }
65 //  - Property setters
66           
67       public Properties addRole (String role) throws InvalidPropertyException
68       {
69         if (role.length() == 0) throw new InvalidPropertyException("role");
70         if (this.role == null) {
71           this.role = new Role(username, role);
72         } else {
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);
76         }
77         return this;
78       }
79       public Properties setDisplayName (String display) throws InvalidPropertyException
80       {
81         if (display.length() == 0) throw new InvalidPropertyException("displayname");
82         this.display = display;
83         return this;
84       }
85       public Properties setFirstName (String first) throws InvalidPropertyException
86       {
87         if (first.length() == 0) throw new InvalidPropertyException("firstname");
88         this.first = first;
89         return this;
90       }
91       public Properties setMailAddress (String address) throws InvalidPropertyException
92       {
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");
97         this.email = address;
98         return this;
99       }
100       public Properties setName (String last) throws InvalidPropertyException
101       {
102         if (last.length() == 0) throw new InvalidPropertyException("lastname");
103         this.last = last;
104         return this;
105       }
106       public Properties setOrganizationName (String organization) throws InvalidPropertyException
107       {
108         if (organization.length() == 0) throw new InvalidPropertyException("organization");
109         this.organid = organization;
110         return this;
111       }
112       public Properties setPassword (String password) throws InvalidPropertyException
113       {
114         if (password != null) {
115           if (password.length() < 1) throw new InvalidPropertyException("password");
116           this.password = String.valueOf(password.hashCode());
117         }
118         return this;
119       }
120       public Properties setUsername (String username) throws InvalidPropertyException
121       {
122         if (username.length() == 0) throw new InvalidPropertyException("username");
123         this.username = username;
124         return this;
125       }
126 //  - Global validity check
127       
128       public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
129       {
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        
136       }
137     }
138
139 //  ==============================================================================================================================
140 //  Constructors
141 //  ==============================================================================================================================
142
143 //  Database fetch constructor
144     protected User () {
145 //  --------------
146         }
147 //  Anonymous user supposed not to be saved
148         public User (String name) {
149 //  -------------------------
150           username = null;
151           password = null;
152           first    = null;
153           last     = null;
154           display  = name;
155           role     = null;
156           email    = null;
157           organid  = null;
158         }
159 //  New user
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;
171         }
172
173 //  ==============================================================================================================================
174 //  Public member functions
175 //  ==============================================================================================================================
176
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
182       }
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
187       } else {
188         return false;
189       }
190     }
191
192     public String getDisplayName () {
193 //  -------------------------------
194       if (display == null) return last + " " + first;
195       else                 return display;
196     }
197
198     public String getFirstName () {
199 //  -----------------------------
200       return first;
201     }
202
203     public String getMailAddress () {
204 //  -------------------------------
205       return email;
206     }
207
208     public String getName () {
209 //  ------------------------
210       return last;
211     }
212
213     public String getOrganizationName () {
214 //  ------------------------------------
215       return organid;
216     }
217
218     public String getRoleNames () {
219 //  -----------------------------
220       return role.getName();
221     }
222
223     public Role[] getRoles () {
224 //  -------------------------
225       return role.toArray();
226     }
227
228     public String getUsername () {
229 //  ----------------------------
230       return username;
231     }
232
233     public String toString () {
234 //  -------------------------
235       return last + " " + first;
236     }
237 }