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.nonfacesrequest;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.context.SessionScoped;
26  import javax.inject.Named;
27  import java.io.Serializable;
28  import java.lang.invoke.MethodHandles;
29  import java.util.HashMap;
30  import java.util.Map;
31  import java.util.Random;
32  
33  @SessionScoped
34  @Named
35  public class FishPond implements Serializable {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38  
39    private Map<Integer, String> fishes;
40  
41    private Integer selectedFishId = null;
42  
43    public FishPond() {
44      fishes = new HashMap<>();
45      fishes.put(0, "Scholle");
46      fishes.put(1, "Hai");
47      fishes.put(2, "Luce");
48      fishes.put(3, "Halibut");
49      fishes.put(4, "Tamboril");
50    }
51  
52    public void action() {
53      LOG.info("Event is called! selectedFishId='{}'", selectedFishId);
54      // not needed for this example
55    }
56  
57    public String random() {
58      final Random random = new Random(System.currentTimeMillis());
59      selectedFishId = random.nextInt(fishes.size());
60  
61      LOG.info("select via random: '" + getSelectedFish() + "'");
62  
63      return null; // is AJAX
64    }
65  
66    public String select(final Integer fishId) {
67      selectedFishId = fishId;
68      LOG.info("select via id: '" + getSelectedFish() + "'");
69      return "/content/90-non-faces-request/x-fish-pond.xhtml?faces-redirect=true";
70    }
71  
72    public String getSelectedFish() {
73      return fishes.get(selectedFishId);
74    }
75  
76    public Integer getSelectedFishId() {
77      return selectedFishId;
78    }
79  
80    public void setSelectedFishId(final Integer selectedFishId) {
81      this.selectedFishId = selectedFishId;
82      LOG.info("setSelectedFishId via setter: '" + selectedFishId + "'");
83    }
84  }