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.adapter.base.ConverterBasedHtmlPreviewAdapter;
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.url.DocumentViewImpl;
049import org.nuxeo.ecm.platform.url.api.DocumentView;
050import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
051import org.nuxeo.ecm.webapp.helpers.EventNames;
052import org.nuxeo.runtime.api.Framework;
053import org.nuxeo.runtime.services.config.ConfigurationService;
054
055/**
056 * Seam Action bean to handle the preview tabs and associated actions.
057 *
058 * @author <a href="mailto:enriqueperez@yerbabuena.es">Enrique Perez</a>
059 * @author tiry
060 */
061@Name("previewActions")
062@Scope(ScopeType.CONVERSATION)
063public class PreviewActionBean implements Serializable {
064
065    private static final long serialVersionUID = 1L;
066
067    private static final Log log = LogFactory.getLog(PreviewActionBean.class);
068
069    public static final String PREVIEW_POPUP_VIEW = "preview_popup";
070
071    public static final String PREVIEWURL_PREFIX = "restAPI/preview/";
072
073    /**
074     * @since 10.3
075     */
076    public static final String PREVIEWURL_DEFAULTXPATH = "default";
077
078    @In(create = true, required = false)
079    transient NavigationContext navigationContext;
080
081    @In(create = true, required = false)
082    protected transient CoreSession documentManager;
083
084    @In(create = true, required = false)
085    protected WebActions webActions;
086
087    @RequestParameter
088    private String fieldXPath;
089
090    @RequestParameter
091    private String previewTabId;
092
093    private String fieldXPathValue;
094
095    public boolean getHasPreview() {
096        DocumentModel currentDocument = navigationContext.getCurrentDocument();
097        return documentHasPreview(currentDocument);
098    }
099
100    public boolean documentHasPreview(DocumentModel document) {
101        if (document == null) {
102            return false;
103        }
104        if (PreviewHelper.typeSupportsPreview(document)) {
105            try {
106                return PreviewHelper.docHasBlobToPreview(document);
107            } catch (PreviewException e) {
108                return false;
109            }
110        } else {
111            return false;
112        }
113
114    }
115
116    public String getPreviewURL() {
117        DocumentModel currentDocument = navigationContext.getCurrentDocument();
118        if (currentDocument == null) {
119            return null;
120        }
121        return getPreviewURL(currentDocument);
122    }
123
124    public String getPreviewURL(DocumentModel doc) {
125        ConfigurationService cs = Framework.getService(ConfigurationService.class);
126        return cs.isBooleanPropertyTrue(ConverterBasedHtmlPreviewAdapter.OLD_PREVIEW_PROPERTY)
127                ? getOldPreviewURL(doc, fieldXPathValue)
128                : PreviewHelper.getPreviewURL(doc, fieldXPathValue);
129    }
130
131    public String getPreviewURL(DocumentModel doc, String xpath) {
132        ConfigurationService cs = Framework.getService(ConfigurationService.class);
133        return cs.isBooleanPropertyTrue(ConverterBasedHtmlPreviewAdapter.OLD_PREVIEW_PROPERTY)
134                ? getOldPreviewURL(doc, protectField(xpath))
135                : PreviewHelper.getPreviewURL(doc, xpath);
136    }
137
138    /**
139     * @since 10.3
140     */
141    public String getOldPreviewURL(DocumentModel doc, String xpath) {
142        if (xpath == null) {
143            xpath = PREVIEWURL_DEFAULTXPATH;
144        }
145
146        StringBuilder sb = new StringBuilder();
147
148        sb.append(PREVIEWURL_PREFIX);
149        sb.append(doc.getRepositoryName());
150        sb.append("/");
151        sb.append(doc.getId());
152        sb.append("/");
153        sb.append(xpath);
154        sb.append("/");
155
156        return sb.toString();
157    }
158
159    public String getPreviewWithBlobPostProcessingURL() {
160        String url = getPreviewURL();
161        url += "?blobPostProcessing=true";
162        return url;
163    }
164
165    public String getPreviewWithBlobPostProcessingURL(DocumentModel doc) {
166        String url = getPreviewURL(doc);
167        url += "?blobPostProcessing=true";
168        return url;
169    }
170
171    public String getCurrentDocumentPreviewPopupURL() {
172        return getPreviewPopupURL(navigationContext.getCurrentDocument());
173    }
174
175    public String getPreviewPopupURL(DocumentModel doc) {
176        return getPreviewPopupURL(doc, false);
177    }
178
179    /**
180     * @since 5.7
181     */
182    public String getPreviewPopupURL(DocumentModel doc, boolean newConversation) {
183        DocumentLocation docLocation = new DocumentLocationImpl(doc.getRepositoryName(), doc.getRef());
184        DocumentView docView = new DocumentViewImpl(docLocation, PREVIEW_POPUP_VIEW);
185        docView.setPatternName("id");
186        URLPolicyService urlPolicyService = Framework.getService(URLPolicyService.class);
187        String url = urlPolicyService.getUrlFromDocumentView(docView, null);
188        if (!newConversation) {
189            url = RestHelper.addCurrentConversationParameters(url);
190        }
191        return VirtualHostHelper.getContextPathProperty() + "/" + url;
192    }
193
194    @WebRemote
195    public String getPreviewPopupURL(String docId) {
196        try {
197            DocumentModel doc = documentManager.getDocument(new IdRef(docId));
198            return getPreviewPopupURL(doc, true);
199        } catch (DocumentNotFoundException e) {
200            log.error(e, e);
201            return "";
202        }
203    }
204
205    /**
206     * @since 8.2
207     */
208    public boolean hasBlobPreview(DocumentModel doc, String field) {
209        return PreviewHelper.blobSupportsPreview(doc, field);
210    }
211
212    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.DOCUMENT_CHANGED }, create = false)
213    @BypassInterceptors
214    public void resetFields() {
215        fieldXPathValue = null;
216    }
217
218    public String doSetFieldXPath() {
219        if (fieldXPath != null) {
220            fieldXPathValue = protectField(fieldXPath);
221        }
222        return webActions.setCurrentTabAndNavigate(previewTabId);
223    }
224
225    protected String protectField(String field) {
226        return field.replace("/", "-");
227    }
228
229}