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