001/* 002 * (C) Copyright 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 * Funsho David 018 */ 019 020package org.nuxeo.ecm.core.bulk.io; 021 022import java.io.IOException; 023import java.io.Serializable; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.HashMap; 027import java.util.Map; 028import java.util.stream.Collectors; 029import java.util.stream.StreamSupport; 030 031import org.apache.commons.lang3.StringUtils; 032import org.nuxeo.ecm.core.api.NuxeoException; 033 034import com.fasterxml.jackson.databind.JsonNode; 035import com.fasterxml.jackson.databind.ObjectMapper; 036 037/** 038 * @since 10.3 039 */ 040public class BulkParameters { 041 042 protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 043 044 private BulkParameters() { 045 // utility class 046 } 047 048 public static Map<String, Serializable> paramsToMap(String jsonParams) throws IOException { 049 if (StringUtils.isBlank(jsonParams)) { 050 return Collections.emptyMap(); 051 } 052 return paramsToMap(OBJECT_MAPPER.readTree(jsonParams)); 053 } 054 055 public static HashMap<String, Serializable> paramsToMap(JsonNode node) { 056 // we declare variable and method return type as HashMap to be compliant with Serializable in params map 057 HashMap<String, Serializable> params = new HashMap<>(); 058 if (node == null) { 059 return params; 060 } 061 Iterable<Map.Entry<String, JsonNode>> paramNodes = node::fields; 062 for (Map.Entry<String, JsonNode> paramNode : paramNodes) { 063 params.put(paramNode.getKey(), toSerializable(paramNode.getValue())); 064 } 065 return params; 066 } 067 068 protected static ArrayList<Serializable> toList(JsonNode value) { 069 // we declare method return type as ArrayList to be compliant with Serializable in params map 070 // spliterator calls iterator which is a bridge method of JsonNode#elements 071 return StreamSupport.stream(value.spliterator(), false) 072 .map(BulkParameters::toSerializable) 073 .collect(Collectors.toCollection(ArrayList::new)); 074 } 075 076 protected static Serializable toSerializable(JsonNode value) { 077 Serializable serializableValue; 078 switch (value.getNodeType()) { 079 case STRING: 080 case BINARY: // binary will be converted to base64 081 serializableValue = value.asText(); 082 break; 083 case BOOLEAN: 084 serializableValue = value.asBoolean(); 085 break; 086 case NUMBER: 087 serializableValue = value.asLong(); 088 break; 089 case ARRAY: 090 serializableValue = toList(value); 091 break; 092 case OBJECT: 093 serializableValue = paramsToMap(value); 094 break; 095 default: 096 throw new NuxeoException("Node type=" + value.getNodeType() + " is not supported"); 097 } 098 return serializableValue; 099 } 100}