JobEventXml.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * blueHour
  6.  * http://bluehour.tidalwave.it - git clone git@bitbucket.org:tidalwave/bluehour-src.git
  7.  * %%
  8.  * Copyright (C) 2013 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.accounting.exporter.xml.impl.xml;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import java.time.LocalDateTime;
  33. import javax.xml.bind.annotation.XmlAttribute;
  34. import javax.xml.bind.annotation.XmlElement;
  35. import javax.xml.bind.annotation.XmlID;
  36. import javax.xml.bind.annotation.XmlAccessorOrder;
  37. import javax.xml.bind.annotation.XmlAccessorType;
  38. import javax.xml.bind.annotation.XmlElementWrapper;
  39. import javax.xml.bind.annotation.XmlRootElement;
  40. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  41. import it.tidalwave.util.Id;
  42. import it.tidalwave.accounting.model.JobEvent;
  43. import it.tidalwave.accounting.model.types.Money;
  44. import it.tidalwave.accounting.exporter.xml.impl.adapters.MoneyAdapter;
  45. import it.tidalwave.accounting.exporter.xml.impl.adapters.EventTypeAdapter;
  46. import it.tidalwave.accounting.exporter.xml.impl.adapters.IdAdapter;
  47. import it.tidalwave.accounting.exporter.xml.impl.adapters.LocalDateTimeAdapter;
  48. import lombok.NoArgsConstructor;
  49. import lombok.ToString;
  50. import static java.util.stream.Collectors.toList;
  51. import static javax.xml.bind.annotation.XmlAccessOrder.ALPHABETICAL;
  52. import static javax.xml.bind.annotation.XmlAccessType.FIELD;

  53. /***********************************************************************************************************************
  54.  *
  55.  * @author  Fabrizio Giudici
  56.  *
  57.  **********************************************************************************************************************/
  58. @NoArgsConstructor @ToString
  59. @XmlRootElement(name = "event") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
  60. public class JobEventXml
  61.   {
  62.     @XmlAttribute(name = "id")
  63.     @XmlID
  64.     @XmlJavaTypeAdapter(IdAdapter.class)
  65.     private Id id;
  66.    
  67.     @XmlElement(name = "type")
  68.     @XmlJavaTypeAdapter(EventTypeAdapter.class)
  69.     private JobEvent.Type type;
  70.    
  71.     @XmlElement(name = "startDateTime")
  72.     @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
  73.     private LocalDateTime startDateTime;
  74.    
  75.     @XmlElement(name = "endDateTime")
  76.     @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
  77.     private LocalDateTime endDateTime;
  78.    
  79.     @XmlElement(name = "name")
  80.     private String name;
  81.    
  82.     @XmlElement(name = "description")
  83.     private String description;
  84.    
  85.     @XmlElement(name = "earnings")
  86.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  87.     private Money earnings;
  88.    
  89.     @XmlElement(name = "hourlyRate")
  90.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  91.     private Money hourlyRate;
  92.    
  93.     @XmlElementWrapper(name = "events")
  94.     @XmlElement(name = "event")
  95.     private List<JobEventXml> jobEventsXml;
  96.    
  97.     public JobEventXml (@Nonnull final JobEvent jobEvent)
  98.       {
  99.         final var builder = jobEvent.toBuilder();
  100.         this.id = builder.getId();
  101.         this.type = builder.getType();
  102.         this.startDateTime = builder.getStartDateTime();
  103.         this.endDateTime = builder.getEndDateTime();
  104.         this.name = builder.getName();
  105.         this.description = builder.getDescription();
  106.         this.earnings = builder.getEarnings();
  107.         this.hourlyRate = builder.getHourlyRate();
  108.         this.jobEventsXml = builder.getEvents().isEmpty()
  109.                     ? null
  110.                     : builder.getEvents().stream().map(JobEventXml::new).collect(toList());
  111.       }
  112.    
  113.     @Nonnull
  114.     public JobEvent.Builder toBuilder()
  115.       {
  116.         return JobEvent.builder().withId(id)
  117.                                  .withType(type)
  118.                                  .withStartDateTime(startDateTime)
  119.                                  .withEndDateTime(endDateTime)
  120.                                  .withName(name)
  121.                                  .withDescription(description)
  122.                                  .withEarnings(earnings)
  123.                                  .withHourlyRate(hourlyRate)
  124.                                  .withEvents(toJobEvents(jobEventsXml));
  125.       }
  126.    
  127.     @Nonnull
  128.     public static List<JobEvent> toJobEvents (@Nullable final List<? extends JobEventXml> jobEventsXml)
  129.       {
  130.         return (jobEventsXml == null)
  131.                 ? Collections.emptyList()
  132.                 : jobEventsXml.stream().map(jobEventXml ->  jobEventXml.toBuilder().create()).collect(toList());
  133.       }    
  134.   }