You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ko...@apache.org on 2014/07/28 13:49:29 UTC

git commit: Add support for collections

Repository: olingo-odata4-js
Updated Branches:
  refs/heads/master 369e3804d -> 4379cf7c2


Add support for collections


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/commit/4379cf7c
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/tree/4379cf7c
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/diff/4379cf7c

Branch: refs/heads/master
Commit: 4379cf7c2c2c07e63f63d9c3e2d66113e60d8304
Parents: 369e380
Author: Sven Kobler <sv...@sap.com>
Authored: Mon Jul 28 13:49:09 2014 +0200
Committer: Sven Kobler <sv...@sap.com>
Committed: Mon Jul 28 13:49:09 2014 +0200

----------------------------------------------------------------------
 datajs/demo/tester.html      | 38 +++++++++++++++++++++++++-
 datajs/src/lib/odata/json.js | 57 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 88 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/4379cf7c/datajs/demo/tester.html
----------------------------------------------------------------------
diff --git a/datajs/demo/tester.html b/datajs/demo/tester.html
index 75695a2..05d3847 100644
--- a/datajs/demo/tester.html
+++ b/datajs/demo/tester.html
@@ -44,6 +44,8 @@
         <button id="btnJSONwithMetaData">JSON with MetaData</button><br/>
         <button id="btnPOST_entry_food">POST food entry</button><br/>
         <button id="btnPOST_entry_categorie">POST categorie entry</button><br/>
+        <button id="btnGET_collection_full">GET collection odata.metadata=full</button><br/>
+        <button id="btnGET_collection_minimal">GET collection odata.metadata=minimal</button><br/>
         <div id='resultsArea' data-type="json">
         </div>
         <script>
@@ -121,7 +123,13 @@
                 };
                 datajs.V4.oData.read(requestUri, success, errorFunc);
             });
-
+            $('#btnPOST_entry_food').on("click", function(){
+                var requestUri = {
+                    requestUri : 'http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Foods(0)/Providers',
+                    headers : { Accept : 'application/json;odata.metadata=full' }
+                };
+                datajs.V4.oData.read(requestUri, success, errorFunc);
+            });
             $('#btnJSON_full_to_all').on("click", function(){
                 var requestUri = {
                     requestUri : 'http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Foods',
@@ -131,6 +139,34 @@
                 };
                 datajs.V4.oData.read(requestUri, success, errorFunc);
             });
+            $('#btnGET_collection_full').on("click", function(){
+                var metaDatasuccess = function(metadata){
+                    var requestUri = {
+                        requestUri : 'http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Foods(0)/Providers',
+                        headers : { Accept : 'application/json;odata.metadata=full' },
+                        recognizeDates : false
+                    };
+
+                    datajs.V4.oData.read(requestUri, success, errorFunc, null, null, metadata);
+                  
+                }
+                getMetaData(metaDatasuccess);
+            });
+
+            $('#btnGET_collection_minimal').on("click", function(){
+                var metaDatasuccess = function(metadata){
+                    var requestUri = {
+                        requestUri : 'http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Foods(0)/Providers',
+                        headers : { Accept : 'application/json;odata.metadata=minimal' },
+                        recognizeDates : false
+                    };
+
+                    datajs.V4.oData.read(requestUri, success, errorFunc, null, null, metadata);
+                  
+                }
+                getMetaData(metaDatasuccess);
+            });
+
             $('#btnJSON_full_to_all_date_conversion').on("click", function(){
                 var requestUri = {
                     requestUri : 'http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Foods',

http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/4379cf7c/datajs/src/lib/odata/json.js
----------------------------------------------------------------------
diff --git a/datajs/src/lib/odata/json.js b/datajs/src/lib/odata/json.js
index 33f4364..04e6ef8 100644
--- a/datajs/src/lib/odata/json.js
+++ b/datajs/src/lib/odata/json.js
@@ -479,10 +479,22 @@ var parseContextUriFragment = function( fragments, model ) {
             if ( ret.detectedPayloadKind === PAYLOADTYPE_FEED || ret.detectedPayloadKind === PAYLOADTYPE_ENTRY) {
                 var property = lookupProperty(ret.type.property, fragment);
                 if (property !== null) {
+                    //PAYLOADTYPE_COLLECTION
                     ret.typeName = property.type;
-                    ret.type = lookupComplexType(ret.typeName, model);
+                    
+                    
+                    if (utils.startsWith(property.type, 'Collection')) {
+                        ret.detectedPayloadKind = PAYLOADTYPE_COLLECTION;
+                        var tmp12 =  property.type.substring(10+1,property.type.length - 1);
+                        ret.typeName = tmp12;
+                        ret.type = lookupComplexType(tmp12, model);    
+                        ret.detectedPayloadKind = PAYLOADTYPE_COLLECTION;
+                    } else {
+                        ret.type = lookupComplexType(property.type, model);    
+                        ret.detectedPayloadKind = PAYLOADTYPE_PROPERTY;
+                    }    
+
                     ret.name = fragment;
-                    ret.detectedPayloadKind = PAYLOADTYPE_PROPERTY;
                     // Capter 10.15
                 }
                 continue;
@@ -556,7 +568,7 @@ var readPayloadMinimal = function (data, model, recognizeDates) {
         case PAYLOADTYPE_ENTRY:
             return readPayloadMinimalEntry(data, model, payloadInfo, baseURI, recognizeDates);
         case PAYLOADTYPE_COLLECTION:
-            return data; // Just return "data" before the ReadPayload function for this kind is implmented.
+            return readPayloadMinimalCollection(data, model, payloadInfo, baseURI, recognizeDates);
         case PAYLOADTYPE_PRIMITIVE:
             return data;
         case PAYLOADTYPE_SVCDOC:
@@ -597,6 +609,37 @@ var jsonLightGetEntryKey = function (data, entityModel) {
     return entityInstanceKey;
 };
 
+var readPayloadMinimalCollection = function (data, model, collectionInfo, baseURI, recognizeDates) {
+
+    data['@odata.type'] = '#Collection('+collectionInfo.typeName + ')';
+
+    var entries = [];
+
+    var items = data.value;
+    for (i = 0, len = items.length; i < len; i++) {
+        var item = items[i];
+        if ( defined(item['@odata.type'])) { // in case of mixed collections
+            var typeName = item['@odata.type'].substring(1);
+            var type = lookupEntityType( typeName, model);
+            var entryInfo = {
+                contentTypeOdata : collectionInfo.contentTypeOdata,
+                detectedPayloadKind : collectionInfo.detectedPayloadKind,
+                name : collectionInfo.name,
+                type : type,
+                typeName : typeName
+            };
+
+            entry = readPayloadMinimalObject(item, entryInfo, baseURI, model, recognizeDates);
+        } else {
+            entry = readPayloadMinimalObject(item, collectionInfo, baseURI, model, recognizeDates);
+        }
+        
+        entries.push(entry);
+    }
+    data.value = entries;
+    return data;
+};
+
 var readPayloadMinimalFeed = function (data, model, feedInfo, baseURI, recognizeDates) {
     var entries = [];
     var items = data.value;
@@ -725,9 +768,11 @@ var readPayloadMinimalObject = function (data, objectInfo, baseURI, model, recog
         keyType = lookupEntityType(keyType.baseType, model);
     }
 
-    var lastIdSegment = objectInfo.name + jsonLightGetEntryKey(data, keyType);
-    data['@odata.id'] = baseURI.substring(0, baseURI.lastIndexOf("$metadata")) + lastIdSegment;
-    data['@odata.editLink'] = lastIdSegment;
+    if (keyType.key !== undefined) {
+        var lastIdSegment = objectInfo.name + jsonLightGetEntryKey(data, keyType);
+        data['@odata.id'] = baseURI.substring(0, baseURI.lastIndexOf("$metadata")) + lastIdSegment;
+        data['@odata.editLink'] = lastIdSegment;
+    }
 
     var serviceURI = baseURI.substring(0, baseURI.lastIndexOf("$metadata"));
     //jsonLightComputeUrisIfMissing(data, entryInfo, actualType, serviceURI, dataModel, baseTypeModel);