View Javadoc

1   /*
2    Wotonomy: OpenStep design patterns for pure Java applications.
3    Copyright (C) 2000 Blacksmith, Inc.
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, see http://www.gnu.org
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          super();
36      }
37  
38      public WORepetition(String aName, NSDictionary aMap, WOElement template) {
39          super(aName, aMap, template);
40      }
41  
42      public void setCount(int value) {
43          count = value;
44      }
45      
46      public int count() {
47          return count;
48      }
49  
50      public void setIndex(int value) {
51          index = value;
52      }
53      public int index() {
54          return index;
55      }
56  
57      public void setItem(Object value) {
58          item = value;
59      }
60      public Object item() {
61          return item;
62      }
63  
64      public void setList(Collection value) {
65          list = value;
66      }
67      public Collection list() {
68          return list;
69      }
70  
71      public void takeValuesFromRequest(WORequest r, WOContext c) {
72          c.appendZeroElementIDComponent();
73          pullValuesFromParent(c.component());
74          index = 0;
75          WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count");
76          item = getNextItem();
77          while (item != null) {
78              pushValuesToParent(c.component());
79              rootElement.takeValuesFromRequest(r, c);
80              index++;
81              c.incrementLastElementIDComponent();
82              if (countAsoc != null && index >= count())
83                  item = null;
84              else
85                  item = getNextItem();
86          }
87          iterator = null;
88          c.deleteLastElementIDComponent();
89      }
90  
91      public WOActionResults invokeAction(WORequest r, WOContext c) {
92          c.appendZeroElementIDComponent();
93          pullValuesFromParent(c.component());
94          index = 0;
95          WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count");
96          item = getNextItem();
97          while (item != null) {
98              pushValuesToParent(c.component());
99              WOActionResults e = rootElement.invokeAction(r, c);
100             if (e != null)
101             {
102                 iterator = null;
103                 return e;
104             }
105             index++;
106             c.incrementLastElementIDComponent();
107             if (countAsoc != null && index >= count())
108                 item = null;
109             else
110                 item = getNextItem();
111         }
112         iterator = null;
113         c.deleteLastElementIDComponent();
114         return null;
115     }
116 
117     public void appendToResponse(WOResponse r, WOContext c) {
118         c.appendZeroElementIDComponent();
119         pullValuesFromParent(c.component());
120         index = 0;
121         WOAssociation countAsoc = (WOAssociation)associations.objectForKey("count");
122         item = getNextItem();
123         while (item != null) {
124             pushValuesToParent(c.component());
125             rootElement.appendToResponse(r, c);
126             index++;
127             c.incrementLastElementIDComponent();
128             if (countAsoc != null || index < count())
129                 item = null;
130             else
131                 item = getNextItem();
132         }
133         iterator = null;
134         c.deleteLastElementIDComponent();
135     }
136 
137     protected Object getNextItem() {
138         if ( iterator == null )
139         {
140             if ( list == null ) return null;
141             iterator = list.iterator();
142         }
143         if ( iterator.hasNext() ) 
144         {
145             return iterator.next();
146         }
147         return null;
148     }
149 
150     protected void pullValuesFromParent(WOComponent c) {
151         Object value;
152         
153         value = valueForProperty("count", c);
154         if ( value instanceof Number )
155         {
156             setCount( ((Number)value).intValue() );
157         }
158         else
159         {
160             setCount( -1 );
161         }
162         
163         value = valueForProperty("list", c);
164         if ( value instanceof Collection )
165         {
166             setList( (Collection) value );
167         }
168         else
169         {
170             setList( null );
171         }
172     }
173 
174     protected void pushValuesToParent(WOComponent c) {
175         setValueForProperty("index", new Integer(index), c);
176         setValueForProperty("item", item, c);
177     }
178 
179 }