detachedContact.setContactEntryUpdated(new Date());
// Get contact from it and find it
- final Contact foundContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
-
- // Should be found
- assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
+ final Contact foundContact = this.createManaged(detachedContact);
// Debug message
this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeContactData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
}
+ /**
+ * Merges given department's data
+ * <p>
+ * @param detachedDepartment Department instance to merge
+ * <p>
+ * @return Detached contact instance
+ */
+ protected Department mergeDepartmentData (final Department detachedDepartment) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeDepartmentData: detachedDepartment={0} - CALLED!", detachedDepartment)); //NOI18N
+
+ // The contact instance must be valid
+ if (null == detachedDepartment) {
+ // Throw NPE again
+ throw new NullPointerException("detachedDepartment is null"); //NOI18N
+ } else if (detachedDepartment.getDepartmentId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("detachedDepartment.departmentId is null"); //NOI18N
+ } else if (detachedDepartment.getDepartmentId() < 1) {
+ // Not valid
+ throw new IllegalStateException(MessageFormat.format("detachedDepartment.departmentId ={0} is not valid.", detachedDepartment.getDepartmentId())); //NOI18N
+ }
+
+ // Set updated timestamp
+ detachedDepartment.setDepartmentEntryUpdated(new Date());
+
+ // Get contact from it and find it
+ final Department foundDepartment = this.createManaged(detachedDepartment);
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeDepartmentData: foundContact.contactId={0}", foundDepartment.getDepartmentId())); //NOI18N
+
+ // Copy all
+ Departments.copyDepartmentData(detachedDepartment, foundDepartment);
+
+ // Merge contact instance
+ final Department managedDepartment = this.getEntityManager().merge(foundDepartment);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeDepartmentData: managedDepartment={0} - EXIT!", managedDepartment)); //NOI18N
+
+ // Return detached contact
+ return managedDepartment;
+ }
+
+ /**
+ * Returns a detached instance from given fax instance merged into current.
+ * <p>
+ * @param faxNumber Fax instance
+ * @param fetchedNumber Found fax number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableFaxNumber mergeFaxNumberData (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == faxNumber) {
+ // Throw NPE
+ throw new NullPointerException("faxNumber is null"); //NOI18N
+ } else if (fetchedNumber.getPhoneId() == null) {
+ // ..and again
+ throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+ // Init query instance
+ final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableFaxNumber detachedNumber = null;
+
+ // Is there a difference?
+ if (!FaxNumbers.isSameFaxNumber(faxNumber, fetchedNumber)) {
+ // @TODO Copy all to foundNumber, then merge
+
+ // Merge this entry
+ detachedNumber = this.getEntityManager().merge(foundNumber);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
+ /**
+ * Returns a detached instance from given land-line instance merged with
+ * current.
+ * <p>
+ * @param landLineNumber Land-line instance
+ * @param fetchedNumber Found land-line number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableLandLineNumber mergeLandLineNumberData (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == landLineNumber) {
+ // Throw NPE
+ throw new NullPointerException("landLineNumber is null"); //NOI18N
+ } else if (fetchedNumber.getPhoneId() == null) {
+ // ..and again
+ throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+ // Init query instance
+ final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableLandLineNumber detachedNumber = null;
+
+ // Is there a difference?
+ if (!LandLineNumbers.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
+ // @TODO Copy all to foundNumber, then merge
+
+ // Merge this entry
+ detachedNumber = this.getEntityManager().merge(foundNumber);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
+ /**
+ * Returns a detached instance from given mobile instance merged with
+ * current.
+ * <p>
+ * @param mobileNumber Mobile instance
+ * @param fetchedNumber Found mobile number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableMobileNumber mergeMobileNumberData (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == mobileNumber) {
+ // Throw NPE
+ throw new NullPointerException("mobileNumber is null"); //NOI18N
+ } else if (fetchedNumber.getMobileId() == null) {
+ // ..and again
+ throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getMobileId())); //NOI18N
+
+ // Init query instance
+ final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getMobileId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableMobileNumber detachedNumber = null;
+
+ // Is there a difference?
+ if (!MobileNumbers.isSameMobileNumber(mobileNumber, fetchedNumber)) {
+ // @TODO Copy all to foundNumber, then merge
+
+ // Merge this entry
+ detachedNumber = this.getEntityManager().merge(foundNumber);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
/**
* Sends an email with given subject line, template name to given recipient
* and user data
}
@Override
- public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
+ public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
// Update user status and remove confirmation key
managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
managedUser.setUserConfirmKey(null);
- managedUser.setUserUpdated(new Date());
+ managedUser.setUserEntryUpdated(new Date());
// Send out email
this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
}
@Override
- public User updateUserData (final User user) {
+ public User updateUserData (final User detachedUser) throws UserNotFoundException {
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
} else if (!this.ifUserExists(user)) {
// User does not exist
- throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
+ throw new UserNotFoundException(detachedUser.getUserId());
}
// Find the instance
assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
// Set as updated
- managedUser.setUserUpdated(new Date());
+ managedUser.setUserEntryUpdated(new Date());
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
final User managedUser = this.updateUserData(user);
// Update user account
- managedUser.setUserUpdated(new Date());
+ managedUser.setUserEntryUpdated(new Date());
// Create history entry
PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
Users.copyUserData(user, managedUser);
// Set as updated
- managedUser.setUserUpdated(new Date());
+ managedUser.setUserEntryUpdated(new Date());
// Update user data
final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
package org.mxchange.juserlogincore.model.user.register;
import java.text.MessageFormat;
+import java.util.List;
import java.util.Objects;
import javax.ejb.EJB;
import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.Query;
-import org.mxchange.jcontacts.model.contact.Contact;
-import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
-import org.mxchange.jusercore.model.user.LoginUser;
import org.mxchange.jusercore.model.user.User;
import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
import org.mxchange.juserlogincore.login.UserLoginUtils;
throw new NullPointerException("user is null"); //NOI18N
}
- // Create named instance
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
+ // Fetch whole list
+ final List<User> users = this.userBean.fetchAllUsers();
// Init confirmation key
String confirmationKey = null;
// Find a free one
while (confirmationKey == null) {
// Create new one
- final String key = UserLoginUtils.generatedConfirmationKey(user);
-
- // Set key as parameter
- query.setParameter("confirmKey", key); //NOI18N
-
- // Try it
- try {
- // Get contact instance
- final Contact contact = (Contact) query.getSingleResult();
-
- // Warning message
- this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
- } catch (final NoResultException ex) {
- // Not found, normal case
- confirmationKey = key;
- break;
+ confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
+
+ // Check all entries
+ for (final User currentUser : users) {
+ // Does the key match?
+ if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
+ // Set key to null and exit loop
+ confirmationKey = null;
+ break;
+ }
}
}
// Is password set?
if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
- // Switch to other template
+ // Switch to template with random password exposed
templateName = "user_registration_random"; //NOI18N
}