Package nexj.core.runtime

Examples of nexj.core.runtime.InstanceList


            }

            oid = getKey(query.getParentItem());
         }

         InstanceList instanceList;
         boolean bAdd = true;

         if (container == null)
         {
            instanceList = m_instanceList;
         }
         else
         {
            Object value = container.getOldValueDirect(assoc.getOrdinal());

            if (value == Undefined.VALUE)
            {
               if (assoc.isCollection())
               {
                  instanceList = new InstanceArrayList();
                  instanceList.setAssociation(container, assoc, true);
                  instanceList.setLazy(false);
                  container.setOldValueDirect(assoc.getOrdinal(), instanceList);
               }
               else
               {
                  instanceList = null;

                  if (oid == null)
                  {
                     container.setOldValueDirect(assoc.getOrdinal(), null);
                  }
               }
            }
            else
            {
               if (assoc.isCollection() && !(value instanceof Undefined))
               {
                  instanceList = (InstanceList)value;
                  instanceList.setLazy(false);
               }
               else
               {
                  instanceList = null;
                  bAdd = false;
               }
            }
         }

         Metaclass metaclass;

         if (bIdentity)
         {
            if (oid == null)
            {
               if (query.getRoot() == m_query)
               {
                  m_instanceArray[query.getOrdinal()] = null;
               }

               continue;
            }

            if (query.getRoot() != m_query && !query.isLazy())
            {
               if (!bSkipInstance)
               {
                  query.addParentInstance(container, oid);
               }

               continue;
            }

            Field field = query.getTypeCodeField();

            if (field != null)
            {
               metaclass = query.getPersistenceMapping().findMetaclassByTypeCode(getValue(field));

               if (metaclass == null)
               {
                  m_instanceArray[query.getOrdinal()] = null;

                  continue;
               }
            }
            else
            {
               metaclass = getMetaclass(query);
            }
         }
         else
         {
            metaclass = getMetaclass(query);
         }

         InstanceRef ref = m_query.getInvocationContext().findInstanceRef(metaclass, oid);
         Instance instance = (ref == null) ? null : ref.getInstance();

         // Check if the instance has already been retrieved
         if (instance != null)
         {
            // Already retrieved instance
            if (instance.isLazy())
            {
               if (query.isLazy())
               {
                  if (!metaclass.isUpcast(instance.getLazyMetaclass()))
                  {
                     instance.setLazyMetaclass(metaclass);
                  }
               }
               else
               {
                  instance.setMetaclass(metaclass);
               }
            }

            if (instanceList != null)
            {
               bAdd = !instanceList.contains(instance);

               if (bAdd &&
                  container == null &&
                  m_nMaxCount >= 0 &&
                  instanceList.getCount() == m_nOffset + m_nMaxCount)
               {
                  m_bEOF = bDiscardExtra;
                  m_bEOP = !bDiscardExtra;

                  return false;
               }
            }

            if (bAdd)
            {
               switch (instance.getState())
               {
                  case Instance.DELETED:
                     bAdd = false;
                     break;

                  case Instance.DIRTY:
                     if (reverse != null)
                     {
                        Object reverseValue = instance.getValueDirect(reverse.getOrdinal());

                        if (reverseValue != Undefined.VALUE && reverseValue != container)
                        {
                           bAdd = false;
                        }
                     }

                     break;
               }

               if (!bAdd && instanceList == null)
               {
                  if (container.getValueDirect(assoc.getOrdinal()) == Undefined.VALUE)
                  {
                     // Set the new value to null, old value to the retrieved instance
                     container.setValueDirect(assoc.getOrdinal(), null);
                  }

                  bAdd = true;
               }
            }

            if (m_instanceSet.get(query, instance) == null)
            {
               // Merge the not yet retrieved attribute values

               if (!bSkipInstance)
               {
                  // Overwrite only if the instance is clean
                  boolean bOverwrite = (instance.getUnitOfWork() == null);
                  Field lockingField = query.getLockingField();

                  if (lockingField != null)
                  {
                     Attribute lockingAttribute = lockingField.getAttribute();
                     Object oldValue = instance.getNewValueDirect(lockingAttribute.getOrdinal());

                     if (oldValue != Undefined.VALUE)
                     {
                        // Check the old lock value
                        Primitive primitive = (Primitive)lockingAttribute.getType();
                        Object value = getValue(lockingField);

                        if (((Boolean)primitive.findNEFunction(primitive).invoke(oldValue, value)).booleanValue())
                        {
                           // The lock values do not match
                           // If the instance is dirty/deleted or share-locked, throw an exception
                           if (!bOverwrite || ref.isLocked())
                           {
                              throw new OptimisticLockException(instance);
                           }

                           // Discard all the instance attributes
                           for (int i = 0, n = metaclass.getInstanceAttributeCount(); i != n; ++i)
                           {
                              discard(instance, metaclass.getInstanceAttribute(i));
                           }

                           instance.setOldValueDirect(lockingAttribute.getOrdinal(), value);
                        }
                        else
                        {
                           if (bOverwrite)
                           {
                              discard(instance, query);

                              if (reverse != null && bAdd && !instance.isLazy())
                              {
                                 instance.setValueDirect(reverse.getOrdinal(), container);
                              }

                              bOverwrite = false;
                           }
                        }
                     }
                     else
                     {
                        // If the instance is clean, discard the
                        // instance assocs referenced in this query
                        if (bOverwrite)
                        {
                           discard(instance, query);
                        }
                     }
                  }
                  else
                  {
                     // If the instance is clean, discard the
                     // instance assocs referenced in this query
                     if (bOverwrite)
                     {
                        discard(instance, query);
                     }
                  }

                  if (bOverwrite && reverse != null && bAdd && !instance.isLazy())
                  {
                     instance.setValueDirect(reverse.getOrdinal(), container);
                  }

                  addInstance(instance, ref, metaclass, query, bOverwrite);
               }
            }
         }
         else
         {
            // New instance

            if (container == null &&
               m_nMaxCount >= 0 &&
               instanceList.getCount() == m_nOffset + m_nMaxCount)
            {
               m_bEOF = bDiscardExtra;
               m_bEOP = !bDiscardExtra;

               return false;
            }

            instance = new Instance(metaclass, query.isLazy(), m_query.getInvocationContext());
            ref = instance.cache(oid);

            if (!bSkipInstance)
            {
               addInstance(instance, ref, metaclass, query, true);
            }
         }

         if (bAdd)
         {
            if (instanceList != null)
            {
               instanceList.add(instance, InstanceList.DIRECT);
            }
            else
            {
               container.setOldValueDirect(assoc.getOrdinal(), instance);
            }
View Full Code Here


         }
      }

      // Remove the empty elements according to the query offset

      InstanceList instanceList;

      if (m_nOffset > 0)
      {
         int nCount = m_instanceList.getCount() - m_nOffset;

         if (nCount < 0)
         {
            nCount = 0;
         }

         instanceList = new InstanceArrayList(nCount);

         for (int i = m_nOffset; nCount > 0; ++i, --nCount)
         {
            instanceList.add(m_instanceList.getInstance(i), InstanceList.DIRECT);
         }

         m_instanceList = instanceList;
         m_nOffset = 0;
      }
View Full Code Here

         return new InstanceArrayList(0);
      }

      internalNext(nMaxCount, false);

      InstanceList instanceList = m_instanceList;

      m_instanceList = null;

      return instanceList;
   }
View Full Code Here

                  if (!(value instanceof Undefined))
                  {
                     if (attribute.isCollection())
                     {
                        InstanceList list = (InstanceList)value;
                        int nCount = list.getCount();

                        for (int i = 0; i < nCount; ++i)
                        {
                           invalidateDependency((InverseDependencyList)dep, (Accessor)list.get(i), undef);
                        }
                     }
                     else
                     {
                        invalidateDependency((InverseDependencyList)dep, (Accessor)value, undef);
View Full Code Here

         {
            addPrimitiveWork(uow, instance, primaryWork, attributeMapping, value);
         }
         else
         {
            InstanceList list = null;
            Instance inst = null;
            boolean bNull;

            if (attribute.isCollection())
            {
               list = (InstanceList)value;
               bNull = (list == null || list.isEmpty());
            }
            else
            {
               inst = (Instance)value;
               bNull = (inst == null);
            }

            if (!bNull)
            {
               // TODO: Insert with composition mappings.
               ClassMapping assocMapping = (ClassMapping)attributeMapping;
               boolean bSuccessor = isCreateSuccessor(primaryWork, assocMapping, bOID);

               if (bSuccessor)
               {
                  if (list == null)
                  {
                     inst.getAdapter().addDependency(uow, primaryWork, inst,
                        assocMapping.getKey(true), assocMapping.getKey(false), true);
                  }
                  else
                  {
                     for (int k = 0; k < list.getCount(); ++k)
                     {
                        inst = list.getInstance(k);

                        if (inst != null)
                        {
                           inst.getAdapter().addDependency(uow, primaryWork, inst,
                              assocMapping.getKey(true), assocMapping.getKey(false), true);
                        }
                     }
                  }
               }
               else
               {
                  Work work = addClassWork(uow, instance, primaryWork, assocMapping);

                  if (list != null)
                  {
                     inst = list.getInstance(0);
                  }

                  if (inst.getOID() != null)
                  {
                     work.setKeyValue(assocMapping.getKey(false), assocMapping.getKey(true), inst);
View Full Code Here

         {
            addPrimitiveWork(uow, instance, primaryWork, attributeMapping, value);
         }
         else
         {
            InstanceList list = null;
            Instance inst = null;
            boolean bNull;

            if (attribute.isCollection())
            {
               list = (InstanceList)value;
               bNull = (list == null || list.isEmpty());
            }
            else
            {
               inst = (Instance)value;
               bNull = (inst == null);
            }

            ClassMapping assocMapping = (ClassMapping)attributeMapping;
            boolean bSuccessor;

            // TODO: Update composition mappings

            if (assocMapping.getKey(false).isObjectKey())
            {
               bSuccessor = true;
            }
            else if (assocMapping.getKey(true).isObjectKey())
            {
               bSuccessor = false;
            }
            else if (attribute.isCollection())
            {
               bSuccessor = true;
            }
            else
            {
               bSuccessor = false;
            }

            if (bSuccessor)
            {
               if (!bNull)
               {
                  if (list == null)
                  {
                     inst.getAdapter().addDependency(uow, primaryWork, inst,
                        assocMapping.getKey(true), assocMapping.getKey(false), true);
                  }
                  else
                  {
                     for (int k = 0; k < list.getCount(); ++k)
                     {
                        inst = list.getInstance(k);

                        if (inst != null)
                        {
                           inst.getAdapter().addDependency(uow, primaryWork, inst,
                              assocMapping.getKey(true), assocMapping.getKey(false), true);
                        }
                     }
                  }
               }
            }
            else
            {
               Work work = addClassWork(uow, instance, primaryWork, assocMapping);

               if (bNull)
               {
                  if (assocMapping.getKey(false) != mapping.getObjectKey())
                  {
                     work.setKeyValue(assocMapping.getKey(false), assocMapping.getKey(true), null);
                  }
               }
               else
               {
                  if (list != null)
                  {
                     inst = list.getInstance(0);
                  }

                  if (inst.getOID() != null)
                  {
                     if (assocMapping.getKey(false) != mapping.getObjectKey())
View Full Code Here

   }

   public void testReadAssocMultipartVirtualFKToRelationalPK()
   {
      Metaclass testClass = getMetadata().getMetaclass("TestVirtualClass");
      InstanceList list = Query.createRead(testClass,
         parse("(f1)"), parse("(= (@ f) 1)"), null, -1, 0, false, Query.SEC_ALL, m_context
      ).read();

      assertEquals("Cottage", ((Instance)list.getInstance(0).getValue("f1")).getValue("caption"));
   }
View Full Code Here

            if (assoc.isCollection())
            {
               // Add composition instances to collection on parent
               Instance childInst;
               InstanceList childInstList = (InstanceList)container.getOldValueDirect(
                  assoc.getOrdinal());

               while ((childInst = cursor.next()) != null)
               {
                  childInstList.setLazy(false);
                  childInstList.add(childInst, InstanceList.DIRECT | InstanceList.REPLACE);
               }
            }
            else
            {
               assert childList.size() == 1;
View Full Code Here

            // A composition attribute
            Cursor cursor = new VirtualCursor(getCompositionQuery(attribute, query), getIterator(value, metaclass));

            try
            {
               InstanceList instanceList = new InstanceArrayList();

               instanceList.setAssociation(instance, attribute, true);
               instanceList.setLazy(false);
               instance.setOldValueDirect(nOrdinal, instanceList);

               Instance assoc;

               while ((assoc = cursor.next()) != null)
               {
                  if (!instanceList.contains(assoc))
                  {
                     instanceList.add(assoc, InstanceList.DIRECT);
                  }
               }
            }
            finally
            {
View Full Code Here

      }
      else
      {
         if (attribute.isCollection() && value != null)
         {
            InstanceList list = (InstanceList)value;
            int nCount = list.size();
            ArrayList collection = new ArrayList(nCount);

            for (int i = 0; i < nCount; i++)
            {
               collection.add(((Instance)list.get(i)).getOID());
            }

            tobj.setValue(attribute.getName(), collection);
         }
         else
View Full Code Here

TOP

Related Classes of nexj.core.runtime.InstanceList

Copyright © 2018 www.massapicom. 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.