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.temperatureconverter.controller;
18  
19  import java.io.Serializable;
20  
21  import javax.enterprise.context.RequestScoped;
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.temperatureconverter.ejb.Scale;
28  import org.jboss.as.quickstarts.temperatureconverter.ejb.Temperature;
29  import org.jboss.as.quickstarts.temperatureconverter.ejb.TemperatureConvertEJB;
30  
31  /**
32   * A simple managed bean that is used to invoke the TemperatureConvertEJB and store the response. The response is obtained by
33   * invoking temperatureConvertEJB.convert().
34   * 
35   * Code borrowed and modified from another quickstart written by Paul Robinson
36   * 
37   * @author Bruce Wolfe
38   */
39  @SuppressWarnings("serial")
40  @Named("temperatureConverter")
41  @RequestScoped
42  public class TemperatureConverter implements Serializable {
43  
44      /*
45       * Injected TemperatureConvertEJB client
46       */
47      @Inject
48      private TemperatureConvertEJB temperatureConvertEJB;
49  
50      /*
51       * Stores the response from the call to temperatureConvertEJB.convert()
52       */
53      private String temperature;
54  
55      private String sourceTemperature = "0.0";
56  
57      private Scale defaultScale = Scale.CELSIUS;
58  
59      /**
60       * Invoke temperatureConvertEJB.convert() and store the temperature
61       * 
62       * @param sourceTemperature The temperature to be converted
63       * @param defaultScale The default source temperature scale
64       */
65      public void convert() {
66          try {
67              temperature = temperatureConvertEJB.convert(Temperature.parse(sourceTemperature, defaultScale)).toString();
68          } catch (IllegalArgumentException e) {
69              temperature = "0.0 Err";
70              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.toString()));
71          }
72      }
73  
74      public String getSourceTemperature() {
75          return sourceTemperature;
76      }
77  
78      public void setSourceTemperature(String sourceTemperature) {
79          this.sourceTemperature = sourceTemperature;
80      }
81  
82      public Scale getDefaultScale() {
83          return defaultScale;
84      }
85  
86      public void setDefaultScale(Scale defaultScale) {
87          this.defaultScale = defaultScale;
88      }
89  
90      public String getTemperature() {
91          return temperature;
92      }
93  
94  }