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