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