JobEventXml.java

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

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

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