Coverage Report - org.apache.myfaces.application.TreeStructureManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeStructureManager
0%
0/51
0%
0/26
2.25
TreeStructureManager$TreeStructComponent
0%
0/14
N/A
2.25
 
 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  
 package org.apache.myfaces.application;
 20  
 
 21  
 import org.apache.myfaces.shared.util.ClassUtils;
 22  
 
 23  
 import javax.faces.component.UIComponent;
 24  
 import javax.faces.component.UIViewRoot;
 25  
 import javax.faces.context.FacesContext;
 26  
 
 27  
 import java.io.Serializable;
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * @author Manfred Geiler (latest modification by $Author$)
 34  
  * @version $Revision$ $Date$
 35  
  */
 36  
 public class TreeStructureManager
 37  
 {
 38  
     //private static final Log log = LogFactory.getLog(TreeStructureManager.class);
 39  
 
 40  
     //private FacesContext _facesContext;
 41  
 
 42  
     public TreeStructureManager()
 43  0
     {
 44  
         //_facesContext = facesContext;
 45  0
     }
 46  
 
 47  
     public Object buildTreeStructureToSave(UIViewRoot viewRoot)
 48  
     {
 49  0
         return internalBuildTreeStructureToSave(viewRoot);
 50  
     }
 51  
 
 52  
     private TreeStructComponent internalBuildTreeStructureToSave(UIComponent component)
 53  
     {
 54  0
         TreeStructComponent structComp = new TreeStructComponent(component.getClass().getName(),
 55  
                                                                  component.getId());
 56  
 
 57  
         //children
 58  0
         if (component.getChildCount() > 0)
 59  
         {
 60  0
             List<TreeStructComponent> structChildList = new ArrayList<TreeStructComponent>();
 61  0
             for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
 62  
             {
 63  0
                 UIComponent child = component.getChildren().get(i);
 64  0
                 if (!child.isTransient())
 65  
                 {
 66  0
                     TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
 67  0
                     structChildList.add(structChild);
 68  
                 }
 69  
             }
 70  
             
 71  0
             TreeStructComponent[] childArray = structChildList.toArray(new TreeStructComponent[structChildList.size()]);
 72  0
             structComp.setChildren(childArray);
 73  
         }
 74  
 
 75  
         //facets
 76  
         
 77  0
         if (component.getFacetCount() > 0)
 78  
         {
 79  0
             Map<String, UIComponent> facetMap = component.getFacets();
 80  0
             List<Object[]> structFacetList = new ArrayList<Object[]>();
 81  0
             for (Map.Entry<String, UIComponent> entry : facetMap.entrySet())
 82  
             {
 83  0
                 UIComponent child = entry.getValue();
 84  0
                 if (!child.isTransient())
 85  
                 {
 86  0
                     String facetName = entry.getKey();
 87  0
                     TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
 88  0
                     structFacetList.add(new Object[] {facetName, structChild});
 89  
                 }
 90  0
             }
 91  
             
 92  0
             Object[] facetArray = structFacetList.toArray(new Object[structFacetList.size()]);
 93  0
             structComp.setFacets(facetArray);
 94  
         }
 95  
 
 96  0
         return structComp;
 97  
     }
 98  
 
 99  
 
 100  
     public UIViewRoot restoreTreeStructure(Object treeStructRoot)
 101  
     {
 102  0
         if (treeStructRoot instanceof TreeStructComponent)
 103  
         {
 104  0
             return (UIViewRoot)internalRestoreTreeStructure((TreeStructComponent)treeStructRoot, true);
 105  
         }
 106  
         
 107  
         
 108  0
         throw new IllegalArgumentException("TreeStructure of type " + treeStructRoot.getClass().getName() + 
 109  
                                            " is not supported.");
 110  
         
 111  
     }
 112  
 
 113  
     private UIComponent internalRestoreTreeStructure(TreeStructComponent treeStructComp, boolean checkViewRoot)
 114  
     {
 115  0
         String compClass = treeStructComp.getComponentClass();
 116  0
         String compId = treeStructComp.getComponentId();
 117  0
         UIComponent component = (UIComponent)ClassUtils.newInstance(compClass);
 118  0
         component.setId(compId);
 119  
 
 120  0
         if (checkViewRoot && component instanceof UIViewRoot)
 121  
         {
 122  0
             FacesContext.getCurrentInstance().setViewRoot((UIViewRoot) component);
 123  
         }
 124  
         //children
 125  0
         TreeStructComponent[] childArray = treeStructComp.getChildren();
 126  0
         if (childArray != null)
 127  
         {
 128  0
             List<UIComponent> childList = component.getChildren();
 129  0
             for (int i = 0, len = childArray.length; i < len; i++)
 130  
             {
 131  0
                 UIComponent child = internalRestoreTreeStructure(childArray[i], false);
 132  0
                 childList.add(child);
 133  
             }
 134  
         }
 135  
 
 136  
         //facets
 137  0
         Object[] facetArray = treeStructComp.getFacets();
 138  0
         if (facetArray != null)
 139  
         {
 140  0
             Map<String, UIComponent> facetMap = component.getFacets();
 141  0
             for (int i = 0, len = facetArray.length; i < len; i++)
 142  
             {
 143  0
                 Object[] tuple = (Object[])facetArray[i];
 144  0
                 String facetName = (String)tuple[0];
 145  0
                 TreeStructComponent structChild = (TreeStructComponent)tuple[1];
 146  0
                 UIComponent child = internalRestoreTreeStructure(structChild, false);
 147  0
                 facetMap.put(facetName, child);
 148  
             }
 149  
         }
 150  
 
 151  0
         return component;
 152  
     }
 153  
 
 154  
 
 155  
     public static class TreeStructComponent
 156  
             implements Serializable
 157  
     {
 158  
         private static final long serialVersionUID = 5069109074684737231L;
 159  
         private String _componentClass;
 160  
         private String _componentId;
 161  0
         private TreeStructComponent[] _children = null;    // Array of children
 162  0
         private Object[] _facets = null;            // Array of Array-tuples with Facetname and TreeStructComponent
 163  
 
 164  
         TreeStructComponent(String componentClass, String componentId)
 165  0
         {
 166  0
             _componentClass = componentClass;
 167  0
             _componentId = componentId;
 168  0
         }
 169  
 
 170  
         public String getComponentClass()
 171  
         {
 172  0
             return _componentClass;
 173  
         }
 174  
 
 175  
         public String getComponentId()
 176  
         {
 177  0
             return _componentId;
 178  
         }
 179  
 
 180  
         void setChildren(TreeStructComponent[] children)
 181  
         {
 182  0
             _children = children;
 183  0
         }
 184  
 
 185  
         TreeStructComponent[] getChildren()
 186  
         {
 187  0
             return _children;
 188  
         }
 189  
 
 190  
         Object[] getFacets()
 191  
         {
 192  0
             return _facets;
 193  
         }
 194  
 
 195  
         void setFacets(Object[] facets)
 196  
         {
 197  0
             _facets = facets;
 198  0
         }
 199  
     }
 200  
 
 201  
 }