ProjectXml.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 java.time.LocalDate;
  29. import java.util.List;
  30. import javax.xml.bind.annotation.XmlAccessorOrder;
  31. import javax.xml.bind.annotation.XmlAccessorType;
  32. import javax.xml.bind.annotation.XmlAttribute;
  33. import javax.xml.bind.annotation.XmlElement;
  34. import javax.xml.bind.annotation.XmlElementWrapper;
  35. import javax.xml.bind.annotation.XmlID;
  36. import javax.xml.bind.annotation.XmlIDREF;
  37. import javax.xml.bind.annotation.XmlRootElement;
  38. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  39. import it.tidalwave.accounting.exporter.xml.impl.adapters.IdAdapter;
  40. import it.tidalwave.accounting.exporter.xml.impl.adapters.LocalDateAdapter;
  41. import it.tidalwave.accounting.exporter.xml.impl.adapters.MoneyAdapter;
  42. import it.tidalwave.accounting.exporter.xml.impl.adapters.ProjectStatusAdapter;
  43. import it.tidalwave.accounting.model.Accounting;
  44. import it.tidalwave.accounting.model.Project;
  45. import it.tidalwave.accounting.model.types.Money;
  46. import it.tidalwave.util.Id;
  47. import lombok.Getter;
  48. import lombok.NoArgsConstructor;
  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. //@Mutable
  58. @NoArgsConstructor
  59. @XmlRootElement(name = "project") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
  60. public class ProjectXml
  61.   {
  62.     @Getter // FIXME  
  63.     @XmlAttribute(name = "id")
  64.     @XmlID
  65.     @XmlJavaTypeAdapter(IdAdapter.class)
  66.     private Id id;
  67.    
  68.     @XmlElement(name = "customer")
  69.     @XmlIDREF
  70.     private CustomerXml customerXml;
  71.    
  72.     @XmlElement(name = "name")
  73.     private String name;
  74.    
  75.     @XmlElement(name = "number")
  76.     private String number;
  77.    
  78.     @XmlElement(name = "description")
  79.     private String description;
  80.    
  81.     @XmlElement(name = "notes")
  82.     private String notes;
  83.    
  84.     @XmlElement(name = "status")
  85.     @XmlJavaTypeAdapter(ProjectStatusAdapter.class)
  86.     private Project.Status status;
  87.    
  88.     @XmlElement(name = "hourlyRate")
  89.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  90.     private Money hourlyRate;
  91.    
  92.     @XmlElement(name = "amount")
  93.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  94.     private Money budget;
  95.    
  96.     @XmlElement(name = "startDate")
  97.     @XmlJavaTypeAdapter(LocalDateAdapter.class)
  98.     private LocalDate startDate;
  99.    
  100.     @XmlElement(name = "endDate")
  101.     @XmlJavaTypeAdapter(LocalDateAdapter.class)
  102.     private LocalDate endDate;
  103.    
  104.     @XmlElementWrapper(name = "events")
  105.     @XmlElement(name = "event")
  106.     private List<JobEventXml> jobEventsXml;
  107.    
  108.     public ProjectXml (@Nonnull final Project project)
  109.       {
  110.         final var builder = project.toBuilder();
  111.         this.id = builder.getId();
  112.         this.customerXml = new CustomerXml(builder.getCustomer());
  113.         this.name = builder.getName();
  114.         this.number = builder.getNumber();
  115.         this.description = builder.getDescription();
  116.         this.notes = builder.getNotes();
  117.         this.status = builder.getStatus();
  118.         this.hourlyRate = builder.getHourlyRate();
  119.         this.budget = builder.getBudget();
  120.         this.startDate = builder.getStartDate();
  121.         this.endDate = builder.getEndDate();
  122.         this.jobEventsXml = project.findChildren().stream().map(JobEventXml::new).collect(toList());
  123.       }
  124.    
  125.     @Nonnull
  126.     public Project.Builder toBuilder (@Nonnull final Accounting accounting)
  127.       {
  128.         final var customer = accounting.getCustomerRegistry()
  129.                                        .findCustomers()
  130.                                        .withId(customerXml.getId())
  131.                                        .optionalResult()
  132.                 .orElseThrow(RuntimeException::new);
  133.         return new Project.Builder().withId(id)
  134.                                     .withCustomer(customer)
  135.                                     .withName(name)
  136.                                     .withNumber(number)
  137.                                     .withDescription(description)
  138.                                     .withNotes(notes)
  139.                                     .withStatus(status)
  140.                                     .withHourlyRate(hourlyRate)
  141.                                     .withBudget(budget)
  142.                                     .withStartDate(startDate)
  143.                                     .withEndDate(endDate)
  144.                                     .withEvents(JobEventXml.toJobEvents(jobEventsXml));
  145.       }
  146.   }