Coverage Report - org.apache.tapestry5.services.ajax.MultiZoneUpdateEventResultProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiZoneUpdateEventResultProcessor
88%
15/17
100%
2/2
0
MultiZoneUpdateEventResultProcessor$1
100%
2/2
N/A
0
 
 1  
 // Copyright 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.services.ajax;
 16  
 
 17  
 import org.apache.tapestry5.MarkupWriter;
 18  
 import org.apache.tapestry5.ajax.MultiZoneUpdate;
 19  
 import org.apache.tapestry5.internal.services.PageRenderQueue;
 20  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 21  
 import org.apache.tapestry5.ioc.services.TypeCoercer;
 22  
 import org.apache.tapestry5.runtime.RenderCommand;
 23  
 import org.apache.tapestry5.runtime.RenderQueue;
 24  
 import org.apache.tapestry5.services.ComponentEventResultProcessor;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Handler for {@link org.apache.tapestry5.ajax.MultiZoneUpdate} responses from a component event handler method. Works
 31  
  * by adding {@link org.apache.tapestry5.services.ajax.SingleZonePartialRendererFilter}s for each zone to the
 32  
  * {@linkplain org.apache.tapestry5.internal.services.PageRenderQueue#addPartialMarkupRendererFilter(org.apache.tapestry5.services.PartialMarkupRendererFilter)
 33  
  * filter stack}.  Each zone writes its content as a string in the zones object of the reply, keyed on its id.
 34  
  * JavaScript and CSS are collected for all zones rendered in the request (not for each individua zone).  The final
 35  
  * repsonse will have some combination of "script", "scripts", "stylesheets", "content" (which is expected to be blank)
 36  
  * and "zones".
 37  
  *
 38  
  * @since 5.1.0.1
 39  
  */
 40  2
 public class MultiZoneUpdateEventResultProcessor implements ComponentEventResultProcessor<MultiZoneUpdate>
 41  
 {
 42  
     private final PageRenderQueue queue;
 43  
 
 44  
     private final TypeCoercer typeCoercer;
 45  
 
 46  
     public MultiZoneUpdateEventResultProcessor(PageRenderQueue queue, TypeCoercer typeCoercer)
 47  2
     {
 48  2
         this.queue = queue;
 49  2
         this.typeCoercer = typeCoercer;
 50  2
     }
 51  
 
 52  
     public void processResultValue(final MultiZoneUpdate value) throws IOException
 53  
     {
 54  
         // There has to be at least a single command in the queue to force a render.
 55  2
         queue.initializeForPartialPageRender(new RenderCommand()
 56  
         {
 57  2
             public void render(MarkupWriter writer, RenderQueue queue)
 58  
             {
 59  2
             }
 60  
         });
 61  
 
 62  2
         queue.addPartialMarkupRendererFilter(new SetupZonesFilter());
 63  
 
 64  2
         Map<String, Object> map = value.getZoneToRenderMap();
 65  
 
 66  2
         for (String zoneId : map.keySet())
 67  
         {
 68  6
             Object provided = map.get(zoneId);
 69  
 
 70  6
             RenderCommand zoneRenderCommand = toRenderer(zoneId, provided);
 71  
 
 72  6
             queue.addPartialMarkupRendererFilter(new SingleZonePartialRendererFilter(zoneId, zoneRenderCommand, queue));
 73  6
         }
 74  2
     }
 75  
 
 76  
     private RenderCommand toRenderer(String zoneId, Object provided)
 77  
     {
 78  
         try
 79  
         {
 80  6
             return typeCoercer.coerce(provided, RenderCommand.class);
 81  
         }
 82  0
         catch (Exception ex)
 83  
         {
 84  0
             throw new IllegalArgumentException(String.format("Failure converting renderer for zone '%s': %s",
 85  
                                                              zoneId,
 86  
                                                              InternalUtils.toMessage(ex)), ex);
 87  
         }
 88  
     }
 89  
 }