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 */
019package org.nuxeo.ecm.core.bulk.io;
020
021import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_ACTION;
022import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_ENTITY_TYPE;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_PARAMS;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_QUERY;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_REPOSITORY;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_USERNAME;
027import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
028import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
029
030import java.util.HashMap;
031import java.util.Iterator;
032import java.util.Map;
033import java.util.function.Function;
034
035import org.nuxeo.ecm.core.api.NuxeoException;
036import org.nuxeo.ecm.core.bulk.BulkCommand;
037import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
038import org.nuxeo.ecm.core.io.registry.reflect.Setup;
039
040import com.fasterxml.jackson.databind.JsonNode;
041
042/**
043 * @since 10.2
044 */
045@Setup(mode = SINGLETON, priority = REFERENCE)
046public class BulkCommandJsonReader extends EntityJsonReader<BulkCommand> {
047
048    public BulkCommandJsonReader() {
049        super(COMMAND_ENTITY_TYPE);
050    }
051
052    @Override
053    protected BulkCommand readEntity(JsonNode jn) {
054        // everything is mandatory except parameters
055        Function<String, String> getter = fieldName -> jn.get(fieldName).asText();
056
057        Map<String, String> params = new HashMap<>();
058        JsonNode commandNode = jn.get(COMMAND_PARAMS);
059        if (commandNode != null) {
060            fillParams(jn.get(COMMAND_PARAMS), params);
061        }
062
063        return new BulkCommand().withUsername(getter.apply(COMMAND_USERNAME))
064                                .withRepository(getter.apply(COMMAND_REPOSITORY))
065                                .withQuery(getter.apply(COMMAND_QUERY))
066                                .withAction(getter.apply(COMMAND_ACTION))
067                                .withParams(params);
068    }
069
070    protected void fillParams(JsonNode node, Map<String, String> params) {
071        for (Iterator<Map.Entry<String, JsonNode>> paramsNode = node.fields(); paramsNode.hasNext();) {
072            Map.Entry<String, JsonNode> paramNode = paramsNode.next();
073            switch (paramNode.getValue().getNodeType()) {
074            case STRING:
075            case BOOLEAN:
076            case NUMBER:
077            case BINARY: // binary will be converted to base64
078                params.put(paramNode.getKey(), paramNode.getValue().asText());
079                break;
080            case ARRAY:
081            case OBJECT:
082            default:
083                throw new NuxeoException("Node type=" + paramNode.getValue().getNodeType() + " is not supported");
084            }
085        }
086    }
087
088}