Skip to content

Commit

Permalink
Bean Validation for Wicket 6
Browse files Browse the repository at this point in the history
  • Loading branch information
dilgerm committed Jan 19, 2013
0 parents commit 6303425
Show file tree
Hide file tree
Showing 20 changed files with 710 additions and 0 deletions.
194 changes: 194 additions & 0 deletions pom.xml
@@ -0,0 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>de.effectivetrainings</groupId>
<artifactId>bean-validations</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!-- TODO project name -->
<name>quickstart</name>
<description></description>
<!--
<organization>
<name>company name</name>
<url>company url</url>
</organization>
-->
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<wicket.version>6.4.0</wicket.version>
<jetty.version>7.6.3.v20120416</jetty.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>${wicket.version}</version>
</dependency>

<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-bean-validation</artifactId>
<version>0.5</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>

<!-- OPTIONAL DEPENDENCY
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
<version>${wicket.version}</version>
</dependency>
-->

<!-- LOGGING DEPENDENCIES - LOG4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

<!-- JUNIT DEPENDENCY FOR TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<!-- JETTY DEPENDENCIES FOR TESTING -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<filtering>false</filtering>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<filtering>false</filtering>
<directory>src/test/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>3600000</maxIdleTime>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
<port>8443</port>
<maxIdleTime>3600000</maxIdleTime>
<keystore>${project.build.directory}/test-classes/keystore</keystore>
<password>wicket</password>
<keyPassword>wicket</keyPassword>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>Apache Nexus</id>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
12 changes: 12 additions & 0 deletions src/main/java/de/effectivetrainings/ExtendedValidation.java
@@ -0,0 +1,12 @@
package de.effectivetrainings;

import javax.validation.GroupSequence;
import javax.validation.groups.Default;

/**
* @author martindilger
* Date: 06.01.13
* Time: 17:43
*/
public interface ExtendedValidation {
}
20 changes: 20 additions & 0 deletions src/main/java/de/effectivetrainings/HomePage.html
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Apache Wicket Quickstart</title>
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:regular,bold' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" title="Stylesheet" />
</head>
<body style="padding: 25px">
<div wicket:id="feedback"/>
<form wicket:id="form">
<input type="text" wicket:id="name"/> <br/>
<input type="text" wicket:id="email"/> <br/>
<input type="text" wicket:id="phone"/> <br/>
<input type="text" wicket:id="birthDay"/> <br/>
<select wicket:id="license"/> <br/>
<input type="submit"/>
</form>
</body>
</html>
39 changes: 39 additions & 0 deletions src/main/java/de/effectivetrainings/HomePage.java
@@ -0,0 +1,39 @@
package de.effectivetrainings;

import org.apache.wicket.bean.validation.Property;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.markup.html.WebPage;

import java.util.Arrays;

public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;

public HomePage(final PageParameters parameters) {
super(parameters);
IModel<Trainer> trainerModel = Model.of(new Trainer());
add(new FeedbackPanel("feedback"));
Form<Trainer> form = new Form<Trainer>("form", new CompoundPropertyModel<Trainer>(trainerModel));
form.add(new TextField("name")
.add(new PropertyValidator(new Property(Trainer.class,"name"))));
form.add(new TextField("email")
.add(new PropertyValidator(new Property(Trainer.class,"email"))));
form.add(new TextField("phone")
.add(new PropertyValidator(new Property(Trainer.class,"phone"))));
form.add(new TextField("birthDay")
.add(new PropertyValidator(new Property(Trainer.class,"birthDay"))));
form.add(new DropDownChoice<License>("license",
Arrays.asList(License.values()))
.add(new PropertyValidator(
new Property(Trainer.class,"license"),TrainerGroupSequence.class)));
add(form);
}
}
8 changes: 8 additions & 0 deletions src/main/java/de/effectivetrainings/HomePage.properties
@@ -0,0 +1,8 @@
name.Required=Bitte geben Sie Ihren Namen ein
email.Required=Bitte geben Sie Ihre E-Mailadresse ein.
email.invalid = Die E-Mailadresse ist nicht gueltig
phone.invalid=Ihre Telefonnummer sollte aus Ziffern bestehen
birthDay.invalid=Sie koennen nicht in der Zukunft Geburtstag haben
birthDay.Required=Bitte geben Sie Ihr Geburtsdatum ein
license.invalid=Leider koennen wir keine Lizenzen von Ebay akzeptieren
license.unavailable=Sie brauchen schon eine Lizenz, um als Trainer zu arbeiten.
13 changes: 13 additions & 0 deletions src/main/java/de/effectivetrainings/License.java
@@ -0,0 +1,13 @@
package de.effectivetrainings;

public enum License {
NONE,EBAY, CERTIFICATION;

public boolean isValid(){
return EBAY != this;
}

public boolean isAvailable(){
return NONE != this;
}
}
24 changes: 24 additions & 0 deletions src/main/java/de/effectivetrainings/LicenseAvailable.java
@@ -0,0 +1,24 @@
package de.effectivetrainings;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {LicenseAvailableValidator.class})
public @interface LicenseAvailable {

String message() default "{de.effectivetrainings.LicenseAvailable.message}";

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

Class<? extends Payload>[] payload() default {};
}
21 changes: 21 additions & 0 deletions src/main/java/de/effectivetrainings/LicenseAvailableValidator.java
@@ -0,0 +1,21 @@
package de.effectivetrainings;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* @author martindilger
* Date: 06.01.13
* Time: 17:21
*/
public class LicenseAvailableValidator implements ConstraintValidator<LicenseAvailable,License>{

@Override
public void initialize(LicenseAvailable constraintAnnotation) {
}

@Override
public boolean isValid(License license, ConstraintValidatorContext context) {
return license.isAvailable();
}
}
26 changes: 26 additions & 0 deletions src/main/java/de/effectivetrainings/LicenseValid.java
@@ -0,0 +1,26 @@
package de.effectivetrainings;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;



@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {LicenseValidator.class})
public @interface LicenseValid {

String message() default "{de.effectivetrainings.LicenseValid.message}";

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

Class<? extends Payload>[] payload() default {};
}
28 changes: 28 additions & 0 deletions src/main/java/de/effectivetrainings/LicenseValidator.java
@@ -0,0 +1,28 @@
package de.effectivetrainings;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* @author martindilger
* Date: 06.01.13
* Time: 17:21
*/
public class LicenseValidator implements ConstraintValidator<LicenseValid,License>{

@Override
public void initialize(LicenseValid constraintAnnotation) {
}

@Override
public boolean isValid(License license, ConstraintValidatorContext context) {
//external systems are so slow..
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
return false;
}
return license.isValid();

}
}

0 comments on commit 6303425

Please sign in to comment.