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