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