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 464373 2006-10-16 04:21:54Z rahul $
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.config.Globals;
31  import org.apache.shale.clay.config.beans.ComponentBean;
32  
33  /***
34   * <p>
35   * Iterates over the child {@link org.apache.shale.clay.config.beans.ElementBean} collection of a
36   * {@link ComponentBean}, and invokes the {@link CreateComponentCommand} for
37   * each.
38   * <p>
39   */
40  public class AssignChildrenCommand extends AbstractCommand {
41  
42      /***
43       * <p>
44       * This {@link Command} iterates over the {@link ComponentBean}
45       * <code>children</code> collection and invokes the
46       * <code>Globals.ADD_COMPONENT_COMMAND_NAME</code> for each
47       * {@link org.apache.shale.clay.config.beans.ElementBean}.
48       * </p>
49       *
50       * @param context commons chains
51       * @return <code>true</code> if the chain is complete and should stop
52       * @exception Exception propagated up to the top of the chain
53       */
54      public boolean execute(Context context) throws Exception {
55  
56          boolean isFinal = false;
57  
58          ClayContext clayContext = (ClayContext) context;
59          if (clayContext == null) {
60              throw new NullPointerException(getMessages()
61                      .getMessage("clay.null.clayContext"));
62          }
63  
64          UIComponent parent = (UIComponent) clayContext.getChild();
65          if (parent == null) {
66              throw new NullPointerException(getMessages()
67                      .getMessage("clay.null.childComponent"));
68          }
69  
70          ComponentBean displayElement = clayContext.getDisplayElement();
71          if (displayElement == null) {
72              throw new NullPointerException(getMessages()
73                      .getMessage("clay.null.componentBean"));
74          }
75  
76          Iterator vi = displayElement.getChildrenIterator();
77  
78          int childIndex = 0;
79          while (vi.hasNext()) {
80              ComponentBean childDisplayElement = (ComponentBean) vi.next();
81  
82              ClayContext subContext = (ClayContext) clayContext.clone();
83              subContext.setDisplayElement(childDisplayElement);
84              subContext.setParent(parent);
85              subContext.setChild(null);
86              subContext.setChildIndex(childIndex);
87  
88              Catalog catalog = getCatalog();
89              Command command = catalog
90                      .getCommand(Globals.ADD_COMPONENT_COMMAND_NAME);
91              command.execute(subContext);
92  
93              UIComponent child = (UIComponent) subContext.getChild();
94  
95              // Increment the index if the new component is not a facet
96              if (parent.getChildren().contains(child)) {
97                  ++childIndex;
98              }
99          }
100 
101         return isFinal;
102     }
103 
104 }
105