olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbo...@apache.org
Subject [18/50] [abbrv] git commit: Fixes for name generation
Date Fri, 06 Dec 2013 16:53:00 GMT
Fixes for name generation


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/ebe36434
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/ebe36434
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/ebe36434

Branch: refs/heads/ODataServlet
Commit: ebe364348beb1e4f4e97064b65d10b14c5fb6194
Parents: 2e9b114
Author: Michael Bolz <mibo@apache.org>
Authored: Mon Nov 18 14:03:44 2013 +0100
Committer: Tamara Boehm <tamara.boehm@sap.com>
Committed: Fri Dec 6 17:49:23 2013 +0100

----------------------------------------------------------------------
 .../annotation/data/AnnotationInMemoryDs.java   |   4 +-
 .../odata2/core/annotation/data/DataStore.java  |   4 +
 .../annotation/edm/AnnotationEdmProvider.java   |  79 ++++-------
 .../core/annotation/edm/AnnotationHelper.java   | 133 +++++++++++--------
 .../src/main/webapp/index.jsp                   |   5 +-
 5 files changed, 115 insertions(+), 110 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ebe36434/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 b2f2618..3b79e27 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
@@ -58,8 +58,8 @@ public class AnnotationInMemoryDs implements ListsDataSource {
     for (Class<?> clz : foundClasses) {
 
       DataStore<Object> dhs = (DataStore<Object>) DataStore.createInMemory(clz);
-      EdmEntityType entityType = clz.getAnnotation(EdmEntityType.class);
-      dataStores.put(entityType.name(), dhs);
+      String entityTypeName = ANNOTATION_HELPER.extractEntityTypeName(clz);
+      dataStores.put(entityTypeName, dhs);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ebe36434/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
index f8045f1..55a839e 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
@@ -79,6 +79,10 @@ public class DataStore<T> {
     return dataTypeClass;
   }
   
+  public String getEntityTypeName() {
+    return ANNOTATION_HELPER.extractEntityTypeName(dataTypeClass);
+  }
+  
   public T createInstance() {
     try {
       return dataTypeClass.newInstance();

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ebe36434/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 75a92a2..dc253d8 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
@@ -21,6 +21,7 @@ package org.apache.olingo.odata2.core.annotation.edm;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -339,17 +340,21 @@ public class AnnotationEdmProvider extends EdmProvider {
     private final List<NavigationProperty> navProperties = new ArrayList<NavigationProperty>();
     private final List<Association> associations = new ArrayList<Association>();
 
-    public TypeBuilder(String namespace, String name) {
-      this.namespace = namespace;
-      this.name = name;
+//    public TypeBuilder(String namespace, String name) {
+//      this.namespace = namespace;
+//      this.name = name;
+//    }
+    public TypeBuilder(FullQualifiedName fqn) {
+      this.namespace = fqn.getNamespace();
+      this.name = fqn.getName();
     }
 
     public static TypeBuilder init(EdmEntityType entity, Class<?> aClass) {
-      return new TypeBuilder(entity.namespace(), entity.name()).withClass(aClass);
+      return new TypeBuilder(ANNOTATION_HELPER.extractEntityTypeFqn(aClass)).withClass(aClass);
     }
 
     public static TypeBuilder init(EdmComplexType entity, Class<?> aClass) {
-      return new TypeBuilder(entity.namespace(), entity.name()).withClass(aClass);
+      return new TypeBuilder(ANNOTATION_HELPER.extractComplexTypeFqn(aClass)).withClass(aClass);
     }
 
     private TypeBuilder withClass(Class<?> aClass) {
@@ -517,9 +522,9 @@ public class AnnotationEdmProvider extends EdmProvider {
     }
 
     private String getCanonicalRole(NavigationEnd navEnd, Class<?> fallbackClass) {
-        String toRole = extractEntitTypeName(navEnd.entitySet());
+        String toRole = ANNOTATION_HELPER.extractEntityTypeName(navEnd.entitySet());
         if(toRole == null) {
-          toRole = extractEntitTypeName(fallbackClass);
+          toRole = ANNOTATION_HELPER.extractEntityTypeName(fallbackClass);
         }
         return "r_" + toRole;
     }
@@ -527,10 +532,14 @@ public class AnnotationEdmProvider extends EdmProvider {
     private EdmSimpleTypeKind getEdmSimpleType(Class<?> type) {
       if (type == String.class) {
         return EdmSimpleTypeKind.String;
-      } else if (type == int.class) {
-        return EdmSimpleTypeKind.Int32;
-      } else if (type == Integer.class) {
+      } else if (type == int.class || type == Integer.class) {
         return EdmSimpleTypeKind.Int32;
+      } else if (type == double.class || type == Double.class) {
+        return EdmSimpleTypeKind.Double;
+      } else if (type == long.class || type == Long.class) {
+        return EdmSimpleTypeKind.Int64;
+      } else if (type == Calendar.class) {
+        return EdmSimpleTypeKind.DateTime;
       } else if (type == Byte[].class || type == byte[].class) {
         return EdmSimpleTypeKind.Binary;
       } else {
@@ -538,30 +547,26 @@ public class AnnotationEdmProvider extends EdmProvider {
       }
     }
 
-    private EdmEntityType checkForBaseEntity(Class<?> aClass) {
+    private Class<?> checkForBaseEntityClass(Class<?> aClass) {
       Class<?> superClass = aClass.getSuperclass();
       if (superClass == Object.class) {
         return null;
       } else {
         EdmEntityType edmEntity = superClass.getAnnotation(EdmEntityType.class);
         if (edmEntity == null) {
-          return checkForBaseEntity(superClass);
+          return checkForBaseEntityClass(superClass);
         } else {
-          return edmEntity;
+          return superClass;
         }
       }
     }
 
     private FullQualifiedName createBaseEntityFqn(Class<?> aClass) {
-      EdmEntityType baseEntity = checkForBaseEntity(aClass);
-      if (baseEntity == null) {
+      Class<?> baseEntityClass = checkForBaseEntityClass(aClass);
+      if (baseEntityClass == null) {
         return null;
       }
-      String beName = baseEntity.name();
-      if (beName.isEmpty()) {
-        beName = aClass.getName();
-      }
-      return new FullQualifiedName(baseEntity.namespace(), beName);
+      return ANNOTATION_HELPER.extractEntityTypeFqn(baseEntityClass);
     }
 
     private Association createAssociation(Field field, NavigationProperty navProperty) {
@@ -573,7 +578,7 @@ public class AnnotationEdmProvider extends EdmProvider {
       fromEnd.setRole(navProperty.getFromRole());
       String typeName = extractEntitTypeName(from);
       if (typeName.isEmpty()) {
-        typeName = extractEntitTypeName(field.getDeclaringClass());
+        typeName = ANNOTATION_HELPER.extractEntityTypeName(field.getDeclaringClass());
       }
       fromEnd.setType(new FullQualifiedName(namespace, typeName));
       fromEnd.setMultiplicity(from.multiplicity());
@@ -584,7 +589,7 @@ public class AnnotationEdmProvider extends EdmProvider {
       toEnd.setRole(navProperty.getToRole());
       String toTypeName = extractEntitTypeName(to);
       if (toTypeName.isEmpty()) {
-        toTypeName = extractEntitTypeName(field.getType());
+        toTypeName = ANNOTATION_HELPER.extractEntityTypeName(field.getType());
       }
       toEnd.setType(new FullQualifiedName(namespace, toTypeName));
 
@@ -608,14 +613,8 @@ public class AnnotationEdmProvider extends EdmProvider {
       return ANNOTATION_HELPER.getCanonicalName(field);
     }
     
-    private String getCanonicalName(Class<?> clazz) {
-      return ANNOTATION_HELPER.getCanonicalName(clazz);
-    }
-
     private boolean isAnnotatedEntity(Class<?> clazz) {
-      boolean isComplexEntity = clazz.getAnnotation(EdmComplexType.class) != null;
-      boolean isEntity = clazz.getAnnotation(EdmEntityType.class) != null;
-      return isComplexEntity || isEntity;
+      return ANNOTATION_HELPER.isEdmTypeAnnotated(clazz);
     }
 
     private String extractEntitTypeName(NavigationEnd navEnd) {
@@ -629,28 +628,6 @@ public class AnnotationEdmProvider extends EdmProvider {
       }
       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.
-     * 
-     * @param annotatedClass
-     * @return 
-     */
-    private String extractEntitTypeName(Class<?> annotatedClass) {
-      if(annotatedClass == Object.class) {
-        return null;
-      }
-      EdmEntityType type = annotatedClass.getAnnotation(EdmEntityType.class);
-      if(type == null) {
-        return null;
-      }
-      if(type.name().isEmpty()) {
-        return getCanonicalName(annotatedClass);
-      }
-      return type.name();
-    }
   }
 
   static class SchemaBuilder {

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ebe36434/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 cca0b5e..fbc0c7c 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
@@ -10,7 +10,7 @@
  * Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the
  * specific language governing permissions and limitations under the License.
- *****************************************************************************
+ * ****************************************************************************
  */
 package org.apache.olingo.odata2.core.annotation.edm;
 
@@ -33,6 +33,7 @@ import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
 import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
 import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException;
 import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
 import org.apache.olingo.odata2.api.exception.ODataException;
 import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
 
@@ -79,16 +80,74 @@ public class AnnotationHelper {
   }
 
   /**
-   * Get the set property name from an EdmProperty or EdmNavigationProperty annotation.
+   * 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.
+   *
+   * @param annotatedClass
+   * @return
+   */
+  public String extractEntityTypeName(Class<?> annotatedClass) {
+    return extractTypeName(annotatedClass, EdmEntityType.class);
+  }
+
+  public FullQualifiedName extractEntityTypeFqn(Class<?> annotatedClass) {
+    EdmEntityType type = annotatedClass.getAnnotation(EdmEntityType.class);
+    return new FullQualifiedName(type.namespace(), extractEntityTypeName(annotatedClass));
+  }
+
+  public FullQualifiedName extractComplexTypeFqn(Class<?> annotatedClass) {
+    EdmComplexType type = annotatedClass.getAnnotation(EdmComplexType.class);
+    return new FullQualifiedName(type.namespace(), extractComplexTypeName(annotatedClass));
+  }
+
+  public String extractComplexTypeName(Class<?> annotatedClass) {
+    return extractTypeName(annotatedClass, EdmComplexType.class);
+  }
+
+  /**
    * 
+   * 
+   * @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)}
+   */
+  private <T extends Annotation> String extractTypeName(Class<?> annotatedClass,
Class<T> typeAnnotation) {
+    if (annotatedClass == Object.class) {
+      return null;
+    }
+    T type = annotatedClass.getAnnotation(typeAnnotation);
+    if (type == null) {
+      return null;
+    }
+    
+    String name;
+    if(typeAnnotation == EdmEntityType.class) {
+      name = ((EdmEntityType)type).name();
+    } else if(typeAnnotation == EdmComplexType.class) {
+      name = ((EdmComplexType)type).name();
+    } else {
+      return null;
+    }
+
+    if (name.isEmpty()) {
+      return getCanonicalName(annotatedClass);
+    }
+    return name;
+  }
+  
+  /**
+   * Get the set property name from an EdmProperty or EdmNavigationProperty annotation.
+   *
    * @param field
-   * @return 
+   * @return
    */
   public String getPropertyNameFromAnnotation(Field field) {
     EdmProperty property = field.getAnnotation(EdmProperty.class);
-    if(property == null) {
+    if (property == null) {
       EdmNavigationProperty navProperty = field.getAnnotation(EdmNavigationProperty.class);
-      if(navProperty == null) {
+      if (navProperty == null) {
         throw new EdmAnnotationException("Given field '" + field
                 + "' has no EdmProperty or EdmNavigationProperty annotation.");
       }
@@ -98,11 +157,11 @@ public class AnnotationHelper {
   }
 
   public String getPropertyName(Field field) {
-      String propertyName = getPropertyNameFromAnnotation(field);
-      if (propertyName.isEmpty()) {
-        propertyName = getCanonicalName(field);
-      }
-      return propertyName;
+    String propertyName = getPropertyNameFromAnnotation(field);
+    if (propertyName.isEmpty()) {
+      propertyName = getCanonicalName(field);
+    }
+    return propertyName;
   }
 
   public static final class ODataAnnotationException extends ODataException {
@@ -118,7 +177,7 @@ public class AnnotationHelper {
       throw new IllegalStateException("Wrong amount of key properties. Expected read keys
= "
               + fields + " given key predicates = " + keys);
     }
-    
+
     for (Field field : fields) {
       String propertyName = getPropertyName(field);
       Object keyValue = keys.get(propertyName);
@@ -128,20 +187,6 @@ public class AnnotationHelper {
     return instance;
   }
 
-  public <T> T setKeyFields__(T instance, Object[] keyValues) {
-    List<Field> fields = getAnnotatedFields(instance, EdmKey.class);
-    if (fields.size() != keyValues.length) {
-      throw new IllegalStateException("Wrong amount of key properties. Expected read keys
= "
-              + fields + " given key predicates = " + Arrays.toString(keyValues));
-    }
-
-    String propertyName = getCanonicalName(fields.get(0));
-    setValueForProperty(instance, propertyName, keyValues[0]);
-
-    return instance;
-  }
-
-  
   public Field getCommonNavigationFieldFromTarget(Class<?> sourceClass, Class<?>
targetClass) {
     List<Field> sourceFields = getAnnotatedFields(sourceClass, EdmNavigationProperty.class);
     List<Field> targetFields = getAnnotatedFields(targetClass, EdmNavigationProperty.class);
@@ -158,7 +203,6 @@ public class AnnotationHelper {
     return null;
   }
 
-
   public Class<?> getFieldTypeForProperty(Object instance, String propertyName) throws
ODataAnnotationException {
     if (instance == null) {
       return null;
@@ -194,31 +238,6 @@ public class AnnotationHelper {
     }
   }
 
-//  private Object getValueForPropertyName(Object instance, String propertyName, 
-//      Class<?> resultClass, boolean inherited) {
-//    if (instance == null) {
-//      return null;
-//    }
-//
-//    Field[] fields = resultClass.getDeclaredFields();
-//    for (Field field : fields) {
-//      EdmProperty property = field.getAnnotation(EdmProperty.class);
-//      if (property != null) {
-//        if(property.name().equals(propertyName)) {
-//          return getFieldValue(instance, field);
-//        } else if(field.getName().equals(propertyName)) {
-//          return getFieldValue(instance, field);
-//        }
-//      }
-//    }
-//
-//    Class<?> superClass = resultClass.getSuperclass();
-//    if (inherited && superClass != Object.class) {
-//      return getValueForPropertyName(instance, propertyName, superClass, true);
-//    }
-//
-//    return null;
-//  }
   private Field getFieldForPropertyName(Object instance, String propertyName,
           Class<?> resultClass, boolean inherited) {
     if (instance == null) {
@@ -309,7 +328,7 @@ public class AnnotationHelper {
   }
 
   public List<Field> getAnnotatedFields(Object instance, Class<? extends Annotation>
annotation) {
-    if(instance == null) {
+    if (instance == null) {
       return null;
     }
     return getAnnotatedFields(instance.getClass(), annotation, true);
@@ -413,7 +432,7 @@ public class AnnotationHelper {
       return type.getEdmSimpleTypeInstance().valueOfString(propertyValue,
               EdmLiteralKind.DEFAULT, null, fieldClass);
     } catch (EdmSimpleTypeException ex) {
-      throw new ODataRuntimeException("Conversion failed for string property with error:
" 
+      throw new ODataRuntimeException("Conversion failed for string property with error:
"
               + ex.getMessage(), ex);
     }
   }
@@ -425,6 +444,12 @@ public class AnnotationHelper {
     return isEdmAnnotated(object.getClass());
   }
 
+  public boolean isEdmTypeAnnotated(Class<?> clazz) {
+    boolean isComplexEntity = clazz.getAnnotation(EdmComplexType.class) != null;
+    boolean isEntity = clazz.getAnnotation(EdmEntityType.class) != null;
+    return isComplexEntity || isEntity;
+  }
+
   public boolean isEdmAnnotated(Class<?> clazz) {
     if (clazz == null) {
       return false;
@@ -451,8 +476,8 @@ public class AnnotationHelper {
     return content.substring(0, 1).toUpperCase(Locale.ENGLISH) + content.substring(1);
   }
 
-  
   private static class EdmAnnotationException extends RuntimeException {
+
     public EdmAnnotationException(String message) {
       super(message);
     }

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ebe36434/odata2-edm-annotation/edm-annotation-webref/src/main/webapp/index.jsp
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-webref/src/main/webapp/index.jsp b/odata2-edm-annotation/edm-annotation-webref/src/main/webapp/index.jsp
index dffa84d..2219d10 100644
--- a/odata2-edm-annotation/edm-annotation-webref/src/main/webapp/index.jsp
+++ b/odata2-edm-annotation/edm-annotation-webref/src/main/webapp/index.jsp
@@ -68,8 +68,7 @@ th, td { border: 1px solid; padding: 20px; }
 					<li><a href="ReferenceScenario.svc/Managers" target="_blank">Managers</a></li>
 					<li><a href="ReferenceScenario.svc/Buildings" target="_blank">Buildings</a></li>
 					<li><a href="ReferenceScenario.svc/Rooms" target="_blank">Rooms</a></li>
-					<li><a href="ReferenceScenario.svc/Container2.Photos"
-						target="_blank">Container2.Photos</a></li>
+					<li><a href="ReferenceScenario.svc/Photos" target="_blank">Photos</a></li>
 				</ul>
 				<h3>Entities</h3>
 				<ul>
@@ -81,7 +80,7 @@ th, td { border: 1px solid; padding: 20px; }
 						target="_blank">Buildings(1)</a></li>
 					<li><a href="ReferenceScenario.svc/Rooms('1')" target="_blank">Rooms('1')</a></li>
  					<li><a href="ReferenceScenario.svc/Photos(Name='1',Type='2')"
-                           target="_blank">Container2.Photos(Name='1',Type='2')</a></li>
+                           target="_blank">Photos(Name='1',Type='2')</a></li>
 				</ul>
 			</td>
 			<td valign="top">


Mime
View raw message