From 4283d966dc8bf85bfbc7460c7b47b6c99edbd09f Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 16 Oct 2015 21:16:55 +0200 Subject: [PATCH] Continued: - moved converter classes to better locations: org.mxchange.addressbook.converter.foo is better :) - added + used user converter to convert user id to User instance - maybe c:choose/c:when/c:otherwise is better than ui:fragment? Still the page is a dummy - updated jar(s) --- lib/juser-core.jar | Bin 28664 -> 28830 bytes .../addressbook/beans/user/UserWebBean.java | 30 ++++ .../beans/user/UserWebController.java | 13 ++ .../converter/country}/CountryConverter.java | 4 +- .../smsprovider}/SmsProviderConverter.java | 4 +- .../converter/user/UserConverter.java | 132 ++++++++++++++++++ .../localization/bundle_de_DE.properties | 1 + .../localization/bundle_en_US.properties | 1 + web/user/user_profile.xhtml | 24 ++-- 9 files changed, 197 insertions(+), 12 deletions(-) rename src/java/org/mxchange/{jcountry/data/converter => addressbook/converter/country}/CountryConverter.java (98%) rename src/java/org/mxchange/{jphone/phonenumbers/smsprovider/converter => addressbook/converter/smsprovider}/SmsProviderConverter.java (98%) create mode 100644 src/java/org/mxchange/addressbook/converter/user/UserConverter.java diff --git a/lib/juser-core.jar b/lib/juser-core.jar index 703a0b80f39f09238c27db8637f60068503db2d3..6c0c84ec3a4aa2d7f6afaf8aa489c846041de01a 100644 GIT binary patch delta 1045 zcmY+DUr19?9LIn6-g&pSYtE}~x~;rRbDQ(ulvoCuU}jSsg7~LonRT~0r<>Y8uq1pF z>P60zK17*HdoiNO9_pzAi4tFol1K#!g76_K0wX%Vv(&tB&gXo;=lA=ad(S!d*D7~= zos*gy1&tm+2QZr$mt6czV%&R&pGu5>P_W-}(Vi4=6^Kr*3NgvmB38L$2rFNM2=Mg? zyWk>5vw^JedX%5}%A{M3=oT6gb3z@(qaVT-4!M@nCnEilkLih2yMX$cv4t@`Gr;_T ztPURi*X9llGT&CIk*5;P^;vJ)GgbruY*j$kqb@ROH*=Z$m#&;w6<7no={nT||M zn;`=-sRA%V7WFumX~s?3hXdk41hq zK{hT_(#t|}Rd)Q{Y8ee8L}ll_sMey&2sR1^@I^mdZLKD2vfHrDynah0fgGi@UAzGsy;f5coDsly61|JlYM6u zbc#wh68q?d!B$7NXZx8R9m}Uz$VtBTxv@e{zm5OyU`|)R1D!GI%((wKBJsH`WV2tw zncs9KUP?SdKB>2HV4v%Nay^yppxPQr1& zp{Ti$<1>ZiKZOr5CO2&>`4r_lR=@JEFIO8SX^eSEZ#e})J6lb?G0WCBv(0- NO+Lm=;&l~E{RQ8PFO~oR delta 756 zcmYL{O=uHA6vuaFlQqP|Cfbm;F1o29%?JI8g%F`ts3ncDAy71xbR%NE6PqoFhbn>x zLA-_MVU;2ZJ=8-jnqK^XSg>H#g9jD#lByuIDheVfIn;dx>*{;J6L7V)D>+wGy~R=hezP3D<^4Tfs&4BNAi~af1va>f``oH$8-qX$axq zJz%kKhxgQo@-q!K-66y&K7zQ*k7%g=;@1gsOt+X!`mtr4(b_6AU$IS4 z^q<=&bV#Qd)LG}%SSa!(`DcLVIGls<#OHxohZSbT7MOI8z^3#G_PWgA=#0RlZW{@} zv=o3BdotApFJ%W`a~3esefJ{EchC2X@q91oz&sbj-FoCg^r^v&`3}EX>EZ>Bv&ZNI&j_WuF^fx!R#-6F{0WVWg%-qFq{~2Zr3Xe{Ro|NH@Kj zJBp5kEz)21lBITz!`aXMA9I&hd!V0p!eVdBj#z}yR=)Z8w8KeRq%TKVbj_rLP&EW+E 0)); } + @Override + public User lookupUserById (final Long userId) throws UserNotFoundException { + // Init variable + User user = null; + + // Try to lookup it in visible user list + for (final Iterator iterator = this.visibleUserList.iterator(); iterator.hasNext();) { + // Get next user + User next = iterator.next(); + + // Is the user id found? + if (Objects.equals(next.getUserId(), userId)) { + // Copy to other variable + user = next; + break; + } + } + + // Is it still null? + if (null == user) { + // Not visible for the current user + throw new UserNotFoundException(userId); + } + + // Return it + return user; + } + /** * Adds user's name and email address to bean's internal list. It also * updates the public user list if the user has decided to ha } diff --git a/src/java/org/mxchange/addressbook/beans/user/UserWebController.java b/src/java/org/mxchange/addressbook/beans/user/UserWebController.java index 9a71ae45..dd79d6e0 100644 --- a/src/java/org/mxchange/addressbook/beans/user/UserWebController.java +++ b/src/java/org/mxchange/addressbook/beans/user/UserWebController.java @@ -23,6 +23,7 @@ import org.mxchange.jcontacts.contact.gender.Gender; import org.mxchange.jcountry.data.Country; import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; +import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; @@ -33,6 +34,18 @@ import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; */ public interface UserWebController extends Serializable { + /** + * Tries to lookup user by given id number. If the user is not found or the + * account status is not CONFIRMED proper exceptions are thrown. + *

+ * @param userId User id + *

+ * @return User instance + *

+ * @throws UserNotFoundException If the user is not found + */ + User lookupUserById (final Long userId) throws UserNotFoundException; + /** * Event observer for new user registrations *

diff --git a/src/java/org/mxchange/jcountry/data/converter/CountryConverter.java b/src/java/org/mxchange/addressbook/converter/country/CountryConverter.java similarity index 98% rename from src/java/org/mxchange/jcountry/data/converter/CountryConverter.java rename to src/java/org/mxchange/addressbook/converter/country/CountryConverter.java index b8f516f5..25d172ff 100644 --- a/src/java/org/mxchange/jcountry/data/converter/CountryConverter.java +++ b/src/java/org/mxchange/addressbook/converter/country/CountryConverter.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jcountry.data.converter; +package org.mxchange.addressbook.converter.country; import java.text.MessageFormat; import java.util.List; @@ -42,7 +42,7 @@ import org.mxchange.jcountry.data.Country; public class CountryConverter implements Converter { /** - * Category bean + * Country bean */ @Inject private CountryWebController countryController; diff --git a/src/java/org/mxchange/jphone/phonenumbers/smsprovider/converter/SmsProviderConverter.java b/src/java/org/mxchange/addressbook/converter/smsprovider/SmsProviderConverter.java similarity index 98% rename from src/java/org/mxchange/jphone/phonenumbers/smsprovider/converter/SmsProviderConverter.java rename to src/java/org/mxchange/addressbook/converter/smsprovider/SmsProviderConverter.java index 41bafe7c..06422b04 100644 --- a/src/java/org/mxchange/jphone/phonenumbers/smsprovider/converter/SmsProviderConverter.java +++ b/src/java/org/mxchange/addressbook/converter/smsprovider/SmsProviderConverter.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jphone.phonenumbers.smsprovider.converter; +package org.mxchange.addressbook.converter.smsprovider; import java.text.MessageFormat; import java.util.List; @@ -48,7 +48,7 @@ public class SmsProviderConverter implements Converter { private LoggerBeanLocal loggerBeanLocal; /** - * Category bean + * SMS provider bean */ @Inject private SmsProviderWebController providerController; diff --git a/src/java/org/mxchange/addressbook/converter/user/UserConverter.java b/src/java/org/mxchange/addressbook/converter/user/UserConverter.java new file mode 100644 index 00000000..92f93d2f --- /dev/null +++ b/src/java/org/mxchange/addressbook/converter/user/UserConverter.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.converter.user; + +import java.text.MessageFormat; +import javax.annotation.PostConstruct; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; +import javax.faces.convert.FacesConverter; +import javax.inject.Inject; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.beans.user.UserWebController; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.model.user.User; + +/** + * Converter for user id <-> valid user instance + *

+ * @author Roland Haeder + */ +@FacesConverter (value = "UserConverter") +public class UserConverter implements Converter { + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * User bean + */ + @Inject + private UserWebController userController; + + @Override + public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { + // Trace message + this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contect={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N + + // Is the value null or empty? + if ((null == submittedValue) || (submittedValue.trim().isEmpty())) { + // Return null + return null; + } + + // Init instance + User user = null; + + try { + // Try to parse the value as long + Long userId = Long.valueOf(submittedValue); + + // Debug message + this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId)); + + // Try to get user instance from it + user = this.userController.lookupUserById(userId); + + // Debug message + this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user)); + } catch (final NumberFormatException ex) { + // Throw again + throw new ConverterException(ex); + } catch (final UserNotFoundException ex) { + // Debug message + this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex)); + + // Return null + return null; + } + + // Trace message + this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user)); + + // Return it + return user; + } + + @Override + public String getAsString (final FacesContext context, final UIComponent component, final Object value) { + // Is the object null? + if ((null == value) || ((String.valueOf(value)).isEmpty())) { + // Is null + return ""; //NOI18N + } else if (!(value instanceof User)) { + // Not same interface + throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N + } + + // Return category id + return String.valueOf(((User) value).getUserId()); + } + + /** + * Initialization of this converter + */ + @PostConstruct + public void init () { + // Try to get it + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new RuntimeException("context.lookup() failed.", ex); //NOI18N + } + } +} diff --git a/src/java/org/mxchange/localization/bundle_de_DE.properties b/src/java/org/mxchange/localization/bundle_de_DE.properties index edeadb2a..16e384a7 100644 --- a/src/java/org/mxchange/localization/bundle_de_DE.properties +++ b/src/java/org/mxchange/localization/bundle_de_DE.properties @@ -248,3 +248,4 @@ ADDRESSBOOK_OWNER=Besitzer: ADDRESSBOOK_STATUS=Status: PAGE_TITLE_INDEX_SHOW_ADDRESSBOOK=\u00d6ffentlicher Teil des Adressbuchs anzeigen USER_PROFILE_NOT_PUBLICLY_VISIBLE=Das Benutzerprofil ist privat. +PAGE_TITLE_LOGIN_USER_PROFILE=Benutzerprofil ansehen diff --git a/src/java/org/mxchange/localization/bundle_en_US.properties b/src/java/org/mxchange/localization/bundle_en_US.properties index 65a5a353..4b7a0fe7 100644 --- a/src/java/org/mxchange/localization/bundle_en_US.properties +++ b/src/java/org/mxchange/localization/bundle_en_US.properties @@ -248,3 +248,4 @@ ADDRESSBOOK_OWNER=Owner: ADDRESSBOOK_STATUS=Status: PAGE_TITLE_INDEX_SHOW_ADDRESSBOOK=Show public part of address book USER_PROFILE_NOT_PUBLICLY_VISIBLE=The user profile is private. +PAGE_TITLE_LOGIN_USER_PROFILE=View user profile diff --git a/web/user/user_profile.xhtml b/web/user/user_profile.xhtml index fc0f1200..59f8e6a8 100644 --- a/web/user/user_profile.xhtml +++ b/web/user/user_profile.xhtml @@ -4,16 +4,18 @@ xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" - > + xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"> - + + + #{msg.PAGE_TITLE_LOGIN_USER_PROFILE} #{msg.PAGE_TITLE_INDEX_USER_PROFILE} @@ -21,13 +23,19 @@ - - Here goes your content. - + + + + - - - + + Here goes your content. + + + + #{msg.ERROR_PROFILE_NOT_VISIBLE} + + -- 2.39.5