public void SeedDummyRecords()
        {
            var queryBuilder = new FluentQueryBuilder();
            AddMembership(queryBuilder);
            AddEntities(queryBuilder);
            queryBuilder.Execute(GraphClient.Cypher);

        }
        public IHttpActionResult SaveScreen([FromBody] List<KeyValuePair<SaveEntityVm, List<SavePropertyVm>>> saveData)
        {
            var queryBuilder = new FluentQueryBuilder();

            foreach (var saveGroup in saveData)
            {
                var entity = new Entity() { guid = saveGroup.Key.entityGuid };
                var entityInstance = new EntityInstance() { guid = saveGroup.Key.entityInstanceGuid };

                queryBuilder.Merge(entityInstance);
                queryBuilder.RelateTo(entityInstance, new InstanceOf(), entity);

                foreach (var propertyGroup in saveGroup.Value)
                {

                    var property = new Property() { guid = propertyGroup.propertyGuid };

                    if (propertyGroup.type == "dataProperty")
                    {
                        var propertyValue = new PropertyValue() {guid = Guid.NewGuid().ToString("N"), value = propertyGroup.value};
                        queryBuilder.Merge(propertyValue);
                        queryBuilder.Replace(entityInstance, new HasPropertyValue(), new PropertyValue(), entityInstance, new HasPropertyValue(), propertyValue, property);
                        queryBuilder.RelateTo(propertyValue, new IsValueForProperty(), property);
                    }
                    else if (propertyGroup.type == "entityProperty")
                    {
                        var propertyValueInstance = new EntityInstance() { guid = propertyGroup.value };
                        queryBuilder.Replace(entityInstance, new HasPropertyValue(), new EntityInstance(), entityInstance, new HasPropertyValue(), propertyValueInstance, property);
                        queryBuilder.RelateTo(propertyValueInstance, new IsValueForProperty(), property);
                    }

                    
                }

            }

            queryBuilder.Execute(StagedDbContext.Instance.GraphClient.Cypher);

            
            return Ok();
        }
        private void AddMembership(FluentQueryBuilder queryBuilder)
        {
            var ownerOfApplication = new ApplicationOwner() { guid = Guid.NewGuid().ToString("N"), name = "RAM" };
            var user = new User() { guid = Guid.NewGuid().ToString("N"), firstName = "Alex", lastName = "Stroukov", email = "*****@*****.**" };
            var membership = new Membership() { guid = Guid.NewGuid().ToString("N"), licence = "asajzjdZKHJXZJKSjsLSlSjjs897JS8shKSJSHLSKS" };




            var userRole = new UserRole() { guid = Guid.NewGuid().ToString("N"), name = "Administrator" };

            





            var hasUserRelationship = new HasUser() { };
            var hasRoleRelationship = new HasAction() { };
            var hasApplicationRelationship = new HasApplication() { };
            var usedByApplicationRelationship = new UsedByApplication() { };
            var userOfApplicationRelationship = new UserOfApplication() { };

            var entityGridBasicTemplate = new Template { guid = Guid.NewGuid().ToString("N"), name = "entityGridBasic", location = string.Format("StagedSpa/templates/{0}", "entity-grid-basic.html") };


            var entityDetailsBasicTemplate = new Template { guid = Guid.NewGuid().ToString("N"), name = "entityDetailsBasic", location = string.Format("StagedSpa/templates/{0}", "entity-details-basic.html") };


            
            var hasEntityGridScreen = new HasScreen{ screenType = Enum.ScreenTypeEnum.EntityGrid};
            var hasEntityDetailsScreen = new HasScreen { screenType = Enum.ScreenTypeEnum.EntityDetails };

            var hasTemplate = new HasTemplate { };



            var hasMembershipRelationship = new HasMembership() { runsOutOnMillis = (long)(DateTime.UtcNow.AddMonths(1) - new DateTime(1970, 1, 1)).TotalMilliseconds };



            queryBuilder
                .Create(ownerOfApplication)
                .Create(user)
                .Create(membership)
                .Create(_application)
                .Create(userRole)
                .Create(assetsScreen)
                .Create(assetDetailsScreen)
                .Create(entityGridBasicTemplate)
                .Create(entityDetailsBasicTemplate)
                .Create(_navigateToEntityDetailsCommand)
                .Create(_navigateToEntityDetailsCommand2)

                .Create(_eGridWidget)
                .Create(_eGridTileWidget)
                .Create(_seGridWidget)
                .Create(_seGridTileWidget)
                .Create(_dpTextWidget)
                .Create(_dpTextWidget2)
                .Create(_dpTextWidget3)
                .Create(_epDropdownWidget)

                .RelateTo(_eGridTileWidget, new HasAction(), _navigateToEntityDetailsCommand)
                .RelateTo(_navigateToEntityDetailsCommand, new RelatedActionScreen(), assetDetailsScreen)


                .RelateTo(_seGridTileWidget, new HasAction(), _navigateToEntityDetailsCommand2)
                .RelateTo(_navigateToEntityDetailsCommand2, new RelatedActionScreen(), assetDetailsScreen)

                .RelateTo(ownerOfApplication, hasMembershipRelationship, membership)
                .RelateTo(ownerOfApplication, hasUserRelationship, user)
                .RelateTo(user, hasRoleRelationship, userRole)
                .RelateTo(ownerOfApplication, hasApplicationRelationship, _application)
                .RelateTo(user, userOfApplicationRelationship, _application)

                .RelateTo(userRole, usedByApplicationRelationship, _application)
                .RelateTo(_application, hasEntityDetailsScreen, assetDetailsScreen)
                .RelateTo(_application, hasEntityGridScreen, assetsScreen)

                .RelateTo(assetsScreen, hasTemplate, entityGridBasicTemplate)
                .RelateTo(entityGridBasicTemplate, new HasWidget (), _eGridWidget)
                .RelateTo(_eGridWidget, new HasWidget(), _eGridTileWidget)
                

                .RelateTo(assetDetailsScreen, hasTemplate, entityDetailsBasicTemplate)
                .RelateTo(entityDetailsBasicTemplate, new HasWidget(), _dpTextWidget)
                .RelateTo(entityDetailsBasicTemplate, new HasWidget(), _epDropdownWidget)
                .RelateTo(entityDetailsBasicTemplate, new HasWidget(), _dpTextWidget2)
                .RelateTo(entityDetailsBasicTemplate, new HasWidget(), _dpTextWidget3)
                .RelateTo(entityDetailsBasicTemplate, new HasWidget(), _seGridWidget)
                .RelateTo(_seGridWidget, new HasWidget(), _seGridTileWidget)
                
                ;

        }
        private void AddEntities(FluentQueryBuilder queryBuilder)
        {






            queryBuilder
             .Create(_asset)
             .Create(_assetType)
             .Create(_assetGroup)
             .Create(_manufacturer)
             .Create(_assetWidthProperty)
             .Create(_assetNameProperty)
             .Create(_assetTypeNameProperty)
             .Create(_assetTypeDescriptionProperty)
             .Create(_assetGroupNameProperty)
             .Create(_manufacturerNameProperty)
             .Create(_asset_parentAssetProperty)

             .Create(_asset_assetTypeProperty)
             .Create(_asset_assetTypeProperty2)
             .Create(_assetType_assetGroupProperty)
             .Create(_assetGroup_manufacturerProperty)
             .Create(_manufacturer_assetGroupProperty)
             .Create(_asset_manufacturerProperty)


             .RelateTo(_asset, new HasProperty { }, _asset_parentAssetProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_assetTypeProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_assetTypeProperty2)
             .RelateTo(_assetType, new HasProperty { }, _assetType_assetGroupProperty)
             .RelateTo(_assetGroup, new HasProperty { }, _assetGroup_manufacturerProperty)
             .RelateTo(_manufacturer, new HasProperty { }, _manufacturer_assetGroupProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_manufacturerProperty)

             .RelateTo(_asset, new HasProperty { }, _assetWidthProperty)
             .RelateTo(_asset, new HasProperty { isDefaultDisplayProperty = true }, _assetNameProperty)
             .RelateTo(_assetType, new HasProperty { isDefaultDisplayProperty = true }, _assetTypeNameProperty)
             .RelateTo(_assetType, new HasProperty { }, _assetTypeDescriptionProperty)

             .RelateTo(_assetGroup, new HasProperty { isDefaultDisplayProperty = true }, _assetGroupNameProperty)
             .RelateTo(_manufacturer, new HasProperty { isDefaultDisplayProperty = true }, _manufacturerNameProperty)


             .RelateTo(_asset_parentAssetProperty, new ForEntity(), _asset)
             .RelateTo(_asset_assetTypeProperty, new ForEntity(), _assetType)
             .RelateTo(_asset_assetTypeProperty2, new ForEntity(), _assetType)
             .RelateTo(_assetType_assetGroupProperty, new ForEntity(), _assetGroup)
             .RelateTo(_assetGroup_manufacturerProperty, new ForEntity(), _manufacturer)
             .RelateTo(_manufacturer_assetGroupProperty, new ForEntity(), _assetGroup)
             .RelateTo(_asset_manufacturerProperty, new ForEntity(), _manufacturer)


             .RelateTo(_seGridWidget, new DisplaysValueFor(), _asset_parentAssetProperty)
             .RelateTo(_dpTextWidget3, new DisplaysValueFor(), _assetTypeDescriptionProperty)
             .RelateTo(_dpTextWidget3, new ForThisHeaderProperty(), _asset_assetTypeProperty)
             .RelateTo(_dpTextWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_dpTextWidget, new ForThisHeaderProperty(), _asset_assetTypeProperty)
             .RelateTo(_epDropdownWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_epDropdownWidget, new ForThisHeaderProperty(), _asset_assetTypeProperty)

             .RelateTo(_dpTextWidget2, new DisplaysValueFor(), _assetNameProperty)
                //.RelateTo(_dpTextWidget2, new DisplaysValueFor(), _assetWidthProperty)


             //.RelateTo(_eGridTileWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_eGridTileWidget, new DisplaysValueFor(), _assetNameProperty)
             .RelateTo(_seGridTileWidget, new DisplaysValueFor(), _assetNameProperty)
             ;


            EntityInstance previousAssetInstance = null;

            for (int i = 0; i < 5; i++)
            {
                string assetInstanceGuid = Guid.NewGuid().ToString("N");
                if (i == 0) assetInstanceGuid = "cd49c069209b40aea12b63ae71bfd324";

                var assetInstance = new EntityInstance() { guid = assetInstanceGuid };
                var assetTypeInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var manufacturerInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var assetGroupInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var propertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "100" + i };
                var assetNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset name" + i };
                var assetTypeNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset type name" + i };
                var assetTypeDescriptionPropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset type Description" + i };


                var assetGroupNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset group name" + i };
                var manufacturerNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Manufacturer name" + i };

                queryBuilder
                .Create(assetInstance)
                .Create(assetTypeInstance)
                .Create(assetGroupInstance)
                .Create(manufacturerInstance)
                .Create(propertyValue)
                .Create(assetNamePropertyValue)
                .Create(assetTypeNamePropertyValue)
                .Create(assetTypeDescriptionPropertyValue)
                .Create(assetGroupNamePropertyValue)
                .Create(manufacturerNamePropertyValue)

                .RelateTo(assetInstance, new HasPropertyValue(), assetNamePropertyValue)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetTypeNamePropertyValue)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetTypeDescriptionPropertyValue)
                
                .RelateTo(assetGroupInstance, new HasPropertyValue(), assetGroupNamePropertyValue)
                .RelateTo(manufacturerInstance, new HasPropertyValue(), manufacturerNamePropertyValue)
                .RelateTo(assetInstance, new HasPropertyValue(), propertyValue)

                .RelateTo(assetInstance, new HasPropertyValue(), assetTypeInstance)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetGroupInstance)
                .RelateTo(assetGroupInstance, new HasPropertyValue(), manufacturerInstance)
                .RelateTo(manufacturerInstance, new HasPropertyValue(), assetGroupInstance)
                .RelateTo(assetInstance, new HasPropertyValue(), manufacturerInstance)
                

                .RelateTo(assetTypeInstance, new IsValueForProperty(), _asset_assetTypeProperty)
                .RelateTo(assetTypeInstance, new IsValueForProperty(), _asset_assetTypeProperty2)
                .RelateTo(assetGroupInstance, new IsValueForProperty(), _assetType_assetGroupProperty)

                .RelateTo(manufacturerInstance, new IsValueForProperty(), _assetGroup_manufacturerProperty)
                .RelateTo(assetGroupInstance, new IsValueForProperty(), _manufacturer_assetGroupProperty)


                .RelateTo(propertyValue, new IsValueForProperty(), _assetWidthProperty)

                .RelateTo(assetNamePropertyValue, new IsValueForProperty(), _assetNameProperty)
                .RelateTo(assetTypeNamePropertyValue, new IsValueForProperty(), _assetTypeNameProperty)
                .RelateTo(assetTypeDescriptionPropertyValue, new IsValueForProperty(), _assetTypeDescriptionProperty)
                
                .RelateTo(assetGroupNamePropertyValue, new IsValueForProperty(), _assetGroupNameProperty)
                .RelateTo(manufacturerNamePropertyValue, new IsValueForProperty(), _manufacturerNameProperty)


                .RelateTo(assetInstance, new InstanceOf(), _asset)
                .RelateTo(assetTypeInstance, new InstanceOf(), _assetType)
                .RelateTo(assetGroupInstance, new InstanceOf(), _assetGroup)
                .RelateTo(manufacturerInstance, new InstanceOf(), _manufacturer)
                ;


                if (previousAssetInstance != null)
                    queryBuilder
                        .RelateTo(previousAssetInstance, new HasPropertyValue(), assetInstance)
                        .RelateTo(previousAssetInstance, new IsValueForProperty(), _asset_parentAssetProperty);

                previousAssetInstance = assetInstance;
            }
        }
        public IHttpActionResult Insert([FromBody] CreateScreenVm value)
        {
            var queryBuilder = new FluentQueryBuilder();
            var templates = Enum.GetValues(typeof(TemplateEnum)).Cast<TemplateEnum>().ToList().Select(t => t.ToTemplateVm());

            var matchingTemplate = templates.First(t => t.name == value.screen.template.name);

            var screen = new Screen() { guid = Guid.NewGuid().ToString("N"), name = "New Screen" };
            var template = new Template()
            {
                guid = Guid.NewGuid().ToString("N"),
                name = value.screen.template.name,
                location = matchingTemplate.location
            };

            queryBuilder
                .Create(screen)
                .Create(template)
                .RelateTo(screen, new HasTemplate(), template)
                .RelateTo(StagedDbContext._application, new HasScreen { screenType = value.screen.screenType }, screen)
                
                ;


            foreach (var widget in value.screen.template.widgets)
            {
                var widgetNode = new Widget { guid = Guid.NewGuid().ToString("N"), widgetSchema = widget.widget };

                var propertyNode = new Property() { guid = widget.data.guid };


                queryBuilder
                    .Create(widgetNode)
                    .RelateTo(template, new HasWidget(), widgetNode)
                    .RelateTo(widgetNode, new DisplaysValueFor(), propertyNode)
                    ;

                if (widget.data.next == null || widget.data.next.guid == null) continue;


                if (widget.data.next.next == null || widget.data.next.next.guid == null)
                {
                    var entityPropertyNode = new Property() { guid = widget.data.next.guid };
                    queryBuilder
                        .RelateTo(widgetNode, new ForThisHeaderProperty(), entityPropertyNode);
                }
                else
                {
                    var entityPropertyNode = new Property() { guid = widget.data.next.next.guid };
                    queryBuilder
                        .RelateTo(widgetNode, new ForThisHeaderProperty(), entityPropertyNode);
                }





            }
            

            queryBuilder.Execute(StagedDbContext.Instance.GraphClient.Cypher);

            
            return Ok();
        }