Package nexj.core.runtime

Examples of nexj.core.runtime.Instance


   public void testReadAssocVirtualFKToRelationalNone() throws Exception
   {
      Metaclass virtualClass = getMetadata().getMetaclass("Animal");
      InstanceArrayList list;
      Instance inst, inst2;

      list = (InstanceArrayList)Query.createRead(virtualClass,
         parse("(commonName (virtFKToRelNone firstName lastName))"),
         null,
         null,
         -1, 0, false, Query.SEC_NODE, m_context).read();

      assertEquals(4, list.getCount());
      inst = list.getInstance(0);
      assertEquals("Dog", inst.getValue("commonName"));
      inst2 = (Instance)inst.getValue("virtFKToRelNone");
      assertEquals("Zoe", inst2.getValue("firstName"));

      inst = list.getInstance(1);
      assertEquals("Cat", inst.getValue("commonName"));
      inst2 = (Instance)inst.getValue("virtFKToRelNone");
      assertEquals("Joe", inst2.getValue("firstName"));
      assertEquals("Test", inst2.getValue("lastName"));
     
      inst = list.getInstance(2);
      assertEquals("Spider", inst.getValue("commonName"));
      assertNull(inst.getValue("virtFKToRelNone"));
   }
View Full Code Here


   public void testReadAssocVirtualFKToRelationalAttr() throws Exception
   {
      Metaclass virtualClass = getMetadata().getMetaclass("Animal");
      InstanceArrayList list;
      Instance inst, inst2;

      list = (InstanceArrayList)Query.createRead(virtualClass,
         parse("(commonName (virtFKToRelAttr firstName))"),
         null,
         null,
         -1, 0, false, Query.SEC_NODE, m_context).read();

      assertEquals(4, list.getCount());

      inst = list.getInstance(0);
      assertEquals("Dog", inst.getValue("commonName"));
      assertNull(inst.getValue("virtFKToRelAttr"));

      inst = list.getInstance(2);
      assertEquals("Spider", inst.getValue("commonName"));
      inst2 = (Instance)inst.getValue("virtFKToRelAttr");
      assertEquals("Joe", inst2.getValue("firstName"));
      assertEquals("Test", inst2.getValue("lastName"));

      inst = list.getInstance(3);
      assertEquals("Beaver", inst.getValue("commonName"));
      inst2 = (Instance)inst.getValue("virtFKToRelAttr");
      assertEquals("Zoe", inst2.getValue("firstName"));


      // Read the reverse attribute
      Metaclass relationalClass = getMetadata().getMetaclass("PetOwner");

      list = (InstanceArrayList)Query.createRead(relationalClass,
         parse("(firstName (relAttrFromVirtFK commonName))"),
         null,
         null,
         -1, 0, false, Query.SEC_NODE, m_context).read();

      assertEquals(2, list.getCount());
      inst = list.getInstance(0);
      assertEquals("Joe", inst.getValue("firstName"));
      inst2 = (Instance)inst.getValue("relAttrFromVirtFK");
      assertEquals("Spider", inst2.getValue("commonName"));

      inst = list.getInstance(1);
      assertEquals("Zoe", inst.getValue("firstName"));
      inst2 = (Instance)inst.getValue("relAttrFromVirtFK");
      assertEquals("Beaver", inst2.getValue("commonName"));
      assertEquals("Castor canadensis", inst2.getValue("scientificName"));
   }
View Full Code Here

            Set containerSet = query.getParentInstances(parentOID);

            // composition data are stored *in* a parent, so must have one and only one
            assert containerSet.size() == 1;

            Instance container = (Instance)containerSet.iterator().next();
            Cursor cursor = new VirtualCursor(query, childList.iterator())// creates Instances
            Attribute assoc = query.getAttribute();

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

               while ((childInst = cursor.next()) != null)
               {
View Full Code Here

               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
            {
               cursor.close();
            }
         }
         else if (value instanceof TransferObject)
         {
            Cursor cursor = new VirtualCursor(getCompositionQuery(attribute, query), new SingletonIterator(value));

            try
            {
               instance.setOldValueDirect(nOrdinal, cursor.next());
            }
            finally
            {
               cursor.close();
            }
         }
         else if (value instanceof OID)
         {
            // Associate an instance, creating a lazy instance if not loaded
            OID oid = (OID)value;
            Metaclass assocClass = (Metaclass)attribute.getType();
            InstanceRef assocRef = m_query.getInvocationContext().findInstanceRef(assocClass, oid);
            Instance assoc = (assocRef == null) ? null : assocRef.getInstance();

            if (assoc == null)
            {
               assoc = new Instance(assocClass, true, m_query.getInvocationContext());
               assocRef = assoc.cache(oid);
            }

            if (bOverwrite || instance.getOldValueDirect(nOrdinal) == Undefined.VALUE)
            {
               instance.setOldValueDirect(nOrdinal, assoc);
View Full Code Here

      VirtualDataSourceFragment fragment = (VirtualDataSourceFragment)getFragment();
      ArrayList tobjList = new ArrayList(nEnd - nStart);

      for (int i = nStart; i < nEnd; i++)
      {
         Instance instance = workArray[i].getInstance();
         TransferObject tobj = new TransferObject(instance.getOID(), metaclass.getName(), EVENT, nAttributeCount);

         for (int k = 0; k < nAttributeCount; k++)
         {
            if (instance.isDirty(k))
            {
               Attribute attribute = metaclass.getInstanceAttribute(k);
               AttributeMapping attrMapping = m_mapping.getAttributeMapping(attribute);

               if (attrMapping != null)
               {
                  int nKeyPart = (attrMapping instanceof VirtualPrimitiveMapping) ?
                     ((VirtualPrimitiveMapping)attrMapping).getObjectKeyPart() : -1;
                  Object value;

                  if (nKeyPart >= 0 && instance.getOID() != null)
                  {
                     value = instance.getOID().getValue(nKeyPart);
                  }
                  else
                  {
                     value = instance.getValue(k);
                  }

                  setValue(tobj, attribute, value);
               }
            }
         }

         tobjList.add(tobj);
      }

      try
      {
         operation.invoke(tobjList, m_adapter.getInvocationContext().getMachine(), fragment);
      }
      finally
      {
         // Sets the locking value and OID, if provided by the create mapping.
         for (int k = nStart; k < nEnd; k++)
         {
            Instance instance = workArray[k].getInstance();
            TransferObject tobj = (TransferObject)tobjList.get(k - nStart);

            if (nLockingOrdinal >= 0)
            {
               instance.setValueDirect(nLockingOrdinal,
                  tobj.findValue(sLockingAttributeName, instance.getValueDirect(nLockingOrdinal)));
            }

            if (tobj.getOID() != null)
            {
               instance.setOID(tobj.getOID());
            }

            if (instance.getOID() == null)
            {
               throw new PersistenceException("err.persistence.requiredOID", new Object[]{instance.getMetaclass().getName()});
            }
         }
      }
   }
View Full Code Here

         boolean bFull = update.isFull();

         for (int k = nStart; k < nEnd; k++)
         {
            VirtualUpdate work = (VirtualUpdate)workArray[k];
            Instance instance = work.getInstance();
            TransferObject tobj = new TransferObject(instance.getOID(), metaclass.getName(), EVENT, nUpdateAttrCount);

            if (bFull)
            {
               for (int m = 0, nCount = metaclass.getInstanceAttributeCount(); m < nCount; m++)
               {
                  Attribute attribute = metaclass.getInstanceAttribute(m);

                  if (bIgnoreDirty || instance.isDirty(m))
                  {
                     setValue(tobj, attribute, instance.getValue(m));
                  }
               }
            }
            else
            {
               for (int m = updateAttrSet.nextSetBit(0); m >= 0; m = updateAttrSet.nextSetBit(m + 1))
               {
                  Attribute attribute = metaclass.getInstanceAttribute(m);

                  if (bIgnoreDirty || instance.isDirty(m))
                  {
                     setValue(tobj, attribute, instance.getValue(m));
                  }
               }
            }

            if (nLockingOrdinal >= 0)
            {
               tobj.setValue(sLockingAttributeName, instance.getValue(nLockingOrdinal));
            }

            tobjList.add(tobj);
         }

         boolean bError = true;

         try
         {
            update.invoke(tobjList, m_adapter.getInvocationContext().getMachine(), fragment);
            bError = false;
         }
         finally
         {
            // Set new locking values
            if (nLockingOrdinal >= 0)
            {
               for (int k = nStart; k < nEnd; k++)
               {
                  Instance instance = ((VirtualUpdate)workArray[k]).getInstance();
                  TransferObject tobj = (TransferObject)tobjList.get(k - nStart);
                  Object locking = tobj.findValue(sLockingAttributeName);

                  if (locking == null || ObjUtil.equal(locking, instance.getValue(nLockingOrdinal)))
                  {
                     if (!bError)
                     {
                        PersistenceException ex = new PersistenceException("err.persistence.virtual.lockingNotModified",
                           new Object[]{sLockingAttributeName, instance.getMetaclass().getName()});

                        ex.setValue("result", tobj);

                        throw ex;
                     }
                  }
                  else
                  {
                     instance.setValueDirect(nLockingOrdinal, locking);
                  }
               }
            }
         }
      }
View Full Code Here

    */
   public static Object transferState(Object obj, Set valueSet, Lookup identityMap, int nTF)
   {
      if (obj instanceof Instance)
      {
         Instance instance = (Instance)obj;
         Object result = identityMap.get(obj);

         if (result != null)
         {
            return result;
         }

         Metaclass metaclass = instance.getMetaclass();

         if ((nTF & (TF_READABLE | TF_ALL)) == TF_READABLE && !instance.isReadable())
         {
            return ((nTF & (TF_HIDDEN | TF_REF)) == (TF_HIDDEN | TF_REF)) ? new TransferObject(metaclass.getName(), 0) : null;
         }

         TransferObject tobj = new TransferObject(metaclass.getName());

         tobj.setOID(instance.getOID());
         identityMap.put(obj, tobj);

         if (instance.getOID() != null && metaclass.getPersistenceMapping() != null)
         {
            if (valueSet == null || !valueSet.contains(instance))
            {
               return tobj;
            }

            tobj.setEventName("create");
         }

         nTF |= TF_READABLE | TF_REF;

         for (int i = 0, nCount = metaclass.getInstanceAttributeCount(); i < nCount; ++i)
         {
            Attribute attribute = metaclass.getInstanceAttribute(i);
            Type type = attribute.getType();
            Object value;

            if (type.isPrimitive())
            {
               value = instance.getValue(attribute.getOrdinal());

               if ((nTF & TF_SERIALIZABLE) != 0 &&
                  type == Primitive.ANY &&
                  !TextMarshaller.isSerializable(value))
               {
                  continue;
               }
            }
            else
            {
               value = transferState(instance.getValue(attribute.getOrdinal()), valueSet, identityMap, nTF);
            }

            tobj.setValue(attribute.getName(), value);
         }

         if ((nTF & TF_CACHE) != 0)
         {
            instance.setCached(true);
         }

         return tobj;
      }

View Full Code Here

    */
   public static Object transferUpdates(Object obj, Lookup identityMap, int nTF)
   {
      if (obj instanceof Instance)
      {
         Instance instance = (Instance)obj;
         Object result = identityMap.get(obj);

         if (result != null)
         {
            return result;
         }

         Metaclass metaclass = instance.getMetaclass();

         if ((nTF & (TF_READABLE | TF_ALL)) == TF_READABLE && !instance.isReadable())
         {
            return ((nTF & (TF_HIDDEN | TF_REF)) == (TF_HIDDEN | TF_REF)) ? new TransferObject(metaclass.getName(), 0) : null;
         }

         TransferObject tobj = new TransferObject(metaclass.getName());
         tobj.setOID(instance.getOID());

         switch (instance.getState())
         {
            case Instance.NEW:
               tobj.setEventName("create");
               nTF |= TF_STATE;
               break;

            case Instance.DIRTY:
               tobj.setEventName("update");
               break;

            case Instance.DELETED:
               tobj.setEventName("delete");
               break;
         }

         identityMap.put(obj, tobj);

         nTF |= TF_READABLE | TF_REF;

         for (int i = 0, nCount = metaclass.getInstanceAttributeCount(); i < nCount; ++i)
         {
            Attribute attribute = metaclass.getInstanceAttribute(i);

            if (attribute.isPersistent() && instance.isDirty(attribute.getOrdinal()))
            {
               Type type = attribute.getType();
               Object value;

               if (type.isPrimitive())
               {
                  value = instance.getValue(attribute.getOrdinal());

                  if ((nTF & TF_SERIALIZABLE) != 0 &&
                     type == Primitive.ANY &&
                     !TextMarshaller.isSerializable(value))
                  {
                     continue;
                  }
               }
               else
               {
                  value = transferUpdates(instance.getValue(attribute.getOrdinal()), identityMap, nTF);
               }

               tobj.setValue(attribute.getName(), value);
            }
         }

         // Set the read access attribute value
         Attribute attribute = metaclass.getReadAccessAttribute();

         if (attribute != null)
         {
            tobj.setValue(attribute.getName(), Boolean.valueOf(instance.isReadable()));
         }

         return tobj;
      }

View Full Code Here

         return;
      }

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

         instanceSet.add(instance);

         for (; attributes != null; attributes = attributes.getNext())
         {
            Object head = attributes.getHead();

            if (head instanceof Pair)
            {
               Pair field = (Pair)head;
               Symbol sym = (Symbol)field.getHead();

               if (sym == Symbol.ATAT)
               {
                  field = field.getNext();

                  if (instance.getMetaclass().getMetadata().getMetaclass(
                     ((Symbol)field.getHead()).getName()).isUpcast(instance.getMetaclass()))
                  {
                     collectInstances(instance, field.getNext(), instanceSet);
                  }
               }
               else
               {
                  collectInstances(instance.getValue(sym.getName()), field.getNext(), instanceSet);
               }
            }
            else
            {
               collectInstances(instance.getValue(((Symbol)head).getName()), null, instanceSet);
            }
         }
      }

      if (obj instanceof InstanceList)
View Full Code Here

      try
      {
         context.setSecure(false);

         Instance audit = new Instance(context.getMetadata().getMetaclass(sAuditClass), context);

         audit.setNew();
         audit.setValue("object", instance);
         audit.setValue("class", sClass);
         audit.setValue("event", sEvent);
         audit.setValue("argCount", Primitive.createInteger(nArgCount));
         audit.setValue("user", context.getUser());

         if (nMode != AUDIT_NONE && (instance != null || nMode == AUDIT_MAP))
         {
            PropertyMap map = (PropertyMap)audit.getValue("values");

            for (PropertyIterator itr = tobj.getIterator(); itr.hasNext();)
            {
               itr.next();

               if (nMode <= AUDIT_MAP)
               {
                  Object value;

                  if (nMode == AUDIT_NAMES)
                  {
                     value = null;
                  }
                  else
                  {
                     value = itr.getValue();

                     if (!TextMarshaller.isSerializable(value))
                     {
                        value = String.valueOf(value);
                     }
                  }

                  map.setValue(itr.getName(), value);
               }
               else
               {
                  Attribute attr = instance.getMetaclass().findAttribute(itr.getName());

                  if (attr != null && !attr.isStatic() && attr.getVisibility() == Metaclass.PUBLIC)
                  {
                     Object value = instance.getValueDirect(attr.getOrdinal());

                     if (!(value instanceof Undefined))
                     {
                        if (!TextMarshaller.isSerializable(value))
                        {
                           value = String.valueOf(value);
                        }

                        if (nMode == AUDIT_VALUES)
                        {
                           if (value != null)
                           {
                              map.setValue(attr.getName(), value);
                           }
                        }
                        else
                        {
                           Object oldValue = instance.getOldValueDirect(attr.getOrdinal());

                           if (!ObjUtil.equal(value, oldValue))
                           {
                              map.setValue(attr.getName(), value);

                              if (!(oldValue instanceof Undefined))
                              {
                                 map.setValue('-' + attr.getName(), oldValue);
                              }
                           }
                        }
                     }
                  }
               }
            }
         }

         audit.invoke("create");

         return audit;
      }
      finally
      {
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.