All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.domain.api.profile.Profile Maven / Gradle / Ivy

Go to download

Contains the objects used by the Frontend API, like forms and result objects

There is a newer version: 8.3.3
Show newest version
/*
 * Copyright (C) 2013 All rights reserved
 * VPRO The Netherlands
 */
package nl.vpro.domain.api.profile;

import java.io.IOException;
import java.io.StringWriter;
import java.time.Instant;

import org.checkerframework.checker.nullness.qual.NonNull;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import nl.vpro.domain.media.MediaObject;
import nl.vpro.domain.page.Page;
import nl.vpro.xml.bind.InstantXmlAdapter;

/**
 * @author Roelof Jan Koekoek
 * @since 2.0
 */
@XmlRootElement(name = "profile")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "profileType", propOrder = {"pageProfile", "mediaProfile"})
@JsonSerialize(using = Profile.Serializer.class)
public class Profile implements Comparable {

    @XmlAttribute
    @XmlJavaTypeAdapter(value = InstantXmlAdapter.class)
    @XmlSchemaType(name = "dateTime")
    private Instant timestamp = Instant.now();

    @XmlAttribute
    private String name;

    private ProfileDefinition pageProfile;

    private ProfileDefinition mediaProfile;

    private Profile() {
    }

    public Profile(String name) {
        this.name = name;
    }

    public Profile(String name, ProfileDefinition pageProfile, ProfileDefinition mediaProfile) {
        this.name = name;

        if(pageProfile != null) {
            pageProfile.setProfile(this);
            this.pageProfile = pageProfile;
        }

        if(mediaProfile != null) {
            mediaProfile.setProfile(this);
            this.mediaProfile = mediaProfile;
        }
    }

    public Instant getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Instant timestamp) {
        this.timestamp = timestamp;
    }

    public String getName() {
        return name;
    }

    public ProfileDefinition getPageProfile() {
        return pageProfile;
    }


    public ProfileDefinition getMediaProfile() {
        return mediaProfile;
    }

    @Override
    public String toString() {
        return "Profile{name='" + name + "'}";
    }

    @Override
    public int compareTo(@NonNull Profile o) {
        // inverse ordering on time
        if(o == null) {
            return -1;
        }

        int c = name.compareTo(o.getName());
        if(c != 0) {
            return c;
        }

        return o.getTimestamp().compareTo(timestamp);
    }

    @Override
    public boolean equals(Object o) {
        if(this == o) {
            return true;
        }
        if(o == null || getClass() != o.getClass()) {
            return false;
        }

        Profile profile = (Profile)o;

        if(!name.equals(profile.name)) {
            return false;
        }
        if(!timestamp.equals(profile.timestamp)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = timestamp.hashCode();
        result = 31 * result + name.hashCode();
        return result;
    }

    public static class Serializer extends JsonSerializer {

        @Override
        public void serialize(final Profile profile, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            //
            jsonGenerator.writeObject(new nl.vpro.domain.api.Error(Response.Status.NOT_IMPLEMENTED.getStatusCode(), "Writing to json unsupported. Please use Accept: application/xml (unsupported by by swagger)") {
                @XmlElement
                String getXml() {
                    StringWriter xml = new StringWriter();
                    JAXB.marshal(profile, xml);
                    return xml.toString();
                }
            });
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy