summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WORepetition.java
blob: 607ab97cf8f7bc84cfa1b23c7841f980fa357792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 Wotonomy: OpenStep design patterns for pure Java applications.
 Copyright (C) 2000 Blacksmith, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, see http://www.gnu.org
 */

package net.wotonomy.web;

import java.util.Collection;
import java.util.Iterator;

import net.wotonomy.foundation.NSDictionary;

public class WORepetition extends WODynamicElement {
	private static final long serialVersionUID = -8039846464450349317L;

	protected int count;
	protected int index;
	protected Object item;
	protected Collection list;
	protected Iterator iterator;

	protected WORepetition() {
		super();
	}

	public WORepetition(String aName, NSDictionary aMap, WOElement template) {
		super(aName, aMap, template);
	}

	public void setCount(int value) {
		count = value;
	}

	public int count() {
		return count;
	}

	public void setIndex(int value) {
		index = value;
	}

	public int index() {
		return index;
	}

	public void setItem(Object value) {
		item = value;
	}

	public Object item() {
		return item;
	}

	public void setList(Collection value) {
		list = value;
	}

	public Collection list() {
		return list;
	}

	@Override
	public void takeValuesFromRequest(WORequest r, WOContext c) {
		c.appendZeroElementIDComponent();
		pullValuesFromParent(c.component());
		index = 0;
		WOAssociation countAsoc = (WOAssociation) associations.objectForKey("count");
		item = getNextItem();
		while (item != null) {
			pushValuesToParent(c.component());
			rootElement.takeValuesFromRequest(r, c);
			index++;
			c.incrementLastElementIDComponent();
			if (countAsoc != null && index >= count())
				item = null;
			else
				item = getNextItem();
		}
		iterator = null;
		c.deleteLastElementIDComponent();
	}

	@Override
	public WOActionResults invokeAction(WORequest r, WOContext c) {
		c.appendZeroElementIDComponent();
		pullValuesFromParent(c.component());
		index = 0;
		WOAssociation countAsoc = (WOAssociation) associations.objectForKey("count");
		item = getNextItem();
		while (item != null) {
			pushValuesToParent(c.component());
			WOActionResults e = rootElement.invokeAction(r, c);
			if (e != null) {
				iterator = null;
				return e;
			}
			index++;
			c.incrementLastElementIDComponent();
			if (countAsoc != null && index >= count())
				item = null;
			else
				item = getNextItem();
		}
		iterator = null;
		c.deleteLastElementIDComponent();
		return null;
	}

	@Override
	public void appendToResponse(WOResponse r, WOContext c) {
		c.appendZeroElementIDComponent();
		pullValuesFromParent(c.component());
		index = 0;
		WOAssociation countAsoc = (WOAssociation) associations.objectForKey("count");
		item = getNextItem();
		while (item != null) {
			pushValuesToParent(c.component());
			rootElement.appendToResponse(r, c);
			index++;
			c.incrementLastElementIDComponent();
			if (countAsoc != null || index < count())
				item = null;
			else
				item = getNextItem();
		}
		iterator = null;
		c.deleteLastElementIDComponent();
	}

	protected Object getNextItem() {
		if (iterator == null) {
			if (list == null)
				return null;
			iterator = list.iterator();
		}
		if (iterator.hasNext()) {
			return iterator.next();
		}
		return null;
	}

	protected void pullValuesFromParent(WOComponent c) {
		Object value;

		value = valueForProperty("count", c);
		if (value instanceof Number) {
			setCount(((Number) value).intValue());
		} else {
			setCount(-1);
		}

		value = valueForProperty("list", c);
		if (value instanceof Collection) {
			setList((Collection) value);
		} else {
			setList(null);
		}
	}

	protected void pushValuesToParent(WOComponent c) {
		setValueForProperty("index", new Integer(index), c);
		setValueForProperty("item", item, c);
	}

}