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