AccountingXml.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 java.util.List;
  30. import javax.xml.bind.annotation.XmlAccessorOrder;
  31. import javax.xml.bind.annotation.XmlAccessorType;
  32. import javax.xml.bind.annotation.XmlElement;
  33. import javax.xml.bind.annotation.XmlElementWrapper;
  34. import javax.xml.bind.annotation.XmlRootElement;
  35. import it.tidalwave.accounting.model.Accounting;
  36. import lombok.NoArgsConstructor;
  37. import static java.util.Comparator.*;
  38. import static java.util.stream.Collectors.toList;
  39. import static javax.xml.bind.annotation.XmlAccessOrder.ALPHABETICAL;
  40. import static javax.xml.bind.annotation.XmlAccessType.FIELD;

  41. /***********************************************************************************************************************
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **********************************************************************************************************************/
  46. //@Mutable
  47. @NoArgsConstructor
  48. @XmlRootElement(name = "accounting") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
  49. public class AccountingXml
  50.   {
  51.     @XmlElementWrapper(name = "customers")
  52.     @XmlElement(name = "customer")
  53.     private List<CustomerXml> customersXml;
  54.    
  55.     @XmlElementWrapper(name = "projects")
  56.     @XmlElement(name = "project")
  57.     private List<ProjectXml> projectsXml;
  58.    
  59.     @XmlElementWrapper(name = "invoices")
  60.     @XmlElement(name = "invoice")
  61.     private List<InvoiceXml> invoicesxml;
  62.    
  63.     public AccountingXml (@Nonnull final Accounting accounting)
  64.       {
  65.         customersXml = accounting.getCustomerRegistry().findCustomers().stream().map(CustomerXml::new)
  66.                 .sorted(comparing(CustomerXml::getId)).collect(toList());
  67.         projectsXml = accounting.getProjectRegistry().findProjects().stream().map(ProjectXml::new)
  68.                 .sorted(comparing(ProjectXml::getId)).collect(toList());
  69.         invoicesxml = accounting.getInvoiceRegistry().findInvoices().stream().map(InvoiceXml::new)
  70.                 .sorted(comparing(InvoiceXml::getId)).collect(toList());
  71.       }

  72.     public void fill (@Nonnull final Accounting accounting)
  73.       {
  74.         final var customerRegistry = accounting.getCustomerRegistry();
  75.         final var projectRegistry = accounting.getProjectRegistry();
  76.         final var invoiceRegistry = accounting.getInvoiceRegistry();
  77.         customersXml.forEach(customer -> customerRegistry.addCustomer().with(customer.toBuilder()).create());
  78.         projectsXml.forEach(project -> projectRegistry.addProject().with(project.toBuilder(accounting)).create());
  79.         invoicesxml.forEach(invoice -> invoiceRegistry.addInvoice().with(invoice.toBuilder(accounting)).create());
  80.       }
  81.   }