/* 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 { 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; } 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(); } 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; } 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); } }