2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: AssignChildrenCommand.java 516834 2007-03-11 01:33:36Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import java.util.Iterator;
24  
25  import javax.faces.component.UIComponent;
26  
27  import org.apache.commons.chain.Catalog;
28  import org.apache.commons.chain.Command;
29  import org.apache.commons.chain.Context;
30  import org.apache.shale.clay.component.Clay;
31  import org.apache.shale.clay.config.Globals;
32  import org.apache.shale.clay.config.beans.ComponentBean;
33  
34  /***
35   * <p>
36   * Iterates over the child {@link org.apache.shale.clay.config.beans.ElementBean} collection of a
37   * {@link ComponentBean}, and invokes the {@link CreateComponentCommand} for
38   * each.
39   * <p>
40   */
41  public class AssignChildrenCommand extends AbstractCommand {
42  
43      /***
44       * <p>
45       * This {@link Command} iterates over the {@link ComponentBean}
46       * <code>children</code> collection and invokes the
47       * <code>Globals.ADD_COMPONENT_COMMAND_NAME</code> for each
48       * {@link org.apache.shale.clay.config.beans.ElementBean}.
49       * </p>
50       *
51       * @param context commons chains
52       * @return <code>true</code> if the chain is complete and should stop
53       * @exception Exception propagated up to the top of the chain
54       */
55      public boolean execute(Context context) throws Exception {
56  
57          boolean isFinal = false;
58  
59          ClayContext clayContext = (ClayContext) context;
60          if (clayContext == null) {
61              throw new NullPointerException(getMessages()
62                      .getMessage("clay.null.clayContext"));
63          }
64  
65          UIComponent parent = (UIComponent) clayContext.getChild();
66          if (parent == null) {
67              throw new NullPointerException(getMessages()
68                      .getMessage("clay.null.childComponent"));
69          }
70  
71          ComponentBean displayElement = clayContext.getDisplayElement();
72          if (displayElement == null) {
73              throw new NullPointerException(getMessages()
74                      .getMessage("clay.null.componentBean"));
75          }
76  
77          Iterator vi = displayElement.getChildrenIterator();
78  
79          int childIndex = 0;
80          while (vi.hasNext()) {
81              ComponentBean childDisplayElement = (ComponentBean) vi.next();
82  
83              ClayContext subContext = (ClayContext) clayContext.clone();
84              subContext.setDisplayElement(childDisplayElement);
85              subContext.setParent(parent);
86              subContext.setChild(null);
87              subContext.setChildIndex(childIndex);
88              subContext.setJspIds(clayContext.getJspIds());
89  
90              Catalog catalog = getCatalog();
91              Command command = catalog
92                      .getCommand(Globals.ADD_COMPONENT_COMMAND_NAME);
93              command.execute(subContext);
94  
95              UIComponent child = (UIComponent) subContext.getChild();
96  
97              // Increment the index if the new component is not a facet
98              if (parent.getChildren().contains(child)) {
99                  ++childIndex;
100             }
101         }
102         
103         //clay manages its own subtree; components that
104         //renders children still do not build their own
105         //composition.  clay does both, build its own subtree
106         //and renders its own children.
107         
108         if (!(parent instanceof Clay)) {
109             // remove any components not represented by the metadata graph
110             for (int i = parent.getChildCount() - 1; i > -1; i--) {
111                 UIComponent child = (UIComponent) parent.getChildren().get(i);
112                 Long jspId = (Long) child.getAttributes().get(
113                         Globals.CLAY_JSPID_ATTRIBUTE);
114                 if (jspId != null
115                         && !clayContext.getJspIds().contains(jspId)) {
116                     parent.getChildren().remove(i);
117                     child.setParent(null);
118                 }
119             }
120         }
121        
122         
123         return isFinal;
124     }
125 
126 }
127