001package com.identityworksllc.iiq.common.plugin.vo;
002
003import com.fasterxml.jackson.annotation.JsonAutoDetect;
004import com.fasterxml.jackson.annotation.JsonPropertyDescription;
005
006import java.text.SimpleDateFormat;
007import java.time.ZoneId;
008import java.time.format.TextStyle;
009import java.util.Date;
010import java.util.Locale;
011import java.util.Objects;
012import java.util.StringJoiner;
013import java.util.TimeZone;
014
015/**
016 * A VO class to wrap a date in a known format, allowing clients to
017 * consume it however they wish.
018 */
019@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
020public class ExpandedDate {
021    @JsonPropertyDescription("The date, rendered as an ISO8601 UTC timestamp string")
022    private final String date;
023
024    @JsonPropertyDescription("The server time zone ID, as returned by Java's ZoneId class")
025    private final String serverTimeZoneId;
026    @JsonPropertyDescription("The server's 'short' time zone name")
027    private final String serverTimeZoneName;
028    @JsonPropertyDescription("The timestamp in Unix epoch milliseconds")
029    private final long timestamp;
030
031    /**
032     * Constructs a new ExpandedDate from the given input date
033     * @param in The input date, not null
034     */
035    public ExpandedDate(Date in) {
036        Objects.requireNonNull(in, "Cannot expand a null Date");
037
038        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
039        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
040
041        this.timestamp = in.getTime();
042        this.date = dateFormat.format(in);
043        this.serverTimeZoneId = ZoneId.systemDefault().getId();
044        this.serverTimeZoneName = ZoneId.systemDefault().getDisplayName(TextStyle.SHORT, Locale.US);
045    }
046
047    public String getDate() {
048        return date;
049    }
050
051    public String getServerTimeZoneId() {
052        return serverTimeZoneId;
053    }
054
055    public String getServerTimeZoneName() {
056        return serverTimeZoneName;
057    }
058
059    public long getTimestamp() {
060        return timestamp;
061    }
062
063    @Override
064    public String toString() {
065        StringJoiner joiner = new StringJoiner(", ", ExpandedDate.class.getSimpleName() + "[", "]");
066        if ((date) != null) {
067            joiner.add("date='" + date + "'");
068        }
069        if ((serverTimeZoneId) != null) {
070            joiner.add("serverTimeZoneId='" + serverTimeZoneId + "'");
071        }
072        if ((serverTimeZoneName) != null) {
073            joiner.add("serverTimeZoneName='" + serverTimeZoneName + "'");
074        }
075        joiner.add("timestamp=" + timestamp);
076        return joiner.toString();
077    }
078}