Coverage Report - org.apache.ojb.odmg.collections.DListEntry
 
Classes in this File Line Coverage Branch Coverage Complexity
DListEntry
N/A
N/A
1.6
 
 1  
 package org.apache.ojb.odmg.collections;
 2  
 
 3  
 /* Copyright 2003-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.io.Serializable;
 19  
 
 20  
 import org.apache.commons.lang.builder.ToStringBuilder;
 21  
 import org.apache.ojb.broker.Identity;
 22  
 import org.apache.ojb.broker.OJBRuntimeException;
 23  
 import org.apache.ojb.broker.PBKey;
 24  
 import org.apache.ojb.broker.PersistenceBroker;
 25  
 import org.apache.ojb.broker.PersistenceBrokerAware;
 26  
 import org.apache.ojb.broker.PersistenceBrokerException;
 27  
 import org.apache.ojb.broker.util.logging.Logger;
 28  
 import org.apache.ojb.broker.util.logging.LoggerFactory;
 29  
 import org.apache.ojb.odmg.PBCapsule;
 30  
 import org.apache.ojb.odmg.RuntimeObject;
 31  
 import org.apache.ojb.odmg.TransactionImpl;
 32  
 import org.apache.ojb.odmg.TxManagerFactory;
 33  
 import org.odmg.Transaction;
 34  
 
 35  
 /**
 36  
  * Encapsulates an DList entry object.
 37  
  *
 38  
  * @version $Id: DListEntry.java,v 1.1 2007-08-24 22:17:37 ewestfal Exp $
 39  
  */
 40  
 public class DListEntry implements Serializable, PersistenceBrokerAware
 41  
 {
 42  
     private static final long serialVersionUID = 5251476492626009907L;
 43  
     /*
 44  
      * declare transient, because ManageableCollection entries need to be {@link java.io.Serializable}.
 45  
      */
 46  
     private transient Logger log;
 47  
     protected transient Object realSubject;
 48  
     protected PBKey pbKey;
 49  
 
 50  
     protected Integer id;
 51  
     protected Integer dlistId;
 52  
     protected Identity oid;
 53  
     protected int position;
 54  
 
 55  
     /**
 56  
      * Used to instantiate persistent DLists from DB by the kernel
 57  
      * FOR INTERNAL USE ONLY
 58  
      */
 59  
     public DListEntry()
 60  
     {
 61  
         /*
 62  
         arminw:
 63  
         When PB kernel fill DList with DListEntry, the DListEntry needs to know the current
 64  
         used PBKey, because we need to lookup the real objects when user iterates the list,
 65  
         thus we need the associated PBKey to find right PB/DB connection.
 66  
         TODO: Find a better solution
 67  
         */
 68  
 //        if(getTransaction() == null)
 69  
 //        {
 70  
 //            throw new TransactionNotInProgressException("Materialization of DCollection instances must be done with a tx");
 71  
 //        }
 72  
         getPBKey();
 73  
     }
 74  
 
 75  
     /**
 76  
      * Standard way to instantiate new entries
 77  
      */
 78  
     public DListEntry(DListImpl theDList, Object theObject)
 79  
     {
 80  
         this.dlistId = theDList.getId();
 81  
         this.position = theDList.size();
 82  
         this.realSubject = theObject;
 83  
         this.pbKey = getPBKey();
 84  
     }
 85  
 
 86  
     protected Logger getLog()
 87  
     {
 88  
         if(log == null)
 89  
         {
 90  
             log = LoggerFactory.getLogger(DListEntry.class);
 91  
         }
 92  
         return log;
 93  
     }
 94  
 
 95  
     protected TransactionImpl getTransaction()
 96  
     {
 97  
         return TxManagerFactory.instance().getTransaction();
 98  
     }
 99  
 
 100  
     public PBKey getPBKey()
 101  
     {
 102  
         if(pbKey == null)
 103  
         {
 104  
             TransactionImpl tx = getTransaction();
 105  
             if(tx != null && tx.isOpen())
 106  
             {
 107  
                 pbKey = tx.getBroker().getPBKey();
 108  
             }
 109  
         }
 110  
         return pbKey;
 111  
     }
 112  
 
 113  
     protected void prepareForPersistency(PersistenceBroker broker)
 114  
     {
 115  
         if(oid == null)
 116  
         {
 117  
             if(realSubject == null)
 118  
             {
 119  
                 throw new OJBRuntimeException("Identity and real object are 'null' - Can not persist empty entry");
 120  
             }
 121  
             else
 122  
             {
 123  
                 oid = broker.serviceIdentity().buildIdentity(realSubject);
 124  
             }
 125  
         }
 126  
     }
 127  
 
 128  
     protected void prepareRealSubject(PersistenceBroker broker)
 129  
     {
 130  
         if(oid == null)
 131  
         {
 132  
             throw new OJBRuntimeException("can not return real object, real object and Identity is null");
 133  
         }
 134  
         realSubject = broker.getObjectByIdentity(oid);
 135  
     }
 136  
 
 137  
     public Object getRealSubject()
 138  
     {
 139  
         if(realSubject != null)
 140  
         {
 141  
             return realSubject;
 142  
         }
 143  
         else
 144  
         {
 145  
             TransactionImpl tx = getTransaction();
 146  
             if(tx != null && tx.isOpen())
 147  
             {
 148  
                 prepareRealSubject(tx.getBroker());
 149  
                 if(realSubject != null)
 150  
                 {
 151  
                     RuntimeObject rt = new RuntimeObject(realSubject, tx, false);
 152  
                     tx.lockAndRegister(rt, Transaction.READ, tx.getRegistrationList());
 153  
                 }
 154  
             }
 155  
             else
 156  
             {
 157  
                 PBKey aPbKey = getPBKey();
 158  
                 if(aPbKey != null)
 159  
                 {
 160  
                     PBCapsule capsule = new PBCapsule(aPbKey, null);
 161  
                     try
 162  
                     {
 163  
                         prepareRealSubject(capsule.getBroker());
 164  
                     }
 165  
                     finally
 166  
                     {
 167  
                         capsule.destroy();
 168  
                     }
 169  
                 }
 170  
                 else
 171  
                 {
 172  
                     getLog().warn("No tx, no PBKey - can't materialise object with Identity " + getOid());
 173  
                 }
 174  
             }
 175  
         }
 176  
         return realSubject;
 177  
     }
 178  
 
 179  
     public void setRealSubject(Object realSubject)
 180  
     {
 181  
         this.realSubject = realSubject;
 182  
     }
 183  
 
 184  
     public int getPosition()
 185  
     {
 186  
         return position;
 187  
     }
 188  
 
 189  
     public void setPosition(int newPosition)
 190  
     {
 191  
         position = newPosition;
 192  
     }
 193  
 
 194  
     /**
 195  
      * Gets the dlistId.
 196  
      *
 197  
      * @return Returns a int
 198  
      */
 199  
     public Integer getDlistId()
 200  
     {
 201  
         return dlistId;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Sets the dlistId.
 206  
      *
 207  
      * @param dlistId The dlistId to set
 208  
      */
 209  
     public void setDlistId(Integer dlistId)
 210  
     {
 211  
         this.dlistId = dlistId;
 212  
     }
 213  
 
 214  
     /**
 215  
      * Gets the id.
 216  
      *
 217  
      * @return Returns a int
 218  
      */
 219  
     public Integer getId()
 220  
     {
 221  
         return id;
 222  
     }
 223  
 
 224  
     /**
 225  
      * Sets the id.
 226  
      *
 227  
      * @param id The id to set
 228  
      */
 229  
     public void setId(Integer id)
 230  
     {
 231  
         this.id = id;
 232  
     }
 233  
 
 234  
     public Identity getOid()
 235  
     {
 236  
         return oid;
 237  
     }
 238  
 
 239  
     public void setOid(Identity oid)
 240  
     {
 241  
         this.oid = oid;
 242  
     }
 243  
 
 244  
     /**
 245  
      * return String representation.
 246  
      */
 247  
     public String toString()
 248  
     {
 249  
         ToStringBuilder buf = new ToStringBuilder(this);
 250  
         buf.append("id", id);
 251  
         buf.append("dListId", dlistId);
 252  
         buf.append("position", position);
 253  
         buf.append("identity", oid);
 254  
         buf.append("realSubject", realSubject);
 255  
         return buf.toString();
 256  
     }
 257  
 
 258  
 
 259  
     //===================================================
 260  
     // PersistenceBrokerAware interface methods
 261  
     //===================================================
 262  
     public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
 263  
     {
 264  
         // before insert we have to build the Identity objects of the persistent objects
 265  
         // we can't do this ealier, because we can now expect that the persistent object
 266  
         // was written to DB (we make sure in code that the persistent object was locked before
 267  
         // this entry) and the generated Identity is valid
 268  
         prepareForPersistency(broker);
 269  
     }
 270  
 
 271  
     public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException{}
 272  
     public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException{}
 273  
     public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException{}
 274  
     public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException{}
 275  
     public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException{}
 276  
     public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException{}
 277  
 }