View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
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  
18  package org.apache.shale.examples.mailreaderjpa;
19  
20  import javax.faces.component.UIComponent;
21  import javax.faces.context.FacesContext;
22  import javax.faces.convert.Converter;
23  import javax.faces.convert.ConverterException;
24  import org.apache.mailreaderjpa.Protocol;
25  
26  /***
27   * <p>JSF <code>Converter</code> for mapping between a <code>Protocol</code>
28   * and its string representation.</p>
29   */
30  public class ProtocolConverter implements Converter {
31      
32  
33      // ------------------------------------------------------- Converter Methods
34  
35  
36      /***
37       * <p>Return the string representation of the specified
38       * <code>Protocol</code> value.</p>
39       *
40       * @param context <code>FacesContext</code> for the current request
41       * @param component <code>UIComponent</code> whose value is being converted
42       * @param value {@link Protocol} being converted
43       *
44       * @exception ConverterException if a conversion error occurs
45       */
46      public String getAsString(FacesContext context, UIComponent component,
47              Object value) throws ConverterException {
48  
49          Protocol protocol = (Protocol) value;
50          return "" + protocol.getId();
51  
52      }
53  
54  
55      /***
56       * <p>Return the <code>Protocol</code> instance corresponding to the
57       * specified string representation.</p>
58       *
59       * @param context <code>FacesContext</code> for the current request
60       * @param component <code>UIComponent</code> whose value is being converted
61       * @param value String representation of the value
62       *
63       * @exception ConverterException if a conversion error occurs
64       */
65      public Object getAsObject(FacesContext context, UIComponent component,
66              String value) throws ConverterException {
67  
68          Logic logic = 
69            (Logic) context.getExternalContext().getApplicationMap().get("logic");
70          return logic.findProtocolById(Integer.valueOf(value).intValue());
71  
72      }
73  
74  
75  }