001/*
002 * (C) Copyright 2016-2018 Nuxeo (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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 */
019
020package org.nuxeo.ecm.platform.routing.core.io.enrichers;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026import java.util.Iterator;
027import java.util.List;
028
029import javax.inject.Inject;
030
031import org.apache.commons.lang3.StringUtils;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
034import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper;
035import org.nuxeo.ecm.core.io.registry.reflect.Setup;
036import org.nuxeo.ecm.platform.actions.ActionContext;
037import org.nuxeo.ecm.platform.actions.ELActionContext;
038import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
039import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
040import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
041import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute;
042import org.nuxeo.runtime.api.Framework;
043
044import com.fasterxml.jackson.core.JsonGenerator;
045
046/**
047 * Enricher that add the runnable workflow model on a document for the current user.
048 *
049 * @since 8.3
050 */
051@Setup(mode = SINGLETON, priority = REFERENCE)
052public class RunnableWorkflowJsonEnricher extends AbstractJsonEnricher<DocumentModel> {
053
054    public static final String NAME = "runnableWorkflows";
055
056    @Inject
057    ActionManager actionManager;
058
059    public RunnableWorkflowJsonEnricher() {
060        super(NAME);
061    }
062
063    @Override
064    public void write(JsonGenerator jg, DocumentModel document) throws IOException {
065        jg.writeFieldName(NAME);
066        jg.writeStartArray();
067        try (SessionWrapper wrapper = ctx.getSession(document)) {
068            DocumentRoutingService documentRoutingService = Framework.getService(DocumentRoutingService.class);
069            List<DocumentModel> routeModels = documentRoutingService.searchRouteModels(wrapper.getSession(), "");
070
071            ActionContext actionContext = new ELActionContext();
072            actionContext.setCurrentDocument(document);
073            actionContext.setDocumentManager(wrapper.getSession());
074            actionContext.setCurrentPrincipal(wrapper.getSession().getPrincipal());
075
076            for (Iterator<DocumentModel> it = routeModels.iterator(); it.hasNext();) {
077                DocumentModel route = it.next();
078                Object graphRouteObj = route.getAdapter(GraphRoute.class);
079                if (graphRouteObj instanceof GraphRoute) {
080                    String filter = ((GraphRoute) graphRouteObj).getAvailabilityFilter();
081                    if (!StringUtils.isBlank(filter)) {
082                        if (!actionManager.checkFilter(filter, actionContext)) {
083                            it.remove();
084                        }
085                    }
086                } else {
087                    // old workflow document => ignore
088                    it.remove();
089                }
090            }
091            for (DocumentModel documentRoute : routeModels) {
092                writeEntity(documentRoute.getAdapter(DocumentRoute.class), jg);
093            }
094        } finally {
095            jg.writeEndArray();
096        }
097    }
098
099}