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.internal.util;
21  
22  import org.apache.myfaces.tobago.event.PageAction;
23  import org.apache.myfaces.tobago.event.PageActionEvent;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import java.util.Map;
31  
32  // Only for retrotranslation don't move me to other artifacts
33  public class PagingUtils {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(PagingUtils.class);
36  
37    public static void decode(FacesContext facesContext, UIComponent component) {
38      String actionId = FacesContextUtils.getActionId(facesContext);
39      String clientId = component.getClientId(facesContext);
40      if (LOG.isDebugEnabled()) {
41        LOG.debug("actionId = '" + actionId + "'");
42        LOG.debug("clientId = '" + clientId + "'");
43      }
44      if (actionId != null && actionId.equals(clientId)) {
45  
46        PageAction action;
47        try {
48          action = PageAction.parse(component.getId());
49        } catch (Exception e) {
50          LOG.error("Illegal value for PageAction :" + component.getId());
51          return;
52        }
53        PageActionEvent event = new PageActionEvent(component.getParent(), action);
54  
55        switch (action) {
56          case TO_PAGE:
57          case TO_ROW:
58            Map map = facesContext.getExternalContext().getRequestParameterMap();
59            Object value = map.get(clientId + ComponentUtils.SUB_SEPARATOR + "value");
60            try {
61              event.setValue(Integer.parseInt((String) value));
62            } catch (NumberFormatException e) {
63              LOG.error("Can't parse integer value for action " + action.name() + ": " + value);
64            }
65            break;
66          default:
67            // nothing more to do
68        }
69        component.queueEvent(event);
70      }
71    }
72  }