From 4c71656c7e1bc32fc3023e8e0aaf82d0032cf585 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Wed, 18 May 2016 10:14:55 +0200
Subject: [PATCH] Continued a bit: - some fields are not always set and may
 cause a NPE - added created/updated timestamps for user - added some more
 initial checks on POJO properties/fields

---
 .../jusercore/model/user/UserUtils.java       | 89 +++++++++++++++----
 1 file changed, 71 insertions(+), 18 deletions(-)

diff --git a/src/org/mxchange/jusercore/model/user/UserUtils.java b/src/org/mxchange/jusercore/model/user/UserUtils.java
index 18b1b33..2d663ab 100644
--- a/src/org/mxchange/jusercore/model/user/UserUtils.java
+++ b/src/org/mxchange/jusercore/model/user/UserUtils.java
@@ -203,33 +203,86 @@ public class UserUtils implements Serializable {
 		// Parameter should be valid
 		if (null == user) {
 			// Throw NPE
-			throw new NullPointerException("user is null");
+			throw new NullPointerException("user is null"); //NOI18N
+		} else if (user.getUserId() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userId is null"); //NOI18N
+		} else if (user.getUserId() < 1) {
+			// Not valid number
+			throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
+		} else if (user.getUserName() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userName is null"); //NOI18N
+		} else if (user.getUserName().isEmpty()) {
+			// Empty string
+			throw new IllegalArgumentException("user.userName is empty"); //NOI18N
+		} else if (user.getUserAccountStatus() == null) {
+			// Throw NPE
+			throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
 		}
 
 		// Init properties list
 		Properties properties = new Properties();
 
+		// Init some vbalues with empty strings
+		properties.setProperty("userConfirmKey", ""); //NOI18N
+		properties.setProperty("userUpdated", ""); //NOI18N
+		properties.setProperty("userLastLockedReason", ""); //NOI18N
+		properties.setProperty("contactTitle", ""); //NOI18N
+		properties.setProperty("contactStreet", ""); //NOI18N
+		properties.setProperty("contactHouseNumber", ""); //NOI18N
+		properties.setProperty("contacCity", ""); //NOI18N
+		properties.setProperty("contacCity", ""); //NOI18N
+		properties.setProperty("contactZipCode", ""); //NOI18N
+		properties.setProperty("contactBirthday", ""); //NOI18N
+		properties.setProperty("contactEmailAddress", ""); //NOI18N
+		properties.setProperty("contactUpdated", ""); //NOI18N
+
 		// Set all:
 		// - User data
-		properties.setProperty("userId", Long.toString(user.getUserId()));
-		properties.setProperty("userName", user.getUserName());
-		properties.setProperty("userConfirmKey", user.getUserConfirmKey());
-		properties.setProperty("userLastLockedReason", user.getUserLastLockedReason());
-		properties.setProperty("userAccountStatus", user.getUserAccountStatus().toString());
+		properties.setProperty("userId", Long.toString(user.getUserId())); //NOI18N
+		properties.setProperty("userName", user.getUserName()); //NOI18N
+		properties.setProperty("userCreated", user.getUserCreated().toString()); //NOI18N
+		if (user.getUserUpdated() != null) {
+			properties.setProperty("contactUpdated", user.getUserUpdated().toString()); //NOI18N
+		}
+		if (user.getUserConfirmKey() != null) {
+			properties.setProperty("userConfirmKey", user.getUserConfirmKey()); //NOI18N
+		}
+		if (user.getUserLastLockedReason() != null) {
+			properties.setProperty("userLastLockedReason", user.getUserLastLockedReason()); //NOI18N
+		}
+		properties.setProperty("userAccountStatus", user.getUserAccountStatus().toString()); //NOI18N
 
 		// - Contact data
-		properties.setProperty("contactGender", user.getUserContact().getContactGender().toString());
-		properties.setProperty("contactTitle", user.getUserContact().getContactTitle());
-		properties.setProperty("contactFirstName", user.getUserContact().getContactFirstName());
-		properties.setProperty("contactFamilyName", user.getUserContact().getContactFamilyName());
-		properties.setProperty("contactStreet", user.getUserContact().getContactStreet());
-		properties.setProperty("contactHouseNumber", Short.toString(user.getUserContact().getContactHouseNumber()));
-		properties.setProperty("contacCity", user.getUserContact().getContactCity());
-		properties.setProperty("contactZipCode", Integer.toString(user.getUserContact().getContactZipCode()));
-		properties.setProperty("contactBirthday", user.getUserContact().getContactBirthday().toString());
-		properties.setProperty("contactEmailAddress", user.getUserContact().getContactEmailAddress());
-		properties.setProperty("contactCreated", user.getUserContact().getContactCreated().toString());
-		properties.setProperty("contactUpdated", user.getUserContact().getContactUpdated().toString());
+		properties.setProperty("contactGender", user.getUserContact().getContactGender().toString()); //NOI18N
+		if (user.getUserContact().getContactTitle() != null) {
+			properties.setProperty("contactTitle", user.getUserContact().getContactTitle()); //NOI18N
+		}
+		properties.setProperty("contactFirstName", user.getUserContact().getContactFirstName()); //NOI18N
+		properties.setProperty("contactFamilyName", user.getUserContact().getContactFamilyName()); //NOI18N
+		if (user.getUserContact().getContactStreet() != null) {
+			properties.setProperty("contactStreet", user.getUserContact().getContactStreet()); //NOI18N
+		}
+		if (user.getUserContact().getContactHouseNumber() != null) {
+			properties.setProperty("contactHouseNumber", Short.toString(user.getUserContact().getContactHouseNumber())); //NOI18N
+		}
+		if (user.getUserContact().getContactCity() != null) {
+			properties.setProperty("contacCity", user.getUserContact().getContactCity()); //NOI18N
+		}
+		if (user.getUserContact().getContactZipCode() != null) {
+			properties.setProperty("contactZipCode", Integer.toString(user.getUserContact().getContactZipCode())); //NOI18N
+		}
+		if (user.getUserContact().getContactBirthday() != null) {
+			properties.setProperty("contactBirthday", user.getUserContact().getContactBirthday().toString()); //NOI18N
+		}
+		if (user.getUserContact().getContactEmailAddress() != null) {
+			properties.setProperty("contactEmailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
+		}
+		properties.setProperty("contactCreated", user.getUserContact().getContactCreated().toString()); //NOI18N
+		if (user.getUserContact().getContactUpdated() != null) {
+			properties.setProperty("contactUpdated", user.getUserContact().getContactUpdated().toString()); //NOI18N
+		}
 
 		// Return it
 		return properties;
-- 
2.39.5