--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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<ObservableAdminAddedSkillEvent> 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<ObservableAdminUpdatedSkillEvent> 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
+ * <p>
+ * @return Current department
+ */
+ public Skillable getCurrentSkill () {
+ return this.currentSkill;
+ }
+
+ /**
+ * Setter for current skill
+ * <p>
+ * @param currentSkill Current department
+ */
+ public void setCurrentSkill (final Skillable currentSkill) {
+ this.currentSkill = currentSkill;
+ }
+
+ /**
+ * Getter for primary key
+ * <p>
+ * @return Primary key
+ */
+ public Long getSkillId () {
+ return this.skillId;
+ }
+
+ /**
+ * Setter for primary key
+ * <p>
+ * @param skillId Primary key
+ */
+ public void setSkillId (final Long skillId) {
+ this.skillId = skillId;
+ }
+
+ /**
+ * Getter for skill name
+ * <p>
+ * @return Skill name
+ */
+ public String getSkillName () {
+ return this.skillName;
+ }
+
+ /**
+ * Setter for skill name
+ * <p>
+ * @param skillName Skill name
+ */
+ public void setSkillName (final String skillName) {
+ this.skillName = skillName;
+ }
+
+ /**
+ * Getter for skill status
+ * <p>
+ * @return Skill status
+ */
+ public SkillStatus getSkillStatus () {
+ return this.skillStatus;
+ }
+
+ /**
+ * Setter for skill status
+ * <p>
+ * @param skillStatus Skill status
+ */
+ public void setSkillStatus (final SkillStatus skillStatus) {
+ this.skillStatus = skillStatus;
+ }
+
+ /**
+ * Updates department record with data from this bean.
+ * <p>
+ * @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.
+ * <p>
+ * @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;
+ }
+
+}