1   /*
2    * Copyright 2006-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.test.domain;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
26  import static org.apache.commons.lang.math.RandomUtils.nextLong;
27  
28  import net.sourceforge.domian.entity.Entity;
29  
30  
31  public class Order implements Entity {
32  
33      protected String entityId = String.valueOf(nextLong()); // Not particularly safe...
34  
35      /** For possible use of RDBMS optimistic locking. */
36      protected Integer version;
37      
38      protected Long orderId;
39      protected Date orderDate; // This is just the date, without any clock time set
40      protected Customer customer;
41      protected Address shippingAddress;
42      protected List<OrderLine> orderLines = new ArrayList<OrderLine>();
43      protected Map<Long, OrderLine> selectedOrderLines = new HashMap<Long, OrderLine>(); // orderLineId -> OrderLine
44  
45      /* Test class convenient constructor... and maybe necessary for other stuff as well... right? */
46      /* Yes, needed by misc. reflection-based stuff... ugh. */
47      public Order() {}
48  
49      public Order(final Long orderId) {
50          this();
51          this.orderId = orderId;
52      }
53  
54      public Order(final Long orderId, final Date orderDate, final Customer customer, final List<OrderLine> orderLines) {
55          this(orderId);
56          this.orderDate = orderDate;
57          this.customer = customer;
58          this.orderLines = orderLines;
59      }
60  
61      public String getEntityId() {
62          return this.entityId;
63      }
64  
65      /* For testing purposes, this a rather silly method to put in... */
66      public void setEntityId(final String entityId) {
67          this.entityId = entityId;
68      }
69  
70      public Integer getVersion() {
71          return version;
72      }
73  
74      public void setVersion(Integer version) {
75          this.version = version;
76      }
77  
78      public Long getOrderId() {
79          return orderId;
80      }
81  
82      public void setOrderId(final Long orderId) {
83          this.orderId = orderId;
84      }
85  
86      public Date getOrderDate() {
87          return orderDate;
88      }
89  
90      public void setOrderDate(final Date orderDate) {
91          this.orderDate = orderDate;
92      }
93  
94      /** "Builder setter" to support fluent one-liner constructor */
95      @SuppressWarnings("unchecked")
96      public <T extends Order> T orderDate(final Date orderDate) {
97          this.orderDate = orderDate;
98          return (T) this;
99      }
100 
101     public Customer getCustomer() {
102         return customer;
103     }
104 
105     public void setCustomer(final Customer customer) {
106         this.customer = customer;
107     }
108 
109     public Address getShippingAddress() {
110         return shippingAddress;
111     }
112 
113     public void setShippingAddress(final Address shippingAddress) {
114         this.shippingAddress = shippingAddress;
115     }
116 
117     /** "Builder setter" to support fluent one-liner constructor */
118     @SuppressWarnings("unchecked")
119     public <T extends Order> T shippingAddress(final String shippingAddressline1) {
120         this.shippingAddress = new Address(shippingAddressline1, null, null, null, null, null);
121         return (T) this;
122     }
123 
124     /** "Builder setter" to support fluent one-liner constructor */
125     @SuppressWarnings("unchecked")
126     public <T extends Order> T shippingAddress(final Address shippingAddress) {
127         this.shippingAddress = shippingAddress;
128         return (T) this;
129     }
130 
131     public List<OrderLine> getOrderLines() {
132         return orderLines;
133     }
134 
135     public void setOrderLines(final List<OrderLine> orderLines) {
136         this.orderLines = orderLines;
137     }
138 
139     public void addOrderLine(final OrderLine orderLine) {
140         this.orderLines.add(orderLine);
141     }
142 
143     public Map<Long, OrderLine> getSelectedOrderLines() {
144         return selectedOrderLines;
145     }
146 
147     public void addSelectedOrderLine(final OrderLine orderLine) {
148         if (!orderLines.contains(orderLine)) {
149             addOrderLine(orderLine);
150         }
151         this.selectedOrderLines.put(orderLine.getOrderLineId(), orderLine);
152     }
153 
154     public Boolean isProcessed() {
155         if (orderLines != null && !orderLines.isEmpty()) {
156             for (OrderLine orderLine : orderLines) {
157                 if (!orderLine.isProcessed()) {
158                     return false;
159                 }
160             }
161         }
162         return true;
163     }
164 
165     @Override
166     public boolean equals(Object otherObject) {
167         if (this == otherObject) { return true;}
168         if (otherObject == null) { return false; }
169         if (!(otherObject instanceof Order)) { return false; }
170         return this.entityId.equals(((Order) otherObject).entityId);
171     }
172 
173     @Override
174     public int hashCode() {
175         return this.entityId.hashCode();
176     }
177 
178     @Override
179     public String toString() {
180         return new ReflectionToStringBuilder(this).toString();
181     }
182 }