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.apache.commons.lang3.StringUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.annotation.PostConstruct;
27  import javax.enterprise.context.SessionScoped;
28  import javax.faces.component.UIInput;
29  import javax.inject.Inject;
30  import javax.inject.Named;
31  import java.io.Serializable;
32  import java.lang.invoke.MethodHandles;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.stream.Collectors;
36  
37  @SessionScoped
38  @Named
39  public class SuggestController implements Serializable {
40  
41    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42    private List<String> solarObjects;
43    private String query;
44    private String selection1;
45    private String selection2;
46  
47    @Inject
48    private AstroData astroData;
49  
50    @PostConstruct
51    private void init() {
52      solarObjects = astroData.findAll().map(SolarObject::getName).collect(Collectors.toList());
53    }
54  
55    public String getQuery() {
56      return query;
57    }
58  
59    public void setQuery(final String query) {
60      this.query = query;
61    }
62  
63    public List<String> getSolarObjects() {
64      final String substring = query != null ? query : "";
65      final List<String> filtered =
66          solarObjects.stream().filter(s -> StringUtils.containsIgnoreCase(s, substring)).collect(Collectors.toList());
67      LOG.info("Found {} items for substring: '{}'", filtered.size(), substring);
68      return filtered;
69    }
70  
71    public List<String> getAllSolarObjects() {
72      LOG.info("Found all {} items", solarObjects.size());
73      return solarObjects;
74    }
75  
76    /**
77     * @deprecated use &lt;tc:selectItems/&gt; instead
78     */
79    @Deprecated
80    public List<String> getInputSuggestItems(final UIInput component) {
81      String substring = (String) component.getSubmittedValue();
82      if (substring == null) {
83        substring = "";
84      }
85      LOG.info("Creating items for substring: '" + substring + "'");
86      final List<String> result = new ArrayList<>();
87      for (final String name : solarObjects) {
88        if (StringUtils.containsIgnoreCase(name, substring)) {
89          result.add(name);
90        }
91        if (result.size() > 100) { // this value should not be smaller than the value of the suggest control
92          break;
93        }
94      }
95      return result;
96    }
97  
98    public String getSelection1() {
99      return selection1;
100   }
101 
102   public void setSelection1(final String selection1) {
103     LOG.info("setSelection1 ->" + selection1);
104     this.selection1 = selection1;
105   }
106 
107   public String getSelection2() {
108     return selection2;
109   }
110 
111   public void setSelection2(final String selection2) {
112     this.selection2 = selection2;
113   }
114 }