Package org.apache.ojb.odmg.collections

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

package org.apache.ojb.odmg.collections;

/* Copyright 2003-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.commons.lang.builder.ToStringBuilder;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.util.logging.LoggerFactory;
import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.odmg.TransactionExt;
import org.apache.ojb.odmg.TxManagerFactory;
import org.apache.ojb.odmg.PBCapsule;

import java.io.Serializable;

/**
*
* @author Thomas Mahler
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: DListEntry_2.java,v 1.9 2004/04/04 23:53:39 brianm Exp $
*/
public class DListEntry_2 implements Serializable
{
  private static final long serialVersionUID = 5251476492626009907L;
    /*
     * declare transient, because ManageableCollection entries need to be {@link java.io.Serializable}.
     */
    private transient Logger log;
    /*
    TODO: declare transient or do we adopt persistent object is serialzable?
    */
    protected Object realSubject;
    protected DListImpl_2 dList;
    protected PBKey pbKey;

    protected Integer id;
    protected Integer dlistId;
    protected Identity oid;
    protected int position;

    /**
     * Used to instantiate persistent DLists from DB by the kernel
     * FOR INTERNAL USE ONLY
     */
    public DListEntry_2()
    {
        super();
        /*
        arminw:
        When PB kernel fill DList with DListEntry, the DListEntry needs to know the current
        used PBKey, because we need to lookup the real objects when user iterates the list,
        thus we need the associated PBKey to find right PB/DB connection.
        TODO: Find a better solution
        */
        TransactionExt tx = TxManagerFactory.instance().getTransaction();
        if(tx == null)
        {
            getLog().info("Can't find running transaction to lookup current associated PBKey");
        }
        else
        {
            this.pbKey = tx.getBroker().getPBKey();
        }
    }

    /**
     * Standard way to instantiate new entries
     */
    public DListEntry_2(DListImpl_2 theDList, Object theObject)
    {
        this.dList = theDList;
        if (dList != null)
        {
            this.pbKey = dList.getPBKey();
            this.dlistId = dList.getId();
            this.position = dList.size();          
        }
        this.realSubject = theObject;
    }

    protected Logger getLog()
    {
        if (log == null)
        {
            log = LoggerFactory.getLogger(DListEntry_2.class);
        }
        return log;
    }

    protected void prepareForPersistency(PersistenceBroker broker)
    {
        if (oid == null)
        {
            if (realSubject == null)
            {
                throw new OJBRuntimeException("Identity and real object are 'null' - Can not persist empty entry");
            }
            else
            {
                oid = new Identity(realSubject, broker);
            }
        }
    }

    public PBKey getPBKey()
    {
        if(pbKey == null)
        {
            if(dList != null)
            {
                pbKey = dList.getPBKey();
            }
        }
        return pbKey;
    }

    protected void prepareRealSubject(PersistenceBroker broker)
    {
        if (oid == null)
        {
            throw new OJBRuntimeException("can not return real object, real object and Identity is null");
        }
        realSubject = broker.getObjectByIdentity(oid);
    }

    public Object getRealSubject()
    {
        if (realSubject != null)
        {
            return realSubject;
        }
        else
        {
            TransactionExt tx = TxManagerFactory.instance().getTransaction();
            if (tx != null && tx.isOpen())
            {
                prepareRealSubject(tx.getBroker());
            }
            else
            {
                PBKey aPbKey = getPBKey();
                if(aPbKey != null)
                {
                    PBCapsule capsule = new PBCapsule(aPbKey, null);
                    try
                    {
                        prepareRealSubject(capsule.getBroker());
                    }
                    finally
                    {
                        capsule.destroy();
                    }
                }
                else
                {
                    getLog().warn("No tx, no PBKey - can't materialise object with Identity " + getOid());
                }
            }
        }
        return realSubject;
    }

    public void setRealSubject(Object realSubject)
    {
        this.realSubject = realSubject;
    }

    public int getPosition()
    {
        return position;
    }

    public void setPosition(int newPosition)
    {
        position = newPosition;
    }

    /**
     * Gets the dlistId.
     * @return Returns a int
     */
    public Integer getDlistId()
    {
        return dlistId;
    }

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

    /**
     * Gets the id.
     * @return Returns a int
     */
    public Integer getId()
    {
        return id;
    }

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

    public Identity getOid()
    {
        return oid;
    }

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

    /**
     * return String representation.
     */
    public String toString()
    {
        ToStringBuilder buf = new ToStringBuilder(this);
        buf.append("id", id);
        buf.append("dListId", dlistId);
        buf.append("position", position);
        buf.append("identity", oid);
        buf.append("realSubject", realSubject);
        return buf.toString();
    }
}
TOP

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

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.