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

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

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