From a834b910916e9b8accbcf9e70e18dba9c00c61cc Mon Sep 17 00:00:00 2001 From: Franta Pribyl Date: Sun, 29 Jun 2014 07:09:37 +0200 Subject: [PATCH] =?UTF-8?q?V=20agend=C4=9B=20u=C5=BEivatel=C5=AF=20aplikov?= =?UTF-8?q?=C3=A1na=20validace=20formul=C3=A1=C5=99e.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #98 --- .../services/users/UserServiceImpl.java | 8 +- .../info/bukova/isspst/ui/users/UserForm.java | 108 ++++++++------- .../isspst/validators/BaseValidator.java | 67 ++++++++++ .../isspst/validators/UserFormValidator.java | 61 +++++++++ .../validators/UserPasswordDuplValidator.java | 65 +++++++++ .../validators/UserPasswordOrigValidator.java | 51 +++++++ .../resources/ValidationMessages.properties | 12 +- .../WEB-INF/locales/validators.properties | 5 + src/main/webapp/WEB-INF/zk.xml | 1 + src/main/webapp/admin/users/userForm.zul | 126 +++++++++++++----- 10 files changed, 422 insertions(+), 82 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/validators/BaseValidator.java create mode 100644 src/main/java/info/bukova/isspst/validators/UserFormValidator.java create mode 100644 src/main/java/info/bukova/isspst/validators/UserPasswordDuplValidator.java create mode 100644 src/main/java/info/bukova/isspst/validators/UserPasswordOrigValidator.java create mode 100644 src/main/webapp/WEB-INF/locales/validators.properties diff --git a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java index ae3f5683..a9780c73 100644 --- a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java @@ -55,8 +55,12 @@ public class UserServiceImpl extends AbstractService implements UserServic @Override @Transactional public void saveWithPwd(User user, String password) { - this.setPassword(user, password); - this.update(user); + if ((password != null) && !password.isEmpty()) + { + this.setPassword(user, password); + } + + this.update(user); } @Override diff --git a/src/main/java/info/bukova/isspst/ui/users/UserForm.java b/src/main/java/info/bukova/isspst/ui/users/UserForm.java index 3d6d730c..864ca337 100644 --- a/src/main/java/info/bukova/isspst/ui/users/UserForm.java +++ b/src/main/java/info/bukova/isspst/ui/users/UserForm.java @@ -1,18 +1,20 @@ package info.bukova.isspst.ui.users; +import info.bukova.isspst.data.User; +import info.bukova.isspst.services.users.RoleService; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.ui.FormViewModel; +import info.bukova.isspst.validators.UserFormValidator; + import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.zkoss.bind.Validator; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.zk.ui.select.annotation.WireVariable; -import info.bukova.isspst.data.User; -import info.bukova.isspst.services.users.RoleService; -import info.bukova.isspst.services.users.UserService; -import info.bukova.isspst.ui.FormViewModel; - -public class UserForm extends FormViewModel { - +public class UserForm extends FormViewModel +{ @WireVariable private RoleService roleService; @WireVariable @@ -21,78 +23,92 @@ public class UserForm extends FormViewModel { private String password = ""; private UserRoles userRoles; private boolean loginFree; - + + private Validator userFormValidator; + @Init(superclass = true) - public void init() { + public void init() + { userRoles = new UserRoles(getDataBean(), roleService.getAll()); loginFree = true; + this.setUserFormValidator(new UserFormValidator(getDataBean().getCreated() == null)); } - public UserRoles getUserRoles() { + public UserRoles getUserRoles() + { return userRoles; } - - public String getRetPasswd() { + + public String getRetPasswd() + { return retPasswd; } - @NotifyChange({"canSave", "pwMatches"}) - public void setRetPasswd(String retPasswd) { + public void setRetPasswd(String retPasswd) + { this.retPasswd = retPasswd; } - public String getPassword() { + public String getPassword() + { return password; } - @NotifyChange({"canSave", "pwMatches"}) - public void setPassword(String password) { + public void setPassword(String password) + { this.password = password; } - + @Command - @NotifyChange({"loginFree", "canSave"}) - public void checkLogin() { - try { + @NotifyChange({ "loginFree", "canSave" }) + public void checkLogin() + { + try + { userService.loadUserByUsername(getDataBean().getUsername()); loginFree = false; - } catch(UsernameNotFoundException e) { + } catch (UsernameNotFoundException e) + { loginFree = true; } } - - public boolean isPwMatches() { - return password.equals(retPasswd); - } - - public boolean isLoginFree() { + + public boolean isLoginFree() + { return loginFree || isEdit(); } - - public boolean isEdit() { + + public boolean isEdit() + { return getDataBean().getCreated() != null; } - + @Override - protected void doSave() { - if (!password.isEmpty()) { - userService.saveWithPwd(getDataBean(), this.password); - } else { - super.doSave(); - } + protected void doSave() + { + userService.saveWithPwd(getDataBean(), this.password); } - + @Override - protected void doAdd() { - if (!password.isEmpty()) { - userService.setPassword(getDataBean(), password); - userService.add(getDataBean()); - } + protected void doAdd() + { + userService.setPassword(getDataBean(), password); + userService.add(getDataBean()); } - + @Override - public boolean isCanSave() { - return password.equals(retPasswd) && isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty(); + public boolean isCanSave() + { + return isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty(); } + public Validator getUserFormValidator() + { + return userFormValidator; + } + + public void setUserFormValidator(Validator userFormValidator) + { + this.userFormValidator = userFormValidator; + } } diff --git a/src/main/java/info/bukova/isspst/validators/BaseValidator.java b/src/main/java/info/bukova/isspst/validators/BaseValidator.java new file mode 100644 index 00000000..a92ca562 --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/BaseValidator.java @@ -0,0 +1,67 @@ +package info.bukova.isspst.validators; + +import info.bukova.isspst.StringUtils; + +import org.slf4j.Logger; +import org.zkoss.bind.ValidationContext; +import org.zkoss.bind.validator.AbstractValidator; +import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.HtmlBasedComponent; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zul.Messagebox; + +public abstract class BaseValidator extends AbstractValidator +{ + HtmlBasedComponent htmlComponent; + + protected Logger getLogger() + { + return null; + } + + public void errorMsg(ValidationContext ctx, String msg, String componentIdFocused) + { + this.addInvalidMessage(ctx, msg); + + Logger logger = this.getLogger(); + + if (logger != null) + { + logger.error(msg); + } + + htmlComponent = null; + + if ((componentIdFocused != null) && (!componentIdFocused.isEmpty())) + { + Component component = ctx.getBindContext().getComponent().getFellowIfAny(componentIdFocused, true); + + if ((component != null) && (component instanceof HtmlBasedComponent)) + { + htmlComponent = (HtmlBasedComponent) component; + } + } + + Messagebox.show(msg, StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR, new EventListener() + { + public void onEvent(Event evt) throws InterruptedException + { + if (htmlComponent != null) + { + htmlComponent.setFocus(true); + } + } + }); + } + + public void addErrorMsg(ValidationContext ctx, String msg) + { + this.errorMsg(ctx, msg, null); + } + + @Override + public void validate(ValidationContext ctx) + { + } +} diff --git a/src/main/java/info/bukova/isspst/validators/UserFormValidator.java b/src/main/java/info/bukova/isspst/validators/UserFormValidator.java new file mode 100644 index 00000000..46ca59c2 --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/UserFormValidator.java @@ -0,0 +1,61 @@ +package info.bukova.isspst.validators; + +import info.bukova.isspst.StringUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.Property; +import org.zkoss.bind.ValidationContext; + +public class UserFormValidator extends BaseValidator +{ + private final static Logger log = LoggerFactory.getLogger(UserFormValidator.class.getName()); + + @Override + protected Logger getLogger() + { + return log; + } + + private boolean recordIsNew; + + public UserFormValidator(boolean recordIsNew) + { + this.recordIsNew = recordIsNew; + } + + @Override + public void validate(ValidationContext ctx) + { + Property propertyOrig = ctx.getProperties("password")[0]; + String passwordOrig = (String) propertyOrig.getValue(); + + if (passwordOrig == null) + { + passwordOrig = ""; + } + + if (passwordOrig.isEmpty()) + { + if (this.recordIsNew) + { + this.errorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"), "idUserPasswordOriginal"); + return; + } + } + + Property propertyDupl = ctx.getProperties("retPasswd")[0]; + String passwordDupl = (String) propertyDupl.getValue(); + + if (passwordDupl == null) + { + passwordDupl = ""; + } + + if (!passwordOrig.equals(passwordDupl)) + { + this.errorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"), "idUserPasswordDuplicate"); + return; + } + } +} diff --git a/src/main/java/info/bukova/isspst/validators/UserPasswordDuplValidator.java b/src/main/java/info/bukova/isspst/validators/UserPasswordDuplValidator.java new file mode 100644 index 00000000..fcc22529 --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/UserPasswordDuplValidator.java @@ -0,0 +1,65 @@ +package info.bukova.isspst.validators; + +import info.bukova.isspst.StringUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.Property; +import org.zkoss.bind.ValidationContext; + +public class UserPasswordDuplValidator extends BaseValidator +{ + private final static Logger log = LoggerFactory.getLogger(UserPasswordDuplValidator.class.getName()); + + @Override + protected Logger getLogger() + { + return log; + } + + @Override + public void validate(ValidationContext ctx) + { + return; + } + + public void aaa(ValidationContext ctx) + { + Object argOrig = ctx.getBindContext().getValidatorArg("orig"); + + if (argOrig == null) + { + this.addErrorMsg(ctx, StringUtils.localize("NullPointerErr")); + return; + } + + if (!(argOrig instanceof String)) + { + this.addErrorMsg(ctx, StringUtils.localize("DataTypeErr")); + return; + } + + String propertyNameOrig = (String) argOrig; + + Property propertyOrig = ctx.getProperties(propertyNameOrig)[0]; + String passwordOrig = (String) propertyOrig.getValue(); + + if (passwordOrig == null) + { + passwordOrig = ""; + } + + Property propertyDupl = ctx.getProperty(); + String passwordDupl = (String) propertyDupl.getValue(); + + if (passwordDupl == null) + { + passwordDupl = ""; + } + + if (!passwordOrig.equals(passwordDupl)) + { + this.addErrorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame")); + } + } +} diff --git a/src/main/java/info/bukova/isspst/validators/UserPasswordOrigValidator.java b/src/main/java/info/bukova/isspst/validators/UserPasswordOrigValidator.java new file mode 100644 index 00000000..9f929de9 --- /dev/null +++ b/src/main/java/info/bukova/isspst/validators/UserPasswordOrigValidator.java @@ -0,0 +1,51 @@ +package info.bukova.isspst.validators; + +import info.bukova.isspst.StringUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zkoss.bind.Property; +import org.zkoss.bind.ValidationContext; + +public class UserPasswordOrigValidator extends BaseValidator +{ + private final static Logger log = LoggerFactory.getLogger(UserPasswordOrigValidator.class.getName()); + + @Override + protected Logger getLogger() + { + return log; + } + + private boolean recordIsNew; + + public UserPasswordOrigValidator(boolean recordIsNew) + { + this.recordIsNew = recordIsNew; + } + + @Override + public void validate(ValidationContext ctx) + { + return; + } + + public void aaa(ValidationContext ctx) + { + Property propertyOrig = ctx.getProperty(); + String passwordOrig = (String) propertyOrig.getValue(); + + if (passwordOrig == null) + { + passwordOrig = ""; + } + + if (passwordOrig.isEmpty()) + { + if (this.recordIsNew) + { + this.addErrorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty")); + } + } + } +} diff --git a/src/main/resources/ValidationMessages.properties b/src/main/resources/ValidationMessages.properties index d5f03ae9..f4aa4e21 100644 --- a/src/main/resources/ValidationMessages.properties +++ b/src/main/resources/ValidationMessages.properties @@ -1,3 +1,11 @@ -BuildingsFormCodeConstr = Zadejte k\u00F3d budovy... -MaterialFormCodeConstr = Zadejte k\u00F3d materi\u00E1lu... +BuildingsFormCodeConstr=Zadejte k\u00F3d budovy... +MaterialFormCodeConstr=Zadejte k\u00F3d materi\u00E1lu... MUnitsFormCodeConstr=Zadejte k\u00f3d m\u011brn\u00e9 jednotky... + +Lad = \u00E1 +Led = \u00E9 +Leh = \u011B +Lid = \u00ED +Lod = \u00F3 +Lyd = \u00FD +Lzh = \u017E diff --git a/src/main/webapp/WEB-INF/locales/validators.properties b/src/main/webapp/WEB-INF/locales/validators.properties new file mode 100644 index 00000000..3cce07c1 --- /dev/null +++ b/src/main/webapp/WEB-INF/locales/validators.properties @@ -0,0 +1,5 @@ +NullPointerErr=Chyba ukazatele... +DataTypeErr=Chybný datový typ... + +UserPasswordIsEmpty=Uživatelské heslo musí být zadané... +UserPasswordsAreNotSame=Znovu zadané heslo není stejné... diff --git a/src/main/webapp/WEB-INF/zk.xml b/src/main/webapp/WEB-INF/zk.xml index 009fe9fc..287ac23d 100644 --- a/src/main/webapp/WEB-INF/zk.xml +++ b/src/main/webapp/WEB-INF/zk.xml @@ -21,6 +21,7 @@ /WEB-INF/locales/zk-label.properties /WEB-INF/locales/columns.properties + /WEB-INF/locales/validators.properties diff --git a/src/main/webapp/admin/users/userForm.zul b/src/main/webapp/admin/users/userForm.zul index 75468493..0f2f1368 100644 --- a/src/main/webapp/admin/users/userForm.zul +++ b/src/main/webapp/admin/users/userForm.zul @@ -1,35 +1,97 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file