From: Roland Häder Date: Thu, 19 Jan 2023 07:03:06 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=23e593a2e99bf5f6db7a8c8d318dcd611777d953;p=jjobs-ejb.git Continued: - added protected methods createManaged() and mergeSkilLData() - added stateless session EJB for administrative purposes --- diff --git a/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java b/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java index d67d257..2c42afb 100644 --- a/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java +++ b/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java @@ -40,6 +40,8 @@ import org.mxchange.jcontactsbusiness.model.utils.BranchOfficeUtils; import org.mxchange.jcontactsbusiness.model.utils.DepartmentUtils; import org.mxchange.jcoreee.bean.ejb.BaseEnterpriseBean; import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jjobs.model.skill.Skillable; +import org.mxchange.jjobs.model.utils.SkillUtils; import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper; import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; @@ -83,6 +85,42 @@ public abstract class BaseJobsEnterpriseBean extends BaseEnterpriseBean { super(factoryJndi, queueJndi); } + /** + * Creates a managed instance of given detached Skillable class + *

+ * @param detachedSkill A deteched instance of a Skillable class + * + * @return + */ + protected Skillable createManaged (final Skillable detachedSkill) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedSkill)); //NOI18N + + // user should not be null + if (null == detachedSkill) { + // Abort here + throw new NullPointerException("detachedSkill is null"); //NOI18N + } else if (detachedSkill.getSkillId() == null) { + // Id is set + throw new NullPointerException("detachedSkill.skillId is null"); //NOI18N + } else if (detachedSkill.getSkillId() < 1) { + // Id is set + throw new IllegalArgumentException(MessageFormat.format("detachedSkill.skillId={0} is invalid", detachedSkill.getSkillId())); //NOI18N + } + + // Try to find it (should be there) + final Skillable managedSkill = this.getEntityManager().find(detachedSkill.getClass(), detachedSkill.getSkillId()); + + // Should be there + assert (managedSkill instanceof Skillable) : "managedSkill is null"; //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedSkill={1} - EXIT!", this.getClass().getSimpleName(), managedSkill)); //NOI18N + + // Return it + return managedSkill; + } + /** * Get back a managed instance from given contact *

@@ -1153,6 +1191,51 @@ public abstract class BaseJobsEnterpriseBean extends BaseEnterpriseBean { return detachedNumber; } + /** + * Merges given contact's data + *

+ * @param detachedSkill Contact instance to merge + *

+ * @return Detached contact instance + */ + protected Skillable mergeSkillData (final Skillable detachedSkill) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeSkillData: detachedSkill={0} - CALLED!", detachedSkill)); //NOI18N + + // The contact instance must be valid + if (null == detachedSkill) { + // Throw NPE again + throw new NullPointerException("detachedSkill is null"); //NOI18N + } else if (detachedSkill.getSkillId() == null) { + // Throw NPE again + throw new NullPointerException("detachedSkill.skillId is null"); //NOI18N //NOI18N + } else if (detachedSkill.getSkillId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("detachedSkill.skillId={0} is not valid.", detachedSkill.getSkillId())); //NOI18N + } + + // Get contact from it and find it + final Skillable foundSkill = this.createManaged(detachedSkill); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeSkillData: foundSkill.skillId={0}", foundSkill.getSkillId())); //NOI18N + + // Copy all + SkillUtils.copySkillData(detachedSkill, foundSkill); + + // Merge contact instance + final Skillable managedSkill = this.getEntityManager().merge(foundSkill); + + // Set updated timestamp + managedSkill.setSkillEntryUpdated(new Date()); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeSkillData: managedSkill={0} - EXIT!", managedSkill)); //NOI18N + + // Return detached contact + return managedSkill; + } + /** * Sends an email with given subject line, template name to given recipient * and user data diff --git a/src/java/org/mxchange/jjobs/model/skill/JobsAdminSkillSessionBean.java b/src/java/org/mxchange/jjobs/model/skill/JobsAdminSkillSessionBean.java new file mode 100644 index 0000000..19c671a --- /dev/null +++ b/src/java/org/mxchange/jjobs/model/skill/JobsAdminSkillSessionBean.java @@ -0,0 +1,164 @@ +/* + * 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.model.skill; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean; +import org.mxchange.jjobs.exceptions.SkillAlreadyAddedException; +import org.mxchange.jjobs.model.exceptions.SkillNotFoundException; +import org.mxchange.jjobs.model.utils.SkillUtils; + +/** + * A stateless session bean for administrative skillpurposes + *

+ * @author Roland Häder + */ +@Stateless (name = "adminSkill", description = "An administrative statless bean for handling department data (all)") +public class JobsAdminSkillSessionBean extends BaseJobsEnterpriseBean implements AdminSkillSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_467_386_571_705L; + + /** + * General department bean + */ + @EJB (lookup = "java:global/jjobs-ejb/skill!org.mxchange.jjobs.model.skill.SkillSessionBeanRemote") + private SkillSessionBeanRemote skillBean; + + /** + * Default constructor + */ + public JobsAdminSkillSessionBean () { + // Call super constructor + super(); + } + + @Override + public Skillable addSkill (final Skillable skill) throws SkillAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addSkill(): skill={1} - CALLED!", this.getClass().getSimpleName(), skill)); //NOI18N + + // Validate parameter + if (null == skill) { + // Throw NPE + throw new NullPointerException("department is null"); //NOI18N + } else if (skill.getSkillId() instanceof Long) { + // Should not happen + throw new IllegalArgumentException("skill.skillId should not be set."); //NOI18N + } else if (skill.getSkillName() == null) { + // Throw NPE + throw new NullPointerException("skill.skillName is empty"); //NOI18N + } else if (skill.getSkillName().isEmpty()) { + // Throw NPE + throw new NullPointerException("skill.skillName is null"); //NOI18N + } else if (skill.getSkillStatus() == null) { + // Throw NPE + throw new NullPointerException("skill.skillStatus null"); //NOI18N + } else if (this.isSkillFound(skill)) { + // Already added, abort here + throw new SkillAlreadyAddedException(skill); + } + + // Set created timestamp + skill.setSkillEntryCreated(new Date()); + + // Persist it + this.getEntityManager().persist(skill); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addSkill(): skill.skillId={1} - EXIT!", this.getClass().getSimpleName(), skill.getSkillId())); //NOI18N + + // Return updated instance + return skill; + } + + @Override + public Skillable updateSkill (final Skillable skill) throws SkillNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateSkill(): skill={1} - CALLED!", this.getClass().getSimpleName(), skill)); //NOI18N + + // Validate parameter + if (null == skill) { + // Throw NPE + throw new NullPointerException("skill is null"); //NOI18N + } else if (skill.getSkillId() == null) { + // Throw NPE again + throw new NullPointerException("skill.skillId is null"); //NOI18N + } else if (skill.getSkillId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("skill.skillId={0} is not valid", skill.getSkillId())); //NOI18N + } else if (skill.getSkillName() == null) { + // Throw NPE + throw new NullPointerException("skill.skillName is empty"); //NOI18N + } else if (skill.getSkillName().isEmpty()) { + // Throw NPE + throw new NullPointerException("skill.skillName is null"); //NOI18N + } else if (skill.getSkillStatus() == null) { + // Throw NPE + throw new NullPointerException("skill.skillStatus null"); //NOI18N + } else if (!this.isSkillFound(skill)) { + // Already added, abort here + throw new SkillNotFoundException(skill); + } + + // Merge data + final Skillable updatedSkill = this.mergeSkillData(skill); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updatedSkill(): updatedSkill={1} - EXIT!", this.getClass().getSimpleName(), updatedSkill)); + + // Return updated instance + return updatedSkill; + } + + /** + * Checks if given skill's name is already persisted. The whole (persisted) + * list is being loaded and each name is being matched against the given + * department's address. + *

+ * @param skillable Skill being checked + *

+ * @return Whether it has been found + */ + private boolean isSkillFound (final Skillable skillable) { + // Get whole list + final List skills = this.skillBean.fetchAllSkills(); + + // Default is not found + boolean isFound = false; + + // Check all single addresses + for (final Skillable skill : skills) { + // Is the same address found? + if (SkillUtils.isSameSkill(skill, skillable)) { + // Found one + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + +}