Skip to content

Commit

Permalink
adding MessagesProducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Turini committed Sep 17, 2014
1 parent 753b2da commit e00641a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.enterprise.inject.Vetoed;

import org.slf4j.Logger;

import br.com.caelum.vraptor.Result;

/**
* Managed class that stores all application messages like errors, warnings and info. This
* class is useful to display messages categorized by severity in your view. To choose a severity
Expand All @@ -43,37 +39,14 @@
* @since 4.1
* @author Otávio S Garcia
*/
@RequestScoped
@Vetoed
public class Messages {

public static final String ATTRIBUTE_KEY = "vmessages";

private final static Logger log = getLogger(Messages.class);

private Map<Severity, List<Message>> messages = new HashMap<>();
private boolean unhandledErrors = false;

private final Result result;

/**
* @deprecated CDI eyes only
*/
protected Messages() {
this(null);
}

@Inject
public Messages(Result result) {
this.result = result;
}

@PostConstruct
protected void init() {
if(!result.included().containsKey(ATTRIBUTE_KEY)){
result.include(ATTRIBUTE_KEY, this);
}
}

public Messages add(Message message) {
get(message.getSeverity()).add(message);
if(Severity.ERROR.equals(message.getSeverity())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package br.com.caelum.vraptor.validator;

import static com.google.common.base.Objects.firstNonNull;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import br.com.caelum.vraptor.Result;

@RequestScoped
public class MessagesProducer {

private static final String MESSAGES_KEY = "vmessages";

private final Result result;

/**
* @deprecated CDI eyes only
*/
protected MessagesProducer() {
this(null);
}

@Inject
public MessagesProducer(Result result) {
this.result = result;
}

@Produces @RequestScoped
public Messages create() {
Messages messages = (Messages) result.included().get(MESSAGES_KEY);
messages = firstNonNull(messages, new Messages());
result.include(MESSAGES_KEY, messages);
return messages;
}

}

0 comments on commit e00641a

Please sign in to comment.