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