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.addLineWithNullProperty; 023import static de.isas.mztab2.io.serialization.Serializers.addLineWithProperty; 024import de.isas.mztab2.model.Database; 025import de.isas.mztab2.model.Parameter; 026import java.io.IOException; 027import java.util.Arrays; 028import java.util.Optional; 029import lombok.extern.slf4j.Slf4j; 030import uk.ac.ebi.pride.jmztab2.model.Section; 031 032/** 033 * <p>DatabaseSerializer implementation for {@link de.isas.mztab2.model.Database}.</p> 034 * 035 * @author nilshoffmann 036 * 037 */ 038@Slf4j 039public class DatabaseSerializer extends StdSerializer<Database> { 040 041 /** 042 * <p> 043 * Constructor for DatabaseSerializer.</p> 044 */ 045 public DatabaseSerializer() { 046 this(null); 047 } 048 049 /** 050 * <p> 051 * Constructor for DatabaseSerializer.</p> 052 * 053 * @param t a {@link java.lang.Class} object. 054 */ 055 public DatabaseSerializer(Class<Database> t) { 056 super(t); 057 } 058 059 @Override 060 public void serializeWithType(Database 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(Database database, JsonGenerator jg, 072 SerializerProvider sp) throws IOException { 073 if (database != null) { 074 Serializers.checkIndexedElement(database); 075 Serializers.addLineWithPropertyParameters(jg, Section.Metadata. 076 getPrefix(), 077 null, database, Arrays.asList(database.getParam())); 078 Optional<Parameter> dbParam = Optional.ofNullable(database.getParam()); 079 if (dbParam.isPresent() && dbParam.get(). 080 getName(). 081 equals("no database")) { 082 addLineWithNullProperty(jg, Section.Metadata.getPrefix(), 083 Database.Properties.prefix.getPropertyName(), database); 084 addLineWithProperty(jg, Section.Metadata.getPrefix(), 085 Database.Properties.uri.getPropertyName(), database, 086 "null"); 087 } else { 088 addLineWithProperty(jg, Section.Metadata.getPrefix(), 089 Database.Properties.prefix.getPropertyName(), database, 090 database. 091 getPrefix()); 092 addLineWithProperty(jg, Section.Metadata.getPrefix(), 093 Database.Properties.uri.getPropertyName(), database, 094 database.getUri()); 095 } 096 addLineWithProperty(jg, Section.Metadata.getPrefix(), 097 Database.Properties.version.getPropertyName(), database, 098 Optional.ofNullable(database.getVersion()).orElse("Unknown")); 099 100 } else { 101 log.debug(Database.class.getSimpleName() + " is null!"); 102 } 103 } 104 105}