Package nexj.core.meta.persistence

Examples of nexj.core.meta.persistence.PersistenceMapping


    * @return The persistence mapping object.
    * @throws MetadataLookupException if the persistence mapping does not exist.
    */
   public PersistenceMapping getPersistenceMapping(DataSource dataSource)
   {
      PersistenceMapping persistenceMapping = (PersistenceMapping)m_persistenceMappingMap.get(dataSource);

      if (persistenceMapping != null)
      {
         return persistenceMapping;
      }
View Full Code Here


         {
            ClassAspect base = (ClassAspect)aspect.getAspect(k);

            for (Iterator itr = base.getPersistenceMappingIterator(); itr.hasNext();)
            {
               PersistenceMapping baseMapping = (PersistenceMapping)itr.next();

               if (aspect.findPersistenceMapping(baseMapping.getDataSource()) == null)
               {
                  PersistenceMapping mapping = baseMapping.create();

                  mapping.setMetaclass(aspect);
                  mapping.setDataSource(baseMapping.getDataSource());

                  aspect.addPersistenceMapping(mapping);
               }
            }
         }

         for (Iterator itr = aspect.getPersistenceMappingIterator(); itr.hasNext();)
         {
            PersistenceMapping mapping = (PersistenceMapping)itr.next();

            try
            {
               mapping.resolveInheritance();
            }
            catch (MetadataValidationException e)
            {
               eh = addException(eh, e);
            }
            catch (UncheckedException e)
            {
               MetadataValidationException x = new MetadataValidationException(e);

               mapping.setProperties(x);
               eh = addException(eh, x);
            }
         }
      }
View Full Code Here

    */
   public AttributeMapping findPersistenceMapping(PersistenceMapping compatible, boolean bInverse)
   {
      assert !bInverse || !m_type.isPrimitive();

      PersistenceMapping mapping = ((bInverse) ? (Metaclass)m_type : m_metaclass).getPersistenceMapping();

      if (mapping != null)
      {
         if (compatible != null)
         {
            mapping = mapping.findMapping(compatible);
         }

         if (mapping != null)
         {
            if (bInverse)
            {
               PersistenceMapping persistenceMapping = m_metaclass.getPersistenceMapping();

               if (persistenceMapping != null)
               {
                  return persistenceMapping.findClassMapping(this, mapping);
               }
            }
            else
            {
               return mapping.getAttributeMapping(this);
View Full Code Here

    */
   protected void addCreateDependencies(UnitOfWork uow, Instance instance, Work primaryWork)
   {
      boolean bOID = (instance.getOID() != null);
      Metaclass metaclass = instance.getMetaclass();
      PersistenceMapping mapping = instance.getPersistenceMapping();

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

         if (attributeMapping == null)
         {
            continue;
         }

         Object value = instance.getValueDirect(i);

         // The value cannot be undefined, the Instance.validate() has taken care of that.

         if (attribute.getType().isPrimitive())
         {
            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
View Full Code Here

    * @param primaryWork The work item for which to add the dependencies.
    */
   protected void addUpdateDependencies(UnitOfWork uow, Instance instance, Work primaryWork)
   {
      Metaclass metaclass = instance.getMetaclass();
      PersistenceMapping mapping = instance.getPersistenceMapping();

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

         if (attributeMapping == null)
         {
            continue;
         }

         Object value = instance.getValueDirect(i);

         if (value == Undefined.VALUE || ObjUtil.equal(value, instance.getOldValueDirect(i)))
         {
            continue;
         }

         if (attribute.getType().isPrimitive())
         {
            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
View Full Code Here

   /**
    * @return The persistence mapping for this instance. Can be null.
    */
   public PersistenceMapping getPersistenceMapping()
   {
      PersistenceMapping mapping = m_metaclass.getPersistenceMapping();

      if (mapping != null && mapping.isDynamic())
      {
         mapping = ((PersistenceResolver)mapping.getDataSource().getComponent().getInstance(m_context)).getMapping(this);
      }

      return mapping;
   }
View Full Code Here

   /**
    * @return The data source fragment name of the instance. Can be null.
    */
   public String getFragmentName()
   {
      PersistenceMapping mapping = m_metaclass.getPersistenceMapping();
      String sName = null;

      if (mapping != null && mapping.isDynamic())
      {
         sName = ((PersistenceResolver)mapping.getDataSource().getComponent().getInstance(m_context)).getFragmentName(this);
      }

      if (mapping == null)
      {
         return null;
      }

      if (sName == null)
      {
         sName = m_context.getUnitOfWork().getFragmentName(mapping.getDataSource().getFragmentCount() != 1);
      }

      if (sName != null && sName.length() == 0)
      {
         sName = null;
View Full Code Here

   {
      assert !isLazy();

      if (m_nState == NEW || m_nState == DIRTY)
      {
         PersistenceMapping persistenceMapping = getPersistenceMapping();
         PersistenceAdapter adapter = (persistenceMapping == null) ? null :
            (PersistenceAdapter)persistenceMapping.getDataSource()
               .getComponent().getInstance(m_context);
         ValidationException e = null;

         for (int i = 0, nCount = m_metaclass.getInstanceAttributeCount(); i != nCount; ++i)
         {
            Attribute attribute = m_metaclass.getInstanceAttribute(i);
            Object value = getValueDirect(i);

            if (value instanceof Undefined)
            {
               if ((m_nState == NEW || value == Invalid.VALUE) &&
                  (attribute.isPersistent() ||
                     (attribute.isRequired() ||
                        attribute.getValidationFunction() != null ||
                        m_nState == NEW && attribute.getInitializer() != Undefined.VALUE ||
                        attribute.isConstrained() && attribute.getEnumeration() != attribute.getType()) && bFull))
               {
                  value = getValue(i);
               }
               else
               {
                  continue;
               }
            }

            if (m_nState == DIRTY && !isDirty(i))
            {
               continue;
            }

            Type type = attribute.getType();

            if (attribute.isRequired())
            {
               boolean bMissing = (value == null);

               if (!bMissing)
               {
                  if (type.isPrimitive())
                  {
                     bMissing = (type == Primitive.STRING && value.equals(""));
                  }
                  else
                  {
                     bMissing = (attribute.isCollection() && ((InstanceList)value).isEmpty());
                  }
               }

               if (bMissing)
               {
                  e = createValidationException(e);

                  ValidationException x = new ValidationException("err.validation.requiredAttribute",
                     new Object[]{new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});

                  x.setClassName(m_metaclass.getName());
                  x.setOIDHolder(this);
                  e.addException(attribute.getName(), x);
               }
            }

            if (attribute.getMaxLength() > 0)
            {
               int nLength = 0;

               if (value instanceof String)
               {
                  nLength = ((String)value).length();
               }
               else if (value instanceof Binary)
               {
                  nLength = ((Binary)value).getData().length;
               }

               if (nLength > attribute.getMaxLength())
               {
                  e = createValidationException(e);

                  ValidationException x = new ValidationException("err.validation.maxDataLength",
                     new Object[]{Primitive.createInteger(attribute.getMaxLength()),
                        new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});

                  x.setClassName(m_metaclass.getName());
                  x.setOIDHolder(this);
                  e.addException(attribute.getName(), x);
               }
            }

            if (bFull)
            {
               if (attribute.isConstrained() &&
                  attribute.getEnumeration() != type &&
                  value != null && e == null)
               {
                  if (((InstanceList)attribute.getEnumeration().invoke("read",
                     new Object[]{null, (type.isPrimitive()) ?
                        Pair.attribute(Metaclass.ENUMERATION_VALUE).eq(value) :
                        new Pair(Symbol.AT).eq(value), null, Primitive.createInteger(-1),
                        Primitive.ZERO_INTEGER, Boolean.FALSE})).isEmpty())
                  {
                     e = createValidationException(e);

                     ValidationException x = new ValidationException("err.validation.enumerationValue",
                        new Object[]{new StringId(attribute.getEnumeration().getCaption()),
                           new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});

                     x.setClassName(m_metaclass.getName());
                     x.setOIDHolder(this);
                     e.addException(attribute.getName(), x);
                  }
               }

               if (attribute.getValidationFunction() != null && e == null)
               {
                  Object result = m_context.getMachine().invoke(attribute.getValidationFunction(), this, value, null);

                  if (result != null)
                  {
                     ValidationException x = null;

                     if (result instanceof Boolean)
                     {
                        if (!((Boolean)result).booleanValue())
                        {
                           x = new ValidationException("err.validation.valueRange",
                              new Object[]{new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});
                        }
                     }
                     else if (result instanceof String)
                     {
                        x = new ValidationException((String)result);
                     }
                     else if (result instanceof Pair)
                     {
                        x = new ValidationException((String)((Pair)result).getHead(),
                              Pair.toArray(((Pair)result).getNext()));
                     }

                     if (x != null)
                     {
                        x.setClassName(m_metaclass.getName());
                        x.setOIDHolder(this);
                        e = createValidationException(e);
                        e.addException(attribute.getName(), x);
                     }
                  }
               }
            }

            if (type.isPrimitive() && attribute.isPersistent() && adapter != null)
            {
               try
               {
                  adapter.validate(persistenceMapping.getAttributeMapping(attribute), value);
               }
               catch (ValidationException x)
               {
                  x.setClassName(m_metaclass.getName());
                  x.setOIDHolder(this);
View Full Code Here

    * @return The modified attribute list.
    */
   public Pair load(Pair attributes, boolean bSafe)
   {
      boolean bRead = isLazy();
      PersistenceMapping mapping = getPersistenceMapping();

      // Optimization: Avoid load when invoking event on lazy instance whose metaclass is not polymorphic
      if (bRead && mapping != null && !mapping.isTypeCodeDispatched() && mapping.getLockingAttribute() == null)
      {
         setMetaclass(m_metaclass);
         bRead = false;
      }

      if (m_nState != NEW && m_nState != DELETED && mapping != null)
      {
         boolean bLoad = bRead;

         if (!bRead)
         {
            m_nFlags &= ~LOAD_READ;
            attributes = preload(attributes, null, true);
            bRead = (m_nFlags & LOAD_READ) != 0;
         }

         if (bRead)
         {
            Query query = Query.createRead(m_metaclass, attributes,
               new Pair(Symbol.AT).eq(m_oid), null, -1, 0, false,
               Query.SEC_NONE, m_context);

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

               if ((isLazy() || getValueDirect(attribute.getOrdinal()) == Undefined.VALUE) &&
                  !attribute.isLazy())
               {
                  query.addAttribute(Query.ASSOC_QUERY, attribute, null, false, Query.OUTPUT_EAGER);
               }
            }

            if (query.getFirstOutputField() != null &&
               (query.getFirstOutputField().getNext() != null ||
                  query.getFirstOutputField().getAttribute() !=
                  mapping.getLockingAttribute()) ||
               query.getAssocCount(Query.ASSOC_QUERY) > 0)
            {
               if (s_logger.isDebugEnabled())
               {
                  String sMsg = "Loading attributes " + attributes + " of " + this;
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);
                  }
               }
            }
         }
      }

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

         if (obj instanceof SerializablePropertyMap)
         {
            SerializablePropertyMap map = (SerializablePropertyMap)obj;

            column = ((RelationalPrimitiveMapping)mapping.getAttributeMapping(m_serializedVariablesAttribute)).getColumn();
            sqlWork = (SQLWork)work;

            if (sqlWork.getTable() == column.getTable())
            {
               if (column.getType().equals(Primitive.STRING))
View Full Code Here

TOP

Related Classes of nexj.core.meta.persistence.PersistenceMapping

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.