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