Package org.apache.ojb.odmg.collections

Source Code of org.apache.ojb.odmg.collections.DListEntry

package org.apache.ojb.odmg.collections;

/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.metadata.FieldDescriptor;
import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldFactory;
import org.apache.ojb.broker.util.logging.LoggerFactory;
import org.apache.ojb.odmg.PBCapsule;
import org.apache.ojb.odmg.TransactionImpl;
import org.apache.ojb.odmg.TxManagerFactory;

import java.io.Serializable;

/**
* Insert the type's description here.
* Creation date: (28.01.2001 21:23:26)
* @author Thomas Mahler
*/
public class DListEntry implements Serializable
{
  private static final long serialVersionUID = -3280555665769116060L;
    protected int id = 0;
    protected int dlistId = 0;
    protected DListImpl m_dList;
    protected int position;
    protected Identity oid;
    protected Object realSubject;
    protected PBKey pbKey;

    /**
     * Insert the method's description here.
     * Creation date: (09.02.2001 21:28:01)
     */
    public DListEntry()
    {
        super();
        if (!PersistentFieldFactory.usesAccessorsAndMutators())
            id = generateNewId();
    }

    public DListEntry(PBKey pbKey)
    {
        this.pbKey = pbKey;
        if (!PersistentFieldFactory.usesAccessorsAndMutators())
            id = generateNewId();
    }

    /**
     * DListEntry multi-args constructor used by OJB PersistenceBroker
     */
    public DListEntry(int anId, int aDlistId, int pos, Identity anOid)
    {
        this.id = anId;
        this.dlistId = aDlistId;
        this.position = pos;
        this.oid = anOid;
    }

    /**
     * DListEntry constructor comment.
     */
    public DListEntry(DListImpl theDlist, Object theObject)
    {
        this.pbKey = theDlist.getPBKey();
        this.m_dList = theDlist;
        this.dlistId = theDlist.getId();
        this.position = theDlist.size();
        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
        if (tx != null)
            oid = new Identity(theObject, tx.getBroker());
        else
        {
          PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            oid = new Identity(theObject, broker);
            broker.close();
        }
        this.realSubject = theObject;
        if (!PersistentFieldFactory.usesAccessorsAndMutators())
            id = generateNewId();
    }

    public Identity getOid()
    {
        return oid;
    }

    public void setOid(Identity oid)
    {
        this.oid = oid;
    }

    public PBKey getPBKey()
    {
        return pbKey;
    }

    public void setPBKey(PBKey pbKey)
    {
        this.pbKey = pbKey;
    }

    /**
     * return a unique id
     */
    protected int generateNewId()
    {
        PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
        try
        {
            PersistenceBroker broker = capsule.getBroker();
            FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementFields()[0];
            Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);

            int result = val.intValue();
            return result;
        }
        catch (Exception e)
        {
            LoggerFactory.getDefaultLogger().error("DListEntry: Generation of new id failed", e);
            throw new PersistenceBrokerException(e);
        }
        finally
        {
            capsule.destroy();
        }
    }

    /**
     * Insert the method's description here.
     * Creation date: (06.02.2001 22:06:55)
     * @return int
     */
    public int getPosition()
    {
        return position;
    }

    /**
     * Insert the method's description here.
     * Creation date: (30.01.2001 22:32:10)
     * @return java.lang.Object
     */
    public Object getRealSubject()
    {
        try
        {
            if (realSubject == null)
            {
                materializeRealSubject();
            }
        }
        catch (Exception e)
        {
            LoggerFactory.getDefaultLogger().error("Cannot materialize real subject", e);
        }
        return realSubject;
    }

    /**
     * retrieve the real subject from the underlying RDBMS
     */
    private void materializeRealSubject() throws PersistenceBrokerException
    {
        PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
        try
        {
            realSubject = capsule.getBroker().getObjectByIdentity(oid);
        }
        finally
        {
            if(capsule != null) capsule.destroy();
        }
    }

    /**
     * Insert the method's description here.
     * Creation date: (06.02.2001 22:06:55)
     * @param newPosition int
     */
    public void setPosition(int newPosition)
    {
        position = newPosition;
    }

    /**
     * Insert the method's description here.
     * Creation date: (10.02.2001 17:58:45)
     * @param realSubject java.lang.Object
     */
    void setRealSubject(Object realSubject)
    {
        this.realSubject = realSubject;
    }

    /**
     * return String representation.
     */
    public String toString()
    {
        if (realSubject == null)
        {
            return oid.toString();
        }
        else
        {
            return realSubject.toString();
        }
    }

    /**
     * Gets the dlistId.
     * @return Returns a int
     */
    public int getDlistId()
    {
        if (dlistId == -1)
        {
            dlistId = this.m_dList != null ? this.m_dList.getId() : generateNewId();
        }
        return dlistId;
    }

    /**
     * Sets the dlistId.
     * @param dlistId The dlistId to set
     */
    public void setDlistId(int dlistId)
    {
        this.dlistId = dlistId;
    }

    /**
     * Gets the id.
     * @return Returns a int
     */
    public int getId()
    {
        if (id == -1)
        {
            id = generateNewId();
        }
        return id;
    }

    /**
     * Sets the id.
     * @param id The id to set
     */
    public void setId(int id)
    {
        this.id = id;
    }
}
TOP

Related Classes of org.apache.ojb.odmg.collections.DListEntry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.