package org.opengroupware.jope.eoaccess;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opengroupware.jope.foundation.NSObject;

/*
 * EODatabaseOperation
 * 
 * TODO: document
 */
public class EODatabaseOperation extends NSObject {
  
  protected EOEntity            entity;
  protected Object              eo;
  protected int                 operator;
  protected Map<String, Object> dbSnapshot;
  protected Map<String, Object> newRow;
  
  protected List<EOAdaptorOperation> adaptorOperations;

  public EODatabaseOperation(Object _eo, EOEntity _entity) {
    this.entity = _entity;
    this.eo     = _eo;
  }
  
  /* accessors */
  
  public EOEntity entity() {
    return this.entity;
  }
  
  public Object object() {
    return this.eo;
  }

  public void setDatabaseOperator(int _op) {
    this.operator = _op;
  }
  public int databaseOperator() {
    return this.operator;
  }
  
  public void setDBSnapshot(Map<String, Object> _snap) {
    this.dbSnapshot = _snap;
  }
  public Map<String, Object> dbSnapshot() {
    return this.dbSnapshot;
  }
  
  public void setNewRow(Map<String, Object> _values) {
    this.newRow = _values;
  }
  public Map<String, Object> newRow() {
    return this.newRow;
  }
  
  /* mapped operations */
  
  public void addAdaptorOperation(EOAdaptorOperation _op) {
    if (_op == null) return;
    
    if (this.adaptorOperations == null)
      this.adaptorOperations = new ArrayList<EOAdaptorOperation>(1);
    
    this.adaptorOperations.add(_op);
  }
  public List<EOAdaptorOperation> adaptorOperations() {
    return this.adaptorOperations;
  }

  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    _d.append(" op=" + this.operator);
    
    if (this.entity != null)
      _d.append(" entity=" + this.entity.name());
    
    if (this.eo != null)
      _d.append(" eo=" + this.eo);
  }
}
