Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.sakaiproject.profile2.logic.SakaiProxyImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2008-2010 The Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.profile2.logic;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.api.common.edu.person.SakaiPerson;
import org.sakaiproject.api.common.edu.person.SakaiPersonManager;
import org.sakaiproject.authz.api.SecurityAdvisor;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.content.api.ContentResourceEdit;
import org.sakaiproject.email.api.EmailService;
import org.sakaiproject.emailtemplateservice.model.RenderedTemplate;
import org.sakaiproject.emailtemplateservice.service.EmailTemplateService;
import org.sakaiproject.entity.api.Entity;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.event.api.ActivityService;
import org.sakaiproject.event.api.EventTrackingService;
import org.sakaiproject.event.api.NotificationService;
import org.sakaiproject.exception.IdInvalidException;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.IdUsedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.id.api.IdManager;
import org.sakaiproject.profile2.model.MimeTypeByteArray;
import org.sakaiproject.profile2.util.ProfileConstants;
import org.sakaiproject.profile2.util.ProfileUtils;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.SiteService.SelectionType;
import org.sakaiproject.site.api.SiteService.SortType;
import org.sakaiproject.site.api.ToolConfiguration;
import org.sakaiproject.tool.api.SessionManager;
import org.sakaiproject.tool.api.Tool;
import org.sakaiproject.tool.api.ToolManager;
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.user.api.UserEdit;
import org.sakaiproject.user.api.UserNotDefinedException;
import org.sakaiproject.util.Validator;
/**
* Implementation of SakaiProxy for Profile2.
*
* @author Steve Swinsburg ([email protected] )
*
*/
public class SakaiProxyImpl implements SakaiProxy {
private static final Logger log = Logger.getLogger(SakaiProxyImpl.class);
/**
* {@inheritDoc}
*/
public String getCurrentSiteId(){
return toolManager.getCurrentPlacement().getContext();
}
/**
* {@inheritDoc}
*/
public String getCurrentUserId() {
return sessionManager.getCurrentSessionUserId();
}
/**
* {@inheritDoc}
*/
public User getCurrentUser() {
return userDirectoryService.getCurrentUser();
}
/**
* {@inheritDoc}
*/
public String getUserEid(String userId){
String eid = null;
try {
eid = userDirectoryService.getUser(userId).getEid();
} catch (UserNotDefinedException e) {
log.warn("Cannot get eid for id: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return eid;
}
/**
* {@inheritDoc}
*/
public String getUserIdForEid(String eid) {
String userUuid = null;
try {
userUuid = userDirectoryService.getUserByEid(eid).getId();
} catch (UserNotDefinedException e) {
log.warn("Cannot get id for eid: " + eid + " : " + e.getClass() + " : " + e.getMessage());
}
return userUuid;
}
/**
* {@inheritDoc}
*/
public String getUserDisplayName(String userId) {
String name = null;
try {
name = userDirectoryService.getUser(userId).getDisplayName();
} catch (UserNotDefinedException e) {
log.warn("Cannot get displayname for id: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return name;
}
/**
* {@inheritDoc}
*/
public String getUserFirstName(String userId) {
String email = null;
try {
email = userDirectoryService.getUser(userId).getFirstName();
} catch (UserNotDefinedException e) {
log.warn("Cannot get first name for id: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return email;
}
/**
* {@inheritDoc}
*/
public String getUserLastName(String userId) {
String email = null;
try {
email = userDirectoryService.getUser(userId).getLastName();
} catch (UserNotDefinedException e) {
log.warn("Cannot get last name for id: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return email;
}
/**
* {@inheritDoc}
*/
public String getUserEmail(String userId) {
String email = null;
try {
email = userDirectoryService.getUser(userId).getEmail();
} catch (UserNotDefinedException e) {
log.warn("Cannot get email for id: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return email;
}
/**
* {@inheritDoc}
*/
public boolean checkForUser(String userId) {
User u = null;
try {
u = userDirectoryService.getUser(userId);
if (u != null) {
return true;
}
} catch (UserNotDefinedException e) {
log.info("User with id: " + userId + " does not exist : " + e.getClass() + " : " + e.getMessage());
}
return false;
}
/**
* {@inheritDoc}
*/
public boolean checkForUserByEid(String eid) {
User u = null;
try {
u = userDirectoryService.getUserByEid(eid);
if (u != null) {
return true;
}
} catch (UserNotDefinedException e) {
log.info("User with eid: " + eid + " does not exist : " + e.getClass() + " : " + e.getMessage());
}
return false;
}
/**
* {@inheritDoc}
*/
public boolean isSuperUser() {
return securityService.isSuperUser();
}
/**
* {@inheritDoc}
*/
public boolean isAdminUser() {
return StringUtils.equals(sessionManager.getCurrentSessionUserId(), UserDirectoryService.ADMIN_ID);
}
/**
* {@inheritDoc}
*/
public boolean isSuperUserAndProxiedToUser(String userId) {
return (isSuperUser() && !StringUtils.equals(userId, getCurrentUserId()));
}
/**
* {@inheritDoc}
*/
public String getUserType(String userId) {
String type = null;
try {
type = userDirectoryService.getUser(userId).getType();
} catch (UserNotDefinedException e) {
log.info("User with eid: " + userId + " does not exist : " + e.getClass() + " : " + e.getMessage());
}
return type;
}
/**
* {@inheritDoc}
*/
public User getUserById(String userId) {
User u = null;
try {
u = userDirectoryService.getUser(userId);
} catch (UserNotDefinedException e) {
log.info("User with id: " + userId + " does not exist : " + e.getClass() + " : " + e.getMessage());
}
return u;
}
/**
* {@inheritDoc}
*/
public User getUserQuietly(String userId) {
User u = null;
try {
u = userDirectoryService.getUser(userId);
} catch (UserNotDefinedException e) {
//carry on, no log output. see javadoc for reason.
}
return u;
}
/**
* {@inheritDoc}
*/
public String getCurrentToolTitle() {
Tool tool = toolManager.getCurrentTool();
if(tool != null)
return tool.getTitle();
else
return "Profile";
}
/**
* {@inheritDoc}
*/
public List getUsers(List userIds) {
return userDirectoryService.getUsers(userIds);
}
/**
* {@inheritDoc}
*/
public List getUuids(List users) {
List uuids = new ArrayList();
for(User u: users){
uuids.add(u.getId());
}
return uuids;
}
/**
* {@inheritDoc}
*/
public SakaiPerson getSakaiPerson(String userId) {
SakaiPerson sakaiPerson = null;
try {
sakaiPerson = sakaiPersonManager.getSakaiPerson(userId, sakaiPersonManager.getUserMutableType());
} catch (Exception e) {
log.error("SakaiProxy.getSakaiPerson(): Couldn't get SakaiPerson for: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return sakaiPerson;
}
/**
* {@inheritDoc}
*/
public byte[] getSakaiPersonJpegPhoto(String userId) {
SakaiPerson sakaiPerson = null;
byte[] image = null;
try {
//try normal user type
sakaiPerson = sakaiPersonManager.getSakaiPerson(userId, sakaiPersonManager.getUserMutableType());
if(sakaiPerson != null) {
image = sakaiPerson.getJpegPhoto();
}
//if null try system user type as a profile might have been created with this type
if(image == null) {
sakaiPerson = sakaiPersonManager.getSakaiPerson(userId, sakaiPersonManager.getSystemMutableType());
if(sakaiPerson != null) {
image = sakaiPerson.getJpegPhoto();
}
}
} catch (Exception e) {
log.error("SakaiProxy.getSakaiPersonJpegPhoto(): Couldn't get SakaiPerson Jpeg photo for: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return image;
}
/**
* {@inheritDoc}
*/
public String getSakaiPersonImageUrl(String userId) {
SakaiPerson sakaiPerson = null;
String url = null;
try {
//try normal user type
sakaiPerson = sakaiPersonManager.getSakaiPerson(userId, sakaiPersonManager.getUserMutableType());
if(sakaiPerson != null) {
url = sakaiPerson.getPictureUrl();
}
//if null try system user type as a profile might have been created with this type
if(StringUtils.isBlank(url)) {
sakaiPerson = sakaiPersonManager.getSakaiPerson(userId, sakaiPersonManager.getSystemMutableType());
if(sakaiPerson != null) {
url = sakaiPerson.getPictureUrl();
}
}
} catch (Exception e) {
log.error("SakaiProxy.getSakaiPersonImageUrl(): Couldn't get SakaiPerson image URL for: " + userId + " : " + e.getClass() + " : " + e.getMessage());
}
return url;
}
/**
* {@inheritDoc}
*/
public SakaiPerson getSakaiPersonPrototype() {
SakaiPerson sakaiPerson = null;
try {
sakaiPerson = sakaiPersonManager.getPrototype();
} catch (Exception e) {
log.error("SakaiProxy.getSakaiPersonPrototype(): Couldn't get SakaiPerson prototype: " + e.getClass() + " : " + e.getMessage());
}
return sakaiPerson;
}
/**
* {@inheritDoc}
*/
public SakaiPerson createSakaiPerson(String userId) {
SakaiPerson sakaiPerson = null;
try {
sakaiPerson = sakaiPersonManager.create(userId, sakaiPersonManager.getUserMutableType());
} catch (Exception e) {
log.error("SakaiProxy.createSakaiPerson(): Couldn't create SakaiPerson: " + e.getClass() + " : " + e.getMessage());
}
return sakaiPerson;
}
/**
* {@inheritDoc}
*/
public boolean updateSakaiPerson(SakaiPerson sakaiPerson) {
//the save is void, so unless it throws an exception, its ok (?)
//I'd prefer a return value from sakaiPersonManager. this wraps it.
try {
sakaiPersonManager.save(sakaiPerson);
return true;
} catch (Exception e) {
log.error("SakaiProxy.updateSakaiPerson(): Couldn't update SakaiPerson: " + e.getClass() + " : " + e.getMessage());
e.printStackTrace();
}
return false;
}
/**
* {@inheritDoc}
*/
public int getMaxProfilePictureSize() {
return serverConfigurationService.getInt("profile2.picture.max", ProfileConstants.MAX_IMAGE_UPLOAD_SIZE);
}
private String getProfileGalleryPath(String userId) {
String slash = Entity.SEPARATOR;
StringBuilder path = new StringBuilder();
path.append(slash);
path.append("private");
path.append(slash);
path.append("profileGallery");
path.append(slash);
path.append(userId);
path.append(slash);
return path.toString();
}
/**
* {@inheritDoc}
*/
public String getProfileGalleryImagePath(String userId, String imageId) {
StringBuilder path = new StringBuilder(getProfileGalleryPath(userId));
path.append(ProfileConstants.GALLERY_IMAGE_MAIN);
path.append(Entity.SEPARATOR);
path.append(imageId);
return path.toString();
}
/**
* {@inheritDoc}
*/
public String getProfileGalleryThumbnailPath(String userId, String imageId) {
StringBuilder path = new StringBuilder(getProfileGalleryPath(userId));
path.append(ProfileConstants.GALLERY_IMAGE_THUMBNAILS);
path.append(Entity.SEPARATOR);
path.append(imageId);
return path.toString();
}
/**
* {@inheritDoc}
*/
public String getProfileImageResourcePath(String userId, int type) {
String slash = Entity.SEPARATOR;
StringBuilder path = new StringBuilder();
path.append(slash);
path.append("private");
path.append(slash);
path.append("profileImages");
path.append(slash);
path.append(userId);
path.append(slash);
path.append(type);
path.append(slash);
path.append(ProfileUtils.generateUuid());
return path.toString();
}
/**
* {@inheritDoc}
*/
public boolean saveFile(String fullResourceId, String userId, String fileName, String mimeType, byte[] fileData) {
ContentResourceEdit resource = null;
boolean result = true;
try {
enableSecurityAdvisor();
try {
resource = contentHostingService.addResource(fullResourceId);
resource.setContentType(mimeType);
resource.setContent(fileData);
ResourceProperties props = resource.getPropertiesEdit();
props.addProperty(ResourceProperties.PROP_CONTENT_TYPE, mimeType);
props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, fileName);
props.addProperty(ResourceProperties.PROP_CREATOR, userId);
resource.getPropertiesEdit().set(props);
contentHostingService.commitResource(resource, NotificationService.NOTI_NONE);
result = true;
}
catch (IdUsedException e){
contentHostingService.cancelResource(resource);
log.error("SakaiProxy.saveFile(): id= " + fullResourceId + " is in use : " + e.getClass() + " : " + e.getMessage());
result = false;
}
catch (Exception e){
contentHostingService.cancelResource(resource);
log.error("SakaiProxy.saveFile(): failed: " + e.getClass() + " : " + e.getMessage());
result = false;
}
} catch (Exception e) {
log.error("SakaiProxy.saveFile():" + e.getClass() + ":" + e.getMessage());
result = false;
} finally {
disableSecurityAdvisor();
}
return result;
}
/**
* {@inheritDoc}
*/
public MimeTypeByteArray getResource(String resourceId) {
MimeTypeByteArray mtba = new MimeTypeByteArray();
if(StringUtils.isBlank(resourceId)) {
return null;
}
try {
enableSecurityAdvisor();
try {
ContentResource resource = contentHostingService.getResource(resourceId);
if(resource == null){
return null;
}
mtba.setBytes(resource.getContent());
mtba.setMimeType(resource.getContentType());
return mtba;
}
catch(Exception e){
log.error("SakaiProxy.getResource() failed for resourceId: " + resourceId + " : " + e.getClass() + " : " + e.getMessage());
}
} catch (Exception e) {
log.error("SakaiProxy.getResource():" + e.getClass() + ":" + e.getMessage());
}
finally {
disableSecurityAdvisor();
}
return null;
}
/**
* {@inheritDoc}
*/
public boolean removeResource(String resourceId) {
boolean result = false;
try {
enableSecurityAdvisor();
contentHostingService.removeResource(resourceId);
result = true;
} catch (Exception e) {
log.error("SakaiProxy.removeResource() failed for resourceId "
+ resourceId + ": " + e.getMessage());
return false;
} finally {
disableSecurityAdvisor();
}
return result;
}
/**
* {@inheritDoc}
*/
public List searchUsers(String search) {
return userDirectoryService.searchUsers(search, ProfileConstants.FIRST_RECORD, ProfileConstants.MAX_RECORDS);
}
/**
* {@inheritDoc}
*/
public List searchExternalUsers(String search) {
return userDirectoryService.searchExternalUsers(search, ProfileConstants.FIRST_RECORD, ProfileConstants.MAX_RECORDS);
}
/**
* {@inheritDoc}
*/
public void postEvent(String event,String reference,boolean modify) {
eventTrackingService.post(eventTrackingService.newEvent(event,reference,modify));
}
/**
* {@inheritDoc}
*/
public void sendEmail(final String userId, final String subject, String message) {
class EmailSender {
private String userId;
private String subject;
private String message;
public final String MULTIPART_BOUNDARY = "======sakai-multi-part-boundary======";
public final String BOUNDARY_LINE = "\n\n--"+MULTIPART_BOUNDARY+"\n";
public final String TERMINATION_LINE = "\n\n--"+MULTIPART_BOUNDARY+"--\n\n";
public final String MIME_ADVISORY = "This message is for MIME-compliant mail readers.";
public final String PLAIN_TEXT_HEADERS= "Content-Type: text/plain\n\n";
public final String HTML_HEADERS = "Content-Type: text/html; charset=ISO-8859-1\n\n";
public final String HTML_END = "\n