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.ejb;
18  
19  import static org.jboss.as.quickstarts.temperatureconverter.ejb.Scale.CELSIUS;
20  import static org.jboss.as.quickstarts.temperatureconverter.ejb.Scale.FAHRENHEIT;
21  
22  import javax.ejb.Stateless;
23  import javax.faces.application.FacesMessage;
24  import javax.faces.context.FacesContext;
25  
26  /**
27   * A simple SLSB EJB. The EJB does not use an interface.
28   * 
29   * @author Bruce Wolfe
30   */
31  @Stateless
32  public class TemperatureConvertEJB {
33  
34      /**
35       * This method takes a temperature in Celsius or Fahrenheit and converts it to the other value.
36       * 
37       * @param source the temperature to convert from
38       * @return the converted temperature.
39       */
40      public Temperature convert(Temperature source) {
41  
42          // Convert our Temperature
43          if (source.getScale() == CELSIUS) { // Celsius to Fahrenheit
44              // Easter egg for Absolute Zero.
45              if (source.getTemperature() < Temperature.ABSOLUTE_ZERO_C) {
46                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Below Absolute Zero!"));
47              } else if (source.getTemperature() == Temperature.ABSOLUTE_ZERO_C) {
48                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Absolute Zero!"));
49              }
50              return new Temperature((source.getTemperature() * 9 / 5) + 32, FAHRENHEIT);
51          } else if (source.getScale() == FAHRENHEIT) { // Fahrenheit to Celsius
52              // Easter egg for Absolute Zero.
53              if (source.getTemperature() < Temperature.ABSOLUTE_ZERO_F) {
54                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Below Absolute Zero!"));
55              } else if (source.getTemperature() == Temperature.ABSOLUTE_ZERO_F) {
56                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Absolute Zero!"));
57              }
58              return new Temperature((source.getTemperature() - 32) * 5 / 9, CELSIUS);
59          } else { // Should never get here!
60              throw new IllegalStateException("This is embarrassing - this error should NOT occur!");
61          }
62  
63      }
64  }