KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > repository > User


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.repository;
18
19 import java.util.List JavaDoc;
20
21 import javax.faces.context.FacesContext;
22
23 import org.alfresco.model.ContentModel;
24 import org.alfresco.repo.configuration.ConfigurableService;
25 import org.alfresco.service.ServiceRegistry;
26 import org.alfresco.service.cmr.repository.ChildAssociationRef;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.search.SearchService;
30 import org.alfresco.service.namespace.NamespaceService;
31 import org.alfresco.service.namespace.QName;
32 import org.alfresco.web.app.Application;
33
34 /**
35  * Bean that represents the currently logged in user
36  *
37  * @author gavinc
38  */

39 public final class User
40 {
41    private String JavaDoc homeSpaceId;
42    private String JavaDoc userName;
43    private String JavaDoc ticket;
44    private NodeRef person;
45    private String JavaDoc fullName = null;
46    private Boolean JavaDoc administrator = null;
47    
48    /** cached ref to our user preferences node */
49    private NodeRef preferencesFolderRef = null;
50    
51    /**
52     * Constructor
53     *
54     * @param userName constructor for the user
55     */

56    public User(String JavaDoc userName, String JavaDoc ticket, NodeRef person)
57    {
58       if (userName == null || ticket == null || person == null)
59       {
60          throw new IllegalArgumentException JavaDoc("All user details are mandatory!");
61       }
62       
63       this.userName = userName;
64       this.ticket = ticket;
65       this.person = person;
66    }
67    
68    /**
69     * @return The user name
70     */

71    public String JavaDoc getUserName()
72    {
73       return this.userName;
74    }
75    
76    /**
77     * Return the full name of the Person this User represents
78     *
79     * @param service NodeService to use
80     *
81     * @return The full name
82     */

83    public String JavaDoc getFullName(NodeService service)
84    {
85       if (this.fullName == null)
86       {
87          String JavaDoc lastName = (String JavaDoc)service.getProperty(this.person, ContentModel.PROP_LASTNAME);
88          this.fullName = service.getProperty(this.person, ContentModel.PROP_FIRSTNAME) +
89                          (lastName != null ? (" " + lastName) : "");
90       }
91       
92       return this.fullName;
93    }
94    
95    /**
96     * @return Retrieves the user's home space (this may be the id of the company home space)
97     */

98    public String JavaDoc getHomeSpaceId()
99    {
100       return this.homeSpaceId;
101    }
102
103    /**
104     * @param homeSpaceId Sets the id of the users home space
105     */

106    public void setHomeSpaceId(String JavaDoc homeSpaceId)
107    {
108       this.homeSpaceId = homeSpaceId;
109    }
110
111    /**
112     * @return Returns the ticket.
113     */

114    public String JavaDoc getTicket()
115    {
116       return this.ticket;
117    }
118
119    
120    /**
121     * @return Returns the person NodeRef
122     */

123    public NodeRef getPerson()
124    {
125       return this.person;
126    }
127    
128    /**
129     * @return If the current user has Admin Authority
130     */

131    public boolean isAdmin()
132    {
133       if (administrator == null)
134       {
135          administrator = Repository.getServiceRegistry(FacesContext.getCurrentInstance())
136                .getAuthorityService().hasAdminAuthority();
137       }
138       
139       return administrator;
140    }
141    
142    /**
143     * Get or create the node used to store user preferences.
144     * Utilises the 'configurable' aspect on the Person linked to this user.
145     */

146    public synchronized NodeRef getUserPreferencesRef()
147    {
148       if (this.preferencesFolderRef == null)
149       {
150          FacesContext fc = FacesContext.getCurrentInstance();
151          ServiceRegistry registry = Repository.getServiceRegistry(fc);
152          NodeService nodeService = registry.getNodeService();
153          SearchService searchService = registry.getSearchService();
154          NamespaceService namespaceService = registry.getNamespaceService();
155          ConfigurableService configurableService = Repository.getConfigurableService(fc);
156          
157          NodeRef person = Application.getCurrentUser(fc).getPerson();
158          if (nodeService.hasAspect(person, ContentModel.ASPECT_CONFIGURABLE) == false)
159          {
160             // create the configuration folder for this Person node
161
configurableService.makeConfigurable(person);
162          }
163          
164          // target of the assoc is the configurations folder ref
165
NodeRef configRef = configurableService.getConfigurationFolder(person);
166          if (configRef == null)
167          {
168             throw new IllegalStateException JavaDoc("Unable to find associated 'configurations' folder for node: " + person);
169          }
170          
171          String JavaDoc xpath = NamespaceService.APP_MODEL_PREFIX + ":" + "preferences";
172          List JavaDoc<NodeRef> nodes = searchService.selectNodes(
173                configRef,
174                xpath,
175                null,
176                namespaceService,
177                false);
178          
179          NodeRef prefRef;
180          if (nodes.size() == 1)
181          {
182             prefRef = nodes.get(0);
183          }
184          else
185          {
186             // create the preferences Node for this user
187
ChildAssociationRef childRef = nodeService.createNode(
188                   configRef,
189                   ContentModel.ASSOC_CONTAINS,
190                   QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "preferences"),
191                   ContentModel.TYPE_CMOBJECT);
192             
193             prefRef = childRef.getChildRef();
194          }
195          
196          this.preferencesFolderRef = prefRef;
197       }
198       
199       return this.preferencesFolderRef;
200    }
201 }
202
Popular Tags