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 get a {@see LikeStatus} for a specified document or activity object. 038 * 039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 040 * @since 5.6 041 */ 042@Operation(id = GetLikeStatus.ID, category = Constants.CAT_SERVICES, label = "Get the LikeStatus for a document or an activity", description = "Get the LikeStatus for a document or an activity." 043 + "One of the 'document' or 'activityObject' must be set.") 044public class GetLikeStatus { 045 046 public static final String ID = "Services.GetLikeStatus"; 047 048 @Context 049 protected CoreSession session; 050 051 @Context 052 protected LikeService likeService; 053 054 @Param(name = "document", required = false) 055 protected DocumentModel doc; 056 057 @Param(name = "activityId", required = false) 058 protected String activityId; 059 060 @OperationMethod 061 public Blob run() throws OperationException { 062 String username = session.getPrincipal().getName(); 063 LikeStatus status; 064 if (doc != null) { 065 status = likeService.getLikeStatus(username, doc); 066 } else if (activityId != null) { 067 String activityObject = ActivityHelper.createActivityObject(activityId); 068 status = likeService.getLikeStatus(username, activityObject); 069 } else { 070 throw new OperationException("'document' or 'activityId' parameter must be set."); 071 } 072 073 JSONObject json = JSONObject.fromObject(status.toMap()); 074 return Blobs.createBlob(json.toString(), "application/json"); 075 } 076 077}