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 java.util.Collections.emptyMap;
022import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_ACTION;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_BATCH_SIZE;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_BUCKET_SIZE;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_ENTITY_TYPE;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_PARAMS;
027import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_QUERY;
028import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_REPOSITORY;
029import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_USERNAME;
030import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
031import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
032
033import java.io.Serializable;
034import java.util.Map;
035import java.util.function.Function;
036
037import org.nuxeo.ecm.core.bulk.message.BulkCommand;
038import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
039import org.nuxeo.ecm.core.io.registry.reflect.Setup;
040
041import com.fasterxml.jackson.databind.JsonNode;
042
043/**
044 * @since 10.2
045 */
046@Setup(mode = SINGLETON, priority = REFERENCE)
047public class BulkCommandJsonReader extends EntityJsonReader<BulkCommand> {
048
049    public BulkCommandJsonReader() {
050        super(COMMAND_ENTITY_TYPE);
051    }
052
053    @Override
054    protected BulkCommand readEntity(JsonNode jn) {
055        // everything is mandatory except parameters
056        Function<String, String> getter = fieldName -> jn.get(fieldName).asText();
057
058        Map<String, Serializable> params = emptyMap();
059        if (jn.has(COMMAND_PARAMS)) {
060            params = BulkParameters.paramsToMap(jn.get(COMMAND_PARAMS));
061        }
062        int batchSize = Integer.parseInt(getter.apply(COMMAND_BATCH_SIZE));
063        int bucketSize = Integer.parseInt(getter.apply(COMMAND_BUCKET_SIZE));
064        return new BulkCommand.Builder(getter.apply(COMMAND_ACTION),
065                getter.apply(COMMAND_QUERY)).user(getter.apply(COMMAND_USERNAME))
066                                            .repository(getter.apply(COMMAND_REPOSITORY))
067                                            .bucket(bucketSize)
068                                            .batch(batchSize)
069                                            .params(params)
070                                            .build();
071    }
072
073}