All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.gson.ReportVariableAdapter Maven / Gradle / Ivy

package fr.ird.observe.gson;

/*-
 * #%L
 * ObServe Toolkit :: Dto
 * %%
 * Copyright (C) 2017 - 2021 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */


import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import fr.ird.observe.dto.IdDto;
import fr.ird.observe.dto.report.ReportVariable;
import fr.ird.observe.spi.module.BusinessProject;
import io.ultreia.java4all.util.ServiceLoaders;

import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Tony Chemit - [email protected]
 */
public class ReportVariableAdapter implements JsonDeserializer> {

    private BusinessProject businessProject;

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public ReportVariable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject reportVariableJson = json.getAsJsonObject();

        String name = context.deserialize(reportVariableJson.get(ReportVariable.PROPERTY_NAME), String.class);
        Class type = context.deserialize(reportVariableJson.get(ReportVariable.PROPERTY_TYPE), Class.class);
        String request = context.deserialize(reportVariableJson.get(ReportVariable.PROPERTY_REQUEST), String.class);

        ReportVariable reportVariable = new ReportVariable<>(name, type, request);

        Class valueType = type;

        if (IdDto.class.isAssignableFrom(type)) {
            if (businessProject == null) {
                businessProject = ServiceLoaders.loadUniqueService(BusinessProject.class);
            }
            //FIXME this is weak we should not transform types like this...
            valueType = businessProject.getMapping().getReferenceType((Class) type);
        }

        JsonElement valuesJson = reportVariableJson.get(ReportVariable.PROPERTY_VALUES);

        if (valuesJson != null) {
            Set values = new HashSet<>();
            for (JsonElement valueJson : valuesJson.getAsJsonArray()) {
                Object value = context.deserialize(valueJson, valueType);
                values.add(value);
            }

            reportVariable.setValues(values);
        }

        Object selectedValue = context.deserialize(reportVariableJson.get(ReportVariable.PROPERTY_SELECTED_VALUE), valueType);

        reportVariable.setSelectedValue(selectedValue);

        return reportVariable;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy