From f6c652533c7551483cad8fb3c53cc4044c62dcff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 3 May 2016 10:29:18 +0200 Subject: [PATCH] Cleanups: - hasUsers/allUsers was redundant - moved isContactFound to admin user controller to avoid redundant lists, too MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../helper/JobsAdminWebRequestHelper.java | 30 +++++++++++++++ .../user/JobsAdminUserWebRequestBean.java | 37 +++++++++++++++++++ .../JobsAdminUserWebRequestController.java | 10 +++++ 3 files changed, 77 insertions(+) diff --git a/src/java/org/mxchange/jjobs/beans/helper/JobsAdminWebRequestHelper.java b/src/java/org/mxchange/jjobs/beans/helper/JobsAdminWebRequestHelper.java index 2e796f80..9d469410 100644 --- a/src/java/org/mxchange/jjobs/beans/helper/JobsAdminWebRequestHelper.java +++ b/src/java/org/mxchange/jjobs/beans/helper/JobsAdminWebRequestHelper.java @@ -122,6 +122,36 @@ public class JobsAdminWebRequestHelper implements JobsAdminWebRequestController this.contact = contact; } + @Override + public String getContactUsageMessageKey (final Contact contact) { + // The contact must be valid + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw again ... + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N + } + + // Default key is "unused" + String messageKey = "CONTACT_IS_UNUSED"; //NOI18N + + // Check user/customer + boolean isUserContact = this.adminUserController.isContactFound(contact); + + // Check user first + if (isUserContact) { + // Only user + messageKey = "CONTACT_IS_USER"; //NOI18N + } + + // Return message key + return messageKey; + } + @Override public User getUser () { return this.user; diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java index 12fb8298..194a85be 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java @@ -395,6 +395,43 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr this.userList = this.userBean.allUsers(); } + @Override + public boolean isContactFound (final Contact contact) { + // The contact must be valid + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw again ... + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Get iterator + Iterator iterator = this.allUsers().iterator(); + + // Loop through all entries + while (iterator.hasNext()) { + // Get user + User next = iterator.next(); + + // Compare both objects + if (Objects.equals(contact, next.getUserContact())) { + // Found it + isFound = true; + break; + } + } + + // Return status + return isFound; + } + @Override public User lookupUserById (final Long userId) throws UserNotFoundException { // Parameter must be valid diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java index 3408cbf6..fe084f60 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java @@ -18,6 +18,7 @@ package org.mxchange.jjobs.beans.user; import java.io.Serializable; import java.util.List; +import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; @@ -36,6 +37,15 @@ public interface JobsAdminUserWebRequestController extends Serializable { */ void afterUserUpdatedPersonalData (final UpdatedUserPersonalDataEvent event); + /** + * Checks whether the given contact is a user + *

+ * @param contact Contact to check + *

+ * @return Whether the contact is a user + */ + boolean isContactFound (final Contact contact); + /** * Tries to lookup user by given id number. If the user is not found or the * account status is not CONFIRMED proper exceptions are thrown. -- 2.39.5