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