]> git.mxchange.org Git - juser-activity-core.git/blob - src/org/mxchange/jusercore/model/user/activity/UserActivityLog.java
Continued:
[juser-activity-core.git] / src / org / mxchange / jusercore / model / user / activity / UserActivityLog.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jusercore.model.user.activity;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
41 import org.mxchange.jusercore.model.user.LoginUser;
42 import org.mxchange.jusercore.model.user.User;
43
44 /**
45  * A POJO for user activity log
46  * <p>
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Entity (name = "user_activity_log")
50 @Table (
51                 name = "user_activity_log"
52 )
53 @NamedQueries (
54                 {
55                         @NamedQuery (name = "AllUserActivityLog", query = "SELECT a FROM user_activity_log AS a ORDER BY a.activityId DESC"),
56                         @NamedQuery (name = "SearchAllUsersActivity", query = "SELECT a FROM user_activity_log AS a WHERE a.activityUser = :activityUser ORDER BY a.activityId DESC")
57                 }
58 )
59 @SuppressWarnings ("PersistenceUnitPresent")
60 public class UserActivityLog implements LogableUserActivity {
61
62         /**
63          * Serial number
64          */
65         @Transient
66         private static final long serialVersionUID = 12_945_967_867_290_601L;
67
68         /**
69          * Family name
70          */
71         @Basic (optional = false)
72         @Column (name = "activity_contact_family_name", nullable = false, updatable = false)
73         private String activityContactFamilyName;
74
75         /**
76          * First name
77          */
78         @Basic (optional = false)
79         @Column (name = "activity_contact_first_name", nullable = false, updatable = false)
80         private String activityContactFirstName;
81
82         /**
83          * Personal title instance
84          */
85         @Basic (optional = false)
86         @Column (name = "activity_contact_gender", nullable = false, updatable = false)
87         @Enumerated (EnumType.STRING)
88         private PersonalTitle activityContactPersonalTitle;
89
90         /**
91          * Primary key
92          */
93         @Id
94         @GeneratedValue (strategy = GenerationType.IDENTITY)
95         @Column (name = "activity_id", nullable = false, updatable = false)
96         private Long activityId;
97
98         /**
99          * Message
100          */
101         @Column (name = "activity_message", updatable = false)
102         @Lob
103         private String activityMessage;
104
105         /**
106          * Principal name
107          */
108         @Column (name = "activity_principal_name", updatable = false)
109         private String activityPrincipalName;
110
111         /**
112          * Timestamp
113          */
114         @Basic (optional = false)
115         @Temporal (TemporalType.TIMESTAMP)
116         @Column (name = "activity_timestamp", nullable = false, updatable = false)
117         private Date activityTimestamp;
118
119         /**
120          * Type
121          */
122         @Basic (optional = false)
123         @Column (name = "activity_type", nullable = false, updatable = false)
124         private String activityType;
125
126         /**
127          * User instance
128          */
129         @JoinColumn (name = "activity_user_id", referencedColumnName = "user_id")
130         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL)
131         private User activityUser;
132
133         /**
134          * User name
135          */
136         @Basic (optional = false)
137         @Column (name = "activity_user_name", nullable = false, updatable = false)
138         private String activityUserName;
139
140         /**
141          * Default constructor
142          */
143         public UserActivityLog () {
144         }
145
146         /**
147          * Constructor with message, type and user
148          * <p>
149          * @param activityMessage   Message
150          * @param activityType      Type
151          * @param activityUser      User instance
152          * @param activityTimestamp imestamp
153          */
154         public UserActivityLog (final String activityMessage, final String activityType, final User activityUser, final Date activityTimestamp) {
155                 // Call other constructor
156                 this(activityType, activityUser, activityTimestamp);
157
158                 // Is a message given?
159                 if (null == activityMessage) {
160                         // Throw NPE
161                         throw new NullPointerException("activityMessage is null"); //NOI18N
162                 } else if (activityMessage.isEmpty()) {
163                         // Empty string
164                         throw new IllegalArgumentException("activityMessage is empty"); //NOI18N
165                 }
166
167                 // Set message
168                 this.activityMessage = activityMessage;
169         }
170
171         /**
172          * Constructor with type, user and timestamp
173          * <p>
174          * @param activityType      Type
175          * @param activityUser      User instance
176          * @param activityTimestamp imestamp
177          */
178         public UserActivityLog (final String activityType, final User activityUser, final Date activityTimestamp) {
179                 // Make sure all is set
180                 if (null == activityType) {
181                         // Throw NPE
182                         throw new NullPointerException("activityType is null"); //NOI18N
183                 } else if (activityType.isEmpty()) {
184                         // Empty string
185                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
186                 } else if (null == activityUser) {
187                         // Throw NPE
188                         throw new NullPointerException("activityUser is null"); //NOI18N
189                 } else if (activityUser.getUserId() == null) {
190                         // Throw it again
191                         throw new NullPointerException("activityUser.userId is null"); //NOI18N
192                 } else if (activityUser.getUserId() < 1) {
193                         // Invalid id number
194                         throw new IllegalArgumentException(MessageFormat.format("activityUser.userId={0} is not valid", activityUser.getUserId())); //NOI18N
195                 } else if (null == activityUser.getUserContact()) {
196                         // Throw NPE
197                         throw new NullPointerException("activityUser.userContact is null"); //NOI18N
198                 } else if (null == activityUser.getUserContact().getContactId()) {
199                         // Throw NPE
200                         throw new NullPointerException("activityUser.userContact.contactId is null"); //NOI18N
201                 } else if (activityUser.getUserContact().getContactId() < 1) {
202                         // Invalid id number
203                         throw new IllegalArgumentException(MessageFormat.format("activityUser.userContact.contactId={0} is not valid", activityUser.getUserContact().getContactId())); //NOI18N
204                 } else if (null == activityTimestamp) {
205                         // Throw NPE again
206                         throw new NullPointerException("activityTimestamp is null"); //NOI18N
207                 }
208
209                 // Set all values
210                 this.activityType = activityType;
211                 this.activityUser = activityUser;
212                 this.activityTimestamp = activityTimestamp;
213
214                 // Set all user/contact fields
215                 this.activityUserName = activityUser.getUserName();
216                 this.activityContactPersonalTitle = activityUser.getUserContact().getContactPersonalTitle();
217                 this.activityContactFirstName = activityUser.getUserContact().getContactFirstName();
218                 this.activityContactFamilyName = activityUser.getUserContact().getContactFamilyName();
219         }
220
221         /**
222          * Constructor with message, type, user, timestamp and principal name
223          * <p>
224          * @param activityMessage   Message
225          * @param activityType      Type
226          * @param activityUser      User instance
227          * @param activityTimestamp imestamp
228          * @param principalName     Principal name
229          */
230         public UserActivityLog (final String activityMessage, final String activityType, final User activityUser, final Date activityTimestamp, final String principalName) {
231                 // Call other constructor
232                 this(activityMessage, activityType, activityUser, activityTimestamp);
233
234                 // Set principal name
235                 this.activityPrincipalName = principalName;
236         }
237
238         /**
239          * Consctructor with type, user, timestamp and principal name
240          * <p>
241          * @param activityType      Type
242          * @param activityUser      User instance
243          * @param activityTimestamp imestamp
244          * @param principalName     Principal name
245          */
246         public UserActivityLog (final String activityType, final User activityUser, final Date activityTimestamp, final String principalName) {
247                 // Call other constructor
248                 this(activityType, activityUser, activityTimestamp);
249
250                 // Set principal name
251                 this.activityPrincipalName = principalName;
252         }
253
254         @Override
255         public boolean equals (final Object object) {
256                 if (this == object) {
257                         return true;
258                 } else if (null == object) {
259                         return false;
260                 } else if (this.getClass() != object.getClass()) {
261                         return false;
262                 }
263
264                 final LogableUserActivity other = (LogableUserActivity) object;
265
266                 if (!Objects.equals(this.getActivityMessage(), other.getActivityMessage())) {
267                         return false;
268                 } else if (!Objects.equals(this.getActivityPrincipalName(), other.getActivityPrincipalName())) {
269                         return false;
270                 } else if (!Objects.equals(this.getActivityType(), other.getActivityType())) {
271                         return false;
272                 } else if (!Objects.equals(this.getActivityId(), other.getActivityId())) {
273                         return false;
274                 } else if (!Objects.equals(this.getActivityTimestamp(), other.getActivityTimestamp())) {
275                         return false;
276                 } else if (!Objects.equals(this.getActivityUser(), other.getActivityUser())) {
277                         return false;
278                 }
279
280                 return true;
281         }
282
283         @Override
284         public String getActivityContactFamilyName () {
285                 return this.activityContactFamilyName;
286         }
287
288         @Override
289         public void setActivityContactFamilyName (final String activityContactFamilyName) {
290                 this.activityContactFamilyName = activityContactFamilyName;
291         }
292
293         @Override
294         public String getActivityContactFirstName () {
295                 return this.activityContactFirstName;
296         }
297
298         @Override
299         public void setActivityContactFirstName (final String activityContactFirstName) {
300                 this.activityContactFirstName = activityContactFirstName;
301         }
302
303         @Override
304         public PersonalTitle getActivityContactPersonalTitle () {
305                 return this.activityContactPersonalTitle;
306         }
307
308         @Override
309         public void setActivityContactPersonalTitle (final PersonalTitle activityContactPersonalTitle) {
310                 this.activityContactPersonalTitle = activityContactPersonalTitle;
311         }
312
313         @Override
314         public Long getActivityId () {
315                 return this.activityId;
316         }
317
318         @Override
319         public void setActivityId (final Long activityId) {
320                 this.activityId = activityId;
321         }
322
323         @Override
324         public String getActivityMessage () {
325                 return this.activityMessage;
326         }
327
328         @Override
329         public void setActivityMessage (final String activityMessage) {
330                 this.activityMessage = activityMessage;
331         }
332
333         @Override
334         public String getActivityPrincipalName () {
335                 return this.activityPrincipalName;
336         }
337
338         @Override
339         public void setActivityPrincipalName (final String activityPrincipalName) {
340                 this.activityPrincipalName = activityPrincipalName;
341         }
342
343         @Override
344         @SuppressWarnings ("ReturnOfDateField")
345         public Date getActivityTimestamp () {
346                 return this.activityTimestamp;
347         }
348
349         @Override
350         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
351         public void setActivityTimestamp (final Date activityTimestamp) {
352                 this.activityTimestamp = activityTimestamp;
353         }
354
355         @Override
356         public String getActivityType () {
357                 return this.activityType;
358         }
359
360         @Override
361         public void setActivityType (final String activityType) {
362                 this.activityType = activityType;
363         }
364
365         @Override
366         public User getActivityUser () {
367                 return this.activityUser;
368         }
369
370         @Override
371         public void setActivityUser (final User activityUser) {
372                 this.activityUser = activityUser;
373         }
374
375         @Override
376         public String getActivityUserName () {
377                 return this.activityUserName;
378         }
379
380         @Override
381         public void setActivityUserName (final String activityUserName) {
382                 this.activityUserName = activityUserName;
383         }
384
385         @Override
386         public int hashCode () {
387                 int hash = 7;
388
389                 hash = 83 * hash + Objects.hashCode(this.getActivityId());
390                 hash = 83 * hash + Objects.hashCode(this.getActivityMessage());
391                 hash = 83 * hash + Objects.hashCode(this.getActivityPrincipalName());
392                 hash = 83 * hash + Objects.hashCode(this.getActivityTimestamp());
393                 hash = 83 * hash + Objects.hashCode(this.getActivityType());
394                 hash = 83 * hash + Objects.hashCode(this.getActivityUser());
395
396                 return hash;
397         }
398
399 }