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