Now we will start adding classes. We will add the following classes:

SimpleProperty: an Entity Bean for storing our Properties

Manager: a CDI Bean which acts as a glue between the JSF view and the EJBs

ServiceBean: a Stateless EJB which will carry on transactional jobs (Inserts, Deletes)

GenericProducer: a CDI Producer Bean used to instantiate container resources (The Entity Manager)

Producer: a CDI Producer Bean used as a factory of SimpleProperty for the JSF view

RepositoryManager: a CDI Bean used to perform queries and populate SimpleProperty objects
So start by adding the class com.mastertheboss.model.SimpleProperty class:

@Entity
public class SimpleProperty {
 
    @Id 
    @Column(name="id")
    private String key;

    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
   
}

Now it's the turn of the com.mastertheboss.bean.Manager class:
@Model
public class Manager {
  
    @Inject
    ServiceBean ejb;
    
    @Produces
    @Named
    SimpleProperty property;
    
    @Inject
    Producer producer;
    
    @PostConstruct
    public void initNewProperty() {
        property = new SimpleProperty();
    }
     
    public void save() {
        ejb.put(property);
        initNewProperty();
    }

    public void clear(SimpleProperty property) {
        ejb.delete(property);
         
    }
 

}

Key Point

Notice this class contains a SimpleProperty instance with a @Produces @Named annotation. This means that this field will be used as a factory for an instance of the SimpleProperty, which will be eventually exposed (via @Named annotation) to the JSF EL. The benefit of using this approach is that you won't need creating boilerplate code for the single fields of SimpleProperty that need

Here's the turn of the com.mastertheboss.ejb.ServiceBean EJB which will perform some transactional activities such as storing new SimpleProperty and deleting them as well from the DB:

@Stateless
public class  ServiceBean   {

    @Inject
    private Event<SimpleProperty> propEventSrc;
    
    @Inject
    private EntityManager em;
    
    public void put(SimpleProperty p){
         
          em.persist(p);
          propEventSrc.fire(p);
    }
     
    public void delete(SimpleProperty p){

        Query query = em.createQuery("delete FROM com.sample.model.SimpleProperty p where p.key='"+p.getKey()+"'");

        query.executeUpdate();
        propEventSrc.fire(p);
                   
    }

}

In this example we use a com.mastertheboss.producer.GenericProducer class to instantiate some typical container resources such as the EntityManager, which can now be safely injected into your beans via the @Inject annotation:

public class GenericProducer {
    @SuppressWarnings("unused")
    @Produces
    @PersistenceContext
    private EntityManager em;
}

The other Producer class namely com.mastertheboss.producer.Producer will be used to Produce an ArrayList of @Named SimpleProperty which can be therefore be exposed to the JSF EL:
@RequestScoped
public class Producer {
    @Inject RepositoryManager db;

    private List<SimpleProperty> propertyList;

    public void onMemberListChanged(@Observes(notifyObserver = Reception.IF_EXISTS) final SimpleProperty member) {
        retrieveAllSeatsOrderedByName();
    }
    
    @Produces
    @Named
    public List<SimpleProperty> getPropertyList() {
        return propertyList;
    }

    public void setProperty(List<SimpleProperty> property) {
        this.propertyList = propertyList;
    }

    @PostConstruct
    public void retrieveAllSeatsOrderedByName() {

        propertyList = db.queryCache();

    }
    
}

Key Point

Another feature of this Producer class is the Observer pattern. Observers, just like the name suggests, can be used to observe objects. An observer method is notified whenever an object is created, removed, or updated. In our example, it allows refreshing the list of Seats whenever they are needed.
To be precise, in our example, we are using a conditional Observer, which is denoted by the expression notifyObserver = Reception.IF_EXISTS. This means in practice that the observer method is only called if an instance of the component already exists. If not specified, the default option (ALWAYS) will be that the observer method is always called (If an instance doesn't exist, one will be created).

Finally, the last class we will add is the com.mastertheboss.repository.RepositoryManager class which will use the EntityManager instance to perform a basic query on the SimpleProperty table:

public class RepositoryManager {
 
    @Inject
    private EntityManager em;

    public List<SimpleProperty>  queryCache(){
        Query query = em.createQuery("FROM com.mastertheboss.model.SimpleProperty");

        List <SimpleProperty> list =  query.getResultList();
        return list;          
    }
}

0
0
0
s2smodern