[USERGRID-515] Modifying create collection dialog to take entity

Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/266a111f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/266a111f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/266a111f

Branch: refs/heads/USERGRID-515
Commit: 266a111fb3ac7061b24a4776272a191d981d7ef9
Parents: 88b36b6
Author: ryan bridges <[email protected]>
Authored: Tue Apr 14 12:54:51 2015 -0400
Committer: ryan bridges <[email protected]>
Committed: Tue Apr 14 12:54:51 2015 -0400

----------------------------------------------------------------------
 portal/js/data/data-controller.js | 58 ++++++++++++++++++++++++++++++++--
 portal/js/data/data.html          | 36 +++++++++++++++++++--
 portal/js/global/ug-service.js    | 30 +++++++++++++++++-
 3 files changed, 117 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/266a111f/portal/js/data/data-controller.js
----------------------------------------------------------------------
diff --git a/portal/js/data/data-controller.js 
b/portal/js/data/data-controller.js
index 486373e..0221b71 100644
--- a/portal/js/data/data-controller.js
+++ b/portal/js/data/data-controller.js
@@ -31,7 +31,7 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
       $scope.queryLimitDisplay = 'block';
       $scope.queryStringDisplay = 'block';
       $scope.entitySelected = {};
-      $scope.newCollection = {};
+      $scope.newCollection = {name:"", entity:'{"name":"test"}'};
       $rootScope.queryCollection = {};
       $scope.data = {};
       $scope.data.queryPath = '';
@@ -71,7 +71,7 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
       $scope.collectionList = collectionList;
       $scope.queryBoxesSelected = false;
       if(!$scope.queryPath){
-        
$scope.loadCollection('/'+collectionList[Object.keys(collectionList).sort()[0]].name);
+        
$scope.loadCollection('/'+$scope.collectionList[Object.keys($scope.collectionList).sort()[0]].name);
       }
       $scope.applyScope();
     });
@@ -98,6 +98,7 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
 
     $scope.$on('collection-created',function(){
       $scope.newCollection.name = '';
+      $scope.newCollection.entity = '{ "name":"value" }';
     });
 
     $scope.$on('query-received', function(event, collection) {
@@ -151,6 +152,21 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
       }
     };
 
+    $scope.newCollectionWithEntityDialog = function(modalId){
+      if(!($scope.newCollection.name && $scope.newCollection.name.length>0)){
+        $rootScope.$broadcast('alert', 'error', 'You must specify a collection 
name.');
+      }else if(!($scope.newCollection.entity && 
$scope.newCollection.entity.length>0)){
+        $rootScope.$broadcast('alert', 'error', 'You must specify JSON data 
for the new entity.');
+      }else if(!$scope.validateEntityData(true)){
+        //Do Nothing. Alert is thrown in validateEntityData
+      }else{
+        ug.createCollectionWithEntity($scope.newCollection.name, 
$scope.newCollection.entity);
+        ug.getTopCollections();
+        $rootScope.$broadcast('alert', 'success', 'Collection created 
successfully.');
+        $scope.hideModal(modalId)
+      }
+    };
+
     $scope.addToPath = function(uuid){
       $scope.data.queryPath = '/' + $rootScope.queryCollection._type + '/' + 
uuid;
     }
@@ -199,6 +215,24 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
       $scope.verb = 'DELETE';
     }
 
+    $scope.validateEntityData = function(skipMessage) {
+      var queryBody = $scope.newCollection.entity;
+
+      try {
+        queryBody = JSON.parse(queryBody);
+      } catch (e) {
+        $rootScope.$broadcast('alert', 'error', 'JSON is not valid');
+        return false;
+      }
+
+      queryBody = JSON.stringify(queryBody,null,2);
+
+      !skipMessage && $rootScope.$broadcast('alert','success', 'JSON is 
valid');
+
+      $scope.newCollection.entity = queryBody;
+      return true;
+    }
+
     $scope.validateJson = function(skipMessage) {
       var queryBody = $scope.data.queryBody;
 
@@ -217,6 +251,24 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
       return true;
     }
 
+    $scope.isValidJSON = function(data) {
+      try {
+        data = JSON.parse(data);
+      } catch (e) {
+        return false;
+      }
+      return true;
+    }
+
+    $scope.formatJSON = function(data) {
+      try {
+        data = JSON.parse(data);
+      } catch (e) {
+        return data;
+      }
+      return JSON.stringify(data,null,2);
+    }
+
 
     $scope.saveEntity = function(entity){
       if (!$scope.validateJson()) {
@@ -333,4 +385,4 @@ AppServices.Controllers.controller('DataCtrl', ['ug', 
'$scope', '$rootScope', '$
     $scope.resetNextPrev();
 
 
-  }]);
\ No newline at end of file
+  }]);

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/266a111f/portal/js/data/data.html
----------------------------------------------------------------------
diff --git a/portal/js/data/data.html b/portal/js/data/data.html
index c732d1a..3183f53 100644
--- a/portal/js/data/data.html
+++ b/portal/js/data/data.html
@@ -6,9 +6,9 @@
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
-  
+
   http://www.apache.org/licenses/LICENSE-2.0
-  
+
   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.
@@ -34,6 +34,36 @@
   </fieldset>
   </bsmodal>
 
+<bsmodal id="newCollectionWithEntity" title="Create new collection"
+    close="hideModal" closelabel="Cancel"
+    extrabutton="newCollectionWithEntityDialog" extrabuttonlabel="Create"
+    buttonid="collection" ng-cloak>
+  <fieldset>
+    <div class="control-group">
+      <label for="new-collection-name">Collection Name:</label>
+      <div class="controls">
+        <input type="text" ug-validate required
+          ng-pattern="collectionNameRegex"
+          ng-attr-title="{{collectionNameRegexDescription}}"
+          ng-model="$parent.newCollection.name"
+          placeholder="newCollection"
+          name="collection"
+          id="new-collection-name"
+          class="input-xlarge" />
+        <p class="help-block hide"></p>
+      </div>
+      <div class="controls">
+        <label for="new-collection-entity">Create a new entity for your 
collection:</label>
+        <textarea
+        ng-model="$parent.newCollection.entity" id="new-collection-entity" 
class="span6 pull-left" rows="4">{ "name":"value" }</textarea>
+        <br>
+        <a class="btn pull-left" ng-click="validateEntityData();">Validate 
JSON</a>
+        <p class="help-block hide"></p>
+      </div>
+    </div>
+  </fieldset>
+  </bsmodal>
+
   <div id="intro-page">
     <page-title title=" Collections" icon="&#128254;"></page-title>
   </div>
@@ -41,7 +71,7 @@
   <section class="row-fluid">
     <div id="intro-list" class="span3 user-col">
       <a class="btn btn-primary" id="new-collection-link"
-        ng-click="showModal('newCollection')" title="new collection"><i
+        ng-click="showModal('newCollectionWithEntity')" title="new 
collection"><i
         class="pictogram">&#57347;</i></a> <a class="help_tooltip"
         ng-mouseover="help.sendTooltipGA('data new collection')"
         ng-show="help.helpTooltipsEnabled" href="#"

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/266a111f/portal/js/global/ug-service.js
----------------------------------------------------------------------
diff --git a/portal/js/global/ug-service.js b/portal/js/global/ug-service.js
index cc934b7..e6d7cb9 100644
--- a/portal/js/global/ug-service.js
+++ b/portal/js/global/ug-service.js
@@ -258,6 +258,34 @@ AppServices.Services.factory('ug', function(configuration, 
$rootScope, utility,
 
       });
     },
+    createCollectionWithEntity: function(collectionName, entityData) {
+      try{
+        entityData=JSON.parse(entityData)
+        entityData.type=collectionName;
+      }catch(e){
+        $rootScope.$broadcast('alert', 'error', 'JSON is not valid');
+        return;
+      }
+      var self = this;
+      self.client().createEntity(entityData, function(err, entity) {
+        if (err) {
+          console.error(err);
+          return $rootScope.$broadcast('alert', 'error',
+            'error creating collection');
+        }
+        entity.destroy(function() {
+          self.getTopCollections(function(err, collections) {
+            if (err) {
+              $rootScope.$broadcast('alert', 'error',
+                'error creating collection');
+            } else {
+              $rootScope.$broadcast('collection-created',
+                collections);
+            }
+          });
+        });
+      });
+    },
     getApplications: function() {
       this.client().getApplications(function(err, applications) {
         if (err) {
@@ -449,7 +477,7 @@ AppServices.Services.factory('ug', function(configuration, 
$rootScope, utility,
 
           var queryPath = data.path;
           //remove preceeding slash
-          queryPath = queryPath.replace(/^\//, ''); 
+          queryPath = queryPath.replace(/^\//, '');
           self.getCollection('query', queryPath, null, 'order by modified 
DESC', null);
 
         }

Reply via email to