Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pwener committed Sep 28, 2016
0 parents commit ed80170
Show file tree
Hide file tree
Showing 20 changed files with 625 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target
*.class
.classpath
.project
.settings
.idea
*.iml
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Vraptor message handler
86 changes: 86 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>br.com.caelum.vraptor</groupId>
<artifactId>vraptor-i18n</artifactId>
<version>4.2.0-RC5-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vraptor-i18n</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<weld.version>2.1.2.Final</weld.version>
</properties>

<dependencies>
<dependency>
<groupId>br.com.caelum</groupId>
<artifactId>vraptor</artifactId>
<version>4.2.0-RC3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>${weld.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-impl</artifactId>
<version>${weld.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.20100224</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions src/main/java/com/urutau/vraptor/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.urutau.vraptor;

public enum Category {
DEFAULT,
PRIMARY,
SUCCESS,
INFO,
WARNING,
DANGER;
}
25 changes: 25 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/ErrorMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.urutau.vraptor.handler;

import com.urutau.vraptor.Category;

import br.com.caelum.vraptor.validator.Validator;

public interface ErrorMessageHandler {

/**
* Sets the category that will be show
*
* @param category {@link Category} Object with all contexts of system
*/
Judge use(String category);

/**
* returns {@link Validator#hasErrors()}
*/
boolean hasErrors();

/**
* @return {@link Validator#getErrors().size()}
*/
int errorsCount();
}
50 changes: 50 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/ErrorMessageObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.urutau.vraptor.handler;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import com.urutau.vraptor.handler.qualifier.Category;
import com.urutau.vraptor.handler.qualifier.Condition;
import com.urutau.vraptor.handler.qualifier.Message;
import com.urutau.vraptor.i18n.I18nMessageCreator;

import br.com.caelum.vraptor.validator.I18nMessage;
import br.com.caelum.vraptor.validator.Validator;

/**
* Chain sequence would be: use(category).when(condition).toShow(message)
* .(redirectingTo(XptoController).hello()); // optional
*/
@RequestScoped
public class ErrorMessageObserver {

private final I18nMessageCreator i18nCreator;
private final Validator validator;

public ErrorMessageObserver() {
this(null, null);
}

@Inject
public ErrorMessageObserver(I18nMessageCreator i18nCreator, Validator validator) {
this.i18nCreator = i18nCreator;
this.validator = validator;
}

private String category;
private boolean condition;

public void setCategory(@Observes @Category String category) {
this.category = category;
}

public void setCondition(@Observes @Condition Boolean condition) {
this.condition = condition;
}

public void setMessage(@Observes @Message String message) {
I18nMessage translatedMessage = i18nCreator.translate(message).to(category);
validator.addIf(condition, translatedMessage);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/Judge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.urutau.vraptor.handler;

public interface Judge {
/**
* Sets a condition to throws an specific error
*
* @param condition true to message be showed
* @return {@link Screened} to writes a message
*/
Screened when(Boolean condition);
}
5 changes: 5 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/MessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.urutau.vraptor.handler;

public interface MessageHandler {

}
35 changes: 35 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/Redirector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.urutau.vraptor.handler;

import br.com.caelum.vraptor.validator.Validator;
import br.com.caelum.vraptor.view.Results;

public interface Redirector {
/**
* Uses {@link Validator#onErrorRedirectTo(Class)} to redirects to another action
*
* @param controller contains action that will be called
* @return {@link br.com.caelum.vraptor.Controller} to calls some action
*/
<Controller> Controller redirectingTo(Class<Controller> controller);

/**
* Uses {@link Validator#onErrorUse(Class)} to send a JSON format
* that contains all errors added.
*/
void sendJSON();

/**
* Uses {@link Validator#onErrorUsePageOf(Object)} to redirects to certain page
*
* @param controller contains page references
* @return {@link br.com.caelum.vraptor.Controller} to calls some page
*/
<Controller> Controller onErrorUsePageOf(Class<Controller> controller);

/**
* Stay on the current page
*
* @see {@link Results#referer()}
*/
void stayInCurrent();
}
10 changes: 10 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/Screened.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.urutau.vraptor.handler;

public interface Screened {
/**
* Writes the user message after invoke a {@link #when(Boolean)} method
*
* @param message Error message
*/
public Redirector toShow(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.urutau.vraptor.handler.impl;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.urutau.vraptor.handler.ErrorMessageHandler;
import com.urutau.vraptor.handler.Judge;
import com.urutau.vraptor.handler.qualifier.Category;

import br.com.caelum.vraptor.validator.Validator;

@RequestScoped
public class DefaultErrorMessageHandler implements ErrorMessageHandler {

private static final Logger logger = LoggerFactory.getLogger(DefaultErrorMessageHandler.class);

private final Validator validator;
private final Judge judge;

@Category
private final Event<String> eventCategory;

/**
* @deprecated CDI eyes only
*/
public DefaultErrorMessageHandler() {
this(null, null, null);
}

@Inject
public DefaultErrorMessageHandler(Validator validator, Judge judge,
Event<String> event) {
this.validator = validator;
this.judge = judge;
this.eventCategory = event;
}

public Judge use(String category) {
logger.info("Select category " + category);
eventCategory.fire(category);
return judge;
}

public boolean hasErrors() {
return validator.hasErrors();
}

public int errorsCount() {
return validator.getErrors().size();
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/urutau/vraptor/handler/impl/DefaultJudge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.urutau.vraptor.handler.impl;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.urutau.vraptor.handler.Judge;
import com.urutau.vraptor.handler.Screened;
import com.urutau.vraptor.handler.qualifier.Condition;

@RequestScoped
public class DefaultJudge implements Judge {

private static final Logger logger = LoggerFactory.getLogger(DefaultJudge.class);

private final Screened screened;

@Condition
private final Event<Boolean> eventCondition;

/**
* @deprecated CDI eyes only
*/
public DefaultJudge() {
this(null, null);
}

@Inject
public DefaultJudge(Screened screened, Event<Boolean> event) {
this.screened = screened;
this.eventCondition = event;
}

@Override
public Screened when(Boolean conditionToShow) {
logger.info("Show message is " + conditionToShow);
eventCondition.fire(conditionToShow);
return screened;
}

}
Loading

0 comments on commit ed80170

Please sign in to comment.