Skip to content

Commit

Permalink
[OLINGO-1189] Sorting entities from metamodel before building OData E…
Browse files Browse the repository at this point in the history
…ntity Model
  • Loading branch information
kiransg authored and mibo committed Sep 3, 2023
1 parent 29f2fb9 commit 6257c23
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
******************************************************************************/
package org.apache.olingo.odata2.jpa.processor.core.model;

import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -91,7 +92,7 @@ private class JPAEdmEntityTypeBuilder implements JPAEdmBuilder {
@Override
public void build() throws ODataJPAModelException, ODataJPARuntimeException {

Set<javax.persistence.metamodel.EntityType<?>> jpaEntityTypes = metaModel.getEntities();
Collection<javax.persistence.metamodel.EntityType<?>> jpaEntityTypes = metaModel.getEntities();

if (jpaEntityTypes == null || jpaEntityTypes.isEmpty() == true) {
return;
Expand All @@ -100,6 +101,7 @@ public void build() throws ODataJPAModelException, ODataJPARuntimeException {

}

jpaEntityTypes = sortJPAEntityTypes(jpaEntityTypes);
for (javax.persistence.metamodel.EntityType<?> jpaEntityType : jpaEntityTypes) {
currentEdmEntityType = new EntityType();
currentJPAEntityType = jpaEntityType;
Expand Down Expand Up @@ -142,6 +144,31 @@ public void build() throws ODataJPAModelException, ODataJPARuntimeException {

}

private List<javax.persistence.metamodel.EntityType<?>> sortJPAEntityTypes(
final Collection<javax.persistence.metamodel.EntityType<?>> entities) {

List<javax.persistence.metamodel.EntityType<?>> entityTypeList =
new ArrayList<javax.persistence.metamodel.EntityType<?>>(entities.size());

Iterator<javax.persistence.metamodel.EntityType<?>> itr;
javax.persistence.metamodel.EntityType<?> smallestJpaEntity;
javax.persistence.metamodel.EntityType<?> currentJpaEntity;
while (!entities.isEmpty()) {
itr = entities.iterator();
smallestJpaEntity = itr.next();
while (itr.hasNext()) {
currentJpaEntity = itr.next();
if (smallestJpaEntity.getName().compareTo(currentJpaEntity.getName()) > 0) {
smallestJpaEntity = currentJpaEntity;
}
}
entityTypeList.add(smallestJpaEntity);
entities.remove(smallestJpaEntity);
}

return entityTypeList;
}

private boolean isExcluded(final JPAEdmEntityType jpaEdmEntityType) {
JPAEdmMappingModelAccess mappingModelAccess = jpaEdmEntityType.getJPAEdmMappingModelAccess();
if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()
Expand Down

0 comments on commit 6257c23

Please sign in to comment.