]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jjobs/model/skill/JobsAdminSkillSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jjobs / model / skill / JobsAdminSkillSessionBean.java
1 /*
2  * Copyright (C) 2017 - 2024 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.model.skill;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
25 import org.mxchange.jjobs.exceptions.SkillAlreadyAddedException;
26 import org.mxchange.jjobs.model.exceptions.SkillNotFoundException;
27 import org.mxchange.jjobs.model.utils.SkillUtils;
28
29 /**
30  * A stateless session bean for administrative skillpurposes
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 @Stateless (name = "adminSkill", description = "An administrative statless bean for handling department data (all)")
35 public class JobsAdminSkillSessionBean extends BaseJobsEnterpriseBean implements AdminSkillSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 58_467_386_571_705L;
41
42         /**
43          * General department bean
44          */
45         @EJB (lookup = "java:global/jjobs-ejb/skill!org.mxchange.jjobs.model.skill.SkillSessionBeanRemote")
46         private SkillSessionBeanRemote skillBean;
47
48         /**
49          * Default constructor
50          */
51         public JobsAdminSkillSessionBean () {
52                 // Call super constructor
53                 super();
54         }
55
56         @Override
57         public Skillable addSkill (final Skillable skill) throws SkillAlreadyAddedException {
58                 // Trace message
59                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addSkill(): skill={1} - CALLED!", this.getClass().getSimpleName(), skill)); //NOI18N
60
61                 // Validate parameter
62                 if (null == skill) {
63                         // Throw NPE
64                         throw new NullPointerException("department is null"); //NOI18N
65                 } else if (skill.getSkillId() instanceof Long) {
66                         // Should not happen
67                         throw new IllegalArgumentException("skill.skillId should not be set."); //NOI18N
68                 } else if (skill.getSkillName() == null) {
69                         // Throw NPE
70                         throw new NullPointerException("skill.skillName is empty"); //NOI18N
71                 } else if (skill.getSkillName().isEmpty()) {
72                         // Throw NPE
73                         throw new NullPointerException("skill.skillName is null"); //NOI18N
74                 } else if (skill.getSkillStatus() == null) {
75                         // Throw NPE
76                         throw new NullPointerException("skill.skillStatus null"); //NOI18N
77                 } else if (this.isSkillFound(skill)) {
78                         // Already added, abort here
79                         throw new SkillAlreadyAddedException(skill);
80                 }
81
82                 // Set created timestamp
83                 skill.setSkillEntryCreated(new Date());
84
85                 // Persist it
86                 this.getEntityManager().persist(skill);
87
88                 // Trace message
89                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addSkill(): skill.skillId={1} - EXIT!", this.getClass().getSimpleName(), skill.getSkillId())); //NOI18N
90
91                 // Return updated instance
92                 return skill;
93         }
94
95         @Override
96         public Skillable updateSkill (final Skillable skill) throws SkillNotFoundException {
97                 // Trace message
98                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateSkill(): skill={1} - CALLED!", this.getClass().getSimpleName(), skill)); //NOI18N
99
100                 // Validate parameter
101                 if (null == skill) {
102                         // Throw NPE
103                         throw new NullPointerException("skill is null"); //NOI18N
104                 } else if (skill.getSkillId() == null) {
105                         // Throw NPE again
106                         throw new NullPointerException("skill.skillId is null"); //NOI18N
107                 } else if (skill.getSkillId() < 1) {
108                         // Throw IAE
109                         throw new IllegalArgumentException(MessageFormat.format("skill.skillId={0} is not valid", skill.getSkillId())); //NOI18N
110                 } else if (skill.getSkillName() == null) {
111                         // Throw NPE
112                         throw new NullPointerException("skill.skillName is empty"); //NOI18N
113                 } else if (skill.getSkillName().isEmpty()) {
114                         // Throw NPE
115                         throw new NullPointerException("skill.skillName is null"); //NOI18N
116                 } else if (skill.getSkillStatus() == null) {
117                         // Throw NPE
118                         throw new NullPointerException("skill.skillStatus null"); //NOI18N
119                 } else if (!this.isSkillFound(skill)) {
120                         // Already added, abort here
121                         throw new SkillNotFoundException(skill);
122                 }
123
124                 // Merge data
125                 final Skillable updatedSkill = this.mergeSkillData(skill);
126
127                 // Trace message
128                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updatedSkill(): updatedSkill={1} - EXIT!", this.getClass().getSimpleName(), updatedSkill));
129
130                 // Return updated instance
131                 return updatedSkill;
132         }
133
134         /**
135          * Checks if given skill's name is already persisted. The whole (persisted)
136          * list is being loaded and each name is being matched against the given
137          * department's address.
138          * <p>
139          * @param skillable Skill being checked
140          * <p>
141          * @return Whether it has been found
142          */
143         private boolean isSkillFound (final Skillable skillable) {
144                 // Get whole list
145                 final List<Skillable> skills = this.skillBean.fetchAllSkills();
146
147                 // Default is not found
148                 boolean isFound = false;
149
150                 // Check all single addresses
151                 for (final Skillable skill : skills) {
152                         // Is the same address found?
153                         if (SkillUtils.isSameSkill(skill, skillable)) {
154                                 // Found one
155                                 isFound = true;
156                                 break;
157                         }
158                 }
159
160                 // Return flag
161                 return isFound;
162         }
163
164 }