1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.wotonomy.web; |
20 |
|
|
21 |
|
import java.util.Collection; |
22 |
|
import java.util.Iterator; |
23 |
|
|
24 |
|
import net.wotonomy.foundation.NSDictionary; |
25 |
|
|
26 |
|
public class WORepetition extends WODynamicElement { |
27 |
|
|
28 |
|
protected int count; |
29 |
|
protected int index; |
30 |
|
protected Object item; |
31 |
|
protected Collection list; |
32 |
|
protected Iterator iterator; |
33 |
|
|
34 |
|
protected WORepetition() { |
35 |
0 |
super(); |
36 |
0 |
} |
37 |
|
|
38 |
|
public WORepetition(String aName, NSDictionary aMap, WOElement template) { |
39 |
0 |
super(aName, aMap, template); |
40 |
0 |
} |
41 |
|
|
42 |
|
public void setCount(int value) { |
43 |
0 |
count = value; |
44 |
0 |
} |
45 |
|
|
46 |
|
public int count() { |
47 |
0 |
return count; |
48 |
|
} |
49 |
|
|
50 |
|
public void setIndex(int value) { |
51 |
0 |
index = value; |
52 |
0 |
} |
53 |
|
public int index() { |
54 |
0 |
return index; |
55 |
|
} |
56 |
|
|
57 |
|
public void setItem(Object value) { |
58 |
0 |
item = value; |
59 |
0 |
} |
60 |
|
public Object item() { |
61 |
0 |
return item; |
62 |
|
} |
63 |
|
|
64 |
|
public void setList(Collection value) { |
65 |
0 |
list = value; |
66 |
0 |
} |
67 |
|
public Collection list() { |
68 |
0 |
return list; |
69 |
|
} |
70 |
|
|
71 |
|
public void takeValuesFromRequest(WORequest r, WOContext c) { |
72 |
0 |
c.appendZeroElementIDComponent(); |
73 |
0 |
pullValuesFromParent(c.component()); |
74 |
0 |
index = 0; |
75 |
0 |
WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count"); |
76 |
0 |
item = getNextItem(); |
77 |
0 |
while (item != null) { |
78 |
0 |
pushValuesToParent(c.component()); |
79 |
0 |
rootElement.takeValuesFromRequest(r, c); |
80 |
0 |
index++; |
81 |
0 |
c.incrementLastElementIDComponent(); |
82 |
0 |
if (countAsoc != null && index >= count()) |
83 |
0 |
item = null; |
84 |
|
else |
85 |
0 |
item = getNextItem(); |
86 |
0 |
} |
87 |
0 |
iterator = null; |
88 |
0 |
c.deleteLastElementIDComponent(); |
89 |
0 |
} |
90 |
|
|
91 |
|
public WOActionResults invokeAction(WORequest r, WOContext c) { |
92 |
0 |
c.appendZeroElementIDComponent(); |
93 |
0 |
pullValuesFromParent(c.component()); |
94 |
0 |
index = 0; |
95 |
0 |
WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count"); |
96 |
0 |
item = getNextItem(); |
97 |
0 |
while (item != null) { |
98 |
0 |
pushValuesToParent(c.component()); |
99 |
0 |
WOActionResults e = rootElement.invokeAction(r, c); |
100 |
0 |
if (e != null) |
101 |
|
{ |
102 |
0 |
iterator = null; |
103 |
0 |
return e; |
104 |
|
} |
105 |
0 |
index++; |
106 |
0 |
c.incrementLastElementIDComponent(); |
107 |
0 |
if (countAsoc != null && index >= count()) |
108 |
0 |
item = null; |
109 |
|
else |
110 |
0 |
item = getNextItem(); |
111 |
0 |
} |
112 |
0 |
iterator = null; |
113 |
0 |
c.deleteLastElementIDComponent(); |
114 |
0 |
return null; |
115 |
|
} |
116 |
|
|
117 |
|
public void appendToResponse(WOResponse r, WOContext c) { |
118 |
0 |
c.appendZeroElementIDComponent(); |
119 |
0 |
pullValuesFromParent(c.component()); |
120 |
0 |
index = 0; |
121 |
0 |
WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count"); |
122 |
0 |
item = getNextItem(); |
123 |
0 |
while (item != null) { |
124 |
0 |
pushValuesToParent(c.component()); |
125 |
0 |
rootElement.appendToResponse(r, c); |
126 |
0 |
index++; |
127 |
0 |
c.incrementLastElementIDComponent(); |
128 |
0 |
if (countAsoc != null || index < count()) |
129 |
0 |
item = null; |
130 |
|
else |
131 |
0 |
item = getNextItem(); |
132 |
0 |
} |
133 |
0 |
iterator = null; |
134 |
0 |
c.deleteLastElementIDComponent(); |
135 |
0 |
} |
136 |
|
|
137 |
|
protected Object getNextItem() { |
138 |
0 |
if ( iterator == null ) |
139 |
|
{ |
140 |
0 |
if ( list == null ) return null; |
141 |
0 |
iterator = list.iterator(); |
142 |
|
} |
143 |
0 |
if ( iterator.hasNext() ) |
144 |
|
{ |
145 |
0 |
return iterator.next(); |
146 |
|
} |
147 |
0 |
return null; |
148 |
|
} |
149 |
|
|
150 |
|
protected void pullValuesFromParent(WOComponent c) { |
151 |
|
Object value; |
152 |
|
|
153 |
0 |
value = valueForProperty("count", c); |
154 |
0 |
if ( value instanceof Number ) |
155 |
|
{ |
156 |
0 |
setCount( ((Number)value).intValue() ); |
157 |
0 |
} |
158 |
|
else |
159 |
|
{ |
160 |
0 |
setCount( -1 ); |
161 |
|
} |
162 |
|
|
163 |
0 |
value = valueForProperty("list", c); |
164 |
0 |
if ( value instanceof Collection ) |
165 |
|
{ |
166 |
0 |
setList( (Collection) value ); |
167 |
0 |
} |
168 |
|
else |
169 |
|
{ |
170 |
0 |
setList( null ); |
171 |
|
} |
172 |
0 |
} |
173 |
|
|
174 |
|
protected void pushValuesToParent(WOComponent c) { |
175 |
0 |
setValueForProperty("index", new Integer(index), c); |
176 |
0 |
setValueForProperty("item", item, c); |
177 |
0 |
} |
178 |
|
|
179 |
|
} |