From 2f1162eb6e689d205c48352352255b73cb65a632 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 19 Jan 2023 08:00:56 +0100 Subject: [PATCH] WIP: - added administrative action controller with add/create and update methods - added static data method SkillStatus enumeration - added field for it - added method isSkillAlreadyAdded() - added form field skillStatus with converter --- .../data/JobsDataWebApplicationBean.java | 10 + .../JobsAdminSkillActionWebRequestBean.java | 303 ++++++++++++++++++ ...sAdminSkillActionWebRequestController.java | 27 ++ .../skill/list/JobsSkillListWebViewBean.java | 39 ++- .../list/JobsSkillListWebViewController.java | 9 + .../jobs/skill/JobsSkillStatusConverter.java | 39 +++ web/admin/jobs/skill/admin_skill_list.xhtml | 15 + 7 files changed, 439 insertions(+), 3 deletions(-) create mode 100644 src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestBean.java create mode 100644 src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestController.java create mode 100644 src/java/org/mxchange/jjobs/converter/jobs/skill/JobsSkillStatusConverter.java diff --git a/src/java/org/mxchange/jjobs/beans/data/JobsDataWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/data/JobsDataWebApplicationBean.java index 7915dc4c..4a725f3e 100644 --- a/src/java/org/mxchange/jjobs/beans/data/JobsDataWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/data/JobsDataWebApplicationBean.java @@ -21,6 +21,7 @@ import javax.inject.Named; import org.mxchange.jcontacts.model.contact.title.PersonalTitle; import org.mxchange.jcoreee.dates.DayOfTheWeek; import org.mxchange.jjobs.beans.BaseJobsBean; +import org.mxchange.jjobs.model.skill.status.SkillStatus; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; import org.mxchange.jusercore.model.user.status.UserAccountStatus; @@ -73,6 +74,15 @@ public class JobsDataWebApplicationBean extends BaseJobsBean { return ProfileMode.values(); } + /** + * Returns an array of all skill statuses + *

+ * @return An array of all skill statuses + */ + public SkillStatus[] getSkillStatuses () { + return SkillStatus.values(); + } + /** * Returns an array of all user account statuses *

diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestBean.java new file mode 100644 index 00000000..8fb3b716 --- /dev/null +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestBean.java @@ -0,0 +1,303 @@ +/* + * Copyright (C) 2017 - 2022 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jjobs.beans.jobs.skill.action; + +import java.text.MessageFormat; +import java.util.Objects; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.FacesException; +import javax.faces.application.FacesMessage; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.jjobs.beans.BaseJobsBean; +import org.mxchange.jjobs.beans.jobs.skill.list.JobsSkillListWebViewController; +import org.mxchange.jjobs.events.skill.add.AdminAddedSkillEvent; +import org.mxchange.jjobs.events.skill.add.ObservableAdminAddedSkillEvent; +import org.mxchange.jjobs.events.skill.update.AdminUpdatedSkillEvent; +import org.mxchange.jjobs.events.skill.update.ObservableAdminUpdatedSkillEvent; +import org.mxchange.jjobs.exceptions.SkillAlreadyAddedException; +import org.mxchange.jjobs.model.exceptions.SkillNotFoundException; +import org.mxchange.jjobs.model.skill.AdminSkillSessionBeanRemote; +import org.mxchange.jjobs.model.skill.JobSkill; +import org.mxchange.jjobs.model.skill.Skillable; +import org.mxchange.jjobs.model.skill.status.SkillStatus; +import org.mxchange.jjobs.model.utils.SkillUtils; + +/** + * An administrative action bean for departments + *

+ * @author Roland Häder + */ +@Named ("adminSkillActionController") +@RequestScoped +public class JobsAdminSkillActionWebRequestBean extends BaseJobsBean implements JobsAdminSkillActionWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 5_028_697_360_474L; + + /** + * EJB for administrative purposes + */ + @EJB (lookup = "java:global/jjobs-ejb/adminSkill!org.mxchange.jjobs.model.skill.AdminSkillSessionBeanRemote") + private AdminSkillSessionBeanRemote adminSkillBean; + + /** + * Currently worked on department + */ + private Skillable currentSkill; + + /** + * An event being fired when a skill has been successfully added + */ + @Inject + @Any + private Event skillAddedEvent; + + /** + * Primary key + */ + private Long skillId; + + /** + * A general skill controller (backing bean) + */ + @Inject + private JobsSkillListWebViewController skillListController; + + /** + * Skill name + */ + private String skillName; + + /** + * Skill status + */ + private SkillStatus skillStatus; + + /** + * Event being fired when a department has been updated + */ + @Inject + @Any + private Event updatedSkillEvent; + + /** + * Default constructor + */ + public JobsAdminSkillActionWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Adds skill with all data from this backing bean. First this action method + * will validate if the skill's name is already registered and if found, it + * will output a proper faces message. + */ + public void addSkill () { + // Get instance + final Skillable skill = this.createSkill(); + + // Is the department not created yet? + if (this.skillListController.isSkillAlreadyAdded(skill)) { + // Then show proper faces message + this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N + return; + } + + // Delcare updated instance + final Skillable updatedSkill; + + try { + // Try to call EJB + updatedSkill = this.adminSkillBean.addSkill(skill); + } catch (final SkillAlreadyAddedException ex) { + // Output message + this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N + return; + } + + // Fire event + this.skillAddedEvent.fire(new AdminAddedSkillEvent(updatedSkill)); + } + + /** + * Copies all properties from current skill to this bean. + */ + public void copyAllSkillProperties () { + // Is current skill set? + if (this.getCurrentSkill() == null) { + // Throw NPE + throw new NullPointerException("this.currentSkill is null"); //NOI18N + } else if (this.getCurrentSkill().getSkillId() == null) { + // Throw NPE + throw new NullPointerException("this.currentSkill.departmentId is null"); //NOI18N + } else if (this.getCurrentSkill().getSkillId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("this.currentSkill.departmentId={0} is not valid", this.getCurrentSkill().getSkillId())); //NOI18N + } + + // Copy all fields + this.setSkillName(this.getCurrentSkill().getSkillName()); + this.setSkillId(this.getCurrentSkill().getSkillId()); + } + + /** + * Getter for current skill + *

+ * @return Current department + */ + public Skillable getCurrentSkill () { + return this.currentSkill; + } + + /** + * Setter for current skill + *

+ * @param currentSkill Current department + */ + public void setCurrentSkill (final Skillable currentSkill) { + this.currentSkill = currentSkill; + } + + /** + * Getter for primary key + *

+ * @return Primary key + */ + public Long getSkillId () { + return this.skillId; + } + + /** + * Setter for primary key + *

+ * @param skillId Primary key + */ + public void setSkillId (final Long skillId) { + this.skillId = skillId; + } + + /** + * Getter for skill name + *

+ * @return Skill name + */ + public String getSkillName () { + return this.skillName; + } + + /** + * Setter for skill name + *

+ * @param skillName Skill name + */ + public void setSkillName (final String skillName) { + this.skillName = skillName; + } + + /** + * Getter for skill status + *

+ * @return Skill status + */ + public SkillStatus getSkillStatus () { + return this.skillStatus; + } + + /** + * Setter for skill status + *

+ * @param skillStatus Skill status + */ + public void setSkillStatus (final SkillStatus skillStatus) { + this.skillStatus = skillStatus; + } + + /** + * Updates department record with data from this bean. + *

+ * @return Redirection outcome + */ + public String updateSkill () { + // Is current skill set? + if (this.getCurrentSkill() == null) { + // Throw NPE + throw new NullPointerException("this.currentSkill is null"); //NOI18N + } else if (this.getCurrentSkill().getSkillId() == null) { + // Throw NPE + throw new NullPointerException("this.currentSkill.departmentId is null"); //NOI18N + } else if (this.getCurrentSkill().getSkillId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("this.currentSkill.departmentId={0} is not valid", this.getCurrentSkill().getSkillId())); //NOI18N + } + + // Init instance with current data + final Skillable skill = this.createSkill(); + + // Does current (not updated) and just created (maybe updated) match? + if (Objects.equals(this.getCurrentSkill(), skill)) { + // Yes, then output message + this.showFacesMessage("form-admin-edit-department:departmentI18nKey", "ADMIN_DEPARTMENT_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N + + // Skip below code + return ""; //NOI18N + } + + // Copy all fields + SkillUtils.copySkillData(skill, this.getCurrentSkill()); + + // Initialize updated instance + final Skillable updatedSkill; + + // Try it + try { + // Invoke EJB + updatedSkill = this.adminSkillBean.updateSkill(this.getCurrentSkill()); + } catch (final SkillNotFoundException ex) { + // Throw as a cause + throw new FacesException(ex); + } + + // Fire event + this.updatedSkillEvent.fire(new AdminUpdatedSkillEvent(updatedSkill)); + + // Return to list view + return "admin_list_departments"; //NOI18N + } + + /** + * Prepares an instance of a Skill object (entity) with all data from this + * bean. If a complete fax number or land-line number was provided, it will + * be set in the instance as well. + *

+ * @return An instance of a Skill class (entity) + */ + private Skillable createSkill () { + // Create new skill instance + final Skillable skill = new JobSkill(this.getSkillName(), this.getSkillStatus()); + + // Return fully prepared instance + return skill; + } + +} diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestController.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestController.java new file mode 100644 index 00000000..08331b6f --- /dev/null +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/action/JobsAdminSkillActionWebRequestController.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2023 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jjobs.beans.jobs.skill.action; + +/** + * An interface for administrative "action" controllers, like create, update and + * delete actions. + *

+ * @author Roland Häder + */ +public interface JobsAdminSkillActionWebRequestController { + +} diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java index eeb5f4ba..dae3b6e4 100644 --- a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java @@ -89,12 +89,11 @@ public class JobsSkillListWebViewBean extends BaseJobsBean implements JobsSkillL } /** - * Observers events being fired when an administrator has added company - * basic data. + * Observers events being fired when an administrator has added a new skill. *

* @param event Event being fired */ - public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedSkillEvent event) { + public void afterAdminAddedSkillEvent (@Observes final ObservableAdminAddedSkillEvent event) { // Is the parameter valid? if (null == event) { // Throw NPE @@ -225,6 +224,40 @@ public class JobsSkillListWebViewBean extends BaseJobsBean implements JobsSkillL } } + @Override + public boolean isSkillAlreadyAdded (final Skillable skillable) { + // Validate parameter + if (null == skillable) { + // Throw NPE + throw new NullPointerException("Parameter 'skillable' is null"); //NOI18N + } else if (skillable.getSkillName() == null) { + // Throw it again + throw new NullPointerException("skillable.skillName is null"); //NOI18N + } else if (skillable.getSkillName().isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("skillable.skillName is empty"); //NOI18N + } else if (skillable.getSkillStatus() == null) { + // Throw it again + throw new NullPointerException("skillable.skillStatus is null"); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Check all entries + for (final Skillable currentSkillable : this.getAllSkills()) { + // Is name the same? + if (Objects.equals(currentSkillable.getSkillName(), skillable.getSkillName())) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + @Override public Boolean isSkillNameUsed (final String skillName) { // Validate parameter diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java index f286a2ee..5c45742a 100644 --- a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java @@ -27,6 +27,15 @@ import org.mxchange.jjobs.model.skill.Skillable; */ public interface JobsSkillListWebViewController extends Serializable { + /** + * Checks if given skill is already found + *

+ * @param skillable An instance of a Skillable class + *

+ * @return Whether it has been found + */ + boolean isSkillAlreadyAdded (final Skillable skillable); + /** * Checks if given skill name is already there. *

diff --git a/src/java/org/mxchange/jjobs/converter/jobs/skill/JobsSkillStatusConverter.java b/src/java/org/mxchange/jjobs/converter/jobs/skill/JobsSkillStatusConverter.java new file mode 100644 index 00000000..b3981778 --- /dev/null +++ b/src/java/org/mxchange/jjobs/converter/jobs/skill/JobsSkillStatusConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2017 - 2022 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jjobs.converter.jobs.skill; + +import javax.faces.convert.EnumConverter; +import javax.faces.convert.FacesConverter; +import org.mxchange.jjobs.model.skill.status.SkillStatus; + +/** + * A converter for skill status + *

+ * @author Roland Häder + */ +@FacesConverter ("SkillStatusConverter") +public class JobsSkillStatusConverter extends EnumConverter { + + /** + * Default constructor which calls the super constructor with the proper + * enumeration as class type. + */ + public JobsSkillStatusConverter () { + super(SkillStatus.class); + } + +} diff --git a/web/admin/jobs/skill/admin_skill_list.xhtml b/web/admin/jobs/skill/admin_skill_list.xhtml index 8b218e5d..35c2da4c 100644 --- a/web/admin/jobs/skill/admin_skill_list.xhtml +++ b/web/admin/jobs/skill/admin_skill_list.xhtml @@ -246,6 +246,21 @@ > + + + + + + -- 2.39.5