001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
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 *     vdutat
016 */
017package org.nuxeo.ecm.platform.annotations.jsf.component;
018
019import static org.jboss.seam.ScopeType.STATELESS;
020import static org.jboss.seam.annotations.Install.FRAMEWORK;
021
022import java.io.Serializable;
023import java.net.URI;
024import java.net.URISyntaxException;
025import java.security.Principal;
026
027import javax.faces.context.FacesContext;
028import javax.servlet.http.HttpServletRequest;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.jboss.seam.annotations.In;
033import org.jboss.seam.annotations.Install;
034import org.jboss.seam.annotations.Name;
035import org.jboss.seam.annotations.Scope;
036import org.nuxeo.ecm.core.api.Blob;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.core.api.NuxeoPrincipal;
040import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
041import org.nuxeo.ecm.platform.annotations.api.AnnotationsService;
042import org.nuxeo.ecm.platform.url.DocumentViewImpl;
043import org.nuxeo.ecm.platform.url.api.DocumentView;
044import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
045import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
046import org.nuxeo.runtime.api.Framework;
047import org.nuxeo.runtime.services.config.ConfigurationService;
048
049/**
050 * Handles Annotations related web actions.
051 *
052 * @author <a href="mailto:vdutat@nuxeo.com">Vincent Dutat</a>
053 * @since 5.7
054 */
055@Name("annotationsActions")
056@Scope(STATELESS)
057@Install(precedence = FRAMEWORK)
058public class AnnotationsActions implements Serializable {
059
060    private static final long serialVersionUID = 1L;
061
062    private static final Log log = LogFactory.getLog(AnnotationsActions.class);
063
064    public static final String TEXT_ANNOTATIONS_KEY = "nuxeo.text.annotations";
065
066    @In(create = true)
067    protected transient Principal currentUser;
068
069    public long getAnnotationsCount(DocumentModel doc) {
070        DocumentViewCodecManager documentViewCodecManager = Framework.getLocalService(DocumentViewCodecManager.class);
071        AnnotationsService annotationsService = Framework.getLocalService(AnnotationsService.class);
072        DocumentView docView = new DocumentViewImpl(doc);
073        FacesContext context = FacesContext.getCurrentInstance();
074        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
075        String documentUrl = documentViewCodecManager.getUrlFromDocumentView("docpath", docView, true,
076                VirtualHostHelper.getBaseURL(request));
077        try {
078            return annotationsService.getAnnotationsCount(new URI(documentUrl), (NuxeoPrincipal) currentUser);
079        } catch (URISyntaxException e) {
080            throw new NuxeoException(e);
081        }
082    }
083
084    public boolean isAnnotationsEnabled(DocumentModel doc) {
085        BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
086        Blob blob = blobHolder.getBlob();
087        if (blob == null || blob.getMimeType() == null) {
088            return false;
089        }
090
091        return blob.getMimeType().startsWith("image") || isTextAnnotationsEnabled();
092    }
093
094    protected boolean isTextAnnotationsEnabled() {
095        ConfigurationService cs = Framework.getService(ConfigurationService.class);
096        return cs.isBooleanPropertyTrue(TEXT_ANNOTATIONS_KEY);
097    }
098
099}