001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 * (C) Copyright 2006-2007 YSEngineers Soc. Coop. And. (http://www.yerbabuena.es/) and contributors.
004
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the GNU Lesser General Public License
007 * (LGPL) version 2.1 which accompanies this distribution, and is available at
008 * http://www.gnu.org/licenses/lgpl.html
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * Contributors:
016 *     Yerbabuena - initial implementation
017 *     Nuxeo
018
019 */
020
021package org.nuxeo.ecm.platform.preview.seam;
022
023import java.io.Serializable;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.jboss.seam.ScopeType;
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Observer;
031import org.jboss.seam.annotations.Scope;
032import org.jboss.seam.annotations.intercept.BypassInterceptors;
033import org.jboss.seam.annotations.remoting.WebRemote;
034import org.jboss.seam.annotations.web.RequestParameter;
035import org.nuxeo.ecm.core.api.Blob;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentLocation;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.core.api.DocumentNotFoundException;
040import org.nuxeo.ecm.core.api.IdRef;
041import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
042import org.nuxeo.ecm.platform.preview.api.PreviewException;
043import org.nuxeo.ecm.platform.preview.helper.PreviewHelper;
044import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
045import org.nuxeo.ecm.platform.ui.web.api.WebActions;
046import org.nuxeo.ecm.platform.ui.web.rest.RestHelper;
047import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService;
048import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
049import org.nuxeo.ecm.platform.url.DocumentViewImpl;
050import org.nuxeo.ecm.platform.url.api.DocumentView;
051import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
052import org.nuxeo.ecm.webapp.helpers.EventNames;
053import org.nuxeo.runtime.api.Framework;
054
055import javax.faces.context.FacesContext;
056import javax.servlet.ServletRequest;
057
058/**
059 * Seam Action bean to handle the preview tabs and associated actions.
060 *
061 * @author <a href="mailto:enriqueperez@yerbabuena.es">Enrique Perez</a>
062 * @author tiry
063 */
064@Name("previewActions")
065@Scope(ScopeType.CONVERSATION)
066public class PreviewActionBean implements Serializable {
067
068    private static final long serialVersionUID = 1L;
069
070    private static final Log log = LogFactory.getLog(PreviewActionBean.class);
071
072    public static final String PREVIEW_POPUP_VIEW = "preview_popup";
073
074    @In(create = true, required = false)
075    transient NavigationContext navigationContext;
076
077    @In(create = true, required = false)
078    protected transient CoreSession documentManager;
079
080    @In(create = true, required = false)
081    protected WebActions webActions;
082
083    @RequestParameter
084    private String fieldXPath;
085
086    @RequestParameter
087    private String previewTabId;
088
089    private String fieldXPathValue;
090
091    public boolean getHasPreview() {
092        DocumentModel currentDocument = navigationContext.getCurrentDocument();
093        return documentHasPreview(currentDocument);
094    }
095
096    public boolean documentHasPreview(DocumentModel document) {
097        if (document == null) {
098            return false;
099        }
100        if (PreviewHelper.typeSupportsPreview(document)) {
101            try {
102                return PreviewHelper.docHasBlobToPreview(document);
103            } catch (PreviewException e) {
104                return false;
105            }
106        } else {
107            return false;
108        }
109
110    }
111
112    public String getPreviewURL() {
113        DocumentModel currentDocument = navigationContext.getCurrentDocument();
114        if (currentDocument == null) {
115            return null;
116        }
117        return getPreviewURL(currentDocument);
118    }
119
120    public String getPreviewURL(DocumentModel doc) {
121        return PreviewHelper.getPreviewURL(doc, fieldXPathValue);
122    }
123
124    public String getPreviewURL(DocumentModel doc, String field) {
125        return PreviewHelper.getPreviewURL(doc, protectField(field));
126    }
127
128    public String getPreviewWithBlobPostProcessingURL() {
129        String url = getPreviewURL();
130        url += "?blobPostProcessing=true";
131        return url;
132    }
133
134    public String getPreviewWithBlobPostProcessingURL(DocumentModel doc) {
135        String url = getPreviewURL(doc);
136        url += "?blobPostProcessing=true";
137        return url;
138    }
139
140    public String getCurrentDocumentPreviewPopupURL() {
141        return getPreviewPopupURL(navigationContext.getCurrentDocument());
142    }
143
144    public String getPreviewPopupURL(DocumentModel doc) {
145        return getPreviewPopupURL(doc, false);
146    }
147
148    /**
149     * @since 5.7
150     */
151    public String getPreviewPopupURL(DocumentModel doc, boolean newConversation) {
152        DocumentLocation docLocation = new DocumentLocationImpl(doc.getRepositoryName(), doc.getRef());
153        DocumentView docView = new DocumentViewImpl(docLocation, PREVIEW_POPUP_VIEW);
154        docView.setPatternName("id");
155        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
156        String url = urlPolicyService.getUrlFromDocumentView(docView, null);
157        if (!newConversation) {
158            url = RestHelper.addCurrentConversationParameters(url);
159        }
160        return VirtualHostHelper.getContextPathProperty() + "/" + url;
161    }
162
163    @WebRemote
164    public String getPreviewPopupURL(String docId) {
165        try {
166            DocumentModel doc = documentManager.getDocument(new IdRef(docId));
167            return getPreviewPopupURL(doc, true);
168        } catch (DocumentNotFoundException e) {
169            log.error(e, e);
170            return "";
171        }
172    }
173
174    /**
175     * @since 7.3
176     */
177    public String getViewerURL(DocumentModel doc, String field, Blob blob) {
178        ServletRequest servletRequest = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
179        String baseURL = BaseURL.getBaseURL(servletRequest);
180        return PreviewHelper.getViewerURL(doc, field, blob, baseURL);
181    }
182
183    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.DOCUMENT_CHANGED }, create = false)
184    @BypassInterceptors
185    public void resetFields() {
186        fieldXPathValue = null;
187    }
188
189    public String doSetFieldXPath() {
190        if (fieldXPath != null) {
191            fieldXPathValue = protectField(fieldXPath);
192        }
193        return webActions.setCurrentTabAndNavigate(previewTabId);
194    }
195
196    protected String protectField(String field) {
197        return field.replace("/", "-");
198    }
199
200}