]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/opening_time/BusinessOpeningTime.java
Continued:
[jcontacts-business-core.git] / src / org / mxchange / jcontactsbusiness / model / opening_time / BusinessOpeningTime.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcontactsbusiness.model.opening_time;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.EnumType;
25 import javax.persistence.Enumerated;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.NamedQueries;
30 import javax.persistence.NamedQuery;
31 import javax.persistence.Table;
32 import javax.persistence.Temporal;
33 import javax.persistence.TemporalType;
34 import javax.persistence.Transient;
35 import org.mxchange.jcontactsbusiness.model.opening_time.dayofweek.DayOfTheWeek;
36 import org.mxchange.jcoreutils.Comparables;
37
38 /**
39  * A POJO for business opening hours
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Entity (name = "company_opening_times")
44 @Table (name = "company_opening_times")
45 @NamedQueries (
46                 {
47                         @NamedQuery (name = "AllOpeningTimes", query = "SELECT ot FROM company_opening_times AS ot ORDER BY ot.openingTimeId ASC")
48                 }
49 )
50 @SuppressWarnings ("PersistenceUnitPresent")
51 public class BusinessOpeningTime implements OpeningTime {
52
53         /**
54          * Serial number
55          */
56         @Transient
57         private static final long serialVersionUID = 19_578_871_756_871L;
58
59         /**
60          * Ending day of opening hours (if applyable)
61          */
62         @Basic (optional = false)
63         @Column (name = "opening_times_end_day", nullable = false)
64         @Enumerated (EnumType.STRING)
65         private DayOfTheWeek openingEndDay;
66
67         /**
68          * Ending time (hh:mm)
69          */
70         @Basic (optional = false)
71         @Column (name = "opening_times_end_time", nullable = false)
72         @Temporal (TemporalType.TIME)
73         private Date openingEndTime;
74
75         /**
76          * Starting day of opening times
77          */
78         @Basic (optional = false)
79         @Column (name = "opening_times_start_day", nullable = false)
80         @Enumerated (EnumType.STRING)
81         private DayOfTheWeek openingStartDay;
82
83         /**
84          * Starting time (hh:mm)
85          */
86         @Basic (optional = false)
87         @Column (name = "opening_times_start_time", nullable = false)
88         @Temporal (TemporalType.TIME)
89         private Date openingStartTime;
90
91         /**
92          * When this opening time was created
93          */
94         @Basic (optional = false)
95         @Column (name = "opening_times_entry_created", nullable = false, updatable = false)
96         @Temporal (TemporalType.TIMESTAMP)
97         private Date openingTimeEntryCreated;
98
99         /**
100          * When this opening time was created
101          */
102         @Column (name = "opening_times_entry_updated", insertable = false)
103         @Temporal (TemporalType.TIMESTAMP)
104         private Date openingTimeEntryUpdated;
105
106         /**
107          * Id number
108          */
109         @Id
110         @Column (name = "opening_times_id", nullable = false, updatable = false)
111         @GeneratedValue (strategy = GenerationType.IDENTITY)
112         private Long openingTimeId;
113
114         /**
115          * Default constructor
116          */
117         public BusinessOpeningTime () {
118         }
119
120         /**
121          * Constructor with all required fields except created timestamp
122          * <p>
123          * @param openingEndDay    End day
124          * @param openingEndTime   End time
125          * @param openingStartDay  Start day
126          * @param openingStartTime Start time
127          */
128         public BusinessOpeningTime (final DayOfTheWeek openingEndDay, final Date openingEndTime, final DayOfTheWeek openingStartDay, final Date openingStartTime) {
129                 // Invoke default constructor
130                 this();
131
132                 // Validate parameter
133                 if (null == openingEndDay) {
134                         // Throw NPE
135                         throw new NullPointerException("openingEndDay is null"); //NOI18N
136                 } else if (null == openingEndTime) {
137                         // Throw NPE
138                         throw new NullPointerException("openingEndTime is null"); //NOI18N
139                 } else if (null == openingStartDay) {
140                         // Throw NPE
141                         throw new NullPointerException("openingStartDay is null"); //NOI18N
142                 } else if (null == openingStartTime) {
143                         // Throw NPE
144                         throw new NullPointerException("openingStartTime is null"); //NOI18N
145                 }
146
147                 // Set all fields
148                 this.openingEndDay = openingEndDay;
149                 this.openingEndTime = openingEndTime;
150                 this.openingStartDay = openingStartDay;
151                 this.openingStartTime = openingStartTime;
152         }
153
154         @Override
155         public int compareTo (final OpeningTime openingTime) {
156                 // Check parameter on null-reference and equality to this
157                 if (null == openingTime) {
158                         // Should not happen
159                         throw new NullPointerException("openingTime is null"); //NOI18N
160                 } else if (openingTime.equals(this)) {
161                         // Same object
162                         return 0;
163                 }
164
165                 // Init comparisons
166                 // @TODO Also compare start and end timestamps (java.util.Date)
167                 final int[] comparators = {
168                         // First starting day of the week
169                         this.getOpeningStartDay().compareTo(openingTime.getOpeningStartDay()),
170                         // ... and end day of the week
171                         this.getOpeningEndDay().compareTo(openingTime.getOpeningEndDay())
172                 };
173
174                 // Check all values
175                 final int comparison = Comparables.checkAll(comparators);
176
177                 // Return value
178                 return comparison;
179         }
180
181         @Override
182         public boolean equals (final Object obj) {
183                 if (this == obj) {
184                         return true;
185                 } else if (obj == null) {
186                         return false;
187                 } else if (this.getClass() != obj.getClass()) {
188                         return false;
189                 }
190
191                 final OpeningTime openingTimes = (OpeningTime) obj;
192
193                 if (!Objects.equals(this.getOpeningTimeId(), openingTimes.getOpeningTimeId())) {
194                         return false;
195                 } else if (this.getOpeningStartDay() != openingTimes.getOpeningStartDay()) {
196                         return false;
197                 } else if (this.getOpeningEndDay() != openingTimes.getOpeningEndDay()) {
198                         return false;
199                 } else if (!Objects.equals(this.getOpeningStartTime(), openingTimes.getOpeningStartTime())) {
200                         return false;
201                 } else if (!Objects.equals(this.getOpeningEndTime(), openingTimes.getOpeningEndTime())) {
202                         return false;
203                 }
204
205                 return true;
206         }
207
208         @Override
209         public DayOfTheWeek getOpeningEndDay () {
210                 return this.openingEndDay;
211         }
212
213         @Override
214         public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
215                 this.openingEndDay = openingEndDay;
216         }
217
218         @Override
219         @SuppressWarnings ("ReturnOfDateField")
220         public Date getOpeningEndTime () {
221                 return this.openingEndTime;
222         }
223
224         @Override
225         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
226         public void setOpeningEndTime (final Date openingEndTime) {
227                 this.openingEndTime = openingEndTime;
228         }
229
230         @Override
231         public DayOfTheWeek getOpeningStartDay () {
232                 return this.openingStartDay;
233         }
234
235         @Override
236         public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
237                 this.openingStartDay = openingStartDay;
238         }
239
240         @Override
241         @SuppressWarnings ("ReturnOfDateField")
242         public Date getOpeningStartTime () {
243                 return this.openingStartTime;
244         }
245
246         @Override
247         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
248         public void setOpeningStartTime (final Date openingStartTime) {
249                 this.openingStartTime = openingStartTime;
250         }
251
252         @Override
253         @SuppressWarnings ("ReturnOfDateField")
254         public Date getOpeningTimeEntryCreated () {
255                 return this.openingTimeEntryCreated;
256         }
257
258         @Override
259         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
260         public void setOpeningTimeEntryCreated (final Date openingTimeEntryCreated) {
261                 this.openingTimeEntryCreated = openingTimeEntryCreated;
262         }
263
264         @Override
265         @SuppressWarnings ("ReturnOfDateField")
266         public Date getOpeningTimeEntryUpdated () {
267                 return this.openingTimeEntryUpdated;
268         }
269
270         @Override
271         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
272         public void setOpeningTimeEntryUpdated (final Date openingTimeEntryUpdated) {
273                 this.openingTimeEntryUpdated = openingTimeEntryUpdated;
274         }
275
276         @Override
277         public Long getOpeningTimeId () {
278                 return this.openingTimeId;
279         }
280
281         @Override
282         public void setOpeningTimeId (final Long openingTimeId) {
283                 this.openingTimeId = openingTimeId;
284         }
285
286         @Override
287         public int hashCode () {
288                 int hash = 7;
289
290                 hash = 97 * hash + Objects.hashCode(this.getOpeningTimeId());
291                 hash = 97 * hash + Objects.hashCode(this.getOpeningStartDay());
292                 hash = 97 * hash + Objects.hashCode(this.getOpeningEndDay());
293                 hash = 97 * hash + Objects.hashCode(this.getOpeningStartTime());
294                 hash = 97 * hash + Objects.hashCode(this.getOpeningEndTime());
295
296                 return hash;
297         }
298
299 }