Salome HOME
767523fd91809a58dd308982fcdcc6afcf06f423
[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-2014
6  */
7
8 import java.security.Principal;
9
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.kernel.MissedPropertyException;
12 import org.splat.kernel.MultiplyDefinedException;
13 import org.splat.kernel.Name;
14
15
16 public class User extends Persistent implements Principal, Name {
17
18 //  Persistent fields
19         @SuppressWarnings("unused")
20         private String  password;   // Property without getter function
21
22     private String  username;   // Unique in the user directory
23     private String  first;
24     private String  last;
25     private String  display;    // Optional
26     private Role    role;       // Roles as list (as stored into the database)
27     private String  email;
28     private String  organid;
29
30 //  Fields initialization class    
31     public static class Properties extends Persistent.Properties {
32 //  ------------------------------------------------------------
33       private String  username = null;
34       private String  password = null;;
35       private String  first    = null;
36       private String  last     = null;
37           private String  display  = null;
38           private Role    role     = null;
39           private String  email    = null;
40       private String  organid  = null;
41
42 //  - Public services
43
44       @Override
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 (final String role) throws InvalidPropertyException
68       {
69         if (role.length() == 0) {
70                         throw new InvalidPropertyException("role");
71                 }
72         if (this.role == null) {
73           this.role = new Role(username, role);
74         } else {
75           Role[] curole = this.role.toArray();
76           for (int i=0; i<curole.length; i++) {
77                         if (curole[i].is(role)) {
78                                 return this;
79                         }
80                 }               
81           this.role.addRole(role);
82         }
83         return this;
84       }
85       public Properties setDisplayName (final String display) throws InvalidPropertyException
86       {
87         if (display.length() == 0) {
88                         throw new InvalidPropertyException("displayname");
89                 }
90         this.display = display;
91         return this;
92       }
93       public Properties setFirstName (final String first) throws InvalidPropertyException
94       {
95         if (first.length() == 0) {
96                         throw new InvalidPropertyException("firstname");
97                 }
98         this.first = first;
99         return this;
100       }
101       public Properties setMailAddress (final String address) throws InvalidPropertyException
102       {
103         String[] term = address.split("@");     // Must be of the form x@y
104         if (term.length != 2) {
105                         throw new InvalidPropertyException("address");
106                 }
107         term = term[1].split("\\x2E");          // Must be of the form x@y.z
108         if (term.length != 2) {
109                         throw new InvalidPropertyException("address");
110                 }
111         this.email = address;
112         return this;
113       }
114       public Properties setName (final String last) throws InvalidPropertyException
115       {
116         if (last.length() == 0) {
117                         throw new InvalidPropertyException("lastname");
118                 }
119         this.last = last;
120         return this;
121       }
122       public Properties setOrganizationName (final String organization) throws InvalidPropertyException
123       {
124         if (organization.length() == 0) {
125                         throw new InvalidPropertyException("organization");
126                 }
127         this.organid = organization;
128         return this;
129       }
130       public Properties setPassword (final String password) throws InvalidPropertyException
131       {
132         if (password != null) {
133           if (password.length() < 1) {
134                         throw new InvalidPropertyException("password");
135                 }
136           this.password = String.valueOf(password.hashCode());
137         }
138         return this;
139       }
140       public Properties setUsername (final String username) throws InvalidPropertyException
141       {
142         if (username.length() == 0) {
143                         throw new InvalidPropertyException("username");
144                 }
145         this.username = username;
146         return this;
147       }
148 //  - Global validity check
149       
150       public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
151       {
152         if (username == null) {
153                         throw new MissedPropertyException("username");
154                 }
155         if (first == null) {
156                         throw new MissedPropertyException("firstname");
157                 }
158         if (last == null) {
159                         throw new MissedPropertyException("lastname");
160                 }
161         if (role == null) {
162                         throw new MissedPropertyException("role");
163                 }
164         if (email == null) {
165                         throw new MissedPropertyException("email");
166 //TODO: Check if username exists        
167                 }
168       }
169     }
170
171 //  ==============================================================================================================================
172 //  Constructors
173 //  ==============================================================================================================================
174
175 //  Database fetch constructor
176     protected User () {
177 //  --------------
178         }
179 //  Anonymous user supposed not to be saved
180         public User (final String name) {
181 //  -------------------------
182           username = null;
183           password = null;
184           first    = null;
185           last     = null;
186           display  = name;
187           role     = null;
188           email    = null;
189           organid  = null;
190         }
191 //  New user
192         public User (final Properties uprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
193 //  ------------------------------
194           super(uprop);   // Throws one of the above exception if not valid
195           this.username = uprop.username;
196           this.password = uprop.password;
197           this.first    = uprop.first;
198           this.last     = uprop.last;
199           this.display  = uprop.display;
200           this.role     = uprop.role;
201           this.email    = uprop.email;
202           this.organid  = uprop.organid;
203         }
204
205 //  ==============================================================================================================================
206 //  Public member functions
207 //  ==============================================================================================================================
208
209     @Override
210         public boolean equals (final Object item) {
211 //  -----------------------------------
212       if (item == null) {
213                 return false;
214         }
215       if (item instanceof String) {
216         return  this.username.equals(item);                       // Usernames are unique
217       }
218       else if (item instanceof User) {
219         User given = (User)item;
220         if (isSaved()) {
221                         return (this.getIndex() ==  given.getIndex());
222                 } else {
223                         return (given.username.equals(this.username));     // Usernames are unique
224                 }
225       } else {
226         return false;
227       }
228     }
229
230     public String getDisplayName () {
231 //  -------------------------------
232       if (display == null) {
233                 return last + " " + first;
234         } else {
235                 return display;
236         }
237     }
238
239     public String getFirstName () {
240 //  -----------------------------
241       return first;
242     }
243
244     public String getMailAddress () {
245 //  -------------------------------
246       return email;
247     }
248
249     public String getName () {
250 //  ------------------------
251       return last;
252     }
253
254     public String getOrganizationName () {
255 //  ------------------------------------
256       return organid;
257     }
258
259     public String getRoleNames () {
260 //  -----------------------------
261       return role.getName();
262     }
263
264     public Role[] getRoles () {
265 //  -------------------------
266       return role.toArray();
267     }
268
269     public String getUsername () {
270 //  ----------------------------
271       return username;
272     }
273
274     /** 
275      * {@inheritDoc}
276      * @see org.splat.dal.bo.kernel.Persistent#toString()
277      */
278     @Override
279         public String toString () {
280       return last + " " + first;
281     }
282         /**
283          * Get the role.
284          * @return the role
285          */
286         public Role getRole() {
287                 return role;
288         }
289 }