Skip to content

Commit

Permalink
Merge pull request #60 from digipost/no-timezone-proof
Browse files Browse the repository at this point in the history
Fjern tidssoner fra gyldigheten i Proof
  • Loading branch information
Christian S authored Mar 26, 2020
2 parents 9b19816 + c521853 commit f55e203
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 73 deletions.
1 change: 0 additions & 1 deletion datatypes-examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@
<day>31</day>
<hour>0</hour>
<min>0</min>
<time-zone>+01:00</time-zone>
</to>
</yearly-repeating-period>
</valid-period>
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ Represents a legal document (Certificate, Licence, Permit, etc.) issued to a sin

|Name|Type|Required|Description|
|----|----|--------|-----------|
|from|ZonedDateTime|no|ISO8601 full DateTime|
|to|ZonedDateTime|no|ISO8601 full DateTime|
|from|LocalDateTime|no|ISO8601 full DateTime|
|to|LocalDateTime|no|ISO8601 full DateTime|

### Proof.YearlyRepeatingPeriod

Expand All @@ -621,7 +621,7 @@ Represents a legal document (Certificate, Licence, Permit, etc.) issued to a sin
|day|Integer|yes||
|hour|Integer|no||
|min|Integer|no||
|timeZone|String|no|Timezone ISO-8601|
|timeZone|String|no|Deprecated, do not use. Will be ignored.|

### Proof.ProofHolder

Expand Down Expand Up @@ -670,7 +670,6 @@ Valid values:
<day>31</day>
<hour>0</hour>
<min>0</min>
<time-zone>+01:00</time-zone>
</to>
</yearly-repeating-period>
</valid-period>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private static ObjectMapper initMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setTimeZone(TimeZone.getDefault());
mapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
return mapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.digipost.api.datatypes.marshalling;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeXmlAdapter extends XmlAdapter<String, LocalDateTime> {
@Override
public String marshal(LocalDateTime v) {
if (v == null) {
return null;
}
return v.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

@Override
public LocalDateTime unmarshal(final String s) {
if (s == null) {
return null;
}
return LocalDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(s));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.GregorianCalendar;

public class ZonedDateTimeXmlAdapter extends XmlAdapter<String, ZonedDateTime> {
Expand All @@ -21,7 +24,12 @@ public ZonedDateTime unmarshal(final String s) {
if (s == null) {
return null;
}
return ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(s)).withZoneSameInstant(ZoneId.systemDefault());
final TemporalAccessor parsed = DateTimeFormatter.ISO_DATE_TIME.parse(s);
if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) {
return ZonedDateTime.from(parsed);
} else {
return LocalDateTime.from(parsed).atZone(ZoneOffset.UTC);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public Appointment withDefaultsForMissingOptionalValues() {
}

public static Appointment EXAMPLE = new Appointment(
ZonedDateTime.of(2017, 6, 27, 10, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2017, 6, 27, 11, 0, 0, 0, ZoneId.systemDefault())
ZonedDateTime.of(2017, 6, 27, 10, 0, 0, 0, ZoneId.of("+02:00"))
, ZonedDateTime.of(2017, 6, 27, 11, 0, 0, 0, ZoneId.of("+02:00"))
, "Oppmøte senest 15 minutter før timen"
, "Oslo City Røntgen"
, Address.EXAMPLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Boligdetaljer(Residence residence, List<Hjemmelshaver> hjemmelshavere, In
Residence.EXAMPLE
, Collections.singletonList(new Hjemmelshaver("Gunnar Gunnersen", "[email protected]"))
, 59, 3, 4
, Collections.singletonList(new Omsetningshistorikk(ZonedDateTime.of(2017, 7, 27, 10, 0, 0, 0, ZoneId.systemDefault())
, Collections.singletonList(new Omsetningshistorikk(ZonedDateTime.of(2017, 7, 27, 10, 0, 0, 0, ZoneId.of("+02:00"))
, "Privat salg av sekundærbolig"
, "Bill Isalg"
, "Cooper Coopersen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ExternalLink(final URI url) {
String buttonText;

public static ExternalLink EXAMPLE = new ExternalLink(URI.create("https://www.oslo.kommune.no/barnehage/svar-pa-tilbud-om-plass/"),
ZonedDateTime.of(2017, 9, 30, 13, 37, 0, 0, ZoneId.systemDefault()),
ZonedDateTime.of(2017, 9, 30, 13, 37, 0, 0, ZoneId.of("+02:00")),
"Oslo Kommune ber deg akseptere eller avslå tilbudet om barnehageplass.", "Svar på barnehageplass");

public static ExternalLink EXAMPLE_NO_DEADLINE = new ExternalLink(URI.create("https://www.example.com"),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/digipost/api/datatypes/types/Inkasso.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Inkasso implements DataType {

public static Inkasso EXAMPLE = new Inkasso(
ExternalLink.EXAMPLE_NO_DEADLINE
, ZonedDateTime.of(2019, 12, 10, 0, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2019, 12, 10, 0, 0, 0, 0, ZoneId.of("+01:00"))
, BigDecimal.valueOf(42), "01235424320", "1435025439583420243982723"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SignedDocument implements DataType {
public static SignedDocument EXAMPLE = new SignedDocument(
"Bedrift AS",
"Ansettelseskontrakt",
ZonedDateTime.of(2018, 7, 11, 10, 0, 0, 0, ZoneId.systemDefault())
ZonedDateTime.of(2018, 7, 11, 10, 0, 0, 0, ZoneId.of("+02:00"))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TimeInterval {
ZonedDateTime endTime;

public static TimeInterval EXAMPLE = new TimeInterval(
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2019, 5, 23, 16, 0, 0, 0, ZoneId.systemDefault())
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.of("+02:00"))
, ZonedDateTime.of(2019, 5, 23, 16, 0, 0, 0, ZoneId.of("+02:00"))
);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
@XmlSchema(namespace = DIGIPOST_DATATYPES_NAMESPACE, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class)
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class),
@XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
})
@DataTypePackage
package no.digipost.api.datatypes.types;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.LocalDateTimeXmlAdapter;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import static no.digipost.api.datatypes.marshalling.DataTypesJAXBContext.DIGIPOST_DATATYPES_NAMESPACE;
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public PickupNotice withDefaultsForMissingOptionalValues() {
, "70300492517312675"
, Barcode.EXAMPLE.withBarcodeType("CODE_128")
, "Klimanøytral Servicepakke"
, ZonedDateTime.of(2018, 9, 10, 10, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2018, 9, 24, 10, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2018, 9, 10, 10, 0, 0, 0, ZoneId.of("+02:00"))
, ZonedDateTime.of(2018, 9, 24, 10, 0, 0, 0, ZoneId.of("+02:00"))
, Recipient.EXAMPLE
, Sender.EXAMPLE
, PickupPlace.EXAMPLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public class PickupNoticeStatus implements DataType {

public static PickupNoticeStatus EXAMPLE = new PickupNoticeStatus(
Status.READY_FOR_PICKUP
, ZonedDateTime.of(2019, 1, 10, 10, 10, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2019, 1, 10, 10, 10, 0, 0, ZoneId.of("+01:00"))
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package no.digipost.api.datatypes.types.proof;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;

import javax.validation.constraints.NotNull;
Expand All @@ -16,7 +14,6 @@

@XmlType
@Value
@AllArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@With
public class CalendarDate {
Expand Down Expand Up @@ -44,8 +41,22 @@ public class CalendarDate {

@XmlElement(name = "time-zone", defaultValue = "+01:00")
@Pattern(regexp = "Z|[+-][01]\\d:{0,1}[0-5]\\d|[+-][01]\\d")
@Description("Timezone ISO-8601")
@Description("Deprecated, do not use. Will be ignored.")
String timeZone;

public static CalendarDate EXAMPLE = new CalendarDate(5, 9, 0, 0, "+01:00");
@Deprecated
public CalendarDate(Integer month, Integer day, Integer hour, Integer min, String timeZone) {
this.month = month;
this.day = day;
this.hour = hour;
this.min = min;
this.timeZone = timeZone;
}

public CalendarDate(Integer month, Integer day, Integer hour, Integer min) {
this(month, day, hour, min, null);
}


public static CalendarDate EXAMPLE = new CalendarDate(5, 9, 0, 0);
}
12 changes: 5 additions & 7 deletions src/main/java/no/digipost/api/datatypes/types/proof/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;
import no.digipost.api.datatypes.validation.ValidPeriode;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;

@XmlType
@Value
Expand All @@ -25,15 +23,15 @@
public class Period {
@XmlElement(name = "from")
@Description("ISO8601 full DateTime")
ZonedDateTime from;
LocalDateTime from;

@XmlElement(name = "to")
@Description("ISO8601 full DateTime")
ZonedDateTime to;
LocalDateTime to;

public static Period EXAMPLE = new Period(
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.of("+0100"))
, ZonedDateTime.of(2019, 5, 23, 16, 0, 0, 0, ZoneId.of("+0100"))
LocalDateTime.of(2019, 5, 23, 10, 0, 0, 0)
, LocalDateTime.of(2019, 5, 23, 16, 0, 0, 0)
);

public String getISO8601() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.DataType;
import no.digipost.api.datatypes.documentation.Description;
import no.digipost.api.datatypes.types.Info;
Expand Down Expand Up @@ -88,7 +87,7 @@ public class Proof implements DataType {
new Proof(
"Bekkestua Bibliotek",
"#e1e1e1",
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.systemDefault()),
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.of("+02:00")),
ValidPeriod.EXAMPLE,
ProofHolder.EXAMPLE,
"Lånekort",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;
import no.digipost.api.datatypes.types.Address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.Instant;
import java.time.LocalDateTime;

@XmlType
@Value
Expand Down Expand Up @@ -44,39 +41,35 @@ public class YearlyRepeatingPeriod {
public static YearlyRepeatingPeriod EXAMPLE = new YearlyRepeatingPeriod(
2020,
2022,
new CalendarDate(1, 1, null, null, null),
new CalendarDate(12, 31, 0, 0, "+01:00")
new CalendarDate(1, 1, null, null),
new CalendarDate(12, 31, 0, 0)
);

public String getISO8601() {
Instant now = Instant.now();
ZonedDateTime startDate = buildZonedDateTime(now, from, startYear);
ZonedDateTime endDate = buildZonedDateTime(now, to, endYear);
String startDate = writeLocalDate(from, startYear);
String endDate = writeLocalDate(to, endYear);

if (startYear != null && endYear != null) {
return "R/" + startDate + "/" + endDate;
} else if (endYear != null) {
return "R/" + startDate.toString().substring(5) + "/" + endDate.toString();
return "R/" + startDate.substring(5) + "/" + endDate;
} else if (startYear != null) {
return "R/" + startDate + "/" + endDate.toString().substring(5);
return "R/" + startDate + "/" + endDate.substring(5);
} else {
return "R/" + startDate.toString().substring(5) + "/" + endDate.toString().substring(5);
return "R/" + startDate.substring(5) + "/" + endDate.substring(5);
}
}

private static ZonedDateTime buildZonedDateTime(Instant now, CalendarDate t_date, Integer year) {
private static String writeLocalDate(CalendarDate t_date, Integer year) {
int t_minute = 0;
int t_hour = 0;
int t_day = 1;
int t_month = 1;
ZoneId t_zone;
t_zone = t_date.getTimeZone() != null ? ZoneId.of(t_date.getTimeZone()) : ZoneId.of("+01:00");
t_minute = t_date.getMin() != null ? t_date.getMin() : t_minute;
t_hour = t_date.getHour() != null ? t_date.getHour() : t_hour;
t_day = t_date.getDay() != null ? t_date.getDay() : t_day;
t_month = t_date.getMonth() != null ? t_date.getMonth() : t_month;
ZonedDateTime nowInThatZone = now.atZone(t_zone);
int t_year = year != null ? year : nowInThatZone.getYear();
return ZonedDateTime.of(t_year, t_month, t_day, t_hour, t_minute, 0, 0, t_zone);
int t_year = year != null ? year : 2019; // note: the default value will be discarded, but we need it to create a LocalDateTime
return LocalDateTime.of(t_year, t_month, t_day, t_hour, t_minute, 0, 0).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class),
@XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
})
@DataTypePackage
package no.digipost.api.datatypes.types.proof;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.LocalDateTimeXmlAdapter;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class Receipt implements DataType {
public static Receipt EXAMPLE = new Receipt(
"F96B6805-2453-478A-B58B-CCDFA07E21ED"
, "364567"
, ZonedDateTime.of(2018, 5, 27, 10, 0, 0, 0, ZoneId.systemDefault())
, ZonedDateTime.of(2018, 5, 27, 10, 0, 0, 0, ZoneId.of("+02:00"))
, ReceiptLine.EXAMPLE.getTotalPrice()
, ReceiptLine.EXAMPLE.getTotalVat()
, "NOK"
Expand Down
Loading

0 comments on commit f55e203

Please sign in to comment.