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.apache.commons.lang3.StringUtils.isNotEmpty;
022import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_COMMAND;
023import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_COUNT;
024import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_ENTITY_TYPE;
025import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_ID;
026import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_STATE;
027import static org.nuxeo.ecm.core.bulk.io.BulkConstants.BULK_SUBMIT;
028import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
029import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
030
031import java.io.IOException;
032import java.time.Instant;
033
034import org.nuxeo.ecm.core.bulk.BulkCommand;
035import org.nuxeo.ecm.core.bulk.BulkStatus;
036import org.nuxeo.ecm.core.bulk.BulkStatus.State;
037import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
038import org.nuxeo.ecm.core.io.registry.reflect.Setup;
039
040import com.fasterxml.jackson.databind.JsonNode;
041
042/**
043 * @since 10.2
044 */
045@Setup(mode = SINGLETON, priority = REFERENCE)
046public class BulkJsonReader extends EntityJsonReader<BulkStatus> {
047
048    public BulkJsonReader() {
049        super(BULK_ENTITY_TYPE);
050    }
051
052    @Override
053    public BulkStatus readEntity(JsonNode jn) throws IOException {
054        BulkStatus status = new BulkStatus();
055
056        String id = jn.get(BULK_ID).asText();
057        status.setId(id);
058
059        String state = getStringField(jn, BULK_STATE);
060        if (isNotEmpty(state)) {
061            status.setState(State.valueOf(state));
062        }
063
064        String creation = getStringField(jn, BULK_SUBMIT);
065
066        if (isNotEmpty(creation)) {
067            status.setSubmitTime(Instant.parse(creation));
068        }
069
070        JsonNode jnCommand = jn.get(BULK_COMMAND);
071        if (jnCommand != null && !jnCommand.isNull()) {
072            BulkCommand command = readEntity(BulkCommand.class, BulkCommand.class, jnCommand);
073            status.setCommand(command);
074        }
075
076        Long count = getLongField(jn, BULK_COUNT);
077        status.setCount(count);
078
079        return status;
080    }
081
082}