/
area/
classes/net/sourceforge/pain/logic/
classes/net/sourceforge/pain/logic/event/
classes/net/sourceforge/pain/logic/fn/util/
classes/net/sourceforge/pain/network/console/
classes/net/sourceforge/pain/plugin/
classes/net/sourceforge/pain/plugin/reset/
classes/net/sourceforge/pain/plugin/shutdown/
classes/net/sourceforge/pain/plugin/social/
classest/net/sourceforge/pain/db/data/
doc/
doc/paindb/resources/
src/net/sourceforge/pain/logic/
src/net/sourceforge/pain/logic/event/
src/net/sourceforge/pain/logic/fn/util/
src/net/sourceforge/pain/network/console/
src/net/sourceforge/pain/network/console/telnet/
src/net/sourceforge/pain/plugin/
src/net/sourceforge/pain/plugin/command/
src/net/sourceforge/pain/plugin/reset/
src/net/sourceforge/pain/plugin/shutdown/
src/net/sourceforge/pain/plugin/social/
src/net/sourceforge/pain/util/
tests/
tests/net/sourceforge/pain/db/data/
package net.sourceforge.pain.logic.event;

import net.sourceforge.pain.*;
import net.sourceforge.pain.util.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.data.prototype.*;
import net.sourceforge.pain.data.*;
import net.sourceforge.pain.db.*;
import net.sourceforge.pain.logic.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.logic.fn.factory.*;

import java.util.*;

/**
 * PAiN  Date: 14.04.2003  Time: 0:38:36
 */
public class ResetEvent extends AbstractEvent {
	/** warn: single thread model!
	 */
	private static final Set tmp_passedSpaces = new HashSet();
	private final static String DEFAULT_RESET_MESSAGE = "The earth rumbles with the after affects of another experience gone away...";

	public Object execute(Object param) throws Exception {
		ResetGroup g = (ResetGroup) param;
		String resetMessage = g.getResetMessage();
		Log.debug("ResetPlugin:Making reset on " + g.getGroupInfo());
		if (resetMessage == null) {
			resetMessage = DEFAULT_RESET_MESSAGE;
		}

		Set resets = g.getResets();
		PainDB db = Core.getDB();
		for (Iterator it = resets.iterator(); it.hasNext();) {
			final Reset r = (Reset) it.next();
			final SpaceReset sr = (SpaceReset) r.getRole(SpaceReset.class);
			final Space space = sr.getLocation();
			if (!tmp_passedSpaces.contains(space)) {
				tmp_passedSpaces.add(space);
				MessageOutFn.outSpace(space, resetMessage);
			}
			if (r.getLastResettedObject() != null) {
				continue;
			}
			// single reset done in transaction.
			// it will not agget to others if fail.
			DbTransaction t = new DbTransaction() {
				public Object execute(Object[] params) throws Exception {
					// too simple initial impl: only space resets supported (do not worry: ROM has no other resets types :)) )
					Prototype p = r.getResettedPrototype();
					final LogicalObject obj = GlobalFactory.createObject(p);
					final Located l = (Located) obj.getRole(Located.class);
					if (l == null) {
						throw new RuntimeException("Something wrong: space reset resets not located object!:" + obj);
					}
					RelocateFn.addToSpace(space, l);
					r.setLastResettedObject(obj);
					return null;
				}
			};
			try {
				db.execute(t);
			} catch (Exception e) {
				Log.error("ResetPlugin: something wrong!", e);
			}
		}
		tmp_passedSpaces.clear();
		return param;
	}
}