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.STATUS_ACTION;
022import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_COMMAND_ID;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_COMPLETED_TIME;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ENTITY_TYPE;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ERROR_COUNT;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ERROR_MESSAGE;
027import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_HAS_ERROR;
028import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSED;
029import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_END_TIME;
030import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_MILLIS;
031import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_START_TIME;
032import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_RESULT;
033import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SCROLL_END_TIME;
034import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SCROLL_START_TIME;
035import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_STATE;
036import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SUBMIT_TIME;
037import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_TOTAL;
038import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_USERNAME;
039import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
040import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
041
042import java.io.IOException;
043import java.io.Serializable;
044import java.util.Map;
045
046import org.nuxeo.ecm.core.bulk.message.BulkStatus;
047import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
048import org.nuxeo.ecm.core.io.registry.reflect.Setup;
049
050import com.fasterxml.jackson.core.JsonGenerator;
051
052/**
053 * @since 10.2
054 */
055@Setup(mode = SINGLETON, priority = REFERENCE)
056public class BulkStatusJsonWriter extends ExtensibleEntityJsonWriter<BulkStatus> {
057
058    public BulkStatusJsonWriter() {
059        super(STATUS_ENTITY_TYPE, BulkStatus.class);
060    }
061
062    @Override
063    public void writeEntityBody(BulkStatus entity, JsonGenerator jg) throws IOException {
064        jg.writeStringField(STATUS_COMMAND_ID, entity.getId());
065        jg.writeStringField(STATUS_STATE, entity.getState() != null ? entity.getState().toString() : null);
066        jg.writeNumberField(STATUS_PROCESSED, entity.getProcessed());
067        jg.writeBooleanField(STATUS_HAS_ERROR, entity.hasError());
068        jg.writeNumberField(STATUS_ERROR_COUNT, entity.getErrorCount());
069        if (entity.getErrorMessage() != null) {
070            jg.writeStringField(STATUS_ERROR_MESSAGE, entity.getErrorMessage());
071        }
072        jg.writeNumberField(STATUS_TOTAL, entity.getTotal());
073        jg.writeStringField(STATUS_ACTION, entity.getAction());
074        jg.writeStringField(STATUS_USERNAME, entity.getUsername());
075        jg.writeStringField(STATUS_SUBMIT_TIME,
076                entity.getSubmitTime() != null ? entity.getSubmitTime().toString() : null);
077        jg.writeStringField(STATUS_SCROLL_START_TIME,
078                entity.getScrollStartTime() != null ? entity.getScrollStartTime().toString() : null);
079        jg.writeStringField(STATUS_SCROLL_END_TIME,
080                entity.getScrollEndTime() != null ? entity.getScrollEndTime().toString() : null);
081        jg.writeStringField(STATUS_PROCESSING_START_TIME,
082                entity.getProcessingStartTime() != null ? entity.getProcessingStartTime().toString() : null);
083        jg.writeStringField(STATUS_PROCESSING_END_TIME,
084                entity.getProcessingEndTime() != null ? entity.getProcessingEndTime().toString() : null);
085        jg.writeStringField(STATUS_COMPLETED_TIME,
086                entity.getCompletedTime() != null ? entity.getCompletedTime().toString() : null);
087        jg.writeNumberField(STATUS_PROCESSING_MILLIS, entity.getProcessingDurationMillis());
088        Map<String, Serializable> result = entity.getResult();
089        if (!result.isEmpty()) {
090            jg.writeObjectField(STATUS_RESULT, result);
091        }
092    }
093
094}