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