Program: 59598
Project: 59598
Buggy source:
private OEntity jpaEntityToOEntity(EdmEntitySet ees,EntityType<?> entityType,Object jpaEntity,List<EntitySimpleProperty> expand,List<EntitySimpleProperty> select){
  List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  List<OLink> links=new ArrayList<OLink>();
  try {
    SingularAttribute<?,?> idAtt=JPAEdmGenerator.getIdAttribute(entityType);
    boolean hasEmbeddedCompositeKey=idAtt.getPersistentAttributeType() == PersistentAttributeType.EMBEDDED;
    for (    EdmProperty ep : ees.getType().getProperties()) {
      if (!isSelected(ep.getName(),select)) {
        continue;
      }
      if (hasEmbeddedCompositeKey && ees.getType().getKeys().contains(ep.getName())) {
        Object value=getIdValue(jpaEntity,idAtt,ep.getName());
        properties.add(OProperties.simple(ep.getName(),(EdmSimpleType<?>)ep.getType(),value));
      }
 else {
        Attribute<?,?> att=entityType.getAttribute(ep.getName());
        JPAMember member=JPAMember.create(att,jpaEntity);
        Object value=member.get();
        properties.add(OProperties.simple(ep.getName(),(EdmSimpleType<?>)ep.getType(),value));
      }
    }
    if (expand != null && !expand.isEmpty()) {
      HashMap<String,List<EntitySimpleProperty>> expandedProps=new HashMap<String,List<EntitySimpleProperty>>();
      for (      final EntitySimpleProperty propPath : expand) {
        String[] props=propPath.getPropertyName().split("String_Node_Str",2);
        String prop=props[0];
        String remainingPropPath=props.length > 1 ? props[1] : null;
        if (expandedProps.containsKey(prop)) {
          if (remainingPropPath != null) {
            List<EntitySimpleProperty> remainingPropPaths=expandedProps.get(prop);
            remainingPropPaths.add(org.odata4j.expression.Expression.simpleProperty(remainingPropPath));
          }
        }
 else {
          List<EntitySimpleProperty> remainingPropPaths=new ArrayList<EntitySimpleProperty>();
          if (remainingPropPath != null)           remainingPropPaths.add(org.odata4j.expression.Expression.simpleProperty(remainingPropPath));
          expandedProps.put(prop,remainingPropPaths);
        }
      }
      for (      final String prop : expandedProps.keySet()) {
        List<EntitySimpleProperty> remainingPropPath=expandedProps.get(prop);
        Attribute<?,?> att=entityType.getAttribute(prop);
        if (att.getPersistentAttributeType() == PersistentAttributeType.ONE_TO_MANY || att.getPersistentAttributeType() == PersistentAttributeType.MANY_TO_MANY) {
          Collection<?> value=JPAMember.create(att,jpaEntity).get();
          List<OEntity> relatedEntities=new ArrayList<OEntity>();
          for (          Object relatedEntity : value) {
            EntityType<?> elementEntityType=(EntityType<?>)((PluralAttribute<?,?,?>)att).getElementType();
            EdmEntitySet elementEntitySet=metadata.getEdmEntitySet(JPAEdmGenerator.getEntitySetName(elementEntityType));
            relatedEntities.add(jpaEntityToOEntity(elementEntitySet,elementEntityType,relatedEntity,remainingPropPath,null));
          }
          links.add(OLinks.relatedEntitiesInline(null,prop,null,relatedEntities));
        }
 else         if (att.getPersistentAttributeType() == PersistentAttributeType.ONE_TO_ONE || att.getPersistentAttributeType() == PersistentAttributeType.MANY_TO_ONE) {
          EntityType<?> relatedEntityType=(EntityType<?>)((SingularAttribute<?,?>)att).getType();
          EdmEntitySet relatedEntitySet=metadata.getEdmEntitySet(JPAEdmGenerator.getEntitySetName(relatedEntityType));
          Object relatedEntity=JPAMember.create(att,jpaEntity).get();
          if (relatedEntity == null) {
            links.add(OLinks.relatedEntityInline(null,prop,null,null));
          }
 else {
            links.add(OLinks.relatedEntityInline(null,prop,null,jpaEntityToOEntity(relatedEntitySet,relatedEntityType,relatedEntity,remainingPropPath,null)));
          }
        }
      }
    }
    for (    final EdmNavigationProperty ep : ees.getType().getNavigationProperties()) {
      if (isSelected(ep.getName(),select)) {
        boolean expanded=null != Enumerable.create(links).firstOrNull(new Predicate1<OLink>(){
          @Override public boolean apply(          OLink t){
            return t.getTitle().equals(ep.getName());
          }
        }
);
        if (!expanded) {
          if (ep.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
            links.add(OLinks.relatedEntities(null,ep.getName(),null));
          }
 else {
            links.add(OLinks.relatedEntity(null,ep.getName(),null));
          }
        }
      }
    }
    return OEntities.create(ees,toOEntityKey(jpaEntity,idAtt),properties,links);
  }
 catch (  Exception e) {
    throw new RuntimeException(e);
  }
}

Fixed version (ground truth):
private OEntity jpaEntityToOEntity(EdmEntitySet ees,EntityType<?> entityType,Object jpaEntity,List<EntitySimpleProperty> expand,List<EntitySimpleProperty> select){
  List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  List<OLink> links=new ArrayList<OLink>();
  try {
    SingularAttribute<?,?> idAtt=JPAEdmGenerator.getIdAttribute(entityType);
    boolean hasEmbeddedCompositeKey=idAtt.getPersistentAttributeType() == PersistentAttributeType.EMBEDDED;
    for (    EdmProperty ep : ees.getType().getProperties()) {
      if (!isSelected(ep.getName(),select)) {
        continue;
      }
      if (hasEmbeddedCompositeKey && ees.getType().getKeys().contains(ep.getName())) {
        Object value=getIdValue(jpaEntity,idAtt,ep.getName());
        properties.add(OProperties.simple(ep.getName(),(EdmSimpleType<?>)ep.getType(),value));
      }
 else {
        Attribute<?,?> att=entityType.getAttribute(ep.getName());
        JPAMember member=JPAMember.create(att,jpaEntity);
        Object value=member.get();
        properties.add(OProperties.simple(ep.getName(),(EdmSimpleType<?>)ep.getType(),value));
      }
    }
    if (expand != null && !expand.isEmpty()) {
      HashMap<String,List<EntitySimpleProperty>> expandedProps=new HashMap<String,List<EntitySimpleProperty>>();
      for (      final EntitySimpleProperty propPath : expand) {
        String[] props=propPath.getPropertyName().split("String_Node_Str",2);
        String prop=props[0];
        String remainingPropPath=props.length > 1 ? props[1] : null;
        if (expandedProps.containsKey(prop)) {
          if (remainingPropPath != null) {
            List<EntitySimpleProperty> remainingPropPaths=expandedProps.get(prop);
            remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
          }
        }
 else {
          List<EntitySimpleProperty> remainingPropPaths=new ArrayList<EntitySimpleProperty>();
          if (remainingPropPath != null)           remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
          expandedProps.put(prop,remainingPropPaths);
        }
      }
      for (      final String prop : expandedProps.keySet()) {
        List<EntitySimpleProperty> remainingPropPath=expandedProps.get(prop);
        Attribute<?,?> att=entityType.getAttribute(prop);
        if (att.getPersistentAttributeType() == PersistentAttributeType.ONE_TO_MANY || att.getPersistentAttributeType() == PersistentAttributeType.MANY_TO_MANY) {
          Collection<?> value=JPAMember.create(att,jpaEntity).get();
          List<OEntity> relatedEntities=new ArrayList<OEntity>();
          for (          Object relatedEntity : value) {
            EntityType<?> elementEntityType=(EntityType<?>)((PluralAttribute<?,?,?>)att).getElementType();
            EdmEntitySet elementEntitySet=metadata.getEdmEntitySet(JPAEdmGenerator.getEntitySetName(elementEntityType));
            relatedEntities.add(jpaEntityToOEntity(elementEntitySet,elementEntityType,relatedEntity,remainingPropPath,null));
          }
          links.add(OLinks.relatedEntitiesInline(null,prop,null,relatedEntities));
        }
 else         if (att.getPersistentAttributeType() == PersistentAttributeType.ONE_TO_ONE || att.getPersistentAttributeType() == PersistentAttributeType.MANY_TO_ONE) {
          EntityType<?> relatedEntityType=(EntityType<?>)((SingularAttribute<?,?>)att).getType();
          EdmEntitySet relatedEntitySet=metadata.getEdmEntitySet(JPAEdmGenerator.getEntitySetName(relatedEntityType));
          Object relatedEntity=JPAMember.create(att,jpaEntity).get();
          if (relatedEntity == null) {
            links.add(OLinks.relatedEntityInline(null,prop,null,null));
          }
 else {
            links.add(OLinks.relatedEntityInline(null,prop,null,jpaEntityToOEntity(relatedEntitySet,relatedEntityType,relatedEntity,remainingPropPath,null)));
          }
        }
      }
    }
    for (    final EdmNavigationProperty ep : ees.getType().getNavigationProperties()) {
      if (isSelected(ep.getName(),select)) {
        boolean expanded=null != Enumerable.create(links).firstOrNull(new Predicate1<OLink>(){
          @Override public boolean apply(          OLink t){
            return t.getTitle().equals(ep.getName());
          }
        }
);
        if (!expanded) {
          if (ep.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
            links.add(OLinks.relatedEntities(null,ep.getName(),null));
          }
 else {
            links.add(OLinks.relatedEntity(null,ep.getName(),null));
          }
        }
      }
    }
    return OEntities.create(ees,toOEntityKey(jpaEntity,idAtt),properties,links);
  }
 catch (  Exception e) {
    throw new RuntimeException(e);
  }
}