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