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.activity.ActivityHelper.getUsername;
021import static org.nuxeo.ecm.activity.ActivityHelper.isUser;
022
023import java.io.Serializable;
024import java.text.DateFormat;
025import java.util.Date;
026import java.util.HashMap;
027import java.util.Locale;
028import java.util.Map;
029
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
035 * @since 5.6
036 */
037public class ActivityReplyMessage implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    private final String activityReplyId;
042
043    private final String actor;
044
045    private final String displayActor;
046
047    private final String displayActorLink;
048
049    private final String message;
050
051    private final long publishedDate;
052
053    public ActivityReplyMessage(String activityReplyId, String actor, String displayActor, String displayActorLink,
054            String message, long publishedDate) {
055        this.activityReplyId = activityReplyId;
056        this.actor = actor;
057        this.displayActor = displayActor;
058        this.displayActorLink = displayActorLink;
059        this.message = message;
060        this.publishedDate = publishedDate;
061    }
062
063    public String getActivityReplyId() {
064        return activityReplyId;
065    }
066
067    public String getActor() {
068        return actor;
069    }
070
071    public String getDisplayActor() {
072        return displayActor;
073    }
074
075    public String getDisplayActorLink() {
076        return displayActorLink;
077    }
078
079    public String getMessage() {
080        return message;
081    }
082
083    public long getPublishedDate() {
084        return publishedDate;
085    }
086
087    public Map<String, Object> toMap(CoreSession session, Locale locale) {
088        return toMap(session, locale, null);
089    }
090
091    public Map<String, Object> toMap(CoreSession session, Locale locale, String activityLinkBuilderName)
092            {
093        ActivityLinkBuilder activityLinkBuilder = Framework.getLocalService(ActivityStreamService.class).getActivityLinkBuilder(
094                activityLinkBuilderName);
095
096        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
097
098        Map<String, Object> o = new HashMap<String, Object>();
099        o.put("id", getActivityReplyId());
100        o.put("actor", getActor());
101        o.put("displayActor", getDisplayActor());
102        o.put("displayActorLink", getDisplayActorLink());
103        if (isUser(getActor())) {
104            String actorUsername = getUsername(getActor());
105            o.put("actorAvatarURL", activityLinkBuilder.getUserAvatarURL(session, actorUsername));
106        }
107        o.put("message", getMessage());
108        o.put("publishedDate", dateFormat.format(new Date(getPublishedDate())));
109        return o;
110    }
111}