All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.UserImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.hfg.xml.XMLName;
import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 Container for user information.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class UserImpl implements User { private String mUID; private String mName; private String mFirstName; private String mPreferredName; private String mSurname; private String mEmail; private String mPhoneNumber; private String mLocation; private String mImageUrl; private String mClientHost; private String mClientHostIP; private String mDomain; private ZoneOffset mTimezoneOffset; private Map mFields; private String mTagName = XML_USER; private static final String XML_USER = "User"; private static final String XML_UID = "UID"; private static final String XML_EMAIL = "Email"; private static final String XML_FIRST_NAME = "FirstName"; private static final String XML_PREFERRED_NAME = "PreferredName"; private static final String XML_NAME = "Name"; private static final String XML_LOCATION = "Location"; private static final String XML_IMAGE_URL = "ImageUrl"; private static final String XML_CLIENT_HOST = "ClientHost"; private static final String XML_DOMAIN = "Domain"; private static final String XML_TIMEZONE_OFFSET = "TimezoneOffset"; private static final XMLName NAME_ATTR = new XMLName("name"); //########################################################################### // CONSTRUCTORS //########################################################################### //-------------------------------------------------------------------------- public UserImpl() { } //-------------------------------------------------------------------------- public UserImpl(XMLTag inXMLTag) { inXMLTag.verifyTagName(mTagName); setUID(inXMLTag.getRequiredSubtagByName(XML_UID).getUnescapedContent()); setName(inXMLTag.getRequiredSubtagByName(XML_NAME).getUnescapedContent()); setImageUrl(inXMLTag.getRequiredSubtagByName(XML_IMAGE_URL).getUnescapedContent()); XMLTag emailTag = inXMLTag.getOptionalSubtagByName(XML_EMAIL); if (emailTag != null) { setEmail(emailTag.getUnescapedContent()); } XMLTag locationTag = inXMLTag.getOptionalSubtagByName(XML_LOCATION); if (locationTag != null) { setLocation(locationTag.getUnescapedContent()); } XMLTag clientHostTag = inXMLTag.getOptionalSubtagByName(XML_CLIENT_HOST); if (clientHostTag != null) { setClientHostIP(clientHostTag.getUnescapedContent()); setClientHost(clientHostTag.getAttributeValue(NAME_ATTR)); } XMLTag timezoneOffsetTag = inXMLTag.getOptionalSubtagByName(XML_TIMEZONE_OFFSET); if (timezoneOffsetTag != null) { setTimezoneOffset(ZoneOffset.ofTotalSeconds(Integer.parseInt(timezoneOffsetTag.getUnescapedContent()))); } } //########################################################################### // PUBLIC METHODS //########################################################################### //-------------------------------------------------------------------------- public XMLTag toXMLTag() { XMLTag parentTag = new XMLTag(XML_USER); parentTag.addSubtag(new XMLTag(XML_UID).setContent(mUID)); parentTag.addSubtag(new XMLTag(XML_NAME).setContent(mName)); parentTag.addSubtag(new XMLTag(XML_IMAGE_URL).setContent(mImageUrl)); if (StringUtil.isSet(mFirstName)) { parentTag.addSubtag(new XMLTag(XML_FIRST_NAME).setContent(mFirstName)); } if (StringUtil.isSet(mPreferredName)) { parentTag.addSubtag(new XMLTag(XML_PREFERRED_NAME).setContent(mPreferredName)); } if (StringUtil.isSet(mEmail)) { parentTag.addSubtag(new XMLTag(XML_EMAIL).setContent(mEmail)); } if (StringUtil.isSet(mLocation)) { parentTag.addSubtag(new XMLTag(XML_LOCATION).setContent(mLocation)); } String clientHostIP = getClientHostIP(); String clientHost = getClientHost(); if (StringUtil.isSet(clientHost) || StringUtil.isSet(clientHostIP)) { XMLTag clientHostTag = parentTag.addSubtag(XML_CLIENT_HOST); if (StringUtil.isSet(clientHostIP)) { clientHostTag.setContent(clientHostIP); } if (StringUtil.isSet(clientHost)) { clientHostTag.setAttribute(NAME_ATTR, clientHost); } } if (mTimezoneOffset != null) { parentTag.addSubtag(new XMLTag(XML_TIMEZONE_OFFSET).setContent(mTimezoneOffset.getTotalSeconds())); } return parentTag; } //-------------------------------------------------------------------------- @Override public String toString() { return getName(); } //-------------------------------------------------------------------------- public UserImpl setUID(String inValue) { mUID = inValue; return this; } //-------------------------------------------------------------------------- public String getUID() { return mUID; } //-------------------------------------------------------------------------- public UserImpl setName(String inValue) { mName = inValue; // Make an attempt to set the first name and surname values if (StringUtil.isSet(mName) && (null == mSurname || null == mFirstName)) { if (mName.contains(",")) { String[] pieces = mName.split(","); if (pieces.length > 1) { if (null == mSurname) { String[] surnamePieces = pieces[0].split("\\s+"); StringBuilderPlus surname = new StringBuilderPlus().setDelimiter(" "); for (int i = 0; i < surnamePieces.length; i++) { String piece = surnamePieces[i].trim(); if (! piece.startsWith("(")) { surname.delimitedAppend(piece); } } mSurname = surname.toString(); } if (null == mFirstName) setFirstName(pieces[1].trim()); } } else { String[] pieces = mName.split("\\s+"); if (pieces.length > 1) { if (null == mFirstName) setFirstName(pieces[0].trim()); if (null == mSurname) { StringBuilderPlus surname = new StringBuilderPlus().setDelimiter(" "); for (int i = 1; i < pieces.length; i++) { String piece = pieces[i].trim(); if (! piece.startsWith("(")) { surname.delimitedAppend(piece); } } mSurname = surname.toString(); } } } } return this; } //-------------------------------------------------------------------------- public String getName() { if (null == mName) { StringBuilderPlus name = new StringBuilderPlus().setDelimiter(" "); if (StringUtil.isSet(getPreferredName())) { name.append(getPreferredName()); } else if (StringUtil.isSet(getFirstName())) { name.append(getFirstName()); } if (StringUtil.isSet(getSurname())) { name.delimitedAppend(getSurname()); } if (name.length() > 0) { mName = name.toString(); } } return mName; } //-------------------------------------------------------------------------- public UserImpl setFirstName(String inValue) { mFirstName = inValue; return this; } //-------------------------------------------------------------------------- public String getFirstName() { return mFirstName; } //-------------------------------------------------------------------------- public UserImpl setPreferredName(String inValue) { mPreferredName = inValue; return this; } //-------------------------------------------------------------------------- public String getPreferredName() { return mPreferredName; } //-------------------------------------------------------------------------- public UserImpl setSurname(String inValue) { mSurname = inValue; return this; } //-------------------------------------------------------------------------- public String getSurname() { return mSurname; } //-------------------------------------------------------------------------- public UserImpl setDomain(String inValue) { mDomain = inValue; return this; } //-------------------------------------------------------------------------- public String getDomain() { return mDomain; } //-------------------------------------------------------------------------- public UserImpl setEmail(String inValue) { mEmail = inValue; if (mEmail != null && null == mDomain) { int index = mEmail.indexOf("@"); setDomain(mEmail.substring(index + 1)); } return this; } //-------------------------------------------------------------------------- public String getEmail() { return mEmail; } //-------------------------------------------------------------------------- public UserImpl setPhoneNumber(String inValue) { mPhoneNumber = inValue; return this; } //-------------------------------------------------------------------------- public String getPhoneNumber() { return mPhoneNumber; } //-------------------------------------------------------------------------- public UserImpl setLocation(String inValue) { mLocation = inValue; return this; } //-------------------------------------------------------------------------- public String getLocation() { return mLocation; } //-------------------------------------------------------------------------- public UserImpl setImageUrl(String inValue) { mImageUrl = inValue; return this; } //-------------------------------------------------------------------------- public String getImageUrl() { return mImageUrl; } //-------------------------------------------------------------------------- public UserImpl setClientHost(String inValue) { mClientHost = inValue; return this; } //-------------------------------------------------------------------------- public String getClientHost() { return mClientHost; } //-------------------------------------------------------------------------- public UserImpl setClientHostIP(String inValue) { mClientHostIP = inValue; return this; } //-------------------------------------------------------------------------- public String getClientHostIP() { return mClientHostIP; } //-------------------------------------------------------------------------- public UserImpl setTimezoneOffset(ZoneOffset inValue) { mTimezoneOffset = inValue; return this; } //-------------------------------------------------------------------------- public ZoneOffset getTimezoneOffset() { return mTimezoneOffset; } //-------------------------------------------------------------------------- public UserImpl setField(String inName, String inValue) { if (null == mFields) { mFields = new HashMap<>(20); } mFields.put(inName, inValue); return this; } //-------------------------------------------------------------------------- public String getField(String inName) { return (mFields != null ? mFields.get(inName) : null); } //-------------------------------------------------------------------------- public Set getFieldNames() { return mFields.keySet(); } //-------------------------------------------------------------------------- @Override public int hashCode() { String name = getName(); int result = (StringUtil.isSet(name) ? name.hashCode() : 0); String uid = getUID(); if (uid != null) { result += 31 * uid.hashCode(); } return result; } //-------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { return (0 == compareTo(inObj2)); } //-------------------------------------------------------------------------- @Override public int compareTo(Object inObj2) { int result = -1; if (inObj2 != null && inObj2 instanceof User) { result = CompareUtil.compare(getName(), ((User) inObj2).getName()); if (0 == result) { result = CompareUtil.compare(getUID(), ((User) inObj2).getUID()); } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy