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 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();
        }