View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.connector;
20  
21  import java.io.Serializable;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.springframework.extensions.webscripts.ui.common.StringUtils;
26  
27  /**
28   * Default user profile object.
29   * 
30   * @author muzquiano
31   * @author kevinr
32   */
33  public class User implements java.security.Principal, Serializable
34  {
35      public static String PROP_ID = "id";
36      public static String PROP_FIRST_NAME = "firstName";
37      public static String PROP_MIDDLE_NAME = "middleName";
38      public static String PROP_LAST_NAME = "lastName";
39      public static String PROP_EMAIL = "email";
40      public static String PROP_ORGANIZATION = "organization";
41      public static String PROP_JOB_TITLE = "jobtitle";
42      public static String PROP_LOCATION = "location";
43      public static String PROP_BIOGRAPHY = "persondescription";
44      public static String PROP_TELEPHONE = "telephone";
45      public static String PROP_MOBILE_PHONE = "mobile";
46      public static String PROP_SKYPE = "skype";
47      public static String PROP_INSTANTMSG = "instantmsg";
48      public static String PROP_COMPANY_ADDRESS1 = "companyaddress1";
49      public static String PROP_COMPANY_ADDRESS2 = "companyaddress2";
50      public static String PROP_COMPANY_ADDRESS3 = "companyaddress3";
51      public static String PROP_COMPANY_POSTCODE = "companypostcode";
52      public static String PROP_COMPANY_TELEPHONE = "companytelephone";
53      public static String PROP_COMPANY_FAX = "companyfax";
54      public static String PROP_COMPANY_EMAIL = "companyemail";
55      
56      protected String fullName = null;
57      protected boolean isAdmin = false;
58      protected boolean isGuest = false;
59      protected Map<String, Serializable> map = null;
60      
61      /**
62       * Instantiates a new user.
63       * 
64       * @param id the id
65       */
66      public User(String id)
67      {
68          this.map = new HashMap<String, Serializable>(32);
69          setProperty(PROP_ID, id);
70      }
71  
72      /**
73       * Instantiates a new user.
74       * 
75       * @param id the id
76       * @param isAdmin the is admin
77       */
78      public User(String id, boolean isAdmin, boolean isGuest)
79      {
80          this(id);
81          this.isAdmin = isAdmin;
82          this.isGuest = isGuest;
83      }
84  
85      /* (non-Javadoc)
86       * @see java.security.Principal#getName()
87       */
88      public String getName()
89      {
90          return getId();
91      }
92      
93      /**
94       * Gets the id - this is usually the username.
95       * 
96       * @return the id
97       */
98      public String getId()
99      {
100         return getStringProperty(PROP_ID);
101     }
102 
103     //
104     // Core Properties
105     //
106 
107     /**
108      * Gets the first name.
109      * 
110      * @return the first name
111      */
112     public String getFirstName()
113     {
114         return getStringProperty(PROP_FIRST_NAME);
115     }
116 
117     /**
118      * Sets the first name.
119      * 
120      * @param value the new first name
121      */
122     public void setFirstName(String value)
123     {
124         setProperty(PROP_FIRST_NAME, value);
125     }
126     
127     /**
128      * Gets the last name.
129      * 
130      * @return the last name
131      */
132     public String getLastName()
133     {
134         return getStringProperty(PROP_LAST_NAME);
135     }
136 
137     /**
138      * Sets the last name.
139      * 
140      * @param value the new last name
141      */
142     public void setLastName(String value)
143     {
144         setProperty(PROP_LAST_NAME, value);
145     }
146 
147     /**
148      * Gets the middle name.
149      * 
150      * @return the middle name
151      */
152     public String getMiddleName()
153     {
154         return getStringProperty(PROP_MIDDLE_NAME);
155     }
156 
157     /**
158      * Sets the middle name.
159      * 
160      * @param value the new middle name
161      */
162     public void setMiddleName(String value)
163     {
164         setProperty(PROP_MIDDLE_NAME, value);
165     }
166     
167     /**
168      * Gets the email.
169      * 
170      * @return the email
171      */
172     public String getEmail()
173     {
174         return getStringProperty(PROP_EMAIL);
175     }
176 
177     /**
178      * Sets the email.
179      * 
180      * @param value the new email
181      */
182     public void setEmail(String value)
183     {
184         setProperty(PROP_EMAIL, value);
185     }
186     
187     /**
188      * Gets the organization.
189      * 
190      * @return the organization
191      */
192     public String getOrganization()
193     {
194         return getStringProperty(PROP_ORGANIZATION);
195     }
196 
197     /**
198      * Sets the organization.
199      * 
200      * @param value the new organization
201      */
202     public void setOrganization(String value)
203     {
204         setProperty(PROP_ORGANIZATION, value);
205     }
206     
207     /**
208      * Gets the job title.
209      * 
210      * @return the job title
211      */
212     public String getJobTitle()
213     {
214         return getStringProperty(PROP_JOB_TITLE);
215     }
216 
217     /**
218      * Sets the job title.
219      * 
220      * @param value the new job title
221      */
222     public void setJobTitle(String value)
223     {
224         setProperty(PROP_JOB_TITLE, value);
225     }
226     
227     /**
228      * Get the location
229      * 
230      * @return the location
231      */
232     public String getLocation()
233     {
234         return getStringProperty(PROP_LOCATION);
235     }
236 
237     /**
238      * Set the location
239      * 
240      * @param value the new location
241      */
242     public void setLocation(String value)
243     {
244         setProperty(PROP_LOCATION, value);
245     }
246     
247     /**
248      * Get the biography
249      * 
250      * @return the biography
251      */
252     public String getBiography()
253     {
254         return getStringProperty(PROP_BIOGRAPHY);
255     }
256 
257     /**
258      * Set the biography
259      * 
260      * @param value the new biography
261      */
262     public void setBiography(String value)
263     {
264         if (value != null)
265         {
266             value = StringUtils.stripUnsafeHTMLTags(value);
267         }
268         setProperty(PROP_BIOGRAPHY, value);
269     }
270 
271     /**
272      * Gets the home phone.
273      * 
274      * @return the home phone
275      */
276     public String getTelephone()
277     {
278         return getStringProperty(PROP_TELEPHONE);
279     }
280 
281     /**
282      * Sets the home phone.
283      * 
284      * @param value the new home phone
285      */
286     public void setTelephone(String value)
287     {
288         setProperty(PROP_TELEPHONE, value);
289     }
290     
291     /**
292      * Gets the mobile phone.
293      * 
294      * @return the mobile phone
295      */
296     public String getMobilePhone()
297     {
298         return getStringProperty(PROP_MOBILE_PHONE);
299     }
300 
301     /**
302      * Sets the mobile phone.
303      * 
304      * @param value the new mobile phone
305      */
306     public void setMobilePhone(String value)
307     {
308         setProperty(PROP_MOBILE_PHONE, value);
309     }
310     
311     /**
312      * Gets the skype id.
313      * 
314      * @return the skype id
315      */
316     public String getSkype()
317     {
318         return getStringProperty(PROP_SKYPE);
319     }
320 
321     /**
322      * Sets the skype id
323      * 
324      * @param value the new skype id
325      */
326     public void setSkype(String value)
327     {
328         setProperty(PROP_SKYPE, value);
329     }
330     
331     /**
332      * Gets the instant msg id.
333      * 
334      * @return the instant msg id
335      */
336     public String getInstantMsg()
337     {
338         return getStringProperty(PROP_INSTANTMSG);
339     }
340 
341     /**
342      * Sets the instant msg id
343      * 
344      * @param value the new instant msg id
345      */
346     public void setInstantMsg(String value)
347     {
348         setProperty(PROP_INSTANTMSG, value);
349     }
350     
351     /**
352      * Gets Company Address1
353      * 
354      * @return the Company Address1
355      */
356     public String getCompanyAddress1()
357     {
358         return getStringProperty(PROP_COMPANY_ADDRESS1);
359     }
360 
361     /**
362      * Sets the Company Address1
363      * 
364      * @param value the new Company Address1
365      */
366     public void setCompanyAddress1(String value)
367     {
368         setProperty(PROP_COMPANY_ADDRESS1, value);
369     }
370 
371     /**
372      * Gets Company Address2
373      * 
374      * @return the Company Address2
375      */
376     public String getCompanyAddress2()
377     {
378         return getStringProperty(PROP_COMPANY_ADDRESS2);
379     }
380 
381     /**
382      * Sets the Company Address2
383      * 
384      * @param value the new Company Address2
385      */
386     public void setCompanyAddress2(String value)
387     {
388         setProperty(PROP_COMPANY_ADDRESS2, value);
389     }
390     
391     /**
392      * Gets Company Address3
393      * 
394      * @return the Company Address3
395      */
396     public String getCompanyAddress3()
397     {
398         return getStringProperty(PROP_COMPANY_ADDRESS3);
399     }
400 
401     /**
402      * Sets the Company Address3
403      * 
404      * @param value the new Company Address3
405      */
406     public void setCompanyAddress3(String value)
407     {
408         setProperty(PROP_COMPANY_ADDRESS3, value);
409     }
410     
411     /**
412      * Gets Company Postcode
413      * 
414      * @return the Company Postcode
415      */
416     public String getCompanyPostcode()
417     {
418         return getStringProperty(PROP_COMPANY_POSTCODE);
419     }
420 
421     /**
422      * Sets the Company Postcode
423      * 
424      * @param value the new Company Postcode
425      */
426     public void setCompanyPostcode(String value)
427     {
428         setProperty(PROP_COMPANY_POSTCODE, value);
429     }
430     
431     /**
432      * Gets Company Telephone
433      * 
434      * @return the Company Telephone
435      */
436     public String getCompanyTelephone()
437     {
438         return getStringProperty(PROP_COMPANY_TELEPHONE);
439     }
440 
441     /**
442      * Sets the Company Telephone
443      * 
444      * @param value the new Company Telephone
445      */
446     public void setCompanyTelephone(String value)
447     {
448         setProperty(PROP_COMPANY_TELEPHONE, value);
449     }
450     
451     /**
452      * Gets Company Fax
453      * 
454      * @return the Company Fax
455      */
456     public String getCompanyFax()
457     {
458         return getStringProperty(PROP_COMPANY_FAX);
459     }
460 
461     /**
462      * Sets the Company Fax
463      * 
464      * @param value the new Company Fax
465      */
466     public void setCompanyFax(String value)
467     {
468         setProperty(PROP_COMPANY_FAX, value);
469     }
470     
471     /**
472      * Gets Company Email
473      * 
474      * @return the Company Email
475      */
476     public String getCompanyEmail()
477     {
478         return getStringProperty(PROP_COMPANY_EMAIL);
479     }
480 
481     /**
482      * Sets the Company Email
483      * 
484      * @param value the new Company Email
485      */
486     public void setCompanyEmail(String value)
487     {
488         setProperty(PROP_COMPANY_EMAIL, value);
489     }
490     
491     
492     /**
493      * Gets the property.
494      * 
495      * @param key the key
496      * 
497      * @return the property
498      */
499     public Object getProperty(String key)
500     {
501         return (Object) map.get(key);
502     }
503 
504     /**
505      * Gets the string property.
506      * 
507      * @param key the key
508      * 
509      * @return the string property
510      */
511     public String getStringProperty(String key)
512     {
513         return (String) map.get(key);
514     }
515 
516     /**
517      * Sets the property.
518      * 
519      * @param key the key
520      * @param value the value
521      */
522     public void setProperty(String key, Serializable value)
523     {
524         map.put(key, value);
525     }
526 
527     /**
528      * Gets the properties.
529      * 
530      * @return the properties
531      */
532     public Map<String, Serializable> getProperties()
533     {
534         return map;
535     }
536 
537     /**
538      * Checks if is admin.
539      * 
540      * @return the isAdmin
541      */
542     public boolean isAdmin()
543     {
544         return this.isAdmin;
545     }
546     
547     /**
548      * Returns <code>true</code> if this user is a guest user
549      * 
550      * @return <code>true</code> if this user is a guest user
551      */
552     public boolean isGuest()
553     {
554         return this.isGuest;
555     }
556 
557     /* (non-Javadoc)
558      * @see java.lang.Object#toString()
559      */
560     @Override
561     public String toString()
562     {
563         return map.toString();
564     }
565     
566     /**
567      * Provides the full name for the user.  This makes a best attempt at
568      * building the full name based on what it knows about the user.
569      * 
570      * If a first name is not known, the returned name will be the user id
571      * of the user.
572      * 
573      * If a first name is known, then the first name will be returned.
574      * If a first and middle name are known, then the first and middle name
575      * will be returned.
576      * 
577      * Valid full names are therefore:
578      * 
579      *      jsmith
580      *      Joe
581      *      Joe D
582      *      Joe Smith
583      *      Joe D Smith
584      * 
585      * @return A valid full name
586      */
587     public String getFullName()
588     {
589         if (this.fullName == null)
590         {
591             boolean hasFirstName = (getFirstName() != null && getFirstName().length() != 0);
592             boolean hasMiddleName = (getMiddleName() != null && getMiddleName().length() != 0);
593             boolean hasLastName = (getLastName() != null && getLastName().length() != 0);
594             
595             // if they don't have a first name, then use their user id
596             this.fullName = getId();
597             if (hasFirstName)
598             {
599                 this.fullName = getFirstName();
600                 
601                 if (hasMiddleName)
602                 {
603                     this.fullName += " " + getMiddleName();
604                 }
605                 
606                 if (hasLastName)
607                 {
608                     this.fullName += " " + getLastName();
609                 }
610             }
611         }
612         
613         return this.fullName;
614     }
615     
616     /**
617      * Persist this user
618      */
619     public void save()
620     {
621     }
622 }