2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.jjobs.model.user.skills;
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.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32 import javax.persistence.Temporal;
33 import javax.persistence.TemporalType;
34 import org.mxchange.jcoreutils.comparable.ComparableUtils;
35 import org.mxchange.jcoreutils.number.SafeNumberUtils;
36 import org.mxchange.jjobs.model.skill.JobSkill;
37 import org.mxchange.jjobs.model.skill.Skillable;
38 import org.mxchange.jjobs.model.utils.SkillUtils;
39 import org.mxchange.jusercore.model.user.LoginUser;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.utils.UserUtils;
44 * A POJO entity for user skills
46 * @author Roland Häder<roland@mxchange.org>
48 @Entity (name = "user_skills")
52 @SuppressWarnings ("PersistenceUnitPresent")
53 public class UserSkill implements SkillableUser {
58 private static final long serialVersionUID = 12_837_674_186_902L;
61 * Link to skills entity
63 @JoinColumn (name = "user_skill_skill_id", referencedColumnName = "skill_id", nullable = false, updatable = false)
64 @OneToOne (cascade = CascadeType.REFRESH, optional = false, targetEntity = JobSkill.class)
65 private Skillable jobSkill;
70 @JoinColumn (name = "user_skill_user_id", referencedColumnName = "user_id", nullable = false, updatable = false)
71 @OneToOne (cascade = CascadeType.REFRESH, optional = false, targetEntity = LoginUser.class)
72 private User skillUser;
75 * When this entry has been created
77 @Basic (optional = false)
78 @Column (name = "user_skill_entry_created", nullable = false, updatable = false)
79 @Temporal (TemporalType.TIMESTAMP)
80 private Date userSkillEntryCreated;
83 * When this entry has been updated
85 @Column (name = "user_skill_entry_updated", insertable = false)
86 @Temporal (TemporalType.TIMESTAMP)
87 private Date userSkillEntryUpdated;
90 * Id number (primary key)
93 @GeneratedValue (strategy = GenerationType.IDENTITY)
94 @Column (name = "user_skill_id", nullable = false, updatable = false)
95 private Long userSkillId;
98 * Default constructor, required for the EJB
100 public UserSkill () {
105 * Constructor with all required entity properties
107 * @param jobSkill An instance of a Skillable class
108 * @param skillUser An instance of a User class
110 public UserSkill (final Skillable jobSkill, final User skillUser) {
111 // Invoke default constructor
114 // Validate parameter
115 if (null == jobSkill) {
117 throw new NullPointerException("jobSkill is null"); //NOI18N
118 } else if (jobSkill.getSkillId() == null) {
120 throw new NullPointerException("jobSill.skillId is null"); //NOI18N
121 } else if (jobSkill.getSkillId() < 1) {
123 throw new IllegalArgumentException(MessageFormat.format("jobSill.skillId={0} is invalid", jobSkill.getSkillId())); //NOI18N
124 } else if (null == skillUser) {
126 throw new NullPointerException("skillUser is null"); //NOI18N
127 } else if (skillUser.getUserId() == null) {
129 throw new NullPointerException("skillUser.userId is null"); //NOI18N
130 } else if (skillUser.getUserId() < 1) {
132 throw new NullPointerException(MessageFormat.format("skillUser.userId={0} is invalid", skillUser.getUserId())); //NOI18N
136 this.jobSkill = jobSkill;
137 this.skillUser = skillUser;
141 public int compareTo (final SkillableUser user) {
142 // Checkparameter and return 0 if equal
145 throw new NullPointerException("Parameter 'user' is null"); //NOI18N
146 } else if (Objects.equals(this, user)) {
152 final int comparitors[] = {
153 // First compare user
154 UserUtils.compare(this.getSkillUser(), user.getSkillUser()),
156 SkillUtils.compare(this.getJobSkill(), user.getJobSkill()),
158 SafeNumberUtils.compare(this.getUserSkillId(), user.getUserSkillId())
162 final int comparison = ComparableUtils.checkAll(comparitors);
169 public boolean equals (final Object object) {
170 if (this == object) {
172 } else if (object == null) {
174 } else if (getClass() != object.getClass()) {
178 final SkillableUser skillableUser = (SkillableUser) object;
180 if (!Objects.equals(this.getJobSkill(), skillableUser.getJobSkill())) {
182 } else if (!Objects.equals(this.getSkillUser(), skillableUser.getSkillUser())) {
184 } else if (!Objects.equals(this.getUserSkillId(), skillableUser.getUserSkillId())) {
192 public Skillable getJobSkill () {
193 return this.jobSkill;
197 public void setJobSkill (final Skillable jobSkill) {
198 this.jobSkill = jobSkill;
202 public User getSkillUser () {
203 return this.skillUser;
207 public void setSkillUser (final User skillUser) {
208 this.skillUser = skillUser;
212 @SuppressWarnings ("ReturnOfDateField")
213 public Date getUserSkillEntryCreated () {
214 return this.userSkillEntryCreated;
218 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
219 public void setUserSkillEntryCreated (final Date userSkillEntryCreated) {
220 this.userSkillEntryCreated = userSkillEntryCreated;
224 @SuppressWarnings ("ReturnOfDateField")
225 public Date getUserSkillEntryUpdated () {
226 return this.userSkillEntryUpdated;
230 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
231 public void setUserSkillEntryUpdated (final Date userSkillEntryUpdated) {
232 this.userSkillEntryUpdated = userSkillEntryUpdated;
236 public Long getUserSkillId () {
237 return this.userSkillId;
241 public void setUserSkillId (final Long userSkillId) {
242 this.userSkillId = userSkillId;
246 public int hashCode () {
249 hash = 17 * hash + Objects.hashCode(this.getJobSkill());
250 hash = 17 * hash + Objects.hashCode(this.getSkillUser());
251 hash = 17 * hash + Objects.hashCode(this.getUserSkillId());