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.rating.operations;
019
020import net.sf.json.JSONObject;
021
022import org.nuxeo.ecm.activity.ActivityHelper;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.Blobs;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.rating.api.LikeService;
034import org.nuxeo.ecm.rating.api.LikeStatus;
035
036/**
037 * Operation to cancel a like on a document or activity object.
038 *
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 * @since 5.6
041 */
042@Operation(id = CancelLike.ID, category = Constants.CAT_SERVICES, label = "Cancel a like for a document or an activity object", description = "Cancel a like for a document or an activity object."
043        + "One of the 'document' or 'activityObject' must be set."
044        + "Returns the related LikeStatus once the action is done.")
045public class CancelLike {
046
047    public static final String ID = "Services.CancelLike";
048
049    @Context
050    protected CoreSession session;
051
052    @Context
053    protected LikeService likeService;
054
055    @Param(name = "document", required = false)
056    protected DocumentModel doc;
057
058    @Param(name = "activityId", required = false)
059    protected String activityId;
060
061    @OperationMethod
062    public Blob run() throws OperationException {
063        String username = session.getPrincipal().getName();
064        LikeStatus status;
065        if (doc != null) {
066            likeService.cancel(username, doc);
067            status = likeService.getLikeStatus(username, doc);
068        } else if (activityId != null) {
069            String activityObject = ActivityHelper.createActivityObject(activityId);
070            likeService.cancel(username, activityObject);
071            status = likeService.getLikeStatus(username, activityObject);
072        } else {
073            throw new OperationException("'document' or 'activityId' parameter must be set.");
074        }
075
076        JSONObject json = JSONObject.fromObject(status.toMap());
077        return Blobs.createBlob(json.toString(), "application/json");
078    }
079
080}