2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user.activity;
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;
45 * A POJO for user acitivity log
47 * @author Roland Haeder<roland@mxchange.org>
49 @Entity (name = "user_activity_log")
51 name = "user_activity_log"
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")
60 @SuppressWarnings ("PersistenceUnitPresent")
61 public class UserActivityLog implements LogableUserActivity {
67 private static final long serialVersionUID = 12_945_967_867_290_601L;
72 @Basic (optional = false)
73 @Column (name = "activity_contact_family_name", nullable = false, updatable = false)
74 private String activityContactFamilyName;
79 @Basic (optional = false)
80 @Column (name = "activity_contact_first_name", nullable = false, updatable = false)
81 private String activityContactFirstName;
86 @Basic (optional = false)
87 @Column (name = "activity_contact_gender", nullable = false, updatable = false)
88 @Enumerated (EnumType.STRING)
89 private Gender activityContactGender;
95 @GeneratedValue (strategy = GenerationType.IDENTITY)
96 @Column (name = "activity_id", nullable = false, updatable = false)
97 private Long activityId;
102 @Column (name = "activity_message", updatable = false)
104 private String activityMessage;
109 @Column (name = "activity_principal_name", updatable = false)
110 private String activityPrincipalName;
115 @Basic (optional = false)
116 @Temporal (TemporalType.TIMESTAMP)
117 @Column (name = "activity_timestamp", nullable = false, updatable = false)
118 private Calendar activityTimestamp;
123 @Basic (optional = false)
124 @Column (name = "activity_type", nullable = false, updatable = false)
125 private String activityType;
130 @JoinColumn (name = "activity_user_id", referencedColumnName = "user_id")
131 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL)
132 private User activityUser;
137 @Basic (optional = false)
138 @Column (name = "activity_user_name", nullable = false, updatable = false)
139 private String activityUserName;
142 * Default constructor
144 public UserActivityLog () {
148 * Constructor with message, type and user
150 * @param activityMessage Message
151 * @param activityType Type
152 * @param activityUser User instance
153 * @param activityTimestamp imestamp
155 public UserActivityLog (final String activityMessage, final String activityType, final User activityUser, final Calendar activityTimestamp) {
156 // Call other constructor
157 this(activityType, activityUser, activityTimestamp);
159 // Is a message given?
160 if (null == activityMessage) {
162 throw new NullPointerException("activityMessage is null"); //NOI18N
163 } else if (activityMessage.isEmpty()) {
165 throw new IllegalArgumentException("activityMessage is empty"); //NOI18N
169 this.activityMessage = activityMessage;
173 * Constructor with type, user and timestamp
175 * @param activityType Type
176 * @param activityUser User instance
177 * @param activityTimestamp imestamp
179 public UserActivityLog (final String activityType, final User activityUser, final Calendar activityTimestamp) {
180 // Make sure all is set
181 if (null == activityType) {
183 throw new NullPointerException("activityType is null"); //NOI18N
184 } else if (activityType.isEmpty()) {
186 throw new IllegalArgumentException("activityType is empty"); //NOI18N
187 } else if (null == activityUser) {
189 throw new NullPointerException("activityUser is null"); //NOI18N
190 } else if (activityUser.getUserId() == null) {
192 throw new NullPointerException("activityUser.userId is null"); //NOI18N
193 } else if (activityUser.getUserId() < 1) {
195 throw new IllegalArgumentException(MessageFormat.format("activityUser.userId={0} is not valid", activityUser.getUserId())); //NOI18N
196 } else if (null == activityUser.getUserContact()) {
198 throw new NullPointerException("activityUser.userContact is null"); //NOI18N
199 } else if (null == activityUser.getUserContact().getContactId()) {
201 throw new NullPointerException("activityUser.userContact.contactId is null"); //NOI18N
202 } else if (activityUser.getUserContact().getContactId() < 1) {
204 throw new IllegalArgumentException(MessageFormat.format("activityUser.userContact.contactId={0} is not valid", activityUser.getUserContact().getContactId())); //NOI18N
205 } else if (null == activityTimestamp) {
207 throw new NullPointerException("activityTimestamp is null"); //NOI18N
211 this.activityType = activityType;
212 this.activityUser = activityUser;
213 this.activityTimestamp = activityTimestamp;
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();
223 * Constructor with message, type, user, timestamp and principal name
225 * @param activityMessage Message
226 * @param activityType Type
227 * @param activityUser User instance
228 * @param activityTimestamp imestamp
229 * @param principalName Principal name
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);
235 // Set principal name
236 this.activityPrincipalName = principalName;
240 * Consctructor with type, user, timestamp and principal name
242 * @param activityType Type
243 * @param activityUser User instance
244 * @param activityTimestamp imestamp
245 * @param principalName Principal name
247 public UserActivityLog (final String activityType, final User activityUser, final Calendar activityTimestamp, final String principalName) {
248 // Call other constructor
249 this(activityType, activityUser, activityTimestamp);
251 // Set principal name
252 this.activityPrincipalName = principalName;
256 public boolean equals (final Object object) {
257 if (this == object) {
259 } else if (null == object) {
261 } else if (this.getClass() != object.getClass()) {
265 final LogableUserActivity other = (LogableUserActivity) object;
267 if (!Objects.equals(this.getActivityMessage(), other.getActivityMessage())) {
269 } else if (!Objects.equals(this.getActivityPrincipalName(), other.getActivityPrincipalName())) {
271 } else if (!Objects.equals(this.getActivityType(), other.getActivityType())) {
273 } else if (!Objects.equals(this.getActivityId(), other.getActivityId())) {
275 } else if (!Objects.equals(this.getActivityTimestamp(), other.getActivityTimestamp())) {
277 } else if (!Objects.equals(this.getActivityUser(), other.getActivityUser())) {
285 public String getActivityContactFamilyName () {
286 return this.activityContactFamilyName;
290 public void setActivityContactFamilyName (final String activityContactFamilyName) {
291 this.activityContactFamilyName = activityContactFamilyName;
295 public String getActivityContactFirstName () {
296 return this.activityContactFirstName;
300 public void setActivityContactFirstName (final String activityContactFirstName) {
301 this.activityContactFirstName = activityContactFirstName;
305 public Gender getActivityContactGender () {
306 return this.activityContactGender;
310 public void setActivityContactGender (final Gender activityContactGender) {
311 this.activityContactGender = activityContactGender;
315 public Long getActivityId () {
316 return this.activityId;
320 public void setActivityId (final Long activityId) {
321 this.activityId = activityId;
325 public String getActivityMessage () {
326 return this.activityMessage;
330 public void setActivityMessage (final String activityMessage) {
331 this.activityMessage = activityMessage;
335 public String getActivityPrincipalName () {
336 return this.activityPrincipalName;
340 public void setActivityPrincipalName (final String activityPrincipalName) {
341 this.activityPrincipalName = activityPrincipalName;
345 @SuppressWarnings ("ReturnOfDateField")
346 public Calendar getActivityTimestamp () {
347 return this.activityTimestamp;
351 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
352 public void setActivityTimestamp (final Calendar activityTimestamp) {
353 this.activityTimestamp = activityTimestamp;
357 public String getActivityType () {
358 return this.activityType;
362 public void setActivityType (final String activityType) {
363 this.activityType = activityType;
367 public User getActivityUser () {
368 return this.activityUser;
372 public void setActivityUser (final User activityUser) {
373 this.activityUser = activityUser;
377 public String getActivityUserName () {
378 return this.activityUserName;
382 public void setActivityUserName (final String activityUserName) {
383 this.activityUserName = activityUserName;
387 public int hashCode () {
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());