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