001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.activity;
019
020import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.io.FilenameUtils;
026import org.apache.commons.lang.StringEscapeUtils;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentLocation;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
033import org.nuxeo.ecm.core.io.download.DownloadService;
034import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService;
035import org.nuxeo.ecm.platform.url.DocumentViewImpl;
036import org.nuxeo.ecm.platform.url.api.DocumentView;
037import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
038import org.nuxeo.ecm.user.center.profile.UserProfileService;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * Default {@link ActivityLinkBuilder} computing URLs with the default id codec for documents and user codec for users.
043 *
044 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
045 * @since 5.6
046 */
047public class DefaultActivityLinkBuilder implements ActivityLinkBuilder {
048
049    @Override
050    public String getDocumentLink(String documentActivityObject, String displayValue) {
051        documentActivityObject = StringEscapeUtils.escapeHtml(documentActivityObject);
052        displayValue = StringEscapeUtils.escapeHtml(displayValue);
053        String link = "<a href=\"%s\" target=\"_top\">%s</a>";
054        return String.format(
055                link,
056                getDocumentURL(ActivityHelper.getRepositoryName(documentActivityObject),
057                        ActivityHelper.getDocumentId(documentActivityObject)), displayValue);
058    }
059
060    protected String getDocumentURL(String repositoryName, String documentId) {
061        DocumentLocation docLoc = new DocumentLocationImpl(repositoryName, new IdRef(documentId));
062        DocumentView docView = new DocumentViewImpl(docLoc, "view_documents");
063        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
064        return urlPolicyService.getUrlFromDocumentView("id", docView, VirtualHostHelper.getContextPathProperty());
065    }
066
067    @Override
068    public String getUserProfileLink(String userActivityObject, String displayValue) {
069        userActivityObject = StringEscapeUtils.escapeHtml(userActivityObject);
070        displayValue = StringEscapeUtils.escapeHtml(displayValue);
071        String link = "<span class=\"username\"><a href=\"%s\" target=\"_top\" title=\"%s\">%s</a></span>";
072        String username = ActivityHelper.getUsername(userActivityObject);
073        return String.format(link, getUserProfileURL(username), username, displayValue);
074    }
075
076    protected String getUserProfileURL(String username) {
077        Map<String, String> params = new HashMap<String, String>();
078        params.put("username", username);
079        DocumentView docView = new DocumentViewImpl(null, null, params);
080        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
081        if (urlPolicyService == null) {
082            return "";
083        }
084        return urlPolicyService.getUrlFromDocumentView("user", docView, VirtualHostHelper.getContextPathProperty());
085    }
086
087    @Override
088    public String getUserAvatarURL(CoreSession session, String username) {
089        UserProfileService userProfileService = Framework.getLocalService(UserProfileService.class);
090        DocumentModel profile = userProfileService.getUserProfileDocument(username, session);
091        Blob avatar = (Blob) profile.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
092        if (avatar != null) {
093            DownloadService downloadService = Framework.getService(DownloadService.class);
094            String filename = username + "." + FilenameUtils.getExtension(avatar.getFilename());
095            return VirtualHostHelper.getContextPathProperty() + "/"
096                    + downloadService.getDownloadUrl(profile, USER_PROFILE_AVATAR_FIELD, filename);
097        } else {
098            return VirtualHostHelper.getContextPathProperty() + "/icons/missing_avatar.png";
099        }
100    }
101}