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