001/*
002 * (C) Copyright 2006-2007 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 *     Yerbabuena - initial implementation
018 *     Nuxeo
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.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentLocation;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.DocumentNotFoundException;
039import org.nuxeo.ecm.core.api.IdRef;
040import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
041import org.nuxeo.ecm.platform.preview.api.PreviewException;
042import org.nuxeo.ecm.platform.preview.helper.PreviewHelper;
043import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
044import org.nuxeo.ecm.platform.ui.web.api.WebActions;
045import org.nuxeo.ecm.platform.ui.web.rest.RestHelper;
046import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService;
047import org.nuxeo.ecm.platform.url.DocumentViewImpl;
048import org.nuxeo.ecm.platform.url.api.DocumentView;
049import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
050import org.nuxeo.ecm.webapp.helpers.EventNames;
051import org.nuxeo.runtime.api.Framework;
052
053/**
054 * Seam Action bean to handle the preview tabs and associated actions.
055 *
056 * @author <a href="mailto:enriqueperez@yerbabuena.es">Enrique Perez</a>
057 * @author tiry
058 */
059@Name("previewActions")
060@Scope(ScopeType.CONVERSATION)
061public class PreviewActionBean implements Serializable {
062
063    private static final long serialVersionUID = 1L;
064
065    private static final Log log = LogFactory.getLog(PreviewActionBean.class);
066
067    public static final String PREVIEW_POPUP_VIEW = "preview_popup";
068
069    @In(create = true, required = false)
070    transient NavigationContext navigationContext;
071
072    @In(create = true, required = false)
073    protected transient CoreSession documentManager;
074
075    @In(create = true, required = false)
076    protected WebActions webActions;
077
078    @RequestParameter
079    private String fieldXPath;
080
081    @RequestParameter
082    private String previewTabId;
083
084    private String fieldXPathValue;
085
086    public boolean getHasPreview() {
087        DocumentModel currentDocument = navigationContext.getCurrentDocument();
088        return documentHasPreview(currentDocument);
089    }
090
091    public boolean documentHasPreview(DocumentModel document) {
092        if (document == null) {
093            return false;
094        }
095        if (PreviewHelper.typeSupportsPreview(document)) {
096            try {
097                return PreviewHelper.docHasBlobToPreview(document);
098            } catch (PreviewException e) {
099                return false;
100            }
101        } else {
102            return false;
103        }
104
105    }
106
107    public String getPreviewURL() {
108        DocumentModel currentDocument = navigationContext.getCurrentDocument();
109        if (currentDocument == null) {
110            return null;
111        }
112        return getPreviewURL(currentDocument);
113    }
114
115    public String getPreviewURL(DocumentModel doc) {
116        return PreviewHelper.getPreviewURL(doc, fieldXPathValue);
117    }
118
119    public String getPreviewURL(DocumentModel doc, String field) {
120        return PreviewHelper.getPreviewURL(doc, field);
121    }
122
123    public String getPreviewWithBlobPostProcessingURL() {
124        String url = getPreviewURL();
125        url += "?blobPostProcessing=true";
126        return url;
127    }
128
129    public String getPreviewWithBlobPostProcessingURL(DocumentModel doc) {
130        String url = getPreviewURL(doc);
131        url += "?blobPostProcessing=true";
132        return url;
133    }
134
135    public String getCurrentDocumentPreviewPopupURL() {
136        return getPreviewPopupURL(navigationContext.getCurrentDocument());
137    }
138
139    public String getPreviewPopupURL(DocumentModel doc) {
140        return getPreviewPopupURL(doc, false);
141    }
142
143    /**
144     * @since 5.7
145     */
146    public String getPreviewPopupURL(DocumentModel doc, boolean newConversation) {
147        DocumentLocation docLocation = new DocumentLocationImpl(doc.getRepositoryName(), doc.getRef());
148        DocumentView docView = new DocumentViewImpl(docLocation, PREVIEW_POPUP_VIEW);
149        docView.setPatternName("id");
150        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
151        String url = urlPolicyService.getUrlFromDocumentView(docView, null);
152        if (!newConversation) {
153            url = RestHelper.addCurrentConversationParameters(url);
154        }
155        return VirtualHostHelper.getContextPathProperty() + "/" + url;
156    }
157
158    @WebRemote
159    public String getPreviewPopupURL(String docId) {
160        try {
161            DocumentModel doc = documentManager.getDocument(new IdRef(docId));
162            return getPreviewPopupURL(doc, true);
163        } catch (DocumentNotFoundException e) {
164            log.error(e, e);
165            return "";
166        }
167    }
168
169    /**
170     * @since 8.2
171     */
172    public boolean hasBlobPreview(DocumentModel doc, String field) {
173        return PreviewHelper.blobSupportsPreview(doc, field);
174    }
175
176    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.DOCUMENT_CHANGED }, create = false)
177    @BypassInterceptors
178    public void resetFields() {
179        fieldXPathValue = null;
180    }
181
182    public String doSetFieldXPath() {
183        if (fieldXPath != null) {
184            fieldXPathValue = protectField(fieldXPath);
185        }
186        return webActions.setCurrentTabAndNavigate(previewTabId);
187    }
188
189    protected String protectField(String field) {
190        return field.replace("/", "-");
191    }
192
193}