public int compare(
     OrmSpecifiedPersistentAttribute attribute1,
     OrmSpecifiedPersistentAttribute attribute2) {
   int seq1 = attribute1.getMapping().getXmlSequence();
   int seq2 = attribute2.getMapping().getXmlSequence();
   return (seq1 == seq2) ? 0 : (seq1 < seq2) ? -1 : 1;
 }
  public void testModifyPrivateOwned() throws Exception {
    createTestTypeWithOneToOneAttribute();

    OrmPersistentType ormPersistentType =
        getEntityMappings()
            .addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
    OrmSpecifiedPersistentAttribute ormPersistentAttribute =
        ormPersistentType.addAttributeToXml(
            ormPersistentType.getAttributeNamed("oneToOne"),
            MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
    EclipseLinkOneToOneMapping contextOneToOne =
        (EclipseLinkOneToOneMapping) ormPersistentAttribute.getMapping();
    XmlEntity resourceEntity = (XmlEntity) getXmlEntityMappings().getEntities().get(0);
    XmlOneToOne resourceOneToOne =
        (XmlOneToOne) resourceEntity.getAttributes().getOneToOnes().get(0);

    // check defaults

    assertFalse(resourceOneToOne.isPrivateOwned());
    assertFalse(contextOneToOne.getPrivateOwned().isPrivateOwned());

    // set context private owned  to true, check resource

    contextOneToOne.getPrivateOwned().setPrivateOwned(true);

    assertTrue(resourceOneToOne.isPrivateOwned());
    assertTrue(contextOneToOne.getPrivateOwned().isPrivateOwned());

    // set context private owned back to false, check resource

    contextOneToOne.getPrivateOwned().setPrivateOwned(false);
  }
  public void testModifyConverter() throws Exception {
    createTestEntityWithBasicMapping();

    OrmPersistentType ormPersistentType =
        getEntityMappings()
            .addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
    OrmSpecifiedPersistentAttribute ormPersistentAttribute =
        ormPersistentType.addAttributeToXml(
            ormPersistentType.getAttributeNamed("id"), MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY);
    EclipseLinkOrmBasicMapping ormBasicMapping =
        (EclipseLinkOrmBasicMapping) ormPersistentAttribute.getMapping();
    EclipseLinkStructConverter ormConverter =
        ormBasicMapping.getConverterContainer().addStructConverter("structConverter", 0);
    XmlStructConverter converterResource =
        ((XmlBasic) getXmlEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0))
            .getStructConverters()
            .get(0);

    assertEquals(null, ormConverter.getConverterClass());
    assertEquals(null, converterResource.getConverter());

    // set converter class in the context model, verify resource model updated
    ormConverter.setConverterClass("foo");
    assertEquals("foo", ormConverter.getConverterClass());
    assertEquals("foo", converterResource.getConverter());

    ormConverter.setConverterClass(null);
    assertEquals(null, ormConverter.getConverterClass());
    assertEquals(null, converterResource.getConverter());
  }
 /**
  * Return the specified attribute corresponding to the specified Java resource field, ignoring the
  * specified excluded attribute (since there can be more than one specified attribute per Java
  * resource attribute; albeit erroneously).
  */
 protected OrmSpecifiedPersistentAttribute getSpecifiedAttributeFor(
     JavaResourceField javaResourceField, OrmSpecifiedPersistentAttribute exclude) {
   for (OrmSpecifiedPersistentAttribute ormAttribute : this.getSpecifiedAttributes()) {
     if (ormAttribute == exclude) {
       continue; // skip
     }
     if (ormAttribute.isFor(javaResourceField)) {
       return ormAttribute;
     }
   }
   return null;
 }
 @Override
 public Iterable<String> getCompletionProposals(int pos) {
   Iterable<String> result = super.getCompletionProposals(pos);
   if (result != null) {
     return result;
   }
   result = this.mapping.getCompletionProposals(pos);
   if (result != null) {
     return result;
   }
   for (OrmSpecifiedPersistentAttribute attribute : this.getSpecifiedAttributes()) {
     result = attribute.getCompletionProposals(pos);
     if (result != null) {
       return result;
     }
   }
   return null;
 }
  /**
   * <em>Silently</em> add the new default attribute before removing the specified attribute, or the
   * <em>update</em> will discover the missing default attribute and add it preemptively.
   */
  public OrmPersistentAttribute removeAttributeFromXml(
      OrmSpecifiedPersistentAttribute specifiedAttribute) {
    if (specifiedAttribute.isVirtual()) {
      throw new IllegalArgumentException(
          "Attribute is not specified: " + specifiedAttribute); // $NON-NLS-1$
    }

    int defaultIndex = this.defaultAttributes.size();
    OrmPersistentAttribute defaultAttribute = null;
    // make sure the corresponding resource Java attribute actually exists in the *current* type;
    // do *not* take the context Java attribute directly from the specified ORM
    // attribute we are converting since it may have come from a superclass;
    // instead, use its resource Java attribute (which will match both name and access type,
    // but we still need to check its parent type)
    if (specifiedAttribute.getJavaResourceAttribute() != null) {
      if (specifiedAttribute.getJavaResourceAttribute().getAstNodeType() == AstNodeType.FIELD) {
        JavaResourceField javaResourceField =
            (JavaResourceField) specifiedAttribute.getJavaResourceAttribute();
        if (this.javaResourceFieldWillBeDefault(javaResourceField, specifiedAttribute)) {
          defaultAttribute = this.buildDefaultAttribute(javaResourceField);
          this.defaultAttributes.add(defaultIndex, defaultAttribute);
        }
      } else {
        PropertyAccessor propertyAccessor =
            (PropertyAccessor) specifiedAttribute.getJavaPersistentAttribute().getAccessor();
        JavaResourceMethod resourceGetter = propertyAccessor.getResourceGetter();
        JavaResourceMethod resourceSetter = propertyAccessor.getResourceSetter();

        if (this.javaResourcePropertyWillBeDefault(
            resourceGetter, resourceSetter, specifiedAttribute)) {
          defaultAttribute = this.buildVirtualAttribute(resourceGetter, resourceSetter);
          this.defaultAttributes.add(defaultIndex, defaultAttribute);
        }
      }
    }

    this.removeSpecifiedAttribute(specifiedAttribute); // trigger update

    if (defaultAttribute != null) {
      this.fireItemAdded(DEFAULT_ATTRIBUTES_LIST, defaultIndex, defaultAttribute);
    }
    return defaultAttribute;
  }
  public void testModifyJoinFetch() throws Exception {
    createTestTypeWithOneToOneAttribute();

    OrmPersistentType ormPersistentType =
        getEntityMappings()
            .addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
    OrmSpecifiedPersistentAttribute ormPersistentAttribute =
        ormPersistentType.addAttributeToXml(
            ormPersistentType.getAttributeNamed("oneToOne"),
            MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
    EclipseLinkOneToOneMapping contextOneToOne =
        (EclipseLinkOneToOneMapping) ormPersistentAttribute.getMapping();
    XmlEntity resourceEntity = (XmlEntity) getXmlEntityMappings().getEntities().get(0);
    XmlOneToOne resourceOneToOne =
        (XmlOneToOne) resourceEntity.getAttributes().getOneToOnes().get(0);

    // check defaults

    assertNull(resourceOneToOne.getJoinFetch());
    assertNull(contextOneToOne.getJoinFetch().getValue());

    // set context join fetch to INNER, check resource

    contextOneToOne.getJoinFetch().setValue(EclipseLinkJoinFetchType.INNER);

    assertEquals(XmlJoinFetchType.INNER, resourceOneToOne.getJoinFetch());
    assertEquals(EclipseLinkJoinFetchType.INNER, contextOneToOne.getJoinFetch().getValue());

    // set context join fetch to OUTER, check resource

    contextOneToOne.getJoinFetch().setValue(EclipseLinkJoinFetchType.OUTER);

    assertEquals(XmlJoinFetchType.OUTER, resourceOneToOne.getJoinFetch());
    assertEquals(EclipseLinkJoinFetchType.OUTER, contextOneToOne.getJoinFetch().getValue());

    // set context join fetch to null, check resource

    contextOneToOne.getJoinFetch().setValue(null);

    assertNull(resourceOneToOne.getJoinFetch());
    assertNull(contextOneToOne.getJoinFetch().getValue());
  }
  /**
   * <em>Silently</em> remove the default attribute and add specified attribute before triggering an
   * <em>update</em> or the dangling default attribute will be removed preemptively.
   */
  protected OrmSpecifiedPersistentAttribute convertAttributeToSpecified_(
      OrmPersistentAttribute defaultAttribute, String mappingKey) {
    // silently remove the default attribute
    int defaultIndex = this.defaultAttributes.indexOf(defaultAttribute);
    this.defaultAttributes.remove(defaultIndex);
    defaultAttribute.dispose();

    // silently add the specified attribute
    OrmAttributeMappingDefinition md =
        this.getMappingFileDefinition().getAttributeMappingDefinition(mappingKey);
    XmlAttributeMapping xmlMapping = md.buildResourceMapping(this.getResourceModelFactory());

    OrmSpecifiedPersistentAttribute specifiedAttribute = this.buildSpecifiedAttribute(xmlMapping);
    // we need to add the attribute to the right spot in the list - stupid spec...
    int specifiedIndex = this.getSpecifiedAttributeInsertionIndex(specifiedAttribute);
    this.specifiedAttributes.add(specifiedIndex, specifiedAttribute);

    // this will trigger the initial update;
    // no changes to either collection (default or specified) should be detected at this point
    specifiedAttribute.getMapping().setName(defaultAttribute.getName());

    // fire the list change events
    this.fireItemRemoved(DEFAULT_ATTRIBUTES_LIST, defaultIndex, defaultAttribute);
    this.fireItemAdded(SPECIFIED_ATTRIBUTES_LIST, specifiedIndex, specifiedAttribute);

    // it should be safe to update the XML now
    Attributes xmlAttributes = this.getXmlAttributesForUpdate();
    specifiedAttribute.getMapping().addXmlAttributeMappingTo(xmlAttributes);
    // possibly a NOP, but needed when we trigger the creation of a new 'attributes'
    this.getXmlTypeMapping().setAttributes(xmlAttributes);

    // copy over the specified access(?)
    AccessType oldAccess = defaultAttribute.getJavaPersistentAttribute().getSpecifiedAccess();
    if (oldAccess != null) {
      specifiedAttribute.setSpecifiedAccess(oldAccess);
    }
    return specifiedAttribute;
  }
  public void testSpecifiedMapping() throws Exception {
    createTestEntityOneToOneMapping();
    createTestTargetEntityAddress();

    OrmPersistentType ormPersistentType =
        getEntityMappings()
            .addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME);
    getEntityMappings()
        .addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address");

    ormPersistentType.addAttributeToXml(
        ormPersistentType.getAttributeNamed("address"),
        MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
    assertEquals(2, ormPersistentType.getDefaultAttributesSize());

    OrmSpecifiedPersistentAttribute ormPersistentAttribute =
        ormPersistentType.getSpecifiedAttributes().iterator().next();
    EclipseLinkOrmOneToOneMapping ormOneToOneMapping =
        (EclipseLinkOrmOneToOneMapping) ormPersistentAttribute.getMapping();

    assertEquals("address", ormOneToOneMapping.getName());
    assertNull(ormOneToOneMapping.getSpecifiedFetch());
    assertNull(ormOneToOneMapping.getSpecifiedOptional());
    assertNull(ormOneToOneMapping.getSpecifiedTargetEntity());
    assertNull(ormOneToOneMapping.getRelationship().getMappedByStrategy().getMappedByAttribute());
    assertEquals(FetchType.EAGER, ormOneToOneMapping.getFetch());
    assertEquals(true, ormOneToOneMapping.isOptional());
    // TODO default target entity in xml
    // assertEquals("test.Address", ormOneToOneMapping.getDefaultTargetEntity());

    assertTrue(
        ormOneToOneMapping.getRelationship().getJoinColumnStrategy().getJoinColumnsSize() > 0);

    // TODO default join columns for specified xmlOneToOne mapping
    //		XmlJoinColumn ormJoinColumn = ormOneToOneMapping.defaultJoinColumns().next();
    //		assertNull(ormJoinColumn.getSpecifiedName());
    //		assertNull(ormJoinColumn.getSpecifiedReferencedColumnName());
    //		assertNull(ormJoinColumn.getSpecifiedUnique());
    //		assertNull(ormJoinColumn.getSpecifiedNullable());
    //		assertNull(ormJoinColumn.getSpecifiedInsertable());
    //		assertNull(ormJoinColumn.getSpecifiedUpdatable());
    //		assertNull(ormJoinColumn.getColumnDefinition());
    //		assertNull(ormJoinColumn.getSpecifiedTable());
    //
    //		assertEquals("address", ormJoinColumn.getDefaultName());
    //		assertEquals("address", ormJoinColumn.getDefaultReferencedColumnName());
    //		assertEquals(Boolean.FALSE, ormJoinColumn.getDefaultUnique());
    //		assertEquals(Boolean.TRUE, ormJoinColumn.getDefaultNullable());
    //		assertEquals(Boolean.TRUE, ormJoinColumn.getDefaultInsertable());
    //		assertEquals(Boolean.TRUE, ormJoinColumn.getDefaultUpdatable());
    //		assertEquals(null, ormJoinColumn.getColumnDefinition());
    //		assertEquals(TYPE_NAME, ormJoinColumn.getDefaultTable());

    Cascade cascade = ormOneToOneMapping.getCascade();
    assertFalse(cascade.isAll());
    assertFalse(cascade.isMerge());
    assertFalse(cascade.isPersist());
    assertFalse(cascade.isRemove());
    assertFalse(cascade.isRefresh());

    assertEquals(null, ormOneToOneMapping.getJoinFetch().getValue());
    assertFalse(ormOneToOneMapping.getPrivateOwned().isPrivateOwned());
  }
 public XmlAttributeMapping getResourceElement(OrmSpecifiedPersistentAttribute contextElement) {
   return contextElement.getMapping().getXmlAttributeMapping();
 }
 protected void removeSpecifiedAttribute(OrmSpecifiedPersistentAttribute attribute) {
   this.removeSpecifiedAttribute_(attribute);
   attribute.getMapping().removeXmlAttributeMappingFrom(this.getXmlAttributes());
   this.removeXmlAttributesIfUnset();
 }