001/*
002 * (C) Copyright 2020 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 *     Nour AL KOTOB
018 */
019package org.nuxeo.ecm.restapi.jaxrs.io.management;
020
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023
024import java.io.IOException;
025import java.util.List;
026
027import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
028import org.nuxeo.ecm.core.io.registry.reflect.Setup;
029import org.nuxeo.runtime.migration.Migration;
030import org.nuxeo.runtime.migration.MigrationService.MigrationStatus;
031import org.nuxeo.runtime.migration.MigrationStep;
032
033import com.fasterxml.jackson.core.JsonGenerator;
034
035/**
036 * @since 11.3
037 */
038@Setup(mode = SINGLETON, priority = REFERENCE)
039public class MigrationJsonWriter extends ExtensibleEntityJsonWriter<Migration> {
040
041    public static final String ENTITY_TYPE = "migration";
042
043    public MigrationJsonWriter() {
044        super(ENTITY_TYPE, Migration.class);
045    }
046
047    @Override
048    public void writeEntityBody(Migration entity, JsonGenerator jg) throws IOException {
049        jg.writeStringField("id", entity.getId());
050        jg.writeStringField("description", entity.getDescription());
051        jg.writeStringField("descriptionLabel", entity.getDescriptionLabel());
052        writeMigrationStatus(entity.getStatus(), jg);
053        writeMigrationSteps(entity.getSteps(), jg);
054    }
055
056    protected void writeMigrationStatus(MigrationStatus status, JsonGenerator jg) throws IOException {
057        jg.writeObjectFieldStart("status");
058        jg.writeStringField("state", status.getState());
059        jg.writeStringField("step", status.getStep());
060        jg.writeNumberField("startTime", status.getStartTime());
061        jg.writeNumberField("pingTime", status.getPingTime());
062        jg.writeStringField("progressMessage", status.getProgressMessage());
063        jg.writeNumberField("progressNum", status.getProgressNum());
064        jg.writeNumberField("progressTotal", status.getProgressTotal());
065        jg.writeBooleanField("running", status.isRunning());
066        jg.writeEndObject();
067    }
068
069    protected void writeMigrationSteps(List<MigrationStep> steps, JsonGenerator jg) throws IOException {
070        jg.writeArrayFieldStart("steps");
071        for (MigrationStep step : steps) {
072            jg.writeStartObject();
073            jg.writeStringField("id", step.getId());
074            jg.writeStringField("fromState", step.getFromState());
075            jg.writeStringField("toState", step.getToState());
076            jg.writeStringField("description", step.getDescription());
077            jg.writeStringField("descriptionLabel", step.getDescriptionLabel());
078            jg.writeEndObject();
079        }
080        jg.writeEndArray();
081    }
082
083}