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;
019
020import static org.jboss.seam.ScopeType.STATELESS;
021import static org.jboss.seam.annotations.Install.FRAMEWORK;
022
023import java.io.Serializable;
024import java.security.Principal;
025
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Install;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.faces.FacesMessages;
031import org.jboss.seam.international.StatusMessage;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.rating.api.LikeService;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Handles Like related web actions.
038 *
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 * @since 5.6
041 */
042@Name("likeActions")
043@Scope(STATELESS)
044@Install(precedence = FRAMEWORK)
045public class LikeActions implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    @In(create = true, required = false)
050    protected FacesMessages facesMessages;
051
052    public boolean hasUserLiked(Principal principal, DocumentModel doc) {
053        LikeService likeService = Framework.getLocalService(LikeService.class);
054        return likeService.hasUserLiked(principal.getName(), doc);
055    }
056
057    public void like(Principal principal, DocumentModel doc) {
058        LikeService likeService = Framework.getLocalService(LikeService.class);
059        likeService.like(principal.getName(), doc);
060        facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.document.liked");
061    }
062
063    public void cancelLike(Principal principal, DocumentModel doc) {
064        LikeService likeService = Framework.getLocalService(LikeService.class);
065        likeService.cancel(principal.getName(), doc);
066        facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.document.unliked");
067    }
068
069    public long getLikesCount(DocumentModel doc) {
070        LikeService likeService = Framework.getLocalService(LikeService.class);
071        return likeService.getLikesCount(doc);
072    }
073
074}