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.hibernate4.controller;
18  
19  import javax.annotation.PostConstruct;
20  import javax.enterprise.inject.Model;
21  import javax.enterprise.inject.Produces;
22  import javax.faces.application.FacesMessage;
23  import javax.faces.context.FacesContext;
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  
27  import org.jboss.as.quickstart.hibernate4.model.Member;
28  import org.jboss.as.quickstart.hibernate4.service.MemberRegistration;
29  
30  // The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
31  // EL name
32  // Read more about the @Model stereotype in this FAQ:
33  // http://www.cdi-spec.org/faq/#accordion6
34  @Model
35  public class MemberController {
36  
37      @Inject
38      private FacesContext facesContext;
39  
40      @Inject
41      private MemberRegistration memberRegistration;
42  
43      private Member newMember;
44  
45      @Produces
46      @Named
47      public Member getNewMember() {
48          return newMember;
49      }
50  
51      public void register() {
52          try {
53              memberRegistration.register(newMember);
54              facesContext.addMessage(null,
55                      new FacesMessage(FacesMessage.SEVERITY_INFO, "Registered!", "Registration successful"));
56              initNewMember();
57          } catch (Exception e) {
58              String errorMessage = getRootErrorMessage(e);
59              facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
60                      errorMessage, "Registration unsuccessful"));
61          }
62      }
63  
64      @PostConstruct
65      public void initNewMember() {
66          newMember = new Member();
67      }
68  
69      private String getRootErrorMessage(Exception e) {
70          // Default to general error message that registration failed.
71          String errorMessage = "Registration failed. See server log for more information";
72          if (e == null) {
73              // This shouldn't happen, but return the default messages
74              return errorMessage;
75          }
76  
77          // Start with the exception and recurse to find the root cause
78          Throwable t = e;
79          while (t != null) {
80              // Get the message from the Throwable class instance
81              errorMessage = t.getLocalizedMessage();
82              t = t.getCause();
83          }
84          // This is the root cause message
85          return errorMessage;
86      }
87  
88  }