[jboss-user] [JBoss Seam] - Validation for uniqueness in the DB
adriju
do-not-reply at jboss.com
Fri Feb 15 06:28:34 EST 2008
Hi all again!
I have a form in which I edit or create users from a DB. To check the uniqueness of the username I have created a custom validator:
@Name("userNameValidator")
| @BypassInterceptors
| @Validator
| public class UserNameValidator implements javax.faces.validator.Validator
| {
|
| @Override
| public void validate(FacesContext context, UIComponent component, Object value)
| throws ValidatorException
| {
| UsuariosList uList1 = new UsuariosList();
|
| String s = value.toString();
|
| String select1 = "select u from Usuarios u where u.nombreUsuario = '" + s + "'";
| uList1.setEjbql(select1);
|
| List lu1 = uList1.getResultList();
|
| uList1.restoreEjbql();
|
| if( lu1.size() > 0 )
| {
| FacesMessage message = new FacesMessage();
| message.setDetail("Repeated user in the DB");
| message.setSummary("The user " + s + " already exists in the DB");
| message.setSeverity(FacesMessage.SEVERITY_ERROR);
| FacesMessages.instance().add(message);
| throw new ValidatorException(message);
| }
| }
| }
And the input text is:
<s:decorate id="nombreUsuarioDecoration" template="layout/edit.xhtml">
| <ui:define name="label">User</ui:define>
| <h:inputText id="nombreUsuario"
| required="true"
| value="#{usuariosHome.instance.nombreUsuario}"
| validator="userNameValidator">
| </h:inputText>
| </s:decorate>
This is th entity:
@Entity
| @Table(name = "USUARIOS")
| public class Usuarios implements java.io.Serializable {
|
| private long codUsuario;
| private GruposUsuarios gruposUsuarios;
| private String nombreUsuario;
| private Short prioridadUsuario;
| private String passwordUsuario;
| private String filtroUsuario;
| private String snmpUsuario;
| private Set<TemispConfiguracion> temispConfiguracions = new HashSet<TemispConfiguracion>(
| 0);
| private Set<FiltrosFichas> filtrosFichases = new HashSet<FiltrosFichas>(0);
| private Set<Operacion> operacions = new HashSet<Operacion>(0);
| private Set<Macro> macros = new HashSet<Macro>(0);
| private Set<NuevaConfiguracion> nuevaConfiguracions = new HashSet<NuevaConfiguracion>(
| 0);
| private Set<Grupo> grupos = new HashSet<Grupo>(0);
|
| public Usuarios() {
| }
|
| public Usuarios(long codUsuario, GruposUsuarios gruposUsuarios) {
| this.codUsuario = codUsuario;
| this.gruposUsuarios = gruposUsuarios;
| }
| public Usuarios(long codUsuario, GruposUsuarios gruposUsuarios,
| String nombreUsuario, Short prioridadUsuario,
| String passwordUsuario, String filtroUsuario, String snmpUsuario,
| Set<TemispConfiguracion> temispConfiguracions,
| Set<FiltrosFichas> filtrosFichases, Set<Operacion> operacions,
| Set<Macro> macros, Set<NuevaConfiguracion> nuevaConfiguracions,
| Set<Grupo> grupos) {
| this.codUsuario = codUsuario;
| this.gruposUsuarios = gruposUsuarios;
| this.nombreUsuario = nombreUsuario;
| this.prioridadUsuario = prioridadUsuario;
| this.passwordUsuario = passwordUsuario;
| this.filtroUsuario = filtroUsuario;
| this.snmpUsuario = snmpUsuario;
| this.temispConfiguracions = temispConfiguracions;
| this.filtrosFichases = filtrosFichases;
| this.operacions = operacions;
| this.macros = macros;
| this.nuevaConfiguracions = nuevaConfiguracions;
| this.grupos = grupos;
| }
|
| @Id
| @Column(name = "COD_USUARIO", unique = true, nullable = false, precision = 10, scale = 0)
| @NotNull
| public long getCodUsuario() {
| return this.codUsuario;
| }
|
| public void setCodUsuario(long codUsuario) {
| this.codUsuario = codUsuario;
| }
| @ManyToOne(fetch = FetchType.LAZY)
| @JoinColumn(name = "COD_GRUPOS_USUARIOS", nullable = false)
| @NotNull
| public GruposUsuarios getGruposUsuarios() {
| return this.gruposUsuarios;
| }
|
| public void setGruposUsuarios(GruposUsuarios gruposUsuarios) {
| this.gruposUsuarios = gruposUsuarios;
| }
|
| @Column(name = "NOMBRE_USUARIO", length = 60)
| @Length(max = 60)
| public String getNombreUsuario() {
| return this.nombreUsuario;
| }
|
| public void setNombreUsuario(String nombreUsuario) {
| this.nombreUsuario = nombreUsuario;
| }
|
| @Column(name = "PRIORIDAD_USUARIO", precision = 3, scale = 0)
| public Short getPrioridadUsuario() {
| return this.prioridadUsuario;
| }
|
| public void setPrioridadUsuario(Short prioridadUsuario) {
| this.prioridadUsuario = prioridadUsuario;
| }
|
| @Column(name = "PASSWORD_USUARIO", length = 128)
| @Length(max = 128)
| public String getPasswordUsuario() {
| return this.passwordUsuario;
| }
|
| public void setPasswordUsuario(String passwordUsuario) {
| this.passwordUsuario = passwordUsuario;
| }
|
| @Column(name = "FILTRO_USUARIO", length = 20)
| @Length(max = 20)
| public String getFiltroUsuario() {
| return this.filtroUsuario;
| }
|
| public void setFiltroUsuario(String filtroUsuario) {
| this.filtroUsuario = filtroUsuario;
| }
|
| @Column(name = "SNMP_USUARIO", length = 20)
| @Length(max = 20)
| public String getSnmpUsuario() {
| return this.snmpUsuario;
| }
|
| public void setSnmpUsuario(String snmpUsuario) {
| this.snmpUsuario = snmpUsuario;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<TemispConfiguracion> getTemispConfiguracions() {
| return this.temispConfiguracions;
| }
|
| public void setTemispConfiguracions(
| Set<TemispConfiguracion> temispConfiguracions) {
| this.temispConfiguracions = temispConfiguracions;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<FiltrosFichas> getFiltrosFichases() {
| return this.filtrosFichases;
| }
|
| public void setFiltrosFichases(Set<FiltrosFichas> filtrosFichases) {
| this.filtrosFichases = filtrosFichases;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<Operacion> getOperacions() {
| return this.operacions;
| }
|
| public void setOperacions(Set<Operacion> operacions) {
| this.operacions = operacions;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<Macro> getMacros() {
| return this.macros;
| }
|
| public void setMacros(Set<Macro> macros) {
| this.macros = macros;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<NuevaConfiguracion> getNuevaConfiguracions() {
| return this.nuevaConfiguracions;
| }
|
| public void setNuevaConfiguracions(
| Set<NuevaConfiguracion> nuevaConfiguracions) {
| this.nuevaConfiguracions = nuevaConfiguracions;
| }
| @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "usuarios")
| public Set<Grupo> getGrupos() {
| return this.grupos;
| }
|
| public void setGrupos(Set<Grupo> grupos) {
| this.grupos = grupos;
| }
| }
When I create a new user, it works fine. When I edit one user and try to set the name of another, it works fine. The problem comes when trying to edit a user without editing his name, the validator makes the validation and works 'fine', but I don't want this, I want it only to validate when the inputText changes.
How can I do this?
Thanks in advance.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4129610#4129610
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4129610
More information about the jboss-user
mailing list