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