View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.remotecommand;
23  
24  import java.io.IOException;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.el.ELContext;
29  import javax.el.ValueExpression;
30  import javax.faces.FacesException;
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.UIForm;
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  import javax.faces.convert.Converter;
36  import javax.faces.event.ActionEvent;
37  import javax.faces.event.PhaseId;
38  
39  import org.primefaces.extensions.component.base.AbstractParameter;
40  import org.primefaces.extensions.component.parameters.AssignableParameter;
41  import org.primefaces.extensions.util.ExtAjaxRequestBuilder;
42  import org.primefaces.renderkit.CoreRenderer;
43  import org.primefaces.util.ComponentTraversalUtils;
44  
45  /**
46   * Renderer for the {@link RemoteCommand} component.
47   *
48   * @author Thomas Andraschko / last modified by $Author$
49   * @version $Revision$
50   * @since 0.2
51   */
52  public class RemoteCommandRenderer extends CoreRenderer {
53  
54      @Override
55      public void decode(final FacesContext context, final UIComponent component) {
56          final RemoteCommand command = (RemoteCommand) component;
57  
58          final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
59          final String clientId = command.getClientId(context);
60  
61          if (params.containsKey(clientId)) {
62              final ActionEvent event = new ActionEvent(command);
63              if (command.isImmediate()) {
64                  event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
65              }
66              else {
67                  event.setPhaseId(PhaseId.INVOKE_APPLICATION);
68              }
69  
70              // apply params
71              final ELContext elContext = context.getELContext();
72  
73              for (final AssignableParameter param : command.getAssignableParameters()) {
74                  if (!param.isRendered()) {
75                      continue;
76                  }
77  
78                  final ValueExpression valueExpression = param.getAssignTo();
79                  final String paramValue = params.get(clientId + "_" + param.getName());
80  
81                  final Converter<?> converter = param.getConverter();
82                  if (converter != null) {
83                      final Object convertedValue = converter.getAsObject(context, param, paramValue);
84                      valueExpression.setValue(elContext, convertedValue);
85                  }
86                  else {
87                      valueExpression.setValue(elContext, paramValue);
88                  }
89              }
90  
91              command.queueEvent(event);
92          }
93      }
94  
95      @Override
96      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
97          final UIForm form = ComponentTraversalUtils.closestForm(component);
98          if (form == null) {
99              throw new FacesException("Component " + component.getClientId(context)
100                         + " must be enclosed in a form.");
101         }
102 
103         final ResponseWriter writer = context.getResponseWriter();
104         final RemoteCommand command = (RemoteCommand) component;
105         final String clientId = command.getClientId(context);
106 
107         final List<AbstractParameter> parameters = command.getAllParameters();
108         final String name = command.getName();
109 
110         final ExtAjaxRequestBuilder builder = ExtAjaxRequestBuilder.get(context);
111         builder.init()
112                     .source(clientId)
113                     .form(command, command, form)
114                     .process(component, command.getProcess())
115                     .update(component, command.getUpdate())
116                     .async(command.isAsync())
117                     .global(command.isGlobal())
118                     .partialSubmit(command.isPartialSubmit(), command.isPartialSubmitSet(), command.getPartialSubmitFilter())
119                     .resetValues(command.isResetValues(), command.isResetValuesSet())
120                     .ignoreAutoUpdate(command.isIgnoreAutoUpdate())
121                     .onstart(command.getOnstart())
122                     .onerror(command.getOnerror())
123                     .onsuccess(command.getOnsuccess())
124                     .oncomplete(command.getOncomplete())
125                     .delay(command.getDelay())
126                     .timeout(command.getTimeout());
127 
128         builder.params(clientId, parameters);
129 
130         final String request = builder.build();
131 
132         // script
133         writer.startElement("script", command);
134         writer.writeAttribute("type", "text/javascript", null);
135         writer.writeAttribute("id", command.getClientId(), null);
136 
137         writer.write(name + " = function(");
138 
139         // parameters
140         for (int i = 0; i < parameters.size(); i++) {
141             if (i != 0) {
142                 writer.write(",");
143             }
144 
145             final AbstractParameter param = parameters.get(i);
146             writer.write(param.getName());
147         }
148 
149         writer.write(") {");
150         writer.write(request);
151         writer.write("}");
152 
153         if (command.isAutoRun()) {
154             writer.write(";$(function() {");
155             writer.write(name + "();");
156             writer.write("});");
157         }
158 
159         writer.endElement("script");
160     }
161 }