001/*
002 * (C) Copyright 2006-2011 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.List;
028import java.util.Locale;
029import java.util.Map;
030
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Immutable object representing an Activity message.
036 *
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 * @since 5.5
039 */
040public final class ActivityMessage implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    private final Serializable activityId;
045
046    private final String actor;
047
048    private final String displayActor;
049
050    private final String displayActorLink;
051
052    private final String verb;
053
054    private final String message;
055
056    private final Date publishedDate;
057
058    private final String icon;
059
060    private final List<ActivityReplyMessage> replies;
061
062    /**
063     * @deprecated since 5.6. Use
064     *             {@link ActivityMessage#ActivityMessage(java.io.Serializable, String, String, String, String, String, java.util.Date, String, java.util.List)}
065     *             instead.
066     */
067    @Deprecated
068    public ActivityMessage(Serializable activityId, String message, Date publishedDate) {
069        this(activityId, null, null, null, null, message, publishedDate, null, null);
070    }
071
072    /**
073     * @since 5.6
074     */
075    public ActivityMessage(Serializable activityId, String actor, String displayActor, String displayActorLink,
076            String verb, String message, Date publishedDate, String icon, List<ActivityReplyMessage> replies) {
077        this.activityId = activityId;
078        this.actor = actor;
079        this.displayActor = displayActor;
080        this.displayActorLink = displayActorLink;
081        this.verb = verb;
082        this.message = message;
083        this.publishedDate = publishedDate;
084        this.icon = icon;
085        this.replies = replies;
086    }
087
088    /**
089     * @deprecated since 5.6.
090     */
091    @Deprecated
092    public ActivityMessage(Activity activity, String message) {
093        this(activity.getId(), activity.getActor(), activity.getDisplayActor(), null, activity.getVerb(), message,
094                activity.getPublishedDate(), null, null);
095    }
096
097    public Serializable getActivityId() {
098        return activityId;
099    }
100
101    /**
102     * @since 5.6
103     */
104    public String getActor() {
105        return actor;
106    }
107
108    /**
109     * @since 5.6
110     */
111    public String getDisplayActor() {
112        return displayActor;
113    }
114
115    /**
116     * @since 5.6
117     */
118    public String getDisplayActorLink() {
119        return displayActorLink;
120    }
121
122    /**
123     * @since 5.6
124     */
125    public String getVerb() {
126        return verb;
127    }
128
129    public String getMessage() {
130        return message;
131    }
132
133    public Date getPublishedDate() {
134        return publishedDate;
135    }
136
137    /**
138     * @since 5.6
139     */
140    public String getIcon() {
141        return icon;
142    }
143
144    /**
145     * @since 5.6
146     */
147    public List<ActivityReplyMessage> getActivityReplyMessages() {
148        return replies;
149    }
150
151    /**
152     * @since 5.6
153     */
154    public Map<String, Object> toMap(CoreSession session, Locale locale) {
155        return toMap(session, locale, null);
156    }
157
158    /**
159     * @since 5.6
160     */
161    public Map<String, Object> toMap(CoreSession session, Locale locale, String activityLinkBuilderName)
162            {
163        ActivityLinkBuilder activityLinkBuilder = Framework.getLocalService(ActivityStreamService.class).getActivityLinkBuilder(
164                activityLinkBuilderName);
165
166        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
167
168        Map<String, Object> o = new HashMap<String, Object>();
169        o.put("id", getActivityId());
170        o.put("actor", getActor());
171        o.put("displayActor", getDisplayActor());
172        o.put("displayActorLink", getDisplayActorLink());
173        if (isUser(getActor())) {
174            String actorUsername = getUsername(getActor());
175            o.put("actorAvatarURL", activityLinkBuilder.getUserAvatarURL(session, actorUsername));
176        }
177        o.put("activityVerb", getVerb());
178        o.put("activityMessage", getMessage());
179        o.put("publishedDate", dateFormat.format(getPublishedDate()));
180        o.put("icon", getIcon());
181        return o;
182    }
183
184}