<commit_before>/*******************************************************************************
 * Copyright (c) 1998, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     mnorman - convert DBWS to use new EclipseLink public Dynamic Persistence APIs
 ******************************************************************************/
package org.eclipse.persistence.internal.xr;

//javase imports
import java.beans.PropertyChangeListener;

//EclipseLink imports
import org.eclipse.persistence.descriptors.changetracking.ChangeTracker;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.exceptions.DynamicException;
import org.eclipse.persistence.indirection.IndirectContainer;
import org.eclipse.persistence.indirection.ValueHolderInterface;
import org.eclipse.persistence.internal.descriptors.PersistenceEntity;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.queries.FetchGroup;
import org.eclipse.persistence.queries.FetchGroupTracker;
import org.eclipse.persistence.sessions.Session;
import static org.eclipse.persistence.internal.helper.Helper.getShortClassName;

/**
 * <p>
 * <b>INTERNAL:</b> XRDynamicEntity is used for models where Java classes do not
 * exist.
 * <p>
 * EclipseLink is based around mapping attributes of a Java class to a table (or
 * tables) with the attributes representing either the column data or
 * foreign-key contraints as relationships to other (mapped) classes. For
 * applications that are based around meta-data and the Java class is either not
 * needed or not available, this basic entity can be used. Subclasses of this
 * abstract class can be dynamically generated at runtime.
 *
 * @author <NAME> - <EMAIL>
 * @since EclipseLink 1.x
 */
@SuppressWarnings("unchecked")
public abstract class XRDynamicEntity implements DynamicEntity, PersistenceEntity, ChangeTracker,
    FetchGroupTracker {

    public static final String XR_FIELD_INFO_STATIC = "XRFI";
    
    protected XRField[] _fields;

    public XRDynamicEntity() {
        super();
        int numFields = getFieldInfo().numFields();
        _fields = new XRField[numFields];
        for (int i = 0; i < numFields; i++) {
            _fields[i] = new XRField();
        }
    }
    
    public abstract XRFieldInfo getFieldInfo();

    protected int getFieldIdx(String fieldName) {
        return getFieldInfo().getFieldIdx(fieldName);
    }
    
    public <T> T get(String fieldName) throws DynamicException {
        return (T) get(getFieldIdx(fieldName));
    }
    protected Object get(int fieldIdx) {
        XRField df = _fields[fieldIdx];
        Object value = null;
        if (df.isSet) {
            value = df.fieldValue;
            // trigger any indirection
            if (value instanceof ValueHolderInterface) {
                value = ((ValueHolderInterface)value).getValue();
            }
            else if (value instanceof IndirectContainer) {
                value = ((IndirectContainer)value).getValueHolder().getValue();
            }
        }
        return value;
    }

    public boolean isSet(String fieldName) throws DynamicException {
        return isSet(getFieldIdx(fieldName));
    }
    protected boolean isSet(int fieldIdx) {
        return _fields[fieldIdx].isSet;
    }

    public DynamicEntity set(String fieldName, Object value) throws DynamicException {
        set(getFieldIdx(fieldName), value);
        return (DynamicEntity)this;
    }
    protected void set(int fieldIdx, Object value) {
        XRField df = _fields[fieldIdx];
        Object currentValue = df.fieldValue;
        if (currentValue instanceof ValueHolderInterface) {
            ((ValueHolderInterface)currentValue).setValue(value);
        }
        else {
            df.fieldValue = value;
        }
        df.isSet = true;
    }

    //PersistenceEntity API
    protected Object __pk;
    public Object _persistence_getId() {
        return __pk;
    }
    public void _persistence_setId(Object pk) {
        this.__pk = pk;
    }
    
    //ChangeTracker API
    /**
     * ChangeListener used for attribute change tracking processed in the
     * property. Set through
     * {@link ChangeTracker#_persistence_setPropertyChangeListener(PropertyChangeListener)}
     */
    protected PropertyChangeListener __changeListener = null;
    public PropertyChangeListener _persistence_getPropertyChangeListener() {
        return this.__changeListener;
    }
    public void _persistence_setPropertyChangeListener(PropertyChangeListener listener) {
        this.__changeListener = listener;
    }

    //FetchGroup API
    /**
     * FetchGroup cached by
     * {@link FetchGroupTracker#_persistence_setFetchGroup(FetchGroup)}
     */
    protected FetchGroup __fetchGroup;
    /**
     * {@link FetchGroupTracker#_persistence_setShouldRefreshFetchGroup(boolean)}
     */
    protected boolean __refreshFetchGroup = false;
    /**
     * Session cached by
     * {@link FetchGroupTracker#_persistence_setSession(Session)}
     */
    protected Session __session;
    public FetchGroup _persistence_getFetchGroup() {
        return this.__fetchGroup;
    }
    public Session _persistence_getSession() {
        return this.__session;
    }
    /**
     * Return true if the attribute is in the fetch group being tracked.
     */
    public boolean _persistence_isAttributeFetched(String attribute) {
        return this.__fetchGroup == null || this.__fetchGroup.containsAttribute(attribute);
    }
    /**
     * Reset all attributes of the tracked object to the un-fetched state with
     * initial default values.
     */
    public void _persistence_resetFetchGroup() {
        throw new UnsupportedOperationException(
            "XRDynamicEntity._persistence_resetFetchGroup:: NOT SUPPORTED");
    }
    public void _persistence_setFetchGroup(FetchGroup group) {
        this.__fetchGroup = group;
    }
    public void _persistence_setSession(Session session) {
        this.__session = session;
    }
    public void _persistence_setShouldRefreshFetchGroup(boolean shouldRefreshFetchGroup) {
        this.__refreshFetchGroup = shouldRefreshFetchGroup;
    }
    public boolean _persistence_shouldRefreshFetchGroup() {
        return this.__refreshFetchGroup;
    }
    
    @Override
    public String toString() {
        // this will print something like {Emp 10} or {Phone 234-5678 10}
        StringBuilder sb = new StringBuilder(20);
        sb.append('{');
        sb.append(__pk);
        sb.append('}');
        return sb.toString();
    }

    static class XRField {
        protected Object fieldValue = null;<commit_msg>Clean up after James' changes for 298985<commit_after>/*******************************************************************************
 * Copyright (c) 1998, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     mnorman - convert DBWS to use new EclipseLink public Dynamic Persistence APIs
 ******************************************************************************/
package org.eclipse.persistence.internal.xr;

//javase imports
import java.beans.PropertyChangeListener;

//EclipseLink imports
import org.eclipse.persistence.descriptors.changetracking.ChangeTracker;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.exceptions.DynamicException;
import org.eclipse.persistence.indirection.IndirectContainer;
import org.eclipse.persistence.indirection.ValueHolderInterface;
import org.eclipse.persistence.internal.descriptors.PersistenceEntity;
import org.eclipse.persistence.queries.FetchGroup;
import org.eclipse.persistence.queries.FetchGroupTracker;
import org.eclipse.persistence.sessions.Session;
import static org.eclipse.persistence.internal.helper.Helper.getShortClassName;

/**
 * <p>
 * <b>INTERNAL:</b> XRDynamicEntity is used for models where Java classes do not
 * exist.
 * <p>
 * EclipseLink is based around mapping attributes of a Java class to a table (or
 * tables) with the attributes representing either the column data or
 * foreign-key contraints as relationships to other (mapped) classes. For
 * applications that are based around meta-data and the Java class is either not
 * needed or not available, this basic entity can be used. Subclasses of this
 * abstract class can be dynamically generated at runtime.
 *
 * @author <NAME> - <EMAIL>
 * @since EclipseLink 1.x
 */
@SuppressWarnings("unchecked")
public abstract class XRDynamicEntity implements DynamicEntity, PersistenceEntity, ChangeTracker,
    FetchGroupTracker {

    public static final String XR_FIELD_INFO_STATIC = "XRFI";
    
    protected XRField[] _fields;

    public XRDynamicEntity() {
        super();
        int numFields = getFieldInfo().numFields();
        _fields = new XRField[numFields];
        for (int i = 0; i < numFields; i++) {
            _fields[i] = new XRField();
        }
    }
    
    public abstract XRFieldInfo getFieldInfo();

    protected int getFieldIdx(String fieldName) {
        return getFieldInfo().getFieldIdx(fieldName);
    }
    
    public <T> T get(String fieldName) throws DynamicException {
        return (T) get(getFieldIdx(fieldName));
    }
    protected Object get(int fieldIdx) {
        XRField df = _fields[fieldIdx];
        Object value = null;
        if (df.isSet) {
            value = df.fieldValue;
            // trigger any indirection
            if (value instanceof ValueHolderInterface) {
                value = ((ValueHolderInterface)value).getValue();
            }
            else if (value instanceof IndirectContainer) {
                value = ((IndirectContainer)value).getValueHolder().getValue();
            }
        }
        return value;
    }

    public boolean isSet(String fieldName) throws DynamicException {
        return isSet(getFieldIdx(fieldName));
    }
    protected boolean isSet(int fieldIdx) {
        return _fields[fieldIdx].isSet;
    }

    public DynamicEntity set(String fieldName, Object value) throws DynamicException {
        set(getFieldIdx(fieldName), value);
        return (DynamicEntity)this;
    }
    protected void set(int fieldIdx, Object value) {
        XRField df = _fields[fieldIdx];
        Object currentValue = df.fieldValue;
        if (currentValue instanceof ValueHolderInterface) {
            ((ValueHolderInterface)currentValue).setValue(value);
        }
        else {
            df.fieldValue = value;
        }
        df.isSet = true;
    }

    //PersistenceEntity API
    protected Object __pk;
    public Object _persistence_getId() {
        return __pk;
    }
    public void _persistence_setId(Object pk) {
        this.__pk = pk;
    }
    
    //ChangeTracker API
    /**
     * ChangeListener used for attribute change tracking processed in the
     * property. Set through
     * {@link ChangeTracker#_persistence_setPropertyChangeListener(PropertyChangeListener)}
     */
    protected PropertyChangeListener __changeListener = null;
    public PropertyChangeListener _persistence_getPropertyChangeListener() {
        return this.__changeListener;
    }
    public void _persistence_setPropertyChangeListener(PropertyChangeListener listener) {
        this.__changeListener = listener;
    }

    //FetchGroup API
    /**
     * FetchGroup cached by
     * {@link FetchGroupTracker#_persistence_setFetchGroup(FetchGroup)}
     */
    protected FetchGroup __fetchGroup;
    /**
     * {@link FetchGroupTracker#_persistence_setShouldRefreshFetchGroup(boolean)}
     */
    protected boolean __refreshFetchGroup = false;
    /**
     * Session cached by
     * {@link FetchGroupTracker#_persistence_setSession(Session)}
     */
    protected Session __session;
    public FetchGroup _persistence_getFetchGroup() {
        return this.__fetchGroup;
    }
    public Session _persistence_getSession() {
        return this.__session;
    }
    /**
     * Return true if the attribute is in the fetch group being tracked.
     */
    public boolean _persistence_isAttributeFetched(String attribute) {
        return this.__fetchGroup == null || this.__fetchGroup.containsAttribute(attribute);
    }
    /**
     * Reset all attributes of the tracked object to the un-fetched state with
     * initial default values.
     */
    public void _persistence_resetFetchGroup() {
        throw new UnsupportedOperationException(
            "XRDynamicEntity._persistence_resetFetchGroup:: NOT SUPPORTED");
    }
    public void _persistence_setFetchGroup(FetchGroup group) {
        this.__fetchGroup = group;
    }
    public void _persistence_setSession(Session session) {
        this.__session = session;
    }
    public void _persistence_setShouldRefreshFetchGroup(boolean shouldRefreshFetchGroup) {
        this.__refreshFetchGroup = shouldRefreshFetchGroup;
    }
    public boolean _persistence_shouldRefreshFetchGroup() {
        return this.__refreshFetchGroup;
    }
    
    @Override
    public String toString() {
        // this will print something like {Emp 10} or {Phone 234-5678 10}
        StringBuilder sb = new StringBuilder(20);
        sb.append('{');
        sb.append(getShortClassName(this.getClass()));
        if (__pk != null) {
            sb.append(' ');
            sb.append(__pk);
        }
        sb.append('}');
        return sb.toString();
    }

    static class XRField {
        protected Object fieldValue = null;
{'fraction_non_alphanumeric': 0.10323672519568437, 'fraction_numerical': 0.004795148438050913, 'mean_word_length': 3.1613849765258215, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2566872', 'n_tokens_mistral': 3688, 'n_tokens_neox': 3618, 'n_words': 1010}