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.addSubElementStrings;
024import de.isas.mztab2.model.Assay;
025import de.isas.mztab2.model.MsRun;
026import de.isas.mztab2.model.Sample;
027import java.io.IOException;
028import java.util.Arrays;
029import java.util.Comparator;
030import java.util.List;
031import java.util.stream.Collectors;
032import lombok.extern.slf4j.Slf4j;
033import uk.ac.ebi.pride.jmztab2.model.Section;
034
035/**
036 * <p>AssaySerializer implementation for {@link de.isas.mztab2.model.Assay}.</p>
037 * 
038 * @author nilshoffmann
039 * 
040 */
041@Slf4j
042public class AssaySerializer extends StdSerializer<Assay> {
043
044    /**
045     * <p>Constructor for AssaySerializer.</p>
046     */
047    public AssaySerializer() {
048        this(null);
049    }
050
051    /**
052     * <p>Constructor for AssaySerializer.</p>
053     *
054     * @param t a {@link java.lang.Class} object.
055     */
056    public AssaySerializer(Class<Assay> t) {
057        super(t);
058    }
059    
060    @Override
061    public void serializeWithType(Assay value, JsonGenerator gen,
062        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
063        typeSer.writeTypePrefixForObject(value, gen);
064        serialize(value, gen, serializers);
065        typeSer.writeTypeSuffixForObject(value, gen);
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public void serialize(Assay assay, JsonGenerator jg, SerializerProvider sp) throws IOException {
071        if (assay != null) {
072            Serializers.checkIndexedElement(assay);
073            addLineWithProperty(jg, Section.Metadata.getPrefix(), null, assay,
074                assay.
075                    getName());
076            
077            addLineWithProperty(jg, Section.Metadata.getPrefix(), Assay.Properties.externalUri.getPropertyName(),
078                assay, assay.getExternalUri());
079
080            addSubElementStrings(jg, Section.Metadata.getPrefix(), assay, Assay.Properties.custom.getPropertyName(), assay.getCustom(), false);
081
082            List<MsRun> msRunRef = assay.getMsRunRef();
083            if (msRunRef != null) {
084                addSubElementStrings(jg, Section.Metadata.getPrefix(), assay,
085                    Assay.Properties.msRunRef.getPropertyName(), msRunRef.
086                        stream().
087                        sorted(Comparator.comparing(MsRun::getId,
088                            Comparator.nullsFirst(Comparator.
089                                naturalOrder())
090                        )).
091                        map((mref) ->
092                        {
093                            return new StringBuilder().append(de.isas.mztab2.model.Metadata.Properties.msRun.getPropertyName()).
094                                append("[").
095                                append(mref.getId()).
096                                append("]").
097                                toString();
098                        }).
099                        collect(Collectors.toList()), true);
100            }
101            Sample sampleRef = assay.getSampleRef();
102            if (sampleRef != null) {
103                addSubElementStrings(jg, Section.Metadata.getPrefix(), assay,
104                    Assay.Properties.sampleRef.getPropertyName(), Arrays.asList(sampleRef).
105                        stream().
106                        sorted(Comparator.comparing(Sample::getId,
107                            Comparator.nullsFirst(Comparator.
108                                naturalOrder())
109                        )).
110                        map((sref) ->
111                        {
112                            return new StringBuilder().append(de.isas.mztab2.model.Metadata.Properties.sample.getPropertyName()).
113                                append("[").
114                                append(sref.getId()).
115                                append("]").
116                                toString();
117                        }).
118                        collect(Collectors.toList()), true);
119            }
120
121        } else {
122            log.debug(Assay.class.getSimpleName()+" is null!");
123        }
124    }
125}