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_BATCH_SIZE;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_BUCKET_SIZE;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_ENTITY_TYPE;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_PARAMS;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_QUERY;
027import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_REPOSITORY;
028import static org.nuxeo.ecm.core.bulk.io.BulkConstants.COMMAND_USERNAME;
029import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
030import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
031
032import java.io.IOException;
033import java.io.Serializable;
034import java.util.Map;
035
036import org.nuxeo.ecm.core.bulk.message.BulkCommand;
037import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
038import org.nuxeo.ecm.core.io.registry.reflect.Setup;
039
040import com.fasterxml.jackson.core.JsonGenerator;
041
042/**
043 * @since 10.2
044 */
045@Setup(mode = SINGLETON, priority = REFERENCE)
046public class BulkCommandJsonWriter extends ExtensibleEntityJsonWriter<BulkCommand> {
047
048    public BulkCommandJsonWriter() {
049        super(COMMAND_ENTITY_TYPE, BulkCommand.class);
050    }
051
052    @Override
053    protected void writeEntityBody(BulkCommand command, JsonGenerator jg) throws IOException {
054        // everything is mandatory except parameters
055        jg.writeStringField(COMMAND_USERNAME, command.getUsername());
056        jg.writeStringField(COMMAND_REPOSITORY, command.getRepository());
057        jg.writeStringField(COMMAND_QUERY, command.getQuery());
058        jg.writeStringField(COMMAND_ACTION, command.getAction());
059        jg.writeNumberField(COMMAND_BUCKET_SIZE, command.getBucketSize());
060        jg.writeNumberField(COMMAND_BATCH_SIZE, command.getBatchSize());
061        Map<String, Serializable> params = command.getParams();
062        if (!params.isEmpty()) {
063            jg.writeObjectField(COMMAND_PARAMS, params);
064        }
065    }
066}