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.addLine;
023import de.isas.mztab2.model.Metadata;
024import de.isas.mztab2.model.Publication;
025import java.io.IOException;
026import java.util.Collections;
027import java.util.Optional;
028import java.util.stream.Collectors;
029import lombok.extern.slf4j.Slf4j;
030import uk.ac.ebi.pride.jmztab2.model.Section;
031
032/**
033 * <p>PublicationSerializer implementation for {@link de.isas.mztab2.model.Publication}.</p>
034 *
035 * @author nilshoffmann
036 *
037 */
038@Slf4j
039public class PublicationSerializer extends StdSerializer<Publication> {
040
041    /**
042     * <p>
043     * Constructor for PublicationSerializer.</p>
044     */
045    public PublicationSerializer() {
046        this(null);
047    }
048
049    /**
050     * <p>
051     * Constructor for PublicationSerializer.</p>
052     *
053     * @param t a {@link java.lang.Class} object.
054     */
055    public PublicationSerializer(Class<Publication> t) {
056        super(t);
057    }
058    
059    @Override
060    public void serializeWithType(Publication value, JsonGenerator gen,
061        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
062        typeSer.writeTypePrefixForObject(value, gen);
063        serialize(value, gen, serializers);
064        typeSer.writeTypeSuffixForObject(value, gen);
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public void serialize(Publication publication, JsonGenerator jg,
072        SerializerProvider sp) throws IOException {
073        if (publication != null) {
074            Serializers.checkIndexedElement(publication);
075            addLine(jg, Section.Metadata.getPrefix(),
076                Metadata.Properties.publication+"[" + publication.getId() + "]", Optional.ofNullable(
077                publication.
078                    getPublicationItems()).
079                orElse(Collections.emptyList()).
080                stream().
081                map(pitem ->
082                    pitem.getType().
083                        getValue() + ":" + pitem.getAccession()).
084                collect(Collectors.joining(
085                    "|", "", "")));
086        } else {
087            log.debug(Publication.class.getSimpleName()+" is null!");
088        }
089    }
090}