olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sklev...@apache.org
Subject [04/51] [abbrv] git commit: Minor refactorings for duplicate codings
Date Mon, 17 Feb 2014 12:40:31 GMT
Minor refactorings for duplicate codings


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

Branch: refs/heads/master
Commit: 300bf776b1d0fe99d1aa92bd40c58ee014ca0b27
Parents: ebe3643
Author: Michael Bolz <mibo@apache.org>
Authored: Tue Nov 19 08:45:54 2013 +0100
Committer: Tamara Boehm <tamara.boehm@sap.com>
Committed: Fri Dec 6 17:49:23 2013 +0100

----------------------------------------------------------------------
 .../annotation/data/AnnotationInMemoryDs.java   |  14 +-
 .../annotation/edm/AnnotationEdmProvider.java   |  49 +------
 .../core/annotation/edm/AnnotationHelper.java   | 145 +++++++++++++++++--
 3 files changed, 150 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/300bf776/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
index 3b79e27..a8f4cb9 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
@@ -115,7 +115,9 @@ public class AnnotationInMemoryDs implements ListsDataSource {
       String targetName = targetEntitySet.getEntityType().getName();
       DataStore targetStore = dataStores.get(targetName);
 
-      Field sourceFieldAtTarget = extractSourceField(sourceStore, targetStore);
+      AnnotationHelper.AnnotatedNavInfo navigationInfo = extractNavigationInfo(sourceStore,
targetStore);
+
+      Field sourceFieldAtTarget = navigationInfo.getToField();
       if (sourceFieldAtTarget == null) {
         throw new ODataRuntimeException("Missing source field for related data.");
       }
@@ -136,8 +138,7 @@ public class AnnotationInMemoryDs implements ListsDataSource {
         }
       }
       
-      EdmNavigationProperty navProperty = sourceFieldAtTarget.getAnnotation(EdmNavigationProperty.class);
-      if(navProperty.from().multiplicity() == EdmMultiplicity.ONE) {
+      if(navigationInfo.getToMultiplicity() == EdmMultiplicity.ONE) {
         if(resultData.isEmpty()) {
           return null;
         }
@@ -269,4 +270,11 @@ public class AnnotationInMemoryDs implements ListsDataSource {
     
     return ANNOTATION_HELPER.getCommonNavigationFieldFromTarget(sourceDataTypeClass, targetDataTypeClass);
   }
+
+  private AnnotationHelper.AnnotatedNavInfo extractNavigationInfo(DataStore sourceStore,
DataStore targetStore) {
+    Class sourceDataTypeClass = sourceStore.getDataTypeClass();
+    Class targetDataTypeClass = targetStore.getDataTypeClass();
+    
+    return ANNOTATION_HELPER.getCommonNavigationInfo(sourceDataTypeClass, targetDataTypeClass);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/300bf776/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
index dc253d8..8f9a2f4 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
@@ -495,40 +495,19 @@ public class AnnotationEdmProvider extends EdmProvider {
       navProp.setName(entityName);
       //
       NavigationEnd from = enp.from();
-      String fromRole = from.role();
-      if (fromRole.isEmpty()) {
-        fromRole = getCanonicalRole(from, field.getDeclaringClass());
-      }
+      String fromRole = ANNOTATION_HELPER.extractRoleName(from, field.getDeclaringClass());
       navProp.setFromRole(fromRole);
 
       NavigationEnd to = enp.to();
-      String toRole = to.role();
-      if (toRole.isEmpty()) {
-        toRole = getCanonicalRole(to, field.getType());
-      }
+      String toRole = ANNOTATION_HELPER.extractRoleName(to, field.getType());
       navProp.setToRole(toRole);
 
-      String relationshipName = enp.association();
-      if(relationshipName.isEmpty()) {
-        if(fromRole.compareTo(toRole) > 0) {
-          relationshipName = toRole + "-" + fromRole;
-        } else {
-          relationshipName = fromRole + "-" + toRole;
-        }
-      }
+      String relationshipName = ANNOTATION_HELPER.extractRelationshipName(enp, fromRole,
toRole);
       navProp.setRelationship(new FullQualifiedName(namespace, relationshipName));
 
       return navProp;
     }
 
-    private String getCanonicalRole(NavigationEnd navEnd, Class<?> fallbackClass) {
-        String toRole = ANNOTATION_HELPER.extractEntityTypeName(navEnd.entitySet());
-        if(toRole == null) {
-          toRole = ANNOTATION_HELPER.extractEntityTypeName(fallbackClass);
-        }
-        return "r_" + toRole;
-    }
-
     private EdmSimpleTypeKind getEdmSimpleType(Class<?> type) {
       if (type == String.class) {
         return EdmSimpleTypeKind.String;
@@ -576,10 +555,7 @@ public class AnnotationEdmProvider extends EdmProvider {
       NavigationEnd from = navigation.from();
       AssociationEnd fromEnd = new AssociationEnd();
       fromEnd.setRole(navProperty.getFromRole());
-      String typeName = extractEntitTypeName(from);
-      if (typeName.isEmpty()) {
-        typeName = ANNOTATION_HELPER.extractEntityTypeName(field.getDeclaringClass());
-      }
+      String typeName = ANNOTATION_HELPER.extractEntitTypeName(from, field.getDeclaringClass());
       fromEnd.setType(new FullQualifiedName(namespace, typeName));
       fromEnd.setMultiplicity(from.multiplicity());
       association.setEnd1(fromEnd);
@@ -587,10 +563,7 @@ public class AnnotationEdmProvider extends EdmProvider {
       NavigationEnd to = navigation.to();
       AssociationEnd toEnd = new AssociationEnd();
       toEnd.setRole(navProperty.getToRole());
-      String toTypeName = extractEntitTypeName(to);
-      if (toTypeName.isEmpty()) {
-        toTypeName = ANNOTATION_HELPER.extractEntityTypeName(field.getType());
-      }
+      String toTypeName = ANNOTATION_HELPER.extractEntitTypeName(to, field.getType());
       toEnd.setType(new FullQualifiedName(namespace, toTypeName));
 
       EdmMultiplicity toMultiplicity = to.multiplicity();
@@ -616,18 +589,6 @@ public class AnnotationEdmProvider extends EdmProvider {
     private boolean isAnnotatedEntity(Class<?> clazz) {
       return ANNOTATION_HELPER.isEdmTypeAnnotated(clazz);
     }
-
-    private String extractEntitTypeName(NavigationEnd navEnd) {
-      Class<?> entityTypeClass = navEnd.entitySet();
-      if(entityTypeClass == Object.class) {
-        return "";
-      }
-      EdmEntityType type = entityTypeClass.getAnnotation(EdmEntityType.class);
-      if(type == null) {
-        return "";
-      }
-      return type.name();
-    }
   }
 
   static class SchemaBuilder {

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/300bf776/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
index fbc0c7c..dfa461a 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
@@ -17,7 +17,6 @@ package org.apache.olingo.odata2.core.annotation.edm;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -30,7 +29,9 @@ import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
 import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
 import org.apache.olingo.odata2.api.annotation.edm.EdmNavigationProperty;
 import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
+import org.apache.olingo.odata2.api.annotation.edm.NavigationEnd;
 import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
 import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException;
 import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
 import org.apache.olingo.odata2.api.edm.FullQualifiedName;
@@ -79,6 +80,21 @@ public class AnnotationHelper {
     }
   }
 
+  public String extractEntitTypeName(NavigationEnd navEnd, Class<?> fallbackClass)
{
+    Class<?> entityTypeClass = navEnd.entitySet();
+    if (entityTypeClass == Object.class) {
+      return extractEntityTypeName(fallbackClass);
+    }
+    EdmEntityType type = entityTypeClass.getAnnotation(EdmEntityType.class);
+    if (type == null) {
+      return null;
+    }
+    if(type.name().isEmpty()) {
+      return extractEntityTypeName(fallbackClass);
+    }
+    return type.name();
+  }
+
   /**
    * Returns <code>NULL</code> if given class is not annotated. If annotated
the set entity type name is returned and if
    * no name is set the default name is generated from the simple class name.
@@ -105,13 +121,13 @@ public class AnnotationHelper {
   }
 
   /**
-   * 
-   * 
+   *
+   *
    * @param <T> must be EdmEntityType or EdmComplexType annotation
    * @param annotatedClass
    * @param typeAnnotation
-   * @return null if annotatedClass is not annotated or 
-   * name set via annotation or generated via {@link #getCanonicalName(java.lang.Class)}
+   * @return null if annotatedClass is not annotated or name set via annotation or generated
via
+   * {@link #getCanonicalName(java.lang.Class)}
    */
   private <T extends Annotation> String extractTypeName(Class<?> annotatedClass,
Class<T> typeAnnotation) {
     if (annotatedClass == Object.class) {
@@ -121,12 +137,12 @@ public class AnnotationHelper {
     if (type == null) {
       return null;
     }
-    
+
     String name;
-    if(typeAnnotation == EdmEntityType.class) {
-      name = ((EdmEntityType)type).name();
-    } else if(typeAnnotation == EdmComplexType.class) {
-      name = ((EdmComplexType)type).name();
+    if (typeAnnotation == EdmEntityType.class) {
+      name = ((EdmEntityType) type).name();
+    } else if (typeAnnotation == EdmComplexType.class) {
+      name = ((EdmComplexType) type).name();
     } else {
       return null;
     }
@@ -136,7 +152,7 @@ public class AnnotationHelper {
     }
     return name;
   }
-  
+
   /**
    * Get the set property name from an EdmProperty or EdmNavigationProperty annotation.
    *
@@ -164,6 +180,41 @@ public class AnnotationHelper {
     return propertyName;
   }
 
+  public String extractRoleName(NavigationEnd navigationEnd, Class<?> fallbackClass)
{
+      String role = navigationEnd.role();
+      if (role.isEmpty()) {
+        role = getCanonicalRole(navigationEnd, fallbackClass);
+      }
+      return role;
+  }
+  
+  public String getCanonicalRole(NavigationEnd navEnd, Class<?> fallbackClass) {
+    String toRole = extractEntityTypeName(navEnd.entitySet());
+    if (toRole == null) {
+      toRole = extractEntityTypeName(fallbackClass);
+    }
+    return "r_" + toRole;
+  }
+
+  public String extractRelationshipName(EdmNavigationProperty enp, Field field) {
+    String fromRole = extractRoleName(enp.from(), field.getDeclaringClass());
+    String toRole = extractRoleName(enp.to(), field.getType());
+
+    return extractRelationshipName(enp, fromRole, toRole);
+  }
+    
+  public String extractRelationshipName(EdmNavigationProperty enp, String fromRole, String
toRole) {
+      String relationshipName = enp.association();
+      if(relationshipName.isEmpty()) {
+        if(fromRole.compareTo(toRole) > 0) {
+          relationshipName = toRole + "-" + fromRole;
+        } else {
+          relationshipName = fromRole + "-" + toRole;
+        }
+      }
+      return relationshipName;
+  }
+
   public static final class ODataAnnotationException extends ODataException {
 
     public ODataAnnotationException(String message) {
@@ -187,6 +238,78 @@ public class AnnotationHelper {
     return instance;
   }
 
+  public static class AnnotatedNavInfo {
+
+    private final Field fromField;
+    private final Field toField;
+    private final EdmNavigationProperty fromNavigation;
+    private final EdmNavigationProperty toNavigation;
+
+    public AnnotatedNavInfo(Field fromField, Field toField, EdmNavigationProperty fromNavigation,
+            EdmNavigationProperty toNavigation) {
+      this.fromField = fromField;
+      this.toField = toField;
+      this.fromNavigation = fromNavigation;
+      this.toNavigation = toNavigation;
+    }
+
+    public Field getFromField() {
+      return fromField;
+    }
+
+    public Field getToField() {
+      return toField;
+    }
+
+    public EdmMultiplicity getFromMultiplicity() {
+      EdmMultiplicity from = fromNavigation.from().multiplicity();
+      EdmMultiplicity to = toNavigation.to().multiplicity();
+
+      if (from.equals(to)) {
+        return from;
+      } else {
+        if (EdmMultiplicity.MANY == from || EdmMultiplicity.MANY == to) {
+          return EdmMultiplicity.MANY;
+        } else {
+          return EdmMultiplicity.ONE;
+        }
+      }
+    }
+
+    public EdmMultiplicity getToMultiplicity() {
+      EdmMultiplicity from = fromNavigation.to().multiplicity();
+      EdmMultiplicity to = toNavigation.from().multiplicity();
+
+      if (from.equals(to)) {
+        return from;
+      } else {
+        if (EdmMultiplicity.MANY == from || EdmMultiplicity.MANY == to) {
+          return EdmMultiplicity.MANY;
+        } else {
+          return EdmMultiplicity.ONE;
+        }
+      }
+    }
+  }
+
+  public AnnotatedNavInfo getCommonNavigationInfo(Class<?> sourceClass, Class<?>
targetClass) {
+    List<Field> sourceFields = getAnnotatedFields(sourceClass, EdmNavigationProperty.class);
+    List<Field> targetFields = getAnnotatedFields(targetClass, EdmNavigationProperty.class);
+
+    for (Field targetField : targetFields) {
+      for (Field sourceField : sourceFields) {
+        EdmNavigationProperty sourceNav = sourceField.getAnnotation(EdmNavigationProperty.class);
+        EdmNavigationProperty targetNav = targetField.getAnnotation(EdmNavigationProperty.class);
+        String sourceAssociation = extractRelationshipName(sourceNav, sourceField);
+        String targetAssociation = extractRelationshipName(targetNav, targetField);
+        if (sourceAssociation.equals(targetAssociation)) {
+          return new AnnotatedNavInfo(sourceField, targetField, sourceNav, targetNav);
+        }
+      }
+    }
+    return null;
+  }
+
   public Field getCommonNavigationFieldFromTarget(Class<?> sourceClass, Class<?>
targetClass) {
     List<Field> sourceFields = getAnnotatedFields(sourceClass, EdmNavigationProperty.class);
     List<Field> targetFields = getAnnotatedFields(targetClass, EdmNavigationProperty.class);


Mime
View raw message