View Javadoc
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.model.impl;
27  
28  import javax.annotation.Nonnull;
29  import javax.annotation.concurrent.Immutable;
30  import java.util.Map;
31  import it.tidalwave.accounting.model.Accounting;
32  import it.tidalwave.accounting.model.Project;
33  import it.tidalwave.accounting.model.ProjectRegistry;
34  import it.tidalwave.accounting.model.spi.CustomerSpi;
35  import it.tidalwave.accounting.model.types.Address;
36  import it.tidalwave.util.As;
37  import it.tidalwave.util.Id;
38  import it.tidalwave.util.spi.FinderWithIdMapSupport;
39  import lombok.EqualsAndHashCode;
40  import lombok.Getter;
41  import lombok.Setter;
42  import lombok.ToString;
43  import lombok.experimental.Delegate;
44  import static java.util.stream.Collectors.*;
45  
46  /***************************************************************************************************************************************************************
47   *
48   * This class models a customer.
49   *
50   * @author  Fabrizio Giudici
51   *
52   **************************************************************************************************************************************************************/
53  @Immutable @Getter @EqualsAndHashCode @ToString(exclude = {"accounting", "as"}) // FIXME: remove the @Getter
54  public class InMemoryCustomer implements CustomerSpi
55    {
56      private static final long serialVersionUID = 1L;
57      
58      static class InMemoryProjectFinder extends FinderWithIdMapSupport<Project, InMemoryProject, ProjectRegistry.ProjectFinder>
59                                  implements ProjectRegistry.ProjectFinder
60        {
61          private static final long serialVersionUID = 1L;
62      
63          public InMemoryProjectFinder (@Nonnull final InMemoryProjectFinder other, @Nonnull final Object override)
64            {
65              super(other, override);  
66            }
67          
68          InMemoryProjectFinder (@Nonnull final Map<Id, InMemoryProject> projectMapById)
69            {
70              super(projectMapById);  
71            }
72        }
73      
74      @Delegate
75      private final As as = As.forObject(this);
76  
77      @Getter @Nonnull
78      private final Id id;
79  
80      @Nonnull
81      private final String name;
82  
83      @Nonnull
84      private final Address billingAddress;
85  
86      @Nonnull
87      private final String vatNumber;
88      
89      @Setter @Nonnull // FIXME: drop the setter!
90      private Accounting accounting;
91  
92      /***********************************************************************************************************************************************************
93       **********************************************************************************************************************************************************/
94      public /* FIXME protected */ InMemoryCustomer (@Nonnull final Builder builder)
95        {
96          this.id = builder.getId();
97          this.name = builder.getName();
98          this.billingAddress = builder.getBillingAddress();
99          this.vatNumber = builder.getVatNumber();
100       }
101 
102     /***********************************************************************************************************************************************************
103      * 
104      **********************************************************************************************************************************************************/
105     @Override @Nonnull
106     public ProjectRegistry.ProjectFinder findProjects()
107       {
108         return new InMemoryProjectFinder(accounting.getProjectRegistry().findProjects()
109                                                    .stream()
110                                                    .filter(project -> project.getCustomer().getId().equals(getId()))
111                                                    .collect(toMap(Project::getId, project -> (InMemoryProject)project)));
112       }
113     
114     /***********************************************************************************************************************************************************
115      * {@inheritDoc}
116      **********************************************************************************************************************************************************/
117     @Override @Nonnull
118     public Builder toBuilder()
119       {
120         return new Builder(id, name, billingAddress, vatNumber, InMemoryCustomer.Builder.Callback.DEFAULT);                
121       }
122   }