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