EntityRow.java :  » Database-ORM » jaxor-3.5 » net » sourceforge » jaxor » Java Open Source

Java Open Source » Database ORM » jaxor 3.5 
jaxor 3.5 » net » sourceforge » jaxor » EntityRow.java
/*
 * User: mrettig
 * Date: Jun 24, 2002
 * Time: 5:30:30 PM
 */
package net.sourceforge.jaxor;

import net.sourceforge.jaxor.api.FieldAdapter;
import net.sourceforge.jaxor.api.FieldLifeCycleListener;
import net.sourceforge.jaxor.api.JaxorContext;
import net.sourceforge.jaxor.api.ValueChangeListener;
import net.sourceforge.jaxor.util.ForeignFieldLazyLoader;
import net.sourceforge.jaxor.util.SystemException;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * This class represents the row data for a concrete instance of the
 * AbstractEntity.  All of the attributes defined in the Jaxor file
 * are managed by instances of this class.
 */

public class EntityRow implements Serializable {
    private FieldAdapter[] _list = new FieldAdapter[0];
    private final List  _foreignFields = new ArrayList();
    private final String _table;
    private PrimaryKeySet _pkset = new PrimaryKeySet(new FieldAdapter[0]);
    private List _updateMatchValues;
    private JaxorContext _context;

    public EntityRow(String table) {
        _table = table;
    }

    public void setJaxorContext(JaxorContext context){
        _context = context;
    }

    public JaxorContext getJaxorContext(){
        return _context;
    }

    public void setFields(FieldAdapter[] fields) {
        _list = fields;
    }

    public FieldAdapter[] getList() {
        return _list;
    }

    public void setPrimaryKey(PrimaryKeySet primaryKey) {
        _pkset = primaryKey;
    }


    public void validate() {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            fieldAdapter.validate();
        }
    }

    /**
     * This method allows a ValueChangeListener to be added to each of
     * the mappers in the row.
     *
     * @param listener the change listener to registerLoad
     */

    public void registerLoad(ValueChangeListener listener) {
        initUpdateMatchValues();
        register(listener);
    }

    public void register(ValueChangeListener listener) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            fieldAdapter.addChangeListener(listener);
        }
    }

    /**
     * This method allows a ValueChangeListener to be removed from each
     * of the mappers in the row.
     *
     * @param listener the change listener to remove
     */

    public void unregisterListener(ValueChangeListener listener) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter adapter = _list[i];
            adapter.removeChangeListener(listener);
        }
    }

    public String getTable() {
        return _table;
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer(_table);
        for (int i = 0; i < _list.length; i++) {
            buffer.append("\n");
            FieldAdapter entityField = _list[i];
            buffer.append(entityField.toString());
        }
        return buffer.toString();
    }

    public int hashCode() {
        return _table.hashCode();
    }

    public boolean equals(Object obj) {
        if (obj instanceof EntityRow)
            return equals((EntityRow) obj);
        return false;
    }

    public int size() {
        return _list.length;
    }

    public boolean equals(EntityRow otherRow) {
        if (this == otherRow)
            return true;

        if (size() != otherRow.size())
            return false;

        if (!getTable().equals(otherRow.getTable()))
            return false;

        for (int i = 0; i < _list.length; i++) {
            FieldAdapter entityField = _list[i];
            if (!entityField.equals(otherRow.getField(entityField.getName())))
                return false;
        }
        return true;
    }

    public FieldAdapter getField(String name) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter entityField = _list[i];
            if (entityField.getName().equals(name))
                return entityField;
        }
        return null;
    }

    /**
     * Gets all values as mapped sql objects
     * @return
     */
    public Object[] getAllValues() {
        Object[] vals = new Object[size()];
        int count = 0;
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter adapter = _list[i];
            vals[count] = adapter.getMappedSqlObject();
            count++;
        }
        return vals;
    }


    /**
     * This method returns the primary keys for the object based on
     * the attribute values added during the derived object's
     * constructor.
     *
     * @return the set containing the primary keys for this entity
     */

    public PrimaryKeySet getPrimaryKey() {
        return _pkset;
    }

    public List getUpdateMatchValues() {
        if (_updateMatchValues == null)
            initUpdateMatchValues();
        return _updateMatchValues;
    }

    private void initUpdateMatchValues() {
        _updateMatchValues = new ArrayList();

        for (int i = 0; i < _list.length; i++) {
            FieldAdapter adapter = _list[i];
            if (adapter.isMatchOnUpdate()) {
                SqlAdapter.MatchUpdate matchUpdate = new SqlAdapter.MatchUpdate();
                matchUpdate.mapper = adapter;
                matchUpdate.value = matchUpdate.mapper.getMappedSqlObject();
                _updateMatchValues.add(matchUpdate);
            }
        }
    }

    public void load(ResultSet rs) throws SQLException {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter adapter = _list[i];
            adapter.setValueFromResultSet(rs);
            adapter.validateLoad();
        }
    }

    public void notifyFieldsOfInsert(JaxorContext context) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            if (fieldAdapter instanceof FieldLifeCycleListener)
                ((FieldLifeCycleListener) fieldAdapter).beforeInsert(context);
        }
    }

    public void notifyFieldsOfUpdate(JaxorContext context) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            if (fieldAdapter instanceof FieldLifeCycleListener)
                ((FieldLifeCycleListener) fieldAdapter).beforeUpdate(context);
        }
    }

    public void notifyFieldsOfDelete(JaxorContext context) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            if (fieldAdapter instanceof FieldLifeCycleListener)
                ((FieldLifeCycleListener) fieldAdapter).beforeDelete(context);
        }
    }

    public List getForeignFields() {
        return _foreignFields;
    }

    public void addForeignField(ForeignFieldLazyLoader foreignField){
        _foreignFields.add(foreignField);
    }

    /**
     * Returns a value based upon a property name. If the property is not found, throws a system exception.
     * @param propertyName
     * @return
     */
    public Object getPropertyValue(String propertyName) {
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            if(fieldAdapter.getMetaField().getProperty().equals(propertyName))
                return fieldAdapter.getValue();
        }
        throw new SystemException("Failed to Find property: " + propertyName);
    }

    public void setPropertyValue(String property , Object value){
        for (int i = 0; i < _list.length; i++) {
            FieldAdapter fieldAdapter = _list[i];
            if(fieldAdapter.getMetaField().getProperty().equals(property)){
                fieldAdapter.setValue(value);
                return;
            }
        }
        throw new SystemException("Failed to Find property: " + property);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.