/*
* $Id: Dimension.java,v 1.3 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.brain;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import anvil.server.OperationFailedException;
/**
* class Dimension
*
* @author: Jani Lehtim�ki
*/
public class Dimension
{
protected SynapseImpl _synapse;
protected String _name;
protected ArrayList _dim = new ArrayList();
protected boolean _changed = false;
public Dimension(SynapseImpl synapse, String name, boolean isnew)
{
_synapse = synapse;
_name = name;
_changed = isnew;
}
public synchronized String toString()
{
StringBuffer buf = new StringBuffer();
buf.append("Dimension(");
buf.append(_synapse.getId());
buf.append(":");
int n =_dim.size();
for(int i=0; i<n; i++) {
if (i>0) {
buf.append(", ");
}
buf.append(((Synapse)_dim.get(i)).getId());
}
buf.append(')');
return buf.toString();
}
SynapseImpl getSynapseImpl()
{
return _synapse;
}
public Synapse getSynapse()
{
return _synapse.getFacade();
}
public boolean isChanged()
{
return _changed;
}
public String getName()
{
return _name;
}
public int size()
{
return _dim.size();
}
public synchronized Synapse get(int index)
{
if (index>=0 && index<_dim.size()) {
return (Synapse)_dim.get(index);
}
return null;
}
public synchronized Synapse set(int index, Synapse synapse)
{
Synapse oldsynapse = null;
if (index>=0 && index<_dim.size()) {
_changed = true;
oldsynapse = (Synapse)_dim.set(index, synapse);
if (oldsynapse != null) {
oldsynapse.removeReferer(this);
}
synapse.addReferer(this);
}
return oldsynapse;
}
public synchronized Synapse add(Synapse synapse)
{
_dim.add(synapse);
synapse.addReferer(this);
_changed = true;
return synapse;
}
public synchronized Synapse insert(int index, Synapse synapse)
{
if (index>=0 && index<_dim.size()) {
_dim.add(index, synapse);
} else {
_dim.add(synapse);
}
synapse.addReferer(this);
_changed = true;
return synapse;
}
public synchronized Synapse remove(int index)
{
Synapse synapse = null;
if (index>=0 && index<_dim.size()) {
synapse = (Synapse)_dim.remove(index);
if (synapse != null) {
synapse.removeReferer(this);
}
}
_changed = true;
return synapse;
}
public synchronized void remove(Synapse synapse)
{
boolean unlinked = false;
int size = _dim.size();
for(int i=size-1; i>=0; i--) {
Synapse syn = (Synapse)_dim.get(i);
if (syn.equals(synapse)) {
_dim.remove(i);
if (!unlinked) {
syn.removeReferer(this);
unlinked = true;
}
_changed = true;
}
}
}
public Enumeration elements()
{
return new Enumeration() {
private int _index;
public boolean hasMoreElements()
{
return _index < _dim.size();
}
public Object nextElement()
{
synchronized(Dimension.this) {
if (_index < _dim.size()) {
return _dim.get(_index++);
} else {
return null;
}
}
}
};
}
public synchronized void clear()
{
int size = _dim.size();
for(int i=0; i<size; i++) {
Synapse synapse = (Synapse)_dim.get(i);
synapse.removeReferer(this);
}
_dim.clear();
_changed = true;
}
public synchronized void remove()
{
clear();
_synapse.removeDimension(_name);
}
public synchronized void commit()
throws OperationFailedException
{
try {
_synapse.getBrain().save(this);
_changed = false;
} catch (Throwable t) {
throw new OperationFailedException(t);
}
}
synchronized void commit(java.sql.Connection conn)
throws SQLException
{
_synapse.getBrain().save(conn, _synapse, this);
_changed = false;
}
}