Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bean Validator Example #2

Open
parisni opened this issue Sep 16, 2015 · 2 comments
Open

Bean Validator Example #2

parisni opened this issue Sep 16, 2015 · 2 comments

Comments

@parisni
Copy link

parisni commented Sep 16, 2015

Hello Claude,

Here a simple example of bean validation applied on PA4RDF.
Notice I haven't tested this code, just copy paste from my project.

Pom dependency

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-cdi</artifactId>
            <version>5.2.1.Final</version>
        </dependency>

Book Class, with beans validation annotations

@Subject( namespace="http://example.com/PA4RDF#" )
public class Book {
        @Override
        @Predicate( impl = true, emptyIsNull = true)
    public public void setTitle( String title)
    {
        throw new EntityManagerRequiredException();
    }

        @Override
        @Predicate( impl = true, emptyIsNull = true)
        @NotNull
        public String getTitle()
        {
            throw new EntityManagerRequiredException();
        }

        @Override
        @Predicate( impl = true )
        public void addAuthor( String author )
        {
            throw new EntityManagerRequiredException();
        }

        @Override
        @Predicate( impl = true, type=String.class )
        @Size(min=3)
        public List<String> getAuthor() {
            throw new EntityManagerRequiredException();
        }

        @Override
        @Predicate( impl = true , emptyIsNull = true)
        public void setPageCount( Integer int )
        {
            throw new EntityManagerRequiredException();
        }

        @Override
        @Predicate( impl = true , emptyIsNull = true)  
        public boolean hasPageCount()
        {
            throw new EntityManagerRequiredException();
        }

        @Override
        @Predicate( impl = true, emptyIsNull = true ) 
        @NotNull  
        @Min(1)
        public int getPageCount()
        {
            throw new EntityManagerRequiredException();
        }

}

Example code reading the Book class

try{
    dataset.begin( ReadWrite.WRITE );
    model = dataset.getDefaultModel();
    EntityManager entityManager = EntityManagerFactory.getEntityManager();
    Resource r = model.createResource( "http://example.com/mylibrary/book1" );


    Book book = EntityManager.read( r, Book.class );
    book.addAuthor( "Maurice Naftalin" );
    book.addAuthor( "Philip Wadler" );
    book.setPageCount( 0 );
    book.setTItle( null );

   //VALIDATION PHASE
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book );
    StringBuilder errors = new StringBuilder();
    if ( constraintViolations.size() > 0 ) {
        for ( ConstraintViolation<Book> contraintes : constraintViolations ) {
            errors.append(contraintes.getPropertyPath().toString());
            errors.append(" : ");
            errors.append ( contraintes.getMessage());
            errors.append(",");
        }
        throw new Exception( errors.toString() );
    }
    dataset.commit();
}catch(Exception e){
    dataset.abord();
    System.out.println(e.getMessage());
}
finally{
    dataset.end();
}

Result of example code

Title : Cannot be null, PageCount : must be at least 1,  Author : must be at least 3
@Claudenw
Copy link
Owner

Is there a way to verify that the author has been set? I know you could
have anonymous books but just as an example for collections.

Claude

On Wed, Sep 16, 2015 at 3:29 PM, Paris Nicolas [email protected]
wrote:

Hello Claude,

Here a simple example of bean validation applied on PA4RDF.
Notice I haven't tested this code, just copy paste from my project.
Pom dependency

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-cdi</artifactId>
        <version>5.2.1.Final</version>
    </dependency>

Book Class, with beans validation annotations

@subject( namespace="http://example.com/PA4RDF#" )
public class Book {

public public void setTitle( String title)
{
    throw new EntityManagerRequiredException();
}

@Override
    @Predicate( impl = true )
    @NotNull
    public String getTitle()
    {
        throw new EntityManagerRequiredException();
    }

@Override
    @Predicate( impl = true )
    public void addAuthor( String author )
    {
        throw new EntityManagerRequiredException();
    }

@Override
    @Predicate( impl = true, type=String.class )
    public List<String> getAuthor() {
        throw new EntityManagerRequiredException();
    }

@Override
    @Predicate( impl = true )
    public void setPageCount( Integer int )
    {
        throw new EntityManagerRequiredException();
    }

@Override
    @Predicate( impl = true )
    public boolean hasPageCount()
    {
        throw new EntityManagerRequiredException();
    }

@Override
    @Predicate( impl = true )
    @NotNull
    @Min(1)
    public int getPageCount()
    {
        throw new EntityManagerRequiredException();
    }

}

Example code reading the Book class

try{
dataset.begin( ReadWrite.WRITE );
model = dataset.getDefaultModel();
EntityManager entityManager = EntityManagerFactory.getEntityManager();
Resource r = model.createResource( "http://example.com/mylibrary/book1" );

Book book = EntityManager.read( r, Book.class );
book.addAuthor( "Maurice Naftalin" );
book.addAuthor( "Philip Wadler" );
book.setPageCount( 0 );

//VALIDATION PHASE
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book );
StringBuilder errors = new StringBuilder();
if ( constraintViolations.size() > 0 ) {
    for ( ConstraintViolation<Book> contraintes : constraintViolations ) {
        errors.append(contraintes.getPropertyPath().toString());
        errors.append(" : ");
        errors.append ( contraintes.getMessage());
        errors.append(",");
    }
    throw new Exception( errors.toString() );
}
dataset.commit();

}catch(Exception e){
dataset.abord();
System.out.println(e.getMessage());
}
finally{
dataset.end();
}

Result of example code

Title : Cannot be null, PageCount : must be at least 1,


Reply to this email directly or view it on GitHub
#2.

I like: Like Like - The likeliest place on the web
http://like-like.xenei.com
LinkedIn: http://www.linkedin.com/in/claudewarren

@parisni
Copy link
Author

parisni commented Sep 16, 2015

I guess thanks to the @SiZe annotation
The documentation says :

The size of the field or property is evaluated and must match the specified boundaries. If the field or property is a String, the size of the string is evaluated. If the field or property is a Collection, the size of the Collection is evaluated. If the field or property is a Map, the size of the Map is evaluated. If the field or property is an array, the size of the array is evaluated. Use one of the optional max or min elements to specify the boundaries.

then :

@Size(min=1)

should do the job

I wil give a try to confirm

EDIT :
Notice that @Valid annotation does :

Performs validation recursively on the associated object. If the object is a collection or an array, the elements are validated recursively. If the object is a map, the value elements are validated recursively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants