PageRenderTime 85ms CodeModel.GetById 71ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/root/projects/remote-api/source/java/org/alfresco/rest/api/impl/PeopleImpl.java

https://github.com/deas/alfresco
Java | 358 lines | 265 code | 38 blank | 55 comment | 23 complexity | a5ef7ea8a60ff68acad4f75cc6a8bac6 MD5 | raw file
  1/*
  2 * Copyright (C) 2005-2012 Alfresco Software Limited.
  3 *
  4 * This file is part of Alfresco
  5 *
  6 * Alfresco is free software: you can redistribute it and/or modify
  7 * it under the terms of the GNU Lesser General Public License as published by
  8 * the Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * Alfresco is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU Lesser General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19/*
 20 * Copyright (C) 2005-2012 Alfresco Software Limited.
 21 *
 22 * This file is part of Alfresco
 23 *
 24 * Alfresco is free software: you can redistribute it and/or modify
 25 * it under the terms of the GNU Lesser General Public License as published by
 26 * the Free Software Foundation, either version 3 of the License, or
 27 * (at your option) any later version.
 28 *
 29 * Alfresco is distributed in the hope that it will be useful,
 30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 32 * GNU Lesser General Public License for more details.
 33 *
 34 * You should have received a copy of the GNU Lesser General Public License
 35 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 36 */
 37package org.alfresco.rest.api.impl;
 38
 39import java.io.Serializable;
 40import java.util.HashMap;
 41import java.util.List;
 42import java.util.Map;
 43
 44import org.alfresco.model.ContentModel;
 45import org.alfresco.repo.security.authentication.AuthenticationUtil;
 46import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
 47import org.alfresco.rest.api.Nodes;
 48import org.alfresco.rest.api.People;
 49import org.alfresco.rest.api.Sites;
 50import org.alfresco.rest.api.model.Company;
 51import org.alfresco.rest.api.model.Person;
 52import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
 53import org.alfresco.service.cmr.repository.AssociationRef;
 54import org.alfresco.service.cmr.repository.ContentData;
 55import org.alfresco.service.cmr.repository.ContentReader;
 56import org.alfresco.service.cmr.repository.ContentService;
 57import org.alfresco.service.cmr.repository.NodeRef;
 58import org.alfresco.service.cmr.repository.NodeService;
 59import org.alfresco.service.cmr.security.AuthenticationService;
 60import org.alfresco.service.cmr.security.NoSuchPersonException;
 61import org.alfresco.service.cmr.security.PersonService;
 62import org.alfresco.service.cmr.site.SiteService;
 63import org.alfresco.service.cmr.thumbnail.ThumbnailService;
 64import org.alfresco.service.cmr.usage.ContentUsageService;
 65import org.alfresco.service.namespace.QName;
 66
 67/**
 68 * Centralises access to people services and maps between representations.
 69 * 
 70 * @author steveglover
 71 * @since publicapi1.0
 72 */
 73public class PeopleImpl implements People
 74{
 75	protected Nodes nodes;
 76	protected Sites sites;
 77
 78	protected SiteService siteService;
 79	protected NodeService nodeService;
 80    protected PersonService personService;
 81    protected AuthenticationService authenticationService;
 82    protected ContentUsageService contentUsageService;
 83    protected ContentService contentService;
 84    protected ThumbnailService thumbnailService;
 85
 86	public void setSites(Sites sites)
 87	{
 88		this.sites = sites;
 89	}
 90
 91	public void setSiteService(SiteService siteService)
 92	{
 93		this.siteService = siteService;
 94	}
 95
 96	public void setNodes(Nodes nodes)
 97	{
 98		this.nodes = nodes;
 99	}
100
101	public void setNodeService(NodeService nodeService)
102    {
103        this.nodeService = nodeService;
104    }
105	
106	public void setPersonService(PersonService personService)
107    {
108		this.personService = personService;
109	}
110	
111	public void setAuthenticationService(AuthenticationService authenticationService)
112    {
113		this.authenticationService = authenticationService;
114	}
115
116	public void setContentUsageService(ContentUsageService contentUsageService)
117    {
118		this.contentUsageService = contentUsageService;
119	}
120
121	public void setContentService(ContentService contentService)
122	{
123		this.contentService = contentService;
124	}
125	
126	public void setThumbnailService(ThumbnailService thumbnailService)
127    {
128		this.thumbnailService = thumbnailService;
129	}
130	
131	public String validatePerson(String personId)
132	{
133		return validatePerson(personId, false);
134	}
135
136	public String validatePerson(String personId, boolean validateIsCurrentUser)
137	{
138    	if(personId.equalsIgnoreCase(DEFAULT_USER))
139    	{
140    		personId = AuthenticationUtil.getFullyAuthenticatedUser();
141    	}
142
143    	personId = personService.getUserIdentifier(personId);
144		if(personId == null)
145		{
146            // "User " + personId + " does not exist"
147            throw new EntityNotFoundException("personId is null.");
148		}
149
150		if(validateIsCurrentUser)
151		{
152			String currentUserId = AuthenticationUtil.getFullyAuthenticatedUser();
153			if(!currentUserId.equalsIgnoreCase(personId))
154			{
155				throw new EntityNotFoundException(personId);
156			}
157		}
158
159    	return personId;
160	}
161
162    protected void processPersonProperties(String userName, final Map<QName, Serializable> nodeProps)
163    {
164		if(!contentUsageService.getEnabled())
165		{
166			// quota used will always be 0 in this case so remove from the person properties
167			nodeProps.remove(ContentModel.PROP_SIZE_QUOTA);
168			nodeProps.remove(ContentModel.PROP_SIZE_CURRENT);
169		}
170
171		final ContentData personDescription = (ContentData)nodeProps.get(ContentModel.PROP_PERSONDESC);
172		if(personDescription != null)
173		{
174			nodeProps.remove(ContentModel.PROP_PERSONDESC);
175
176			AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
177			{
178				@Override
179				public Void doWork() throws Exception
180				{
181					ContentReader reader = contentService.getRawReader(personDescription.getContentUrl());
182					if(reader != null && reader.exists())
183					{
184						String description = reader.getContentString();
185						nodeProps.put(Person.PROP_PERSON_DESCRIPTION, description);
186					}
187
188					return null;
189				}
190			});
191		}
192    }
193    
194    public boolean hasAvatar(NodeRef personNodeRef)
195    {
196    	if(personNodeRef != null)
197    	{
198			List<AssociationRef> avatorAssocs = nodeService.getTargetAssocs(personNodeRef, ContentModel.ASSOC_AVATAR);
199			return(avatorAssocs.size() > 0);
200    	}
201    	else
202    	{
203    		return false;
204    	}
205    }
206
207    public NodeRef getAvatar(String personId)
208    {
209    	NodeRef avatar = null;
210
211    	personId = validatePerson(personId);
212    	NodeRef personNode = personService.getPerson(personId);
213    	if(personNode != null)
214    	{
215			List<AssociationRef> avatorAssocs = nodeService.getTargetAssocs(personNode, ContentModel.ASSOC_AVATAR);
216			if(avatorAssocs.size() > 0)
217			{
218				AssociationRef ref = avatorAssocs.get(0);
219				NodeRef thumbnailNodeRef = thumbnailService.getThumbnailByName(ref.getTargetRef(), ContentModel.PROP_CONTENT, "avatar");
220				if(thumbnailNodeRef != null)
221				{
222					avatar = thumbnailNodeRef;
223				}
224				else
225				{
226		    		throw new EntityNotFoundException("avatar");
227				}
228			}
229			else
230			{
231	    		throw new EntityNotFoundException("avatar");
232			}
233    	}
234    	else
235    	{
236    		throw new EntityNotFoundException(personId);
237    	}
238    	
239    	return avatar;
240    }
241
242    /**
243     * 
244     * @throws NoSuchPersonException if personId does not exist
245     */
246    public Person getPerson(String personId)
247    {
248    	Person person = null;
249
250    	personId = validatePerson(personId);
251    	NodeRef personNode = personService.getPerson(personId, false);
252    	if (personNode != null) 
253    	{
254    		Map<QName, Serializable> nodeProps = nodeService.getProperties(personNode);
255    		processPersonProperties(personId, nodeProps);
256    		// TODO this needs to be run as admin but should we do this here?
257    		final String pId = personId;
258    		Boolean enabled = AuthenticationUtil.runAsSystem(new RunAsWork<Boolean>()
259			{
260    			public Boolean doWork() throws Exception
261    			{
262    				return authenticationService.getAuthenticationEnabled(pId);
263    			}
264			});
265    		person = new Person(personNode, nodeProps, enabled);
266
267    		// get avatar information
268    		if(hasAvatar(personNode))
269    		{
270	    		try
271	    		{
272		    		NodeRef avatar = getAvatar(personId);
273		    		person.setAvatarId(avatar);
274	    		}
275	    		catch(EntityNotFoundException e)
276	    		{
277	    			// shouldn't happen, but ok
278	    		}
279    		}
280    	}
281    	else
282    	{
283    		throw new EntityNotFoundException(personId);
284    	}
285
286        return person;
287    }
288
289    private void addToMap(Map<QName, Serializable> properties, QName name, Serializable value)
290    {
291//    	if(name != null && value != null)
292//    	{
293    		properties.put(name, value);
294//    	}
295    }
296
297    public Person updatePerson(String personId, final Person person)
298    {
299    	personId = validatePerson(personId);
300
301    	final Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
302//		addToMap(properties, ContentModel.PROP_USERNAME, person.getUserName());
303    	addToMap(properties, ContentModel.PROP_FIRSTNAME, person.getFirstName());
304    	addToMap(properties, ContentModel.PROP_LASTNAME, person.getLastName());
305    	addToMap(properties, ContentModel.PROP_JOBTITLE, person.getJobTitle());
306    	addToMap(properties, ContentModel.PROP_LOCATION, person.getLocation());
307    	addToMap(properties, ContentModel.PROP_TELEPHONE, person.getTelephone());
308    	addToMap(properties, ContentModel.PROP_MOBILE, person.getMobile());
309    	addToMap(properties, ContentModel.PROP_EMAIL, person.getEmail());
310
311		Company company = person.getCompany();
312		if(company != null)
313		{
314			addToMap(properties, ContentModel.PROP_ORGANIZATION, company.getOrganization());
315			addToMap(properties, ContentModel.PROP_COMPANYADDRESS1, company.getAddress1());
316			addToMap(properties, ContentModel.PROP_COMPANYADDRESS2, company.getAddress2());
317			addToMap(properties, ContentModel.PROP_COMPANYADDRESS3, company.getAddress3());
318			addToMap(properties, ContentModel.PROP_COMPANYPOSTCODE, company.getPostcode());
319			addToMap(properties, ContentModel.PROP_COMPANYTELEPHONE, company.getTelephone());
320			addToMap(properties, ContentModel.PROP_COMPANYFAX, company.getFax());
321			addToMap(properties, ContentModel.PROP_COMPANYEMAIL, company.getEmail());
322		}
323		else
324		{
325			addToMap(properties, ContentModel.PROP_ORGANIZATION, null);
326			addToMap(properties, ContentModel.PROP_COMPANYADDRESS1, null);
327			addToMap(properties, ContentModel.PROP_COMPANYADDRESS2, null);
328			addToMap(properties, ContentModel.PROP_COMPANYADDRESS3, null);
329			addToMap(properties, ContentModel.PROP_COMPANYPOSTCODE, null);
330			addToMap(properties, ContentModel.PROP_COMPANYTELEPHONE, null);
331			addToMap(properties, ContentModel.PROP_COMPANYFAX, null);
332			addToMap(properties, ContentModel.PROP_COMPANYEMAIL, null);
333		}
334
335		addToMap(properties, ContentModel.PROP_SKYPE, person.getSkypeId());
336		addToMap(properties, ContentModel.PROP_INSTANTMSG, person.getInstantMessageId());
337		addToMap(properties, ContentModel.PROP_USER_STATUS, person.getUserStatus());
338		addToMap(properties, ContentModel.PROP_USER_STATUS_TIME, person.getStatusUpdatedAt());
339		addToMap(properties, ContentModel.PROP_GOOGLEUSERNAME, person.getGoogleId());
340		addToMap(properties, ContentModel.PROP_SIZE_QUOTA, person.getQuota());
341		addToMap(properties, ContentModel.PROP_SIZE_CURRENT, person.getQuotaUsed());
342		addToMap(properties, ContentModel.PROP_DESCRIPTION, person.getDescription());
343
344		final String pId = personId;
345		AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
346		{
347			@Override
348			public Void doWork() throws Exception
349			{
350		    	personService.setPersonProperties(pId, properties, false);
351				return null;
352			}
353			
354		});
355
356    	return getPerson(personId);
357    }
358}