2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
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.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
98 if (parent.getChildren().contains(child)) {
99 ++childIndex;
100 }
101 }
102
103
104
105
106
107
108 if (!(parent instanceof Clay)) {
109
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