View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.inject.spi.CDI;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.convert.Converter;
29  import javax.faces.convert.ConverterException;
30  import javax.faces.convert.FacesConverter;
31  import javax.inject.Inject;
32  import java.lang.invoke.MethodHandles;
33  
34  @FacesConverter(forClass = SolarObject.class)// XXX fixme: is not running with Quarkus!
35  public class SolarConverter implements Converter<SolarObject> {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38  
39    @Inject
40    private AstroData astroData;
41  
42    @Override
43    public SolarObject getAsObject(final FacesContext context, final UIComponent component, final String value)
44        throws ConverterException {
45  
46      if (astroData == null) { // XXX @Inject doesn't work in some cases
47        astroData = CDI.current().select(AstroData.class).get();
48      }
49  
50      final SolarObject solarObject = value != null ? astroData.find(value) : null;
51      LOG.info("{} [String] -> {} [SolarObject]", value, solarObject);
52      return solarObject;
53    }
54  
55    @Override
56    public String getAsString(final FacesContext context, final UIComponent component, final SolarObject value)
57        throws ConverterException {
58      final String result = value.getName();
59      LOG.info("{} [SolarObject] -> {} [String]", value, result);
60      return result;
61    }
62  }