Package nexj.core.scripting

Examples of nexj.core.scripting.Pair


   /**
    * @see nexj.core.meta.persistence.PersistenceMapping#getSortKeys(nexj.core.meta.Attribute[], nexj.core.meta.persistence.PersistenceMapping[], nexj.core.meta.Attribute[])
    */
   public Pair getSortKeys(Attribute[] assocs, PersistenceMapping[] mappings, Attribute[] restrictions)
   {
      Pair sortKeys = null;
      Pair lastKey = null;
      BitSet filterSet = null;
      BitSet optionalSet = null;
      int nFilterCount = 0;

      // Create a set of attributes that participate in equality
      // comparisons due to the associations.
      if (assocs != null && assocs.length > 0)
      {
         filterSet = new BitSet(m_metaclass.getInstanceAttributeCount());

         for (int i = 0; i < assocs.length; i++)
         {
            PersistenceMapping mapping = mappings[i];

            if (mapping == null)
            {
               continue;
            }

            AttributeMapping attributeMapping = mapping.getAttributeMapping(assocs[i]);

            if (attributeMapping instanceof VirtualClassMapping)
            {
               VirtualClassMapping classMapping = (VirtualClassMapping)attributeMapping;

               if (classMapping.getMapping() == this && classMapping.getKey(true) instanceof VirtualKey)
               {
                  VirtualKey key = (VirtualKey)classMapping.getKey(true);

                  if (!key.isUnique())
                  {
                     for (int k = 0, nCount = key.getAttributeCount(); k < nCount; k++)
                     {
                        Attribute attr = key.getAttribute(k);

                        assert attr.getMetaclass() == m_metaclass;
                        assert !attr.getType().isPrimitive();

                        filterSet.set(attr.getOrdinal());
                     }
                  }
               }
            }
         }

         nFilterCount = filterSet.cardinality();
      }

      // Create a set of attributes that participate in equality
      // comparisons and can be optionally excluded from the index.
      if (restrictions != null && restrictions.length > 0)
      {
         optionalSet = new BitSet(m_metaclass.getInstanceAttributeCount());

         for (int i = 0; i < restrictions.length; i++)
         {
            optionalSet.set(restrictions[i].getOrdinal());
         }
      }

      // Collect the sort keys
      for (int nSortKey = 0; nSortKey < m_sortKeyList.size(); nSortKey++)
      {
         VirtualSortKey sortKey = (VirtualSortKey)m_sortKeyList.get(nSortKey);

         // Ignore sort keys that do not start with the columns specified in the
         // association filter, in arbitrary order
         if (nFilterCount > 0)
         {
            if (sortKey.getAttributeCount() < nFilterCount)
            {
               continue;
            }

            int i;

            for (i = 0; i < nFilterCount; i++)
            {
               Attribute attr = sortKey.getAttribute(i);

               if (attr == null || !filterSet.get(attr.getOrdinal()))
               {
                  break;
               }
            }

            if (i < nFilterCount)
            {
               continue;
            }
         }

         // Find out if the sort key includes the primary key
         Key pk = getObjectKey();
         boolean bMatch = false;
         boolean bAscending = true;

         for (int i = 0, k = -1; i < sortKey.getAttributeCount(); i++)
         {
            Attribute attr = sortKey.getAttribute(i);

            if (attr == null)
            {
               bMatch = true;
               bAscending = sortKey.isAscending(i);

               break;
            }

            if (optionalSet != null && optionalSet.get(attr.getOrdinal()))
            {
               continue;
            }

            if (getAttributeMapping(attr) instanceof VirtualPrimitiveMapping)
            {
               VirtualPrimitiveMapping mapping = (VirtualPrimitiveMapping)getAttributeMapping(attr);

               if (mapping.getObjectKeyPart() >= 0)
               {
                  if (k == -1)
                  {
                     if (mapping.getObjectKeyPart() == 0)
                     {
                        k = 1;
                        bAscending = sortKey.isAscending(i);

                        continue;
                     }

                     break;
                  }

                  if (mapping.getObjectKeyPart() == k)
                  {
                     if ((pk.isPartAscending(0) ^ pk.isPartAscending(k)) != (bAscending ^ sortKey.isAscending(i)))
                     {
                        break;
                     }
                     else
                     {
                        if (k == pk.getPartCount() - 1)
                        {
                           bMatch = true;

                           break;
                        }
                     }
                  }
                  else
                  {
                     break;
                  }
               }
            }
         }

         Pair key = null;

         // If the above is true, then append the primary key
         // at the end of the sort key
         if (bMatch)
         {
            Pair[] objKeyAttributeArray = new Pair[pk.getPartCount()];

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

               if (getAttributeMapping(attr) instanceof VirtualPrimitiveMapping)
               {
                  VirtualPrimitiveMapping mapping = (VirtualPrimitiveMapping)getAttributeMapping(attr);

                  if (mapping.getObjectKeyPart() >= 0)
                  {
                     objKeyAttributeArray[mapping.getObjectKeyPart()] = new Pair(
                        attr.getSymbol(),
                        Boolean.valueOf(bAscending ^ !pk.isPartAscending(mapping.getObjectKeyPart()))
                     );
                  }
               }
            }

            boolean bObjKeyFullyMapped = true;

            for (int i = 0; i < objKeyAttributeArray.length; i++)
            {
               if (objKeyAttributeArray[i] == null)
               {
                  bObjKeyFullyMapped = false;

                  break;
               }
            }

            if (bObjKeyFullyMapped)
            {
               key = Pair.fromArray(objKeyAttributeArray);
            }
            else
            {
               key = new Pair(new Pair(new Pair(Symbol.AT),
                  Boolean.valueOf(bAscending)));
            }
         }
         else
         {
            pk = null;
         }

         int nCount;

         // Advance, stopping at first non-ignorable non-primitive-mapped attribute
         for (nCount = nFilterCount; nCount < sortKey.getAttributeCount(); nCount++)
         {
            Attribute attr = sortKey.getAttribute(nCount);

            if (attr == null || !attr.getType().isPrimitive())
            {
               if (optionalSet == null || attr == null || !optionalSet.get(attr.getOrdinal()))
               {
                  break;
               }
            }
         }

         bMatch = true;

         // Verify that remainder of index is the primary key, or that there is no remaining index
         if (pk != null)
         {
            if (nCount == sortKey.getAttributeCount())
            {
               key = null;
            }
            else
            {
               for (int i = nCount; i < sortKey.getAttributeCount(); i++)
               {
                  Attribute attr = sortKey.getAttribute(i);

                  if (attr != null)
                  {
                     AttributeMapping mapping = getAttributeMapping(attr);

                     if (mapping instanceof VirtualPrimitiveMapping)
                     {
                        if (((VirtualPrimitiveMapping)mapping).getObjectKeyPart() >= 0)
                        {
                           continue;
                        }
                     }

                     bMatch = false;

                     break;
                  }
               }
            }
         }

         if (!bMatch)
         {
            continue;
         }

         // Add non-ignorable primitive-mapped columns to the sort key
         for (int i = nCount - 1; i >= nFilterCount; i--)
         {
            Attribute attr = sortKey.getAttribute(i);

            if (attr.getType().isPrimitive())
            {
               if (optionalSet == null || !optionalSet.get(attr.getOrdinal()))
               {
                  key = new Pair(
                     new Pair(attr.getSymbol(), Boolean.valueOf(sortKey.isAscending(i))),
                     key
                  );
               }
            }
         }

         // Append the new sort key to the end of the sort key list
         if (key != null)
         {
            key = new Pair(
               Boolean.valueOf(pk != null || sortKey.isUnique() && nCount == sortKey.getAttributeCount()),
               key
            );

            if (lastKey == null)
            {
               sortKeys = lastKey = new Pair(key);
            }
            else
            {
               Pair pair;

               // Filter out the duplicates
               for (pair = sortKeys; pair != null; pair = pair.getNext())
               {
                  if (pair.getHead().equals(key))
                  {
                     break;
                  }
               }

               if (pair == null)
               {
                  key = new Pair(key);
                  lastKey.setTail(key);
                  lastKey = key;
               }
            }
         }
View Full Code Here


    */
   public Pair getUniqueKeys()
   {
      Set keySet = new HashHolder(m_sortKeyList.size())// eliminates duplicates
      BitSet keyAttrSet = new BitSet(m_metaclass.getInstanceAttributeCount());
      Pair uniqueKeys = null;

      for (int nSortKey = m_sortKeyList.size() - 1; nSortKey >= 0; nSortKey--)
      {
         VirtualSortKey sortKey = (VirtualSortKey)m_sortKeyList.get(nSortKey);

         if (!sortKey.isUnique())
         {
            continue;
         }

         for (int i = sortKey.getAttributeCount() - 1; i >= 0 ; i--)
         {
            Attribute attr = sortKey.getAttribute(i);

            if (attr == null)
            {
               keyAttrSet.clear();

               break;
            }
            else
            {
               AttributeMapping mapping = getAttributeMapping(attr);

               if (mapping instanceof VirtualPrimitiveMapping)
               {
                  if (((VirtualPrimitiveMapping)mapping).getObjectKeyPart() >= 0)
                  {
                     keyAttrSet.clear();

                     break;
                  }
               }

               keyAttrSet.set(attr.getOrdinal());
            }
         }

         if (!keyAttrSet.isEmpty() && keySet.add(keyAttrSet))
         {
            Pair key = null;

            for (int i = keyAttrSet.nextSetBit(0); i >= 0; i = keyAttrSet.nextSetBit(i + 1))
            {
               key = new Pair(m_metaclass.getInstanceAttribute(i).getSymbol(), key);
            }

            uniqueKeys = new Pair(key, uniqueKeys);
            keyAttrSet.clear();
         }
      }

      return uniqueKeys;
View Full Code Here

               if (classMapping.getKey(true) instanceof Named)
               {
                  m_writer.writeAttribute("destinationKey", ((Named)classMapping.getKey(true)).getName());
               }

               Pair compositionAttributes = classMapping.getCompositionAttributes();

               if (compositionAttributes != null)
               {
                  m_exporter.exportSExpression(compositionAttributes, true, false, null, "attributes");
               }
View Full Code Here

         {
            attrMap.put(head, head);
         }
         else if (head instanceof Pair)
         {
            Pair subAttrList = (Pair)head;
            Lookup subAttrMap = new HashTab();

            if (!(subAttrList.getHead() instanceof Symbol))
            {
               throw new IllegalStateException();
            }

            attrMap.put(subAttrList.getHead(), subAttrMap);
            getCompositionMap(subAttrList.getNext(), subAttrMap);
         }
         else
         {
            throw new IllegalStateException();
         }
View Full Code Here

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

      Pair list = null;
      Lookup.Iterator itr = attrMap.iterator();

      while (itr.hasNext())
      {
         Symbol attribute = (Symbol)itr.next();
         Object value = itr.getValue();

         if (value instanceof Lookup)
         {
            value = new Pair(attribute, getAttributes((Lookup)value));
         }

         list = new Pair(value, list);
      }

      return list;
   }
View Full Code Here

      for (; attributes != null; attributes = attributes.getNext())
      {
         Object head = attributes.getHead();
         String sName;
         Pair pair;

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

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

               if (metaclass.getMetadata().getMetaclass(
                  ((Symbol)field.getHead()).getName()).isUpcast(metaclass))
               {
                  transfer(instance, field.getNext(), diffMap, identityMap, nTF);
               }

               continue;
            }

            if (sym == Symbol.COLON)
            {
               sName = ((Symbol)field.getNext().getHead()).getName();
               tobj.setValue(sName, transfer(instance.findAnnotation(sName), null, diffMap, identityMap, nTF));

               continue;
            }

            sName = sym.getName();
            pair = field.getNext();
         }
         else
         {
            sName = ((Symbol)head).getName();
View Full Code Here

         {
            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);
View Full Code Here

         {
            Object where = mapping.getWhere();

            if (mapping.isLocal() && parentComposite.getMapping() != null)
            {
               Pair eq = Pair.list(Symbol.ATAT, ((ObjectMessagePartMapping)
                  parentComposite.getMapping()).getMetaclass().getSymbol(),
                  mapping.getAttribute().getSymbol()).eq(m_instanceByTobjMap.get(parentTobj));

               where = eq.and(where);
            }

            InstanceList allInstances = Query.createRead(mapping.getMetaclass(), getAttributes(composite),
               where, null, -1, 0, false, Query.SEC_NONE, m_context).read();
View Full Code Here

                  m_instanceByTobjMap.put(tobj, null);
               }
               else if (!mapping.isLocal() || m_instanceByTobjMap.contains(parentTobj))
               {
                  Pair where = getWhere1(tobj, composite, parentTobj, parentComposite, null, true);
                  InstanceList list = null;

                  if (where != EMPTY && !(mapping.isLocal() && m_instanceByTobjMap.get(parentTobj) == null))
                  {
                     if (mapping.isLocal())
                     {
                        Pair eq = Pair.list(Symbol.ATAT, ((ObjectMessagePartMapping)
                           parentComposite.getMapping()).getMetaclass().getSymbol(),
                           mapping.getAttribute().getSymbol()).eq(m_instanceByTobjMap.get(parentTobj));

                        if (where != null)
                        {
View Full Code Here

    * @return a list of attribute to be read from the datasource
    */
   private Pair getAttributes(CompositeMessagePart composite)
   {
      ObjectMessagePartMapping mapping = (ObjectMessagePartMapping)composite.getMapping();
      Pair attributes = null;

      for (int i = composite.getPartCount() - 1; i >= 0; --i)
      {
         ObjectMessagePartMapping partMapping = ((ObjectMessagePartMapping)composite.getPart(i).getMapping());
         Attribute attr = partMapping.getAttribute();
        
         if (attr != null && !attr.isCollection())
         {
            attributes = new Pair(attr.getSymbol(), attributes);

            if (partMapping.getAccessAttribute() != null)
            {
               attributes = new Pair(partMapping.getAccessAttribute().getSymbol(), attributes);
            }
         }
      }

      if (composite.getParent() == null && mapping.getAccessAttribute() != null)
      {
         attributes = new Pair(mapping.getAccessAttribute().getSymbol(), attributes);
      }

      if (m_context.isSecure())
      {
         Metaclass metaclass = mapping.getMetaclass();

         if (metaclass.getReadAccessAttribute() != null)
         {
            attributes = new Pair(metaclass.getReadAccessAttribute().getSymbol(), attributes);
         }

         if (mapping.isUpdate() && metaclass.getUpdateAccessAttribute() != null)
         {
            attributes = new Pair(metaclass.getUpdateAccessAttribute().getSymbol(), attributes);
         }
      }

      return attributes;
   }
View Full Code Here

TOP

Related Classes of nexj.core.scripting.Pair

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.