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.apache.commons.lang3.StringUtils.isNotEmpty;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ACTION;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_COMMAND_ID;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_COMPLETED_TIME;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ENTITY_TYPE;
027import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ERROR_COUNT;
028import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_ERROR_MESSAGE;
029import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSED;
030import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_END_TIME;
031import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_MILLIS;
032import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_PROCESSING_START_TIME;
033import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_RESULT;
034import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SCROLL_END_TIME;
035import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SCROLL_START_TIME;
036import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_STATE;
037import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_SUBMIT_TIME;
038import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_TOTAL;
039import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_USERNAME;
040import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
041import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
042
043import java.io.Serializable;
044import java.time.Instant;
045import java.util.Map;
046
047import org.nuxeo.ecm.core.bulk.message.BulkStatus;
048import org.nuxeo.ecm.core.bulk.message.BulkStatus.State;
049import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
050import org.nuxeo.ecm.core.io.registry.reflect.Setup;
051
052import com.fasterxml.jackson.databind.JsonNode;
053
054/**
055 * @since 10.2
056 */
057@Setup(mode = SINGLETON, priority = REFERENCE)
058public class BulkStatusJsonReader extends EntityJsonReader<BulkStatus> {
059
060    public BulkStatusJsonReader() {
061        super(STATUS_ENTITY_TYPE);
062    }
063
064    @Override
065    public BulkStatus readEntity(JsonNode jn) {
066        String id = jn.get(STATUS_COMMAND_ID).asText();
067        BulkStatus status = new BulkStatus(id);
068
069        String state = getStringField(jn, STATUS_STATE);
070        if (isNotEmpty(state)) {
071            status.setState(State.valueOf(state));
072        }
073        String action = getStringField(jn, STATUS_ACTION);
074        if (isNotEmpty(action)) {
075            status.setAction(action);
076        }
077        String errorMessage = getStringField(jn, STATUS_ERROR_MESSAGE);
078        if (isNotEmpty(errorMessage)) {
079            status.inError(errorMessage);
080        }
081        Long processed = getLongField(jn, STATUS_PROCESSED);
082        if (processed != null) {
083            status.setProcessed(processed);
084        }
085        Long count = getLongField(jn, STATUS_TOTAL);
086        if (count != null) {
087            status.setTotal(count);
088        }
089        count = getLongField(jn, STATUS_ERROR_COUNT);
090        if (count != null) {
091            status.setErrorCount(count);
092        }
093        String instantString = getStringField(jn, STATUS_SUBMIT_TIME);
094        if (isNotEmpty(instantString)) {
095            status.setSubmitTime(Instant.parse(instantString));
096        }
097        instantString = getStringField(jn, STATUS_SCROLL_START_TIME);
098        if (isNotEmpty(instantString)) {
099            status.setScrollStartTime(Instant.parse(instantString));
100        }
101        instantString = getStringField(jn, STATUS_SCROLL_END_TIME);
102        if (isNotEmpty(instantString)) {
103            status.setScrollEndTime(Instant.parse(instantString));
104        }
105        instantString = getStringField(jn, STATUS_PROCESSING_START_TIME);
106        if (isNotEmpty(instantString)) {
107            status.setProcessingStartTime(Instant.parse(instantString));
108        }
109        instantString = getStringField(jn, STATUS_PROCESSING_END_TIME);
110        if (isNotEmpty(instantString)) {
111            status.setProcessingEndTime(Instant.parse(instantString));
112        }
113        instantString = getStringField(jn, STATUS_COMPLETED_TIME);
114        if (isNotEmpty(instantString)) {
115            status.setCompletedTime(Instant.parse(instantString));
116        }
117        Long processingMillis = getLongField(jn, STATUS_PROCESSING_MILLIS);
118        if (processingMillis != null) {
119            status.setProcessingDurationMillis(processingMillis);
120        }
121        instantString = getStringField(jn, STATUS_USERNAME);
122        if (isNotEmpty(instantString)) {
123            status.setUsername(instantString);
124        }
125        Map<String, Serializable> result = emptyMap();
126        if (jn.has(STATUS_RESULT)) {
127            result = BulkParameters.paramsToMap(jn.get(STATUS_RESULT));
128        }
129        status.setResult(result);
130
131        return status;
132    }
133
134}