1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
96 if (parent.getChildren().contains(child)) {
97 ++childIndex;
98 }
99 }
100
101 return isFinal;
102 }
103
104 }
105