My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 2 ianuarie 2016

[OmniFaces utilities (2.3)] Fire the given CDI event, optionally with the given qualifiers


[OmniFaces utilities] The fireEvent() fires the given CDI event, optionally with the given qualifiers.

Method:
See also: Beans#getManager()
Usages:

In Java EE the observers are marked with the @Observes annotation. The addition of the @Observes annotation to the method signature instructs the container that this method should act as an observer of events of the type it precedes. Check out the below example:

@Named
@Dependent
public class ViningsFireStationBean {

 public void updateSmallFire(@Observes String arg) {
  System.out.println("Vinings fire department will go to a small fire at " + arg);
 }

 public void updateBigFire(@Observes String arg) {
  System.out.println("Vinings fire department will go to a big fire at " + arg);
 }       
}

Now, a CDI managed bean can fire an event that will be observed by ViningsFireStationBean like this:

@Named
@RequestScoped
public class MainFireStationBean {

 @Inject
 Event<String> evt;

 public void fireStarted(String address) {
  evt.fire(address);
 }
}

The above case is pretty classical. Now, if we choose to use the OmniFaces Beans#fireEvent() then we can re-write the MainFireStationBean as below:

import org.omnifaces.util.Beans;
...
@Named
@RequestScoped
public class MainFireStationBean {

 public void fireStarted(String address) {
  Beans.fireEvent(address);
 }
}

But, let's suppose that we need to differentiate between the same object types of objects and set up different observers to listen for them. For example, we may need to distinguish between small fires and big fires. Depending on this aspect, a local fire station may send to the fire address one fire truck or multiple fire trucks. We can model this case via a qualifier:

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface FireType {

 Type value();

 enum Type {
      SMALL, BIG
 }
}

The two enum types (SMALL and BIG)  will be used to act as annotation to mark the strings to be fired by the event instances. So, the MainFireStationBean will be:

@Named
@RequestScoped
public class MainFireStationBean {

 @Inject
 @FireType(Type.SMALL)
 Event<String> small;

 @Inject
 @FireType(Type.BIG)
 Event<String> big;

 public void fireStarted(String address, boolean t) {
  if (t) {
      small.fire(address);
  } else {
      big.fire(address);
  }
 }
}

Finally, add the annotations to the observer part:

@Named
@Dependent
public class ViningsFireStationBean {

 public void updateSmallFire(@Observes @FireType(FireType.Type.SMALL) String arg) {
  System.out.println("Vinings fire department will go to a small fire at " + arg);
 }

 public void updateBigFire(@Observes @FireType(FireType.Type.BIG) String arg) {
  System.out.println("Vinings fire department will go to a big fire at " + arg);
 }
}

In order to re-write this case to take advantage of OmniFaces Beans#fireEvent(), we need to need to declare an abstract qualifier that extends AnnotationLiteral and implements FireType:

public class FireTypeQualifier extends AnnotationLiteral<FireType> implements FireType {

 private final Type type;

 public FireTypeQualifier(Type t) {
  this.type = t;
 }

 @Override
 public Type value() {
  return type;
 }
}

And the MainFireStationBean become:

import org.omnifaces.util.Beans;
...
@Named
@RequestScoped
public class MainFireStationBean {

 public void fireStarted(String address, boolean t) {
  if (t) {
      Beans.fireEvent(address, new FireTypeQualifier(Type.BIG));
  } else {
      Beans.fireEvent(address, new FireTypeQualifier(Type.SMALL));
  }
 }
}

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors