]> git.mxchange.org Git - jjobs-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, 14 May 2016 12:43:39 +0000 (14:43 +0200)
- implemented linkUser()
- use interface for class name

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java

index 5b7b6aecea5d370c66ab05d34c4b45c65168dbb2..63c5ecb6f7f1b1b6f110887f650c9b60c2e93ccf 100644 (file)
@@ -484,8 +484,70 @@ public class JobsUserSessionBean extends BaseJobsDatabaseBean implements UserSes
        }
 
        @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
+               }
+
+               // Check if user is registered
+               if (this.registerBean.isUserNameRegistered(user)) {
+                       // Abort here
+                       throw new UserNameAlreadyRegisteredException(user);
+               } else if (this.registerBean.isEmailAddressRegistered(user)) {
+                       // Abort here
+                       throw new EmailAddressAlreadyRegisteredException(user);
+               }
+
+               // Set created timestamp
+               user.setUserCreated(new GregorianCalendar());
+
+               // Try to find the contact instance
+               Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
+
+               // Try to merge it
+               Contact detachedContact = this.getEntityManager().merge(foundContact);
+
+               // Set it in user
+               user.setUserContact(detachedContact);
+
+               // Persist user
+               this.getEntityManager().persist(user);
+
+               // Flush it to get id
+               this.getEntityManager().flush();
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N
+
+               // Return updated instanc
+               return user;
        }
 
        @Override