001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bjalon 018 */ 019package org.nuxeo.ecm.mobile.webengine; 020 021import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD; 022 023import javax.ws.rs.GET; 024import javax.ws.rs.Path; 025import javax.ws.rs.PathParam; 026import javax.ws.rs.Produces; 027import javax.ws.rs.QueryParam; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.PropertyException; 035import org.nuxeo.ecm.core.io.download.DownloadService; 036import org.nuxeo.ecm.platform.usermanager.UserManager; 037import org.nuxeo.ecm.user.center.profile.UserProfileService; 038import org.nuxeo.ecm.webengine.model.Template; 039import org.nuxeo.ecm.webengine.model.TemplateNotFoundException; 040import org.nuxeo.ecm.webengine.model.View; 041import org.nuxeo.ecm.webengine.model.WebObject; 042import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * Manage Profile view 047 * 048 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a> 049 * @since 5.5 050 */ 051@WebObject(type = "Profile") 052@Produces("text/html;charset=UTF-8") 053public class Profile extends DefaultObject { 054 055 private static final Log log = LogFactory.getLog(Profile.class); 056 057 private boolean hasBeenFetched; 058 059 /************** Bindings *******************/ 060 061 @GET 062 @Path("search") 063 public Object doGetUsers(@QueryParam("q") String query) { 064 DocumentModelList users = getUserManager().searchUsers(query); 065 066 return getView("users").arg("users", users); 067 } 068 069 @GET 070 @Path("{username}") 071 public Template doGetUser(@PathParam("username") String username) { 072 DocumentModel userProfile = getUserProfile(username); 073 DocumentModel userMainInfo = getUserManager().getUserModel(username); 074 075 return getView("view").arg("userProfile", userProfile).arg("userMainInfo", userMainInfo); 076 } 077 078 /************** Actions *******************/ 079 080 public boolean isRichProfileDeployed() { 081 return getUserProfileService() != null; 082 } 083 084 public String getAvatarURI(String username) { 085 // TODO improve to not fetch several times the document and avatar 086 String contextPath = Framework.getProperty("org.nuxeo.ecm.contextPath"); 087 088 DocumentModel userProfile = getUserProfile(username); 089 Blob avatar; 090 try { 091 avatar = (Blob) userProfile.getPropertyValue(USER_PROFILE_AVATAR_FIELD); 092 } catch (PropertyException e) { 093 log.debug("No avatar found"); 094 avatar = null; 095 } 096 097 if (userProfile != null && avatar != null) { 098 DownloadService downloadService = Framework.getService(DownloadService.class); 099 return contextPath + "/" + downloadService.getDownloadUrl(userProfile, USER_PROFILE_AVATAR_FIELD, ""); 100 } else { 101 // TODO : Use a relative path (be careful of proxy stuff) 102 return contextPath + "/site/skin/nuxeo/icons/default_avatar.png"; 103 } 104 } 105 106 /************** OTHERS **********************/ 107 108 private DocumentModel getUserProfile(String username) { 109 return getUserProfileService().getUserProfileDocument(username, ctx.getCoreSession()); 110 } 111 112 private String getViewSuffix() { 113 if (isRichProfileDeployed()) { 114 return "rich"; 115 } else { 116 return "standard"; 117 } 118 } 119 120 /** 121 * Try to return the dedicated view for standard or rich profile (with suffix) if not exists return the view without 122 * suffix. 123 */ 124 @Override 125 public Template getView(final String viewId) { 126 127 String dedicatedViewId = String.format("%s-%s", viewId, getViewSuffix()); 128 129 try { 130 return new View(this, dedicatedViewId).resolve(); 131 } catch (TemplateNotFoundException e) { 132 log.debug("Dedicated view not exists, try use standard binding"); 133 } 134 135 return new View(this, viewId).resolve(); 136 } 137 138 /**** fetch service *****/ 139 140 private UserProfileService userProfileService; 141 142 private UserProfileService getUserProfileService() { 143 if (!hasBeenFetched) { 144 hasBeenFetched = false; 145 try { 146 Class.forName("org.nuxeo.ecm.user.center.profile.UserProfileService"); 147 } catch (ReflectiveOperationException e) { 148 log.info("UserProfileService not deployed, use the UserManager"); 149 return null; 150 } 151 152 userProfileService = Framework.getService(UserProfileService.class); 153 } 154 return userProfileService; 155 } 156 157 private UserManager getUserManager() { 158 return Framework.getService(UserManager.class); 159 } 160 161}