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