import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
-import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
-import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNotFoundException;
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
- // user should not be null
+ // userId should not be null
if (null == user) {
// Abort here
throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() != null) {
- // Not allowed here
- throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
+ } else if (user.getUserId() == null) {
+ // Abort here
+ throw new NullPointerException("user.userId is null"); //NOI18N
+ } else if (user.getUserId() < 1) {
+ // Invalid number
+ throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
}
// Check if user is registered
user.setUserCreated(new GregorianCalendar());
user.getUserContact().setContactCreated(new GregorianCalendar());
- // Get all phone instances
- DialableLandLineNumber landLineNumber = user.getUserContact().getContactLandLineNumber();
- DialableFaxNumber faxNumber = user.getUserContact().getContactFaxNumber();
- DialableCellphoneNumber cellphoneNumber = user.getUserContact().getContactCellphoneNumber();
-
- // Is a phone number instance set?
- if (landLineNumber instanceof DialableLandLineNumber) {
- // Set created timestamp
- landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
- }
-
- // Is a fax number instance set?
- if (faxNumber instanceof DialableFaxNumber) {
- // Set created timestamp
- faxNumber.setPhoneEntryCreated(new GregorianCalendar());
- }
-
- // Is a mobile number instance set?
- if (cellphoneNumber instanceof DialableCellphoneNumber) {
- // Set created timestamp
- cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar());
- }
+ // Set all created timestamps
+ this.setAllContactPhoneEntriesCreated(user.getUserContact());
// Persist it
this.getEntityManager().persist(user);
return true;
}
+ @Override
+ public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+ // userId should not be null
+ if (null == user) {
+ // Abort here
+ throw new NullPointerException("user is null"); //NOI18N
+ } else if (user.getUserContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("user.userContact is null"); //NOI18N
+ } else if (user.getUserContact().getContactId() < 1) {
+ // Not valid id
+ throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //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 instance
+ return user;
+ }
+
@Override
public User updateUserData (final User user) {
// Trace message