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.addSubElementParameter;
024import static de.isas.mztab2.io.serialization.Serializers.addSubElementParameters;
025import de.isas.mztab2.model.Metadata;
026import de.isas.mztab2.model.MsRun;
027import java.io.IOException;
028import lombok.extern.slf4j.Slf4j;
029import uk.ac.ebi.pride.jmztab2.model.Section;
030
031/**
032 * <p>MsRunSerializer implementation for {@link de.isas.mztab2.model.MsRun}.</p>
033 *
034 * @author nilshoffmann
035 *
036 */
037@Slf4j
038public class MsRunSerializer extends StdSerializer<MsRun> {
039
040    /**
041     * <p>
042     * Constructor for MsRunSerializer.</p>
043     */
044    public MsRunSerializer() {
045        this(null);
046    }
047
048    /**
049     * <p>
050     * Constructor for MsRunSerializer.</p>
051     *
052     * @param t a {@link java.lang.Class} object.
053     */
054    public MsRunSerializer(Class<MsRun> t) {
055        super(t);
056    }
057
058    @Override
059    public void serializeWithType(MsRun value, JsonGenerator gen,
060        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
061        typeSer.writeTypePrefixForObject(value, gen);
062        serialize(value, gen, serializers);
063        typeSer.writeTypeSuffixForObject(value, gen);
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public void serialize(MsRun msRun, JsonGenerator jg,
071        SerializerProvider sp) throws IOException {
072        if (msRun != null) {
073            Serializers.checkIndexedElement(msRun);
074            addLineWithProperty(jg, Section.Metadata.getPrefix(),
075                MsRun.Properties.name.getPropertyName(), msRun,
076                msRun.getName());
077            addLineWithProperty(jg, Section.Metadata.getPrefix(),
078                MsRun.Properties.location.getPropertyName(),
079                msRun, msRun.getLocation());
080            if (msRun.getInstrumentRef() != null) {
081                addLineWithProperty(jg, Section.Metadata.getPrefix(),
082                    MsRun.Properties.instrumentRef.getPropertyName(), msRun,
083                    new StringBuilder().append(Metadata.Properties.instrument.
084                        getPropertyName() + "[" + msRun.getInstrumentRef().
085                            getId() + "]").
086                        toString());
087            }
088            addLineWithProperty(jg, Section.Metadata.getPrefix(),
089                MsRun.Properties.hash.getPropertyName(),
090                msRun, msRun.getHash());
091            addSubElementParameter(jg, Section.Metadata.getPrefix(), msRun,
092                MsRun.Properties.hashMethod.getPropertyName(),
093                msRun.getHashMethod());
094            addSubElementParameter(jg, Section.Metadata.getPrefix(), msRun,
095                MsRun.Properties.format.getPropertyName(), msRun.getFormat());
096            addSubElementParameters(jg, Section.Metadata.getPrefix(), msRun,
097                MsRun.Properties.fragmentationMethod.getPropertyName(),
098                msRun.getFragmentationMethod(), false);
099            addSubElementParameters(jg, Section.Metadata.getPrefix(), msRun,
100                MsRun.Properties.scanPolarity.getPropertyName(),
101                msRun.getScanPolarity(), false);
102            addSubElementParameter(jg, Section.Metadata.getPrefix(), msRun,
103                MsRun.Properties.idFormat.getPropertyName(),
104                msRun.getIdFormat());
105        } else {
106            log.debug(MsRun.class.getSimpleName() + " is null!");
107        }
108    }
109}