]> git.mxchange.org Git - addressbook-mailer-ejb.git/commitdiff
Continued a bit:
authorRoland Häder <roland@mxchange.org>
Tue, 3 May 2016 13:13:35 +0000 (15:13 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 7 May 2016 09:49:28 +0000 (11:49 +0200)
- implemented linkUser()
- use interface for class name

src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java

index c303a085eb1bdc9edeb3ef63ef881f5031756496..52df1727ecc7fb6d35dcd9fd299de9910206e58b 100644 (file)
@@ -183,7 +183,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Try to locate it
-               Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("param", user.getUserName()); //NOI18N
@@ -222,7 +222,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Create query instance
-               Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
 
                // Set user id
                query.setParameter("id", userId); //NOI18N
@@ -281,7 +281,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Generate query
-               Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("id", user.getUserId()); //NOI18N
@@ -328,7 +328,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Generate query
-               Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("id", userId); //NOI18N
@@ -375,7 +375,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Generate query
-               Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("param", userName); //NOI18N
@@ -413,7 +413,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Generate query
-               Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
@@ -454,7 +454,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                }
 
                // Generate query
-               Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
 
                // Set parameter
                query.setParameter("param", user.getUserName()); //NOI18N
@@ -484,8 +484,61 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
        }
 
        @Override
-       public User linkUser (User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
+
+               // user should not be null
+               // @TODO Add check for email address
+               if (null == user) {
+                       // Abort here
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() instanceof Long) {
+                       // Id is set
+                       throw new IllegalArgumentException("user.userId is not null"); //NOI18N
+               } else if (user.getUserContact() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userContact is null"); //NOI18N
+               } else if (user.getUserContact().getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
+               } else if (user.getUserContact().getContactId() < 1) {
+                       // Not valid id number
+                       throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
+               } else if (user.getUserAccountStatus() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+               } else if (this.ifUserNameExists(user.getUserName())) {
+                       // Name already found
+                       throw new UserNameAlreadyRegisteredException(user.getUserName());
+               } else if (this.ifUserExists(user)) {
+                       // User does not exist
+                       throw new IllegalStateException("User does already exist."); //NOI18N
+               }
+
+               // Try to find the contact
+               Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
+
+               // Merge the contact to get a detached object back
+               Contact detachedContact = this.getEntityManager().merge(foundContact);
+
+               // Set detached object in rexcruiter instance
+               user.setUserContact(detachedContact);
+
+               // Set timestamp
+               user.setUserCreated(new GregorianCalendar());
+
+               // Perist it
+               this.getEntityManager().persist(user);
+
+               // Flush it to get updated instance back
+               this.getEntityManager().flush();
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N
+
+               // Return updated instanc
+               return user;
        }
 
        @Override