You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/10/25 16:02:42 UTC

git commit: [OLINGO-411] Implement Self Join

Repository: olingo-odata2
Updated Branches:
  refs/heads/master d298dc1aa -> 83ff09155


[OLINGO-411] Implement Self Join



Signed-off-by: Chandan V A <ch...@sap.com>

Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/83ff0915
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/83ff0915
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/83ff0915

Branch: refs/heads/master
Commit: 83ff0915565a76afd358a62f4f9356182c88ef41
Parents: d298dc1
Author: Chandan V A <ch...@sap.com>
Authored: Sat Oct 25 19:23:14 2014 +0530
Committer: Chandan V A <ch...@sap.com>
Committed: Sat Oct 25 19:23:14 2014 +0530

----------------------------------------------------------------------
 .../jpa/processor/core/access/data/JPALink.java |  4 ++-
 .../core/access/data/JPAProcessorImpl.java      | 21 ++++--------
 .../core/access/model/JPAEdmNameBuilder.java    | 11 +++++-
 .../core/model/JPAEdmAssociationEnd.java        |  6 +++-
 .../core/model/JPAEdmAssociationSet.java        | 35 ++++++++++----------
 .../model/JPAEdmReferentialConstraintRole.java  | 20 +++++++----
 .../core/access/data/JPAProcessorImplTest.java  |  2 ++
 .../processor/core/mock/data/EdmMockUtilV2.java |  4 ++-
 8 files changed, 61 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPALink.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPALink.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPALink.java
index 6e85366..a4350c3 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPALink.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPALink.java
@@ -302,12 +302,14 @@ public class JPALink {
       JPAEntityParser entityParser = new JPAEntityParser();
       Method setMethod = entityParser.getAccessModifier(sourceJPAEntity.getClass(),
           navigationProperty, JPAEntityParser.ACCESS_MODIFIER_SET);
-
       switch (navigationProperty.getMultiplicity()) {
       case MANY:
         Method getMethod = entityParser.getAccessModifier(sourceJPAEntity.getClass(),
             navigationProperty, JPAEntityParser.ACCESS_MODIFIER_GET);
         Collection<Object> relatedEntities = (Collection<Object>) getMethod.invoke(sourceJPAEntity);
+        if(relatedEntities == null){
+          throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.ERROR_JPQL_CREATE_REQUEST, null);
+        }
         relatedEntities.addAll(targetJPAEntities);
         setMethod.invoke(sourceJPAEntity, relatedEntities);
         break;

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImpl.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImpl.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImpl.java
index 814a053..d926daf 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImpl.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImpl.java
@@ -139,8 +139,7 @@ public class JPAProcessorImpl implements JPAProcessor {
     }
     JPQLContextType contextType = null;
     try {
-      if (!uriParserResultView.getStartEntitySet().getName()
-          .equals(uriParserResultView.getTargetEntitySet().getName())) {
+      if (uriParserResultView.getNavigationSegments().size() > 0) {
         contextType = JPQLContextType.JOIN;
       } else {
         contextType = JPQLContextType.SELECT;
@@ -210,19 +209,13 @@ public class JPAProcessorImpl implements JPAProcessor {
       throws ODataJPAModelException, ODataJPARuntimeException {
 
     JPQLContextType contextType = null;
-    try {
-      if (uriParserResultView instanceof GetEntityUriInfo) {
-        uriParserResultView = ((GetEntityUriInfo) uriParserResultView);
-        if (!((GetEntityUriInfo) uriParserResultView).getStartEntitySet().getName()
-            .equals(((GetEntityUriInfo) uriParserResultView).getTargetEntitySet().getName())) {
-          contextType = JPQLContextType.JOIN_SINGLE;
-        } else {
-          contextType = JPQLContextType.SELECT_SINGLE;
-        }
+    if (uriParserResultView instanceof GetEntityUriInfo) {
+      uriParserResultView = ((GetEntityUriInfo) uriParserResultView);
+      if (uriParserResultView.getNavigationSegments().size() > 0) {
+        contextType = JPQLContextType.JOIN_SINGLE;
+      } else {
+        contextType = JPQLContextType.SELECT_SINGLE;
       }
-    } catch (EdmException e) {
-      ODataJPARuntimeException.throwException(
-          ODataJPARuntimeException.GENERAL, e);
     }
 
     return readEntity(uriParserResultView, contextType);

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilder.java
index 1016f9e..939ca88 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilder.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilder.java
@@ -503,7 +503,16 @@ public class JPAEdmNameBuilder {
 
     navProp.setName(navPropName);
 
-    if (toName.equals(associationEndTypeOne.getName())) {
+    // Condition for self join
+    if (associationEndTypeOne.getName().equals(associationEndTypeTwo.getName())) {
+      if (jpaAttribute.isCollection()) {
+        navProp.setFromRole(association.getEnd2().getRole());
+        navProp.setToRole(association.getEnd1().getRole());
+      } else {
+        navProp.setToRole(association.getEnd2().getRole());
+        navProp.setFromRole(association.getEnd1().getRole());        
+      }
+    } else if (toName.equals(associationEndTypeOne.getName())) {
       navProp.setFromRole(association.getEnd2().getRole());
       navProp.setToRole(association.getEnd1().getRole());
     } else if (toName.equals(associationEndTypeTwo.getName())) {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
index d3af0e9..87d409c 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java
@@ -83,7 +83,11 @@ public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAs
       JPAEdmNameBuilder.build(JPAEdmAssociationEnd.this, entityTypeView, propertyView);
 
       currentAssociationEnd1.setRole(currentAssociationEnd1.getType().getName());
-      currentAssociationEnd2.setRole(currentAssociationEnd2.getType().getName());
+      if (currentAssociationEnd1.getType().getName().equals(currentAssociationEnd2.getType().getName())) {
+        currentAssociationEnd2.setRole(currentAssociationEnd2.getType().getName() + "2");
+      } else {
+        currentAssociationEnd2.setRole(currentAssociationEnd2.getType().getName());
+      }
 
       setEdmMultiplicity(propertyView.getJPAAttribute().getPersistentAttributeType());
 

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
index f5a0ecd..868e654 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationSet.java
@@ -94,28 +94,27 @@ public class JPAEdmAssociationSet extends JPAEdmBaseViewImpl implements JPAEdmAs
           currentAssociationSet.setAssociation(fQname);
 
           int endCount = 0;
-          short endFlag = 0;
+          FullQualifiedName end1Type = association.getEnd1().getType();
+          FullQualifiedName end2Type = association.getEnd2().getType();
+
           for (EntitySet entitySet : entitySetList) {
             fQname = entitySet.getEntityType();
-            endFlag = 0;
-            if (fQname.equals(association.getEnd1().getType()) || ++endFlag > 1
-                || fQname.equals(association.getEnd2().getType())) {
-
+            if (fQname.equals(end1Type)) {
+              AssociationSetEnd end = new AssociationSetEnd();
+              end.setEntitySet(entitySet.getName());
+              currentAssociationSet.setEnd1(end);
+              end.setRole(association.getEnd1().getRole());
+              endCount++;
+            }
+            if (fQname.equals(end2Type)) {
               AssociationSetEnd end = new AssociationSetEnd();
               end.setEntitySet(entitySet.getName());
-              if (endFlag == 0) {
-                currentAssociationSet.setEnd1(end);
-                end.setRole(association.getEnd1().getRole());
-                endCount++;
-              } else {
-                endCount++;
-                currentAssociationSet.setEnd2(end);
-                end.setRole(association.getEnd2().getRole());
-              }
-
-              if (endCount == 2) {
-                break;
-              }
+              currentAssociationSet.setEnd2(end);
+              end.setRole(association.getEnd2().getRole());
+              endCount++;
+            }
+            if (endCount == 2) {
+              break;
             }
           }
           if (endCount == 2) {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java
index eeaaefa..2a5045a 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java
@@ -183,14 +183,22 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen
             isConsistent = false;
             return;
           }
-          AssociationEnd end = association.getEnd1();
-          if (end.getType().getName().equals(edmEntityType.getName())) {
-            currentRole.setRole(end.getRole());
+          // First condition is required for Self Joins where the entity type on both ends are same
+          AssociationEnd end1 = association.getEnd1();
+          AssociationEnd end2 = association.getEnd2();
+          if (end1.getType().getName().equals(end2.getType().getName())) {
+            if (roleType == RoleType.PRINCIPAL) {
+              currentRole.setRole(end1.getRole());
+            } else {
+              currentRole.setRole(end2.getRole());
+            }
             isConsistent = true;
           } else {
-            end = association.getEnd2();
-            if (end.getType().getName().equals(edmEntityType.getName())) {
-              currentRole.setRole(end.getRole());
+            if (end1.getType().getName().equals(edmEntityType.getName())) {
+              currentRole.setRole(end1.getRole());
+              isConsistent = true;
+            } else if (end2.getType().getName().equals(edmEntityType.getName())) {
+              currentRole.setRole(end2.getRole());
               isConsistent = true;
             }
           }

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java
index b79fac4..f370c26 100644
--- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java
+++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java
@@ -57,6 +57,7 @@ import org.apache.olingo.odata2.api.edm.provider.Mapping;
 import org.apache.olingo.odata2.api.exception.ODataException;
 import org.apache.olingo.odata2.api.processor.ODataContext;
 import org.apache.olingo.odata2.api.uri.KeyPredicate;
+import org.apache.olingo.odata2.api.uri.NavigationSegment;
 import org.apache.olingo.odata2.api.uri.PathInfo;
 import org.apache.olingo.odata2.api.uri.UriInfo;
 import org.apache.olingo.odata2.api.uri.expression.FilterExpression;
@@ -196,6 +197,7 @@ public class JPAProcessorImplTest {
     EasyMock.expect(objUriInfo.getFilter()).andStubReturn(getFilter());
     EasyMock.expect(objUriInfo.getFunctionImport()).andStubReturn(null);
     EasyMock.expect(objUriInfo.getCustomQueryOptions()).andStubReturn(null);
+    EasyMock.expect(objUriInfo.getNavigationSegments()).andStubReturn(new ArrayList<NavigationSegment>());
     EasyMock.replay(objUriInfo);
     return objUriInfo;
   }

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/83ff0915/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/mock/data/EdmMockUtilV2.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/mock/data/EdmMockUtilV2.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/mock/data/EdmMockUtilV2.java
index fbe6206..dc8fe8b 100644
--- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/mock/data/EdmMockUtilV2.java
+++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/mock/data/EdmMockUtilV2.java
@@ -197,6 +197,7 @@ public class EdmMockUtilV2 {
   public static EdmAssociation mockEdmAssociation(final String navigationPropertyName) throws EdmException {
     EdmAssociation edmAssociation = EasyMock.createMock(EdmAssociation.class);
     EasyMock.expect(edmAssociation.getEnd("TO")).andReturn(mockEdmAssociatioEnd(navigationPropertyName, "TO"));
+    EasyMock.expect(edmAssociation.getEnd2()).andReturn(mockEdmAssociatioEnd(navigationPropertyName, "TO"));
     EasyMock.expect(edmAssociation.getEnd("FROM")).andReturn(mockEdmAssociatioEnd(navigationPropertyName, "FROM"));
     EasyMock.replay(edmAssociation);
     return edmAssociation;
@@ -230,7 +231,8 @@ public class EdmMockUtilV2 {
     EasyMock.expect(navigationProperty.getMapping()).andReturn(
         (EdmMapping) mockEdmMapping(null, null, navigationPropertyName)).anyTimes();
     EasyMock.expect(navigationProperty.getToRole()).andReturn("TO");
-    EasyMock.expect(navigationProperty.getRelationship()).andReturn(mockEdmAssociation(navigationPropertyName));
+    EasyMock.expect(navigationProperty.getRelationship()).andReturn(mockEdmAssociation(navigationPropertyName))
+        .anyTimes();
     if (multiplicity.equals(EdmMultiplicity.ONE)) {
       EasyMock.expect(navigationProperty.getName()).andReturn(JPATypeMock.NAVIGATION_PROPERTY_X).anyTimes();
     }