]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/opening_time/BusinessOpeningTime.java
Updated copyright year
[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.openingId 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          * When this opening time was created
61          */
62         @Basic (optional = false)
63         @Column (name = "opening_times_created", nullable = false, updatable = false)
64         @Temporal (TemporalType.TIMESTAMP)
65         private Date openingCreated;
66
67         /**
68          * Ending day of opening hours (if applyable)
69          */
70         @Basic (optional = false)
71         @Column (name = "opening_times_end_day", nullable = false)
72         @Enumerated (EnumType.STRING)
73         private DayOfTheWeek openingEndDay;
74
75         /**
76          * Ending time (hh:mm)
77          */
78         @Basic (optional = false)
79         @Column (name = "opening_times_end_time", nullable = false)
80         @Temporal (TemporalType.TIME)
81         private Date openingEndTime;
82
83         /**
84          * Id number
85          */
86         @Id
87         @Column (name = "opening_times_id", nullable = false, updatable = false)
88         @GeneratedValue (strategy = GenerationType.IDENTITY)
89         private Long openingId;
90
91         /**
92          * Starting day of opening times
93          */
94         @Basic (optional = false)
95         @Column (name = "opening_times_start_day", nullable = false)
96         @Enumerated (EnumType.STRING)
97         private DayOfTheWeek openingStartDay;
98
99         /**
100          * Starting time (hh:mm)
101          */
102         @Basic (optional = false)
103         @Column (name = "opening_times_start_time", nullable = false)
104         @Temporal (TemporalType.TIME)
105         private Date openingStartTime;
106
107         /**
108          * Default constructor
109          */
110         public BusinessOpeningTime () {
111         }
112
113         /**
114          * Constructor with all required fields except created timestamp
115          * <p>
116          * @param openingEndDay    End day
117          * @param openingEndTime   End time
118          * @param openingStartDay  Start day
119          * @param openingStartTime Start time
120          */
121         public BusinessOpeningTime (final DayOfTheWeek openingEndDay, final Date openingEndTime, final DayOfTheWeek openingStartDay, final Date openingStartTime) {
122                 // Set all fields
123                 this.openingEndDay = openingEndDay;
124                 this.openingEndTime = openingEndTime;
125                 this.openingStartDay = openingStartDay;
126                 this.openingStartTime = openingStartTime;
127         }
128
129         @Override
130         public int compareTo (final OpeningTime openingTime) {
131                 // Check parameter on null-reference and equality to this
132                 if (null == openingTime) {
133                         // Should not happen
134                         throw new NullPointerException("openingTime is null"); //NOI18N
135                 } else if (openingTime.equals(this)) {
136                         // Same object
137                         return 0;
138                 }
139
140                 // Init comparisons
141                 // @TODO Also compare start and end timestamps (java.util.Date)
142                 final int[] comparators = {
143                         // First starting day of the week
144                         this.getOpeningStartDay().compareTo(openingTime.getOpeningStartDay()),
145                         // ... and end day of the week
146                         this.getOpeningEndDay().compareTo(openingTime.getOpeningEndDay())
147                 };
148
149                 // Check all values
150                 final int comparison = Comparables.checkAll(comparators);
151
152                 // Return value
153                 return comparison;
154         }
155
156         @Override
157         public boolean equals (final Object obj) {
158                 if (this == obj) {
159                         return true;
160                 } else if (obj == null) {
161                         return false;
162                 } else if (this.getClass() != obj.getClass()) {
163                         return false;
164                 }
165
166                 final OpeningTime openingTimes = (OpeningTime) obj;
167
168                 if (!Objects.equals(this.getOpeningId(), openingTimes.getOpeningId())) {
169                         return false;
170                 } else if (this.getOpeningStartDay() != openingTimes.getOpeningStartDay()) {
171                         return false;
172                 } else if (this.getOpeningEndDay() != openingTimes.getOpeningEndDay()) {
173                         return false;
174                 } else if (!Objects.equals(this.getOpeningStartTime(), openingTimes.getOpeningStartTime())) {
175                         return false;
176                 } else if (!Objects.equals(this.getOpeningEndTime(), openingTimes.getOpeningEndTime())) {
177                         return false;
178                 }
179
180                 return true;
181         }
182
183         @Override
184         @SuppressWarnings ("ReturnOfDateField")
185         public Date getOpeningCreated () {
186                 return this.openingCreated;
187         }
188
189         @Override
190         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
191         public void setOpeningCreated (final Date openingCreated) {
192                 this.openingCreated = openingCreated;
193         }
194
195         @Override
196         public DayOfTheWeek getOpeningEndDay () {
197                 return this.openingEndDay;
198         }
199
200         @Override
201         public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
202                 this.openingEndDay = openingEndDay;
203         }
204
205         @Override
206         @SuppressWarnings ("ReturnOfDateField")
207         public Date getOpeningEndTime () {
208                 return this.openingEndTime;
209         }
210
211         @Override
212         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
213         public void setOpeningEndTime (final Date openingEndTime) {
214                 this.openingEndTime = openingEndTime;
215         }
216
217         @Override
218         public Long getOpeningId () {
219                 return this.openingId;
220         }
221
222         @Override
223         public void setOpeningId (final Long openingId) {
224                 this.openingId = openingId;
225         }
226
227         @Override
228         public DayOfTheWeek getOpeningStartDay () {
229                 return this.openingStartDay;
230         }
231
232         @Override
233         public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
234                 this.openingStartDay = openingStartDay;
235         }
236
237         @Override
238         @SuppressWarnings ("ReturnOfDateField")
239         public Date getOpeningStartTime () {
240                 return this.openingStartTime;
241         }
242
243         @Override
244         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
245         public void setOpeningStartTime (final Date openingStartTime) {
246                 this.openingStartTime = openingStartTime;
247         }
248
249         @Override
250         public int hashCode () {
251                 int hash = 7;
252
253                 hash = 97 * hash + Objects.hashCode(this.getOpeningId());
254                 hash = 97 * hash + Objects.hashCode(this.getOpeningStartDay());
255                 hash = 97 * hash + Objects.hashCode(this.getOpeningEndDay());
256                 hash = 97 * hash + Objects.hashCode(this.getOpeningStartTime());
257                 hash = 97 * hash + Objects.hashCode(this.getOpeningEndTime());
258
259                 return hash;
260         }
261
262 }