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.operations;
019
020import static org.nuxeo.ecm.activity.ActivityHelper.getUsername;
021import static org.nuxeo.ecm.activity.ActivityMessageHelper.replaceURLsByLinks;
022
023import java.io.IOException;
024import java.io.StringWriter;
025import java.text.DateFormat;
026import java.util.Date;
027import java.util.HashMap;
028import java.util.Locale;
029import java.util.Map;
030
031import org.codehaus.jackson.map.ObjectMapper;
032import org.nuxeo.ecm.activity.ActivityHelper;
033import org.nuxeo.ecm.activity.ActivityLinkBuilder;
034import org.nuxeo.ecm.activity.ActivityReply;
035import org.nuxeo.ecm.activity.ActivityStreamService;
036import org.nuxeo.ecm.automation.core.Constants;
037import org.nuxeo.ecm.automation.core.annotations.Context;
038import org.nuxeo.ecm.automation.core.annotations.Operation;
039import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
040import org.nuxeo.ecm.automation.core.annotations.Param;
041import org.nuxeo.ecm.core.api.Blob;
042import org.nuxeo.ecm.core.api.Blobs;
043import org.nuxeo.ecm.core.api.CoreSession;
044import org.nuxeo.runtime.api.Framework;
045
046/**
047 * Operation to add an activity reply.
048 *
049 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
050 * @since 5.6
051 */
052@Operation(id = AddActivityReply.ID, category = Constants.CAT_SERVICES, label = "Add a reply to an existing activity", description = "Add a reply to an existing activity.")
053public class AddActivityReply {
054
055    public static final String ID = "Services.AddActivityReply";
056
057    @Context
058    protected CoreSession session;
059
060    @Context
061    protected ActivityStreamService activityStreamService;
062
063    @Param(name = "language", required = false)
064    protected String language;
065
066    @Param(name = "activityId", required = true)
067    protected String activityId;
068
069    @Param(name = "message", required = true)
070    protected String message;
071
072    @Param(name = "activityLinkBuilderName", required = true)
073    protected String activityLinkBuilderName;
074
075    @OperationMethod
076    public Blob run() throws IOException {
077        String actor = ActivityHelper.createUserActivityObject(session.getPrincipal());
078        String displayActor = ActivityHelper.generateDisplayName(session.getPrincipal());
079        ActivityReply reply = new ActivityReply(actor, displayActor, message, new Date().getTime());
080        reply = activityStreamService.addActivityReply(Long.valueOf(activityId), reply);
081
082        Locale locale = language != null && !language.isEmpty() ? new Locale(language) : Locale.ENGLISH;
083        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
084        ActivityLinkBuilder activityLinkBuilder = Framework.getLocalService(ActivityStreamService.class).getActivityLinkBuilder(
085                activityLinkBuilderName);
086
087        Map<String, Object> m = new HashMap<String, Object>();
088        m.put("id", reply.getId());
089        m.put("actor", reply.getActor());
090        m.put("displayActor", reply.getDisplayActor());
091        m.put("displayActorLink", getDisplayActorLink(reply.getActor(), reply.getDisplayActor(), activityLinkBuilder));
092        m.put("actorAvatarURL", activityLinkBuilder.getUserAvatarURL(session, getUsername(reply.getActor())));
093        m.put("message", replaceURLsByLinks(reply.getMessage()));
094        m.put("publishedDate", dateFormat.format(new Date(reply.getPublishedDate())));
095        String username = ActivityHelper.getUsername(reply.getActor());
096        m.put("allowDeletion", session.getPrincipal().getName().equals(username));
097
098        ObjectMapper mapper = new ObjectMapper();
099        StringWriter writer = new StringWriter();
100        mapper.writeValue(writer, m);
101
102        return Blobs.createBlob(writer.toString(), "application/json");
103    }
104
105    protected String getDisplayActorLink(String actor, String displayActor, ActivityLinkBuilder activityLinkBuilder) {
106        return activityLinkBuilder.getUserProfileLink(actor, displayActor);
107    }
108
109}