001/* 
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
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 */
016package de.isas.mztab2.io.serialization;
017
018import com.fasterxml.jackson.core.JsonGenerator;
019import com.fasterxml.jackson.databind.SerializerProvider;
020import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
021import com.fasterxml.jackson.databind.ser.std.StdSerializer;
022import static de.isas.mztab2.io.serialization.Serializers.addLineWithProperty;
023import static de.isas.mztab2.io.serialization.Serializers.addSubElementParameters;
024import static de.isas.mztab2.io.serialization.Serializers.addSubElementStrings;
025import de.isas.mztab2.model.Assay;
026import de.isas.mztab2.model.Metadata;
027import de.isas.mztab2.model.StudyVariable;
028import java.io.IOException;
029import java.util.Collections;
030import java.util.Comparator;
031import java.util.Optional;
032import java.util.stream.Collectors;
033import lombok.extern.slf4j.Slf4j;
034import uk.ac.ebi.pride.jmztab2.model.Section;
035
036/**
037* <p>StudyVariableSerializer implementation for {@link de.isas.mztab2.model.StudyVariable}.</p>
038 *
039 * @author nilshoffmann
040 *
041 */
042@Slf4j
043public class StudyVariableSerializer extends StdSerializer<StudyVariable> {
044
045    /**
046     * <p>
047     * Constructor for StudyVariableSerializer.</p>
048     */
049    public StudyVariableSerializer() {
050        this(null);
051    }
052
053    /**
054     * <p>
055     * Constructor for StudyVariableSerializer.</p>
056     *
057     * @param t a {@link java.lang.Class} object.
058     */
059    public StudyVariableSerializer(Class<StudyVariable> t) {
060        super(t);
061    }
062
063    @Override
064    public void serializeWithType(StudyVariable value, JsonGenerator gen,
065        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
066        typeSer.writeTypePrefixForObject(value, gen);
067        serialize(value, gen, serializers);
068        typeSer.writeTypeSuffixForObject(value, gen);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void serialize(StudyVariable studyVariable, JsonGenerator jg,
076        SerializerProvider sp) throws IOException {
077        if (studyVariable != null) {
078            Serializers.checkIndexedElement(studyVariable);
079            addLineWithProperty(jg, Section.Metadata.getPrefix(), null,
080                studyVariable,
081                studyVariable.getName());
082            addLineWithProperty(jg, Section.Metadata.getPrefix(),
083                StudyVariable.Properties.description.getPropertyName(),
084                studyVariable, studyVariable.getDescription());
085            addSubElementParameters(jg, Section.Metadata.getPrefix(),
086                studyVariable,
087                StudyVariable.Properties.factors.getPropertyName(),
088                studyVariable.getFactors(), true);
089            addLineWithProperty(jg, Section.Metadata.getPrefix(),
090                StudyVariable.Properties.averageFunction.getPropertyName(),
091                studyVariable, studyVariable.
092                    getAverageFunction());
093            addLineWithProperty(jg, Section.Metadata.getPrefix(),
094                StudyVariable.Properties.variationFunction.getPropertyName(),
095                studyVariable, studyVariable.
096                    getVariationFunction());
097            addSubElementStrings(jg, Section.Metadata.getPrefix(), studyVariable,
098                StudyVariable.Properties.assayRefs.getPropertyName(), Optional.
099                ofNullable(studyVariable.getAssayRefs()).
100                orElse(Collections.emptyList()).
101                stream().
102                sorted(Comparator.comparing(Assay::getId,
103                    Comparator.nullsFirst(Comparator.
104                        naturalOrder())
105                )).
106                map((assayRef) ->
107                {
108                    return new StringBuilder().append(Metadata.Properties.assay.
109                        getPropertyName()).
110                        append("[").
111                        append(assayRef.getId()).
112                        append("]").
113                        toString();
114                }).
115                collect(Collectors.toList()), true);
116        } else {
117            log.debug(StudyVariable.class.getSimpleName() + " is null!");
118        }
119    }
120}