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.myfaces.tobago.model.SheetState;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.annotation.PostConstruct;
27  import javax.enterprise.context.RequestScoped;
28  import javax.faces.event.AjaxBehaviorEvent;
29  import javax.faces.event.ComponentSystemEvent;
30  import javax.inject.Inject;
31  import javax.inject.Named;
32  import java.lang.invoke.MethodHandles;
33  import java.util.List;
34  import java.util.stream.Collectors;
35  
36  @RequestScoped
37  @Named
38  public class ExceptionHandlerController {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41  
42    private String value;
43    private List<SolarObject> solarList;
44    private SheetState sheetState;
45  
46    @Inject
47    private AstroData astroData;
48  
49    @PostConstruct
50    private void init() {
51      solarList = astroData.findAll().collect(Collectors.toList());
52    }
53  
54    public String getValue() {
55      return value;
56    }
57  
58    public void setValue(final String value) {
59      this.value = value;
60    }
61  
62    public void update(final AjaxBehaviorEvent event) {
63      if (((String) event.getComponent().getAttributes().get("value")).contains("x")) {
64        throw new DemoException("This exception is thrown, because the input value was 'x'.");
65      }
66      LOG.info("AjaxBehaviorEvent called. Current value: '{}'", value);
67    }
68  
69    public List<SolarObject> getSolarList() {
70      return solarList;
71    }
72  
73    public SheetState getSheetState() {
74      return sheetState;
75    }
76  
77    public void setSheetState(final SheetState sheetState) {
78      this.sheetState = sheetState;
79    }
80  
81    public void preRenderViewListener(final ComponentSystemEvent event) {
82      if (sheetState != null && sheetState.getFirst() > 20) {
83        sheetState.setFirst(0);
84        throw new NullPointerException("This exception is thrown, because page 7 or higher is selected.");
85      }
86    }
87  }