Package org.apache.ojb.odmg.collections

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

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 java.io.Serializable;

import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBroker;
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.Logger;
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 org.odmg.Transaction;

/**
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @version $Id: DMapEntry.java,v 1.18 2004/04/04 23:53:39 brianm Exp $
*/

public class DMapEntry implements java.util.Map.Entry, Serializable
{
  private static final long serialVersionUID = 4382757889982004339L;
    private Logger log = LoggerFactory.getLogger(DMapEntry.class);
    private int id;
    private int dmapId;
    private Identity keyOID;
    private Object keyRealSubject;
    private Identity valueOID;
    private Object valueRealSubject;
    private PBKey pbKey;

    /**
     * DMapEntry constructor comment.
     */
    public DMapEntry()
    {
        super();
    }

    public DMapEntry(PBKey key)
    {
        this.pbKey = key;
    }

    /**
     * DMapEntry constructor comment.
     */
    public DMapEntry(int id, int mapId, Identity keyOID, Identity valueOID)
    {
        this.id = id;
        this.dmapId = mapId;
        this.keyOID = keyOID;
        this.valueOID = valueOID;
    }

    /**
     * DMapEntry constructor comment.
     */
    public DMapEntry(DMapImpl map, Object key, Object value)
    {
        this.pbKey = map.getPBKey();
    if (!PersistentFieldFactory.usesAccessorsAndMutators())
          id = generateNewId();
        this.dmapId = map.getId();
        this.keyRealSubject = key;
        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
        this.keyOID = new Identity(key,broker);
        this.valueRealSubject = value;
        this.valueOID = new Identity(value,broker);
        broker.close();
    }

    /**
     * return a unique id
     */
    protected int generateNewId()
    {
        PBCapsule capsule = new PBCapsule(null, 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("DMapEntry: Generation of new id failed", e);
            throw new PersistenceBrokerException(e);
        }
        finally
        {
            capsule.destroy();
        }
    }

    /**
     * getKey method comment.
     */
    public Object getKey()
    {

        if (keyRealSubject == null)
        {
            try
            {
                PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
                keyRealSubject = capsule.getBroker().getObjectByIdentity(keyOID);
                capsule.destroy();
            }
            catch (Exception e)
            {
                log.error("Could not materialize key with keyOID " + keyOID, e);
            }
        }
        return keyRealSubject;
    }

    /**
     * getValue method comment.
     */
    public Object getValue()
    {

        if (valueRealSubject == null)
        {
            try
            {
                PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
                valueRealSubject = capsule.getBroker().getObjectByIdentity(valueOID);
                capsule.destroy();
            }
            catch (Exception e)
            {
                log.error("Could not materialize value with valueOID " + valueOID, e);
            }
        }
        return valueRealSubject;
    }

    /**
     * setValue method comment.
     */
    public Object setValue(Object obj)
    {
        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
        if (tx != null)
        {
            tx.lock(this, Transaction.WRITE);
        }
        valueRealSubject = obj;
        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
        valueOID = new Identity(obj,broker);
        broker.close();
        return obj;
    }

    /**
     * Gets the dmapId.
     * @return Returns a int
     */
    public int getDmapId()
    {
        return dmapId;
    }

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

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

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

    public Identity getKeyOID()
    {
        return keyOID;
    }

    public void setKeyOID(Identity keyOID)
    {
        this.keyOID = keyOID;
    }

    public Identity getValueOID()
    {
        return valueOID;
    }

    public void setValueOID(Identity valueOID)
    {
        this.valueOID = valueOID;
    }
}
TOP

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

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.