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;
20  
21  import org.springframework.extensions.surf.FrameworkUtil;
22  import org.springframework.extensions.surf.RequestContext;
23  import org.springframework.extensions.surf.exception.UserFactoryException;
24  import org.springframework.extensions.webscripts.ScriptableMap;
25  import org.springframework.extensions.webscripts.ScriptableWrappedMap;
26  import org.springframework.extensions.webscripts.connector.User;
27  
28  /**
29   * Read-only root-scoped script object wrapping the current user for
30   * the current thread of execution.
31   * 
32   * The following is equivalent:
33   * 
34   * var organization = user.organization;
35   * var organization = user.properties.organization;
36   * var organization = user.properties["organization"];
37   * 
38   * @author muzquiano
39   * @author kevinr
40   */
41  public final class ScriptUser extends ScriptBase
42  {
43      private final User user;
44      
45      /**
46       * Instantiates a new ScriptUser object which wraps a given request
47       * context and framework user object.
48       * 
49       * @param context the render context
50       * @param user the user
51       */
52      public ScriptUser(RequestContext context, User user)
53      {
54          super(context);
55          
56          // store a reference to the user object
57          this.user = user;
58      }
59          
60      /**
61       * Provides an associative array of properties that can be accessed via
62       * scripting by using the .properties accessor.
63       * 
64       * @return the properties
65       */
66      protected ScriptableMap buildProperties()
67      {
68          if (this.properties == null)
69          {
70              this.properties = new ScriptableWrappedMap(user.getProperties());
71          }
72          
73          return this.properties;
74      }
75      
76      /**
77       * Gets the id.
78       * 
79       * @return the id
80       */
81      public String getId()
82      {
83          return this.user.getId();
84      }
85      
86      /**
87       * Gets the name (generally this is the username - i.e. same as id)
88       * 
89       * @return the name
90       */
91      public String getName()
92      {
93          return this.user.getName();
94      }
95      
96      public String getFullName()
97      {
98          return this.user.getFullName();
99      }
100     
101     public String getFirstName()
102     {
103         return this.user.getFirstName();
104     }
105     
106     public void setFirstName(String value)
107     {
108         this.user.setFirstName(value);
109     }
110     
111     public String getLastName()
112     {
113         return this.user.getLastName();
114     }
115     
116     public void setLastName(String value)
117     {
118         this.user.setLastName(value);
119     }
120 
121     public String getMiddleName()
122     {
123         return this.user.getMiddleName();
124     }
125     
126     public void setMiddleName(String value)
127     {
128         this.user.setMiddleName(value);
129     }
130     
131     public String getEmail()
132     {
133         return this.user.getEmail();
134     }
135     
136     public void setEmail(String value)
137     {
138         this.user.setEmail(value);
139     }
140     
141     public String getOrganization()
142     {
143         return this.user.getOrganization();
144     }
145     
146     public void setOrganization(String value)
147     {
148         this.user.setEmail(value);
149     }
150     
151     public String getJobTitle()
152     {
153         return this.user.getJobTitle();
154     }
155     
156     public void setJobTitle(String value)
157     {
158         this.user.setJobTitle(value);
159     }
160     
161     public String getLocation()
162     {
163         return this.user.getLocation();
164     }
165     
166     public void setLocation(String value)
167     {
168         this.user.setLocation(value);
169     }
170     
171     public String getBiography()
172     {
173         return this.user.getBiography();
174     }
175     
176     public void setBiography(String value)
177     {
178         this.user.setBiography(value);
179     }
180     
181     public String getTelephone()
182     {
183         return this.user.getTelephone();
184     }
185     
186     public void setTelephone(String value)
187     {
188         this.user.setTelephone(value);
189     }
190     
191     public String getMobilePhone()
192     {
193         return this.user.getMobilePhone();
194     }
195     
196     public void setMobilePhone(String value)
197     {
198         this.user.setMobilePhone(value);
199     }
200     
201     public String getSkype()
202     {
203         return this.user.getSkype();
204     }
205     
206     public void setSkype(String value)
207     {
208         this.user.setSkype(value);
209     }
210     
211     public String getInstantMsg()
212     {
213         return this.user.getInstantMsg();
214     }
215     
216     public void setInstantMsg(String value)
217     {
218         this.user.setInstantMsg(value);
219     }
220     
221     public String getCompanyPostcode()
222     {
223         return this.user.getCompanyPostcode();
224     }
225     
226     public void setCompanyPostcode(String value)
227     {
228         this.user.setCompanyPostcode(value);
229     }
230     
231     public String getCompanyTelephone()
232     {
233         return this.user.getCompanyTelephone();
234     }
235     
236     public void setCompanyTelephone(String value)
237     {
238         this.user.setCompanyTelephone(value);
239     }
240     
241     public String getCompanyFax()
242     {
243         return this.user.getCompanyFax();
244     }
245     
246     public void setCompanyFax(String value)
247     {
248         this.user.setCompanyFax(value);
249     }
250     
251     public String getCompanyEmail()
252     {
253         return this.user.getCompanyEmail();
254     }
255     
256     public void setCompanyEmail(String value)
257     {
258         this.user.setCompanyEmail(value);
259     }
260     
261     public String getCompanyAddress1()
262     {
263         return this.user.getCompanyAddress1();
264     }
265     
266     public void setCompanyAddress1(String value)
267     {
268         this.user.setCompanyAddress1(value);
269     }
270 
271     public String getCompanyAddress2()
272     {
273         return this.user.getCompanyAddress2();
274     }
275     
276     public void setCompanyAddress2(String value)
277     {
278         this.user.setCompanyAddress2(value);
279     }
280     
281     public String getCompanyAddress3()
282     {
283         return this.user.getCompanyAddress3();
284     }
285     
286     public void setCompanyAddress3(String value)
287     {
288         this.user.setCompanyAddress3(value);
289     }
290     
291     public boolean getIsAdmin()
292     {
293         return this.user.isAdmin();
294     }
295     
296     public boolean getIsGuest()
297     {
298         return this.user.isGuest();
299     }
300         
301     /**
302      * Persist user changes
303      */
304     public void save()
305     {
306         this.user.save();
307     }
308     
309     /**
310      * Retrieve a user object with populated details for the given user Id
311      * 
312      * @param userId
313      * 
314      * @return ScriptUser
315      */
316     public ScriptUser getUser(String userId)
317     {
318         try
319         {
320             User user = FrameworkUtil.getServiceRegistry().getUserFactory().loadUser(this.context, userId);
321             return new ScriptUser(this.context, user);
322         }
323         catch (UserFactoryException err)
324         {
325             // unable to load user details - so cannot return a user to the caller
326             return null;
327         }
328     }
329     
330     
331     /* (non-Javadoc)
332      * @see java.lang.Object#toString()
333      */
334     @Override
335     public String toString()
336     {
337         return user.getProperties().toString();
338     }    
339 }