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; 027 028import javax.faces.context.FacesContext; 029import javax.servlet.http.HttpServletRequest; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.jboss.seam.annotations.In; 034import org.jboss.seam.annotations.Install; 035import org.jboss.seam.annotations.Name; 036import org.jboss.seam.annotations.Scope; 037import org.nuxeo.ecm.core.api.Blob; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.NuxeoException; 040import org.nuxeo.ecm.core.api.NuxeoPrincipal; 041import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 042import org.nuxeo.ecm.platform.annotations.api.AnnotationsService; 043import org.nuxeo.ecm.platform.preview.adapter.base.ConverterBasedHtmlPreviewAdapter; 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:[email protected]">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 NuxeoPrincipal currentUser; 070 071 public long getAnnotationsCount(DocumentModel doc) { 072 DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class); 073 AnnotationsService annotationsService = Framework.getService(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), currentUser); 081 } catch (URISyntaxException e) { 082 throw new NuxeoException(e); 083 } 084 } 085 086 public boolean isAnnotationsEnabled(DocumentModel doc) { 087 ConfigurationService cs = Framework.getService(ConfigurationService.class); 088 if (cs.isBooleanPropertyFalse(ConverterBasedHtmlPreviewAdapter.OLD_PREVIEW_PROPERTY)) { 089 // cannot work without old preview 090 return false; 091 } 092 093 BlobHolder blobHolder = doc.getAdapter(BlobHolder.class); 094 Blob blob = blobHolder.getBlob(); 095 if (blob == null || blob.getMimeType() == null) { 096 return false; 097 } 098 099 return blob.getMimeType().startsWith("image") || isTextAnnotationsEnabled(); 100 } 101 102 protected boolean isTextAnnotationsEnabled() { 103 ConfigurationService cs = Framework.getService(ConfigurationService.class); 104 return cs.isBooleanPropertyTrue(TEXT_ANNOTATIONS_KEY); 105 } 106 107}