Package nexj.core.runtime

Examples of nexj.core.runtime.Instance


      {
         Object obj = instance.getValueDirect(m_objectAttribute.getOrdinal());

         if (obj instanceof Instance)
         {
            Instance inst = (Instance)obj;

            if (inst.getState() != Instance.DELETED || m_bSerializingDeleted)
            {
               if (instance.isDirty(m_objectAttribute.getOrdinal()))
               {
                  if (m_oidAttribute != null)
                  {
View Full Code Here


   /**
    * @see nexj.core.persistence.PersistenceHook#prepare(nexj.core.persistence.Work)
    */
   public void prepare(Work work)
   {
      Instance instance = work.getInstance();
      PersistenceMapping mapping = instance.getPersistenceMapping();
      Object obj;
      Column column;
      SQLWork sqlWork;

      if (m_objectAttribute != null)
      {
         obj = instance.getValueDirect(m_objectAttribute.getOrdinal());

         if (obj instanceof Instance)
         {
            Instance inst = (Instance)obj;

            if (inst.getOID() != null && (inst.getState() != Instance.DELETED || m_bSerializingDeleted))
            {
               if (instance.isDirty(m_objectAttribute.getOrdinal()))
               {
                  if (m_oidAttribute != null)
                  {
                     column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_oidAttribute)).getColumn();
                     sqlWork = (SQLWork)work;

                     if (sqlWork.getTable() == column.getTable())
                     {
                        sqlWork.setInstanceValue(column, inst.getOID().toBinary());
                     }
                  }

                  if (m_classAttribute != null)
                  {
                     column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_classAttribute)).getColumn();
                     sqlWork = (SQLWork)work;

                     if (sqlWork.getTable() == column.getTable())
                     {
                        sqlWork.setInstanceValue(column, inst.getMetaclass().getName());
                     }
                  }
               }

               if (m_lockingAttribute != null)
               {
                  column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_lockingAttribute)).getColumn();
                  sqlWork = (SQLWork)work;

                  if (sqlWork.getTable() == column.getTable())
                  {
                     Attribute lockingAttribute = inst.getPersistenceMapping().getLockingAttribute();

                     sqlWork.setInstanceValue(column, (lockingAttribute != null) ? inst.getValue(lockingAttribute.getOrdinal()) : null);
                  }
               }
            }
         }
      }
View Full Code Here

      if (parentItem == null)
      {
         return;
      }

      Instance container;

      if (m_parent instanceof ChildQueryCartesianProductIterator)
      {
         container = (Instance)parentItem;
      }
      else
      {
         container = ((Instance[])parentItem)[m_query.getParent().getOrdinal()];
      }

      if (container == null)
      {
         m_list = Collections.emptyList();
      }
      else
      {
         Attribute attribute = m_query.getAttribute();

         if (container.getMetaclass().isUpcast(attribute.getMetaclass()))
         {
            Object value = container.getValue(attribute.getOrdinal());

            if (value == null)
            {
               m_list = Collections.emptyList();
            }
View Full Code Here

      for (int nQuery = 0, nQueryCount = m_query.getOutputQueryCount(); nQuery < nQueryCount; ++nQuery)
      {
         Query query = m_query.getOutputQuery(nQuery);
         Attribute assoc = query.getAttribute();
         boolean bIdentity = query.isIdentity();
         Instance container;
         Attribute reverse;
         OID oid;

         // First output query must be main query node.
         assert (nQuery == 0) == (query == m_query);

         // Query ordinal is the output query ordinal (unless query is joined heterogeneously).
         assert nQuery == query.getOrdinal() || query.getRoot() != m_query;

         // Query ordinal is, by definition, the output query ordinal of its root.
         assert query.getRoot().getOutputQuery(query.getOrdinal()) == query;

         // Get the query class instance OID

         if (query == m_query)
         {
            container = null;
            reverse = null;
            oid = (bIdentity) ? getKey(query.getItem()) : null;
         }
         else
         {
            assert bIdentity;

            container = m_instanceArray[query.getParent().getOrdinal()];

            if (container == null || !assoc.getMetaclass().isUpcast(container.getMetaclass()))
            {
               if (query.getRoot() == m_query)
               {
                  m_instanceArray[query.getOrdinal()] = null;
               }

               continue;
            }

            assoc = container.getMetaclass().getDerivedAttribute(assoc);
            reverse = assoc.getReverse();

            if (reverse != null && reverse.isCollection())
            {
               reverse = null;
            }

            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);
            }

            if (reverse != null && !instance.isLazy() && instance.getOldValueDirect(reverse.getOrdinal()) == Undefined.VALUE)
            {
               instance.setOldValueDirect(reverse.getOrdinal(), container);
            }
         }

         // If query is heterogeneous then this node is finished--the lazy instance has been created.
         if (query.getRoot() != m_query)
View Full Code Here

      if (m_annotationMap != null)
      {
         for (Lookup2D.Iterator itr = m_annotationMap.valueIterator(); itr.hasNext();)
         {
            Function fun = (Function)itr.next();
            Instance instance = (Instance)itr.getKey1();

            instance.setAnnotation((String)itr.getKey2(),
               instance.getContext().getMachine().invoke(fun, instance, (Pair)null));
         }

         if (m_bEOF)
         {
            m_annotationMap = null;
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);

                     if (assocMapping.getKey(false) == mapping.getObjectKey())
                     {
                        instance.setOID(inst.getOID());
                     }
                  }
                  else
                  {
                     inst.getAdapter().addDependency(uow, work, inst,
                        assocMapping.getKey(false), assocMapping.getKey(true), false);
                  }
               }
            }
         }
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())
                     {
                        work.setKeyValue(assocMapping.getKey(false), assocMapping.getKey(true), inst);
                     }
                  }
                  else
                  {
                     inst.getAdapter().addDependency(uow, work, inst,
                        assocMapping.getKey(false), assocMapping.getKey(true), false);
                  }
               }
            }
         }
View Full Code Here

         {
            Object obj = itr.next();

            if (obj instanceof Instance)
            {
               Instance inst = (Instance)obj;

               if (inst.getOID() != null)
               {
                  if (Query.createRead(inst.getMetaclass(), null, Pair.attribute("").eq(inst.getOID()),
                     null, -1, 0, false, Query.SEC_NONE, m_context).read().isEmpty())
                  {
                     inst.setClean();
                     inst.setNew();
                  }
               }

               inst.setEventPending(false);
            }
         }

         m_context.getUnitOfWork().commit();
      }
View Full Code Here

    * @return The timer object.
    */
   public Instance invoke(Metaclass metaclass, Accessor accessor, Symbol event,
      Timestamp tmStart, Number period, String sUser, ActionContext actx)
   {
      Instance instance = new Instance(metaclass, m_context);
     
      if (tmStart == null)
      {
         tmStart = new Timestamp(System.currentTimeMillis());
      }
     
      if (period == null || period.longValue() < 0)
      {
         period = Primitive.ZERO_LONG;
      }

      instance.setNew();
      instance.setValue("start", tmStart);
      instance.setValue("period", period);
      instance.setValue("next", tmStart);
      instance.setValue("principal", sUser);
     
      if (accessor instanceof Metaclass)
      {
         TransferObject tobj = new TransferObject(((Metaclass)accessor).getName(), 0);
         Request request = new Request();

         tobj.setEventName(event.getName());
         request.addInvocation(tobj);

         try
         {
            instance.setValue("data", Binary.fromObject(request));
         }
         catch (IOException e)
         {
            throw new RPCException("err.rpc.requestSerialization", e);
         }
      }
      else
      {
         instance.setValue("object", accessor);
         instance.setValue("event", event);
      }

      instance.invoke("create");
     
      return instance;
   }
View Full Code Here

    */
   public void testHeterogeneousJoinWithHomogeneousJoinedQuery() throws Exception
   {
      // Add ZachJr for the duration of this test only...
      Metaclass patientClass = getMetadata().getMetaclass("Patient");
      Instance parent = Query.createRead(patientClass, null, parse("(= (@ firstName) \"Zach\")"),
         null, 1, 0, false, Query.SEC_ALL, m_context).read().getInstance(0);
      Instance child = new Instance(patientClass, m_context);

      child.setNew();
      child.setValue("firstName", "ZachJr");
      child.setValue("lastName", "Tachoma");
      child.setValue("parent", parent);
      commit();

      ReadCountHook defaultCountHook = (ReadCountHook)((SQLAdapter)getMetadata().getDataSource("DefaultRelationalDatabase").getComponent().getInstance(m_context)).getSQLHook();
      ReadCountHook externalCountHook = (ReadCountHook)((SQLAdapter)getMetadata().getDataSource("ExternalRelationalDatabase").getComponent().getInstance(m_context)).getSQLHook();
      String sRegularJoinAttributes = "(firstName lastName (children firstName lastName (visits reason (requests code))))";
View Full Code Here

TOP

Related Classes of nexj.core.runtime.Instance

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.