001/* 002 * (C) Copyright 2014 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 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 018 * 019 */ 020package org.nuxeo.ecm.platform.routing.core.io; 021 022import static org.nuxeo.ecm.core.io.marshallers.json.document.DocumentPropertiesJsonReader.DEFAULT_SCHEMA_NAME; 023import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 024import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 025 026import java.io.Closeable; 027import java.io.IOException; 028import java.io.Serializable; 029import java.lang.reflect.ParameterizedType; 030import java.lang.reflect.Type; 031import java.util.ArrayList; 032import java.util.HashMap; 033import java.util.Iterator; 034import java.util.List; 035import java.util.Map; 036 037import javax.inject.Inject; 038import javax.ws.rs.core.MediaType; 039import org.apache.commons.lang3.reflect.TypeUtils; 040import org.apache.commons.logging.Log; 041import org.apache.commons.logging.LogFactory; 042import org.nuxeo.ecm.core.api.CoreSession; 043import org.nuxeo.ecm.core.api.DocumentModel; 044import org.nuxeo.ecm.core.api.IdRef; 045import org.nuxeo.ecm.core.api.model.Property; 046import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader; 047import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper; 048import org.nuxeo.ecm.core.io.registry.reflect.Setup; 049import org.nuxeo.ecm.core.schema.SchemaManager; 050import org.nuxeo.ecm.core.schema.types.CompositeType; 051import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService; 052import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute; 053import org.nuxeo.runtime.api.Framework; 054 055import com.fasterxml.jackson.databind.JsonNode; 056 057/** 058 * @since 8.2 059 */ 060@Setup(mode = SINGLETON, priority = REFERENCE) 061public class WorkflowRequestJsonReader extends EntityJsonReader<WorkflowRequest> { 062 063 protected static final Log log = LogFactory.getLog(WorkflowRequestJsonReader.class); 064 065 public static final String ENTITY_TYPE = "workflow"; 066 067 private static final String USE_LEGACY_CONF_KEY = "nuxeo.document.routing.json.format.legacy"; 068 069 private Boolean useLegacy = null; 070 071 @Inject 072 SchemaManager schemaManager; 073 074 @Inject 075 DocumentRoutingService documentRoutingService; 076 077 public WorkflowRequestJsonReader() { 078 super(WorkflowRequestJsonReader.ENTITY_TYPE); 079 String prop = Framework.getProperty(USE_LEGACY_CONF_KEY); 080 if (prop != null && Boolean.parseBoolean(prop)) { 081 useLegacy = Boolean.TRUE; 082 } else { 083 useLegacy = Boolean.FALSE; 084 } 085 } 086 087 @Override 088 public boolean accept(Class<?> clazz, Type genericType, MediaType mediatype) { 089 return !useLegacy && !ctx.getBooleanParameter(USE_LEGACY_CONF_KEY) 090 && super.accept(clazz, genericType, mediatype); 091 } 092 093 @Override 094 protected WorkflowRequest readEntity(JsonNode jn) throws IOException { 095 String workflowModelName = getStringField(jn, "workflowModelName"); 096 List<String> attachedDocumentIds = new ArrayList<String>(); 097 JsonNode attachedDocumentIdsNode = jn.get("attachedDocumentIds"); 098 if (attachedDocumentIdsNode != null) { 099 for (Iterator<JsonNode> it = attachedDocumentIdsNode.elements(); it.hasNext();) { 100 attachedDocumentIds.add(it.next().textValue()); 101 } 102 } 103 Map<String, Serializable> variables = new HashMap<>(); 104 JsonNode variablesNode = jn.get("variables"); 105 if (variablesNode != null) { 106 try (SessionWrapper closeable = ctx.getSession(null)) { 107 CoreSession session = closeable.getSession(); 108 String workflowModelId = documentRoutingService.getRouteModelDocIdWithId(session, workflowModelName); 109 DocumentModel model = session.getDocument(new IdRef(workflowModelId)); 110 String workflowSchemaFacet = (String) model.getPropertyValue(GraphRoute.PROP_VARIABLES_FACET); 111 CompositeType type = schemaManager.getFacet(workflowSchemaFacet); 112 String workflowSchema = type.getSchemaNames()[0]; 113 variables.putAll(getVariables(variablesNode, workflowSchema)); 114 } 115 } 116 117 return new WorkflowRequest(workflowModelName, attachedDocumentIds, variables); 118 } 119 120 private Map<String, Serializable> getVariables(JsonNode variables, String schemaName) throws IOException { 121 Map<String, Serializable> variable = new HashMap<>(); 122 ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class); 123 try (Closeable resource = ctx.wrap().with(DEFAULT_SCHEMA_NAME, schemaName).open()) { 124 List<Property> properties = readEntity(List.class, genericType, variables); 125 for (Property property : properties) { 126 variable.put(property.getName().substring(schemaName.length() + 1), property.getValue()); 127 } 128 } 129 return variable; 130 } 131 132}