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