View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.kitchensinkjsp.controller;
18  
19  import java.util.logging.Logger;
20  
21  import javax.annotation.PostConstruct;
22  import javax.ejb.Stateful;
23  import javax.enterprise.event.Event;
24  import javax.enterprise.inject.Model;
25  import javax.enterprise.inject.Produces;
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.persistence.EntityManager;
29  
30  import org.jboss.as.quickstarts.kitchensinkjsp.model.Member;
31  
32  // The @Stateful annotation eliminates the need for manual transaction demarcation
33  @Stateful
34  // The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
35  // EL name
36  // Read more about the @Model stereotype in this FAQ:
37  // http://www.cdi-spec.org/faq/#accordion6
38  @Model
39  public class MemberRegistration {
40  
41      @Inject
42      private Logger log;
43  
44      @Inject
45      private EntityManager em;
46  
47      @Inject
48      private Event<Member> memberEventSrc;
49  
50      private Member newMember;
51  
52      @Produces
53      @Named
54      public Member getNewMember() {
55  
56          log.info("getNewMember: called" + newMember);
57          return newMember;
58  
59      }
60  
61      public void register() throws Exception {
62  
63          try {
64  
65              log.info("Registering " + newMember.getName());
66              em.persist(newMember);
67              memberEventSrc.fire(newMember);
68              initNewMember();
69          } catch (Exception e) {
70              Throwable t = e;
71              while ((t.getCause()) != null) {
72                  t = t.getCause();
73              }
74              log.info("Exception:" + t.getMessage());
75              throw ((Exception) t);
76          }
77  
78      }
79  
80      @PostConstruct
81      public void initNewMember() {
82          newMember = new Member();
83          log.info("@PostConstruct:initNewMember called");
84      }
85  }