001: /*
002: * $Id: SynapseImpl.java,v 1.2 2002/09/16 08:05:02 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.brain;
011:
012: import java.util.Enumeration;
013: import java.util.ArrayList;
014: import anvil.java.util.Hashlist;
015: import anvil.script.Namespace;
016: import anvil.java.util.BindingEnumeration;
017: import anvil.core.Any;
018: import anvil.server.OperationFailedException;
019:
020: /**
021: * class SynapseImpl
022: *
023: * @author: Jani Lehtimäki
024: */
025: public class SynapseImpl {
026:
027: protected Synapse _facade;
028: protected Brain _brain;
029: protected long _id;
030: protected String _name;
031: protected String _type;
032: protected boolean _changed = false;
033: protected Hashlist _variables = new Hashlist();
034: protected Hashlist _dimensions = new Hashlist();
035:
036: public SynapseImpl(Brain brain, long id, String type, String name) {
037: _brain = brain;
038: _id = id;
039: _name = name;
040: _type = type;
041: }
042:
043: public Synapse getFacade() {
044: return _facade;
045: }
046:
047: public void setFacade(Synapse facade) {
048: _facade = facade;
049: }
050:
051: public boolean isChanged() {
052: return _changed;
053: }
054:
055: public Brain getBrain() {
056: return _brain;
057: }
058:
059: public long getId() {
060: return _id;
061: }
062:
063: public String getType() {
064: return _type;
065: }
066:
067: public String getName() {
068: return _name;
069: }
070:
071: public void setType(String type) {
072: _type = type;
073: _changed = true;
074: }
075:
076: public void setName(String name) {
077: _name = name;
078: _changed = true;
079: }
080:
081: public BindingEnumeration getVariables() {
082: return _variables.keysAndElements();
083: }
084:
085: public Any getVariable(String name) {
086: Any value = (Any) _variables.get(name);
087: return value != null ? value : Any.UNDEFINED;
088: }
089:
090: public Any setVariable(String name, Any value) {
091: _variables.put(name, value);
092: _changed = true;
093: return value;
094: }
095:
096: public Any checkVariable(String name) {
097: return getVariable(name);
098: }
099:
100: public boolean deleteVariable(String name) {
101: if (_variables.remove(name) != null) {
102: _changed = true;
103: return true;
104: } else {
105: return false;
106: }
107: }
108:
109: public Enumeration getDimensions() {
110: return _dimensions.elements();
111: }
112:
113: public Dimension getDimension(String name) {
114: return (Dimension) _dimensions.get(name);
115: }
116:
117: public synchronized Dimension getOrCreateDimension(String name) {
118: Dimension dim = (Dimension) _dimensions.get(name);
119: if (dim == null) {
120: dim = new Dimension(this , name, true);
121: _dimensions.put(name, dim);
122: _changed = true;
123: }
124: return dim;
125: }
126:
127: public synchronized Dimension createDimension(String name) {
128: Dimension dim = new Dimension(this , name, true);
129: _dimensions.put(name, dim);
130: _changed = true;
131: return dim;
132: }
133:
134: void addDimension(Dimension dimension) {
135: _dimensions.put(dimension.getName(), dimension);
136: _changed = true;
137: }
138:
139: public synchronized Dimension removeDimension(String name) {
140: Dimension dimension = (Dimension) _dimensions.remove(name);
141: if (dimension != null) {
142: _changed = true;
143: }
144: return dimension;
145: }
146:
147: public synchronized void remove() {
148: _variables.clear();
149: Enumeration e = _dimensions.elements();
150: while (e.hasMoreElements()) {
151: Dimension dim = (Dimension) e.nextElement();
152: dim.remove();
153: }
154: }
155:
156: public synchronized void commit() throws OperationFailedException {
157: try {
158: _brain.save(this );
159: _changed = false;
160: } catch (Throwable t) {
161: throw new OperationFailedException(t);
162: }
163: }
164: }
165:
166: /*
167:
168: */
|