-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Class level constraints not working as expected
PostPosted: Wed Oct 16, 2013 9:01 am 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
Hi All,

Testing a class level constraint, though it seems working fine but getting issue while displaying errors to the user end.here is my constraints declaration

Code:
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface FieldMatch
{
    String message() default "Fields are not matching";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    /**
     * @return The first field
     */
    String first();

    /**
     * @return The second field
     */
    String second();

    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see FieldMatch
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
            @interface List
    {
        FieldMatch[] value();
    }



Its implementation is also working fine, but i need to check which is class level violation and which method level violation and here is my piece of code
Code:
protected boolean isActionError(ConstraintViolation<Object> violation) {
       
       if(violation.getPropertyPath().iterator().next().getName() == null){
          return true;
       }
       return false;
    }


But in my case
Code:
violation.getPropertyPath().iterator().next().getName()
is not null and it seems either i misunderstood violation.getPropertyPath() or something is not working as expected.

can anyone help me as where i am doing wrong


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Wed Oct 16, 2013 2:27 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Could you post the code of the validator class (FieldMatchValidator)? It may be that this validator builds a custom constraint violation, specifying a custom path, which e.g. points to the second field of the two compared fields.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Wed Oct 16, 2013 10:48 pm 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
Here is code from my custom constraint class "FieldMatchValidator "

Code:

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
{
   
    private static final Logger LOG=LoggerFactory.getLogger(FieldMatchValidator.class);
    private String firstFieldName;
    private String secondFieldName;
    private BeanUtils beanUtils;

    @Override
    public void initialize(final FieldMatch constraintAnnotation)
    {
        this.firstFieldName = constraintAnnotation.first();
        this.secondFieldName = constraintAnnotation.second();
        this.beanUtils=BeanUtils.newInstance();
    }

    @SuppressWarnings( "nls" )
    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context)
    {
        try
        {
            final Object firstObj = this.beanUtils.getPropertyValue(value, this.firstFieldName);
            final Object secondObj = this.beanUtils.getPropertyValue(value, this.secondFieldName);
            return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }
        catch (final Exception ex)
        {
            LOG.info( "Error while getting values from object", ex );
            return false;
           
        }
       
    }
}



Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 9:36 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Ok, your validator doesn't create a custom violation, so that's not the reason. When you say the node name is not null, which value has it actually? Also, how are you triggering validation of your object, is it maybe via a cascaded validation from another object (using @Valid)? It would be helpful to see the involved type(s) and how you're validating them.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 10:39 am 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
Hi Gunnar,

I am creating plugin with Struts2 so this is application flow

    User enter values in storefront
    Values submitted to Struts2 action (Controller )
    There will be an interceptor which will trigger validation process

here is the code snap shots

Action Class
Code:

@Valid
private DonorProfileDTO donorProfileDTO;

public DonorProfileDTO getDonorProfileDTO() {
   return donorProfileDTO;
}
public void setDonorProfileDTO(DonorProfileDTO donorProfileDTO) {
    this.donorProfileDTO = donorProfileDTO;
}


getter and setters are needed by Framework to transfer values. and this is how my validation is being triggered from inside an interceptor

Code:
  Set<ConstraintViolation<Object>> constraintViolations= validator.validate(action);


where action is name of the action (Controller) being called from the Storefront by user.
In this case node name is coming as
Code:
donorProfileDTO


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 11:56 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Ah, so that's what I thought, you're using a cascaded validation actually.

You should navigate to the end of the path and check for the name (or type in case your using Bean Validation 1.1) of the last node. Generally the property path contains an element for each node from the validated root object to the node with the invalid value; In your case that's one node for the action (the root object you validated) and another node for the DTO which is referenced by that action.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 12:02 pm 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
Thanks Gunnar,

So you mean that i need to iterate through the list and see if end node if null or not?
In case it is null, constraint is property/ field level else it will be a class level constraint?

something like
Code:
while (if(violation.getPropertyPath().iterator().hasNext()){
// get leaf node value
}
check if leave node values is null or not


Please correct me if i am wrong?

Thanks
Umesh


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
You should iterate through the path and check whether the name of the last node is null or not. If you're using Bean Validation 1.1, you should check for the type of the last node instead.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Class level constraints not working as expected
PostPosted: Thu Oct 17, 2013 1:36 pm 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
After going through java docs of specifications i believe
Code:
(violation.getLeafBean() == violation.getInvalidValue()
will work for me.

I am using 1.0


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.