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.quickstart.hibernate3.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.faces.application.FacesMessage;
27  import javax.faces.context.FacesContext;
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  import javax.persistence.EntityManager;
31  import org.hibernate.Session;
32  
33  import org.jboss.as.quickstart.hibernate3.model.Member;
34  
35  /**
36   * @author Madhumita Sadhukhan
37   */
38  // The @Stateful annotation eliminates the need for manual transaction demarcation
39  @Stateful
40  // The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
41  // EL name
42  // Read more about the @Model stereotype in this FAQ:
43  // http://www.cdi-spec.org/faq/#accordion6
44  @Model
45  public class MemberRegistration {
46  
47      @Inject
48      private Logger log;
49  
50      @Inject
51      private FacesContext facesContext;
52  
53      @Inject
54      private EntityManager em;
55  
56      @Inject
57      private Event<Member> memberEventSrc;
58  
59      private Member newMember;
60  
61      @Produces
62      @Named
63      public Member getNewMember() {
64          return newMember;
65      }
66  
67      @Produces
68      public void register() throws Exception {
69          log.info("Registering " + newMember.getName());
70  
71          // using Hibernate session(Native API) and JPA entitymanager
72          Session session = (Session) em.getDelegate();
73          session.persist(newMember);
74  
75          try {
76              memberEventSrc.fire(newMember);
77              initNewMember();
78          } catch (Exception e) {
79              // Display the reason for the error on the form
80              String errorMessage = getRootErrorMessage(e);
81              FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Registration unsuccessful");
82              facesContext.addMessage(null, facesMessage);
83          }
84      }
85  
86      @PostConstruct
87      public void initNewMember() {
88          newMember = new Member();
89      }
90  
91      private String getRootErrorMessage(Exception e) {
92          // Default to general error message that registration failed.
93          String errorMessage = "Registration failed. See server log for more information";
94          if (e == null) {
95              // This shouldn't happen, but return the default messages
96              return errorMessage;
97          }
98  
99          // Start with the exception and recurse to find the root cause
100         Throwable t = e;
101         while (t != null) {
102             // Get the message from the Throwable class instance
103             errorMessage = t.getLocalizedMessage();
104             t = t.getCause();
105         }
106         // This is the root cause message
107         return errorMessage;
108     }
109 
110 }