001/*
002 * (C) Copyright 2012 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 * Contributors:
016 * Nuxeo - initial API and implementation
017 */
018
019package org.nuxeo.ecm.platform.rendition.action;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Collections;
025import java.util.List;
026
027import javax.faces.context.FacesContext;
028import javax.servlet.http.HttpServletRequest;
029
030import org.apache.commons.lang.StringUtils;
031import org.jboss.seam.ScopeType;
032import org.jboss.seam.annotations.Factory;
033import org.jboss.seam.annotations.In;
034import org.jboss.seam.annotations.Name;
035import org.jboss.seam.annotations.Scope;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.platform.rendition.Constants;
040import org.nuxeo.ecm.platform.rendition.Rendition;
041import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
042import org.nuxeo.ecm.platform.rendition.service.RenditionService;
043import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
044import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
045import org.nuxeo.runtime.api.Framework;
046
047/**
048 * Seam bean used to hold Factory used by summary widget
049 *
050 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
051 */
052@Name("renditionAction")
053@Scope(ScopeType.PAGE)
054public class RenditionActionBean implements Serializable {
055
056    private static final long serialVersionUID = 1L;
057
058    public static final String RENDITION_REST_URL_FORMAT = "%sapi/v1/id/%s/@rendition/%s";
059
060    @In(create = true)
061    protected transient NavigationContext navigationContext;
062
063    @In(create = true, required = false)
064    protected transient CoreSession documentManager;
065
066    @Factory(value = "currentDocumentRenditions", scope = ScopeType.EVENT)
067    public List<Rendition> getCurrentDocumentRenditions() throws Exception {
068        DocumentModel doc = navigationContext.getCurrentDocument();
069        RenditionService rs = Framework.getService(RenditionService.class);
070        return rs.getAvailableRenditions(doc);
071    }
072
073    @Factory(value = "currentDocumentVisibleRenditionDefinitions", scope = ScopeType.EVENT)
074    public List<RenditionDefinition> getVisibleRenditionDefinitions() throws Exception {
075
076        List<RenditionDefinition> result = new ArrayList<>();
077        DocumentModel doc = navigationContext.getCurrentDocument();
078        RenditionService rs = Framework.getService(RenditionService.class);
079        for (RenditionDefinition rd : rs.getAvailableRenditionDefinitions(doc)) {
080            if (rd.isVisible()) {
081                result.add(rd);
082            }
083        }
084        return result;
085    }
086
087    /**
088     * @since 7.3
089     */
090    public List<Rendition> getVisibleRenditions(String excludedKinds) {
091        DocumentModel doc = navigationContext.getCurrentDocument();
092        if (doc == null) {
093            return Collections.emptyList();
094        }
095        RenditionService rs = Framework.getService(RenditionService.class);
096        List<Rendition> availableRenditions = rs.getAvailableRenditions(doc, true);
097
098        List<Rendition> filteredRenditions = availableRenditions;
099        if (StringUtils.isNotBlank(excludedKinds)) {
100            filteredRenditions = new ArrayList<>();
101            List<String> excludedKindList = Arrays.asList(excludedKinds.split(","));
102            for (Rendition rendition : availableRenditions) {
103                if (!excludedKindList.contains(rendition.getKind())) {
104                    filteredRenditions.add(rendition);
105                }
106            }
107        }
108        return filteredRenditions;
109    }
110
111    public boolean hasVisibleRenditions(String excludedKinds) {
112        return !getVisibleRenditions(excludedKinds).isEmpty();
113    }
114
115    /**
116     * @since 7.3
117     */
118    public String getRenditionURL(String renditionName) {
119        return getRenditionURL(navigationContext.getCurrentDocument(), renditionName);
120    }
121
122    /**
123     * @since 7.3
124     */
125    public String getRenditionURL(DocumentModel doc, String renditionName) {
126        FacesContext context = FacesContext.getCurrentInstance();
127        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
128        return String.format(RENDITION_REST_URL_FORMAT, BaseURL.getBaseURL(request), doc.getId(), renditionName);
129    }
130
131    /**
132     * @since 8.3
133     */
134    public DocumentModel getRenditionSourceDocumentModel(DocumentModel doc) {
135        String id = (String) doc.getPropertyValue(Constants.RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY);
136        if (id == null) {
137            id = (String) doc.getPropertyValue(Constants.RENDITION_SOURCE_ID_PROPERTY);
138        }
139        return documentManager.getDocument(new IdRef(id));
140    }
141
142}