Exemplo n.º 1
0
        public ResourceReference Save(IResource resource, ResourceOperationContext context)
        {
            if (context.CurrentUser == null)
            {
                throw new InvalidOperationException("User not found");
            }

            Data.Resource dbResource = null;

            //check if the resource already exist in the database
            if (resource.Id > 0)
            {
                dbResource = _dbContext.Resources.Find(resource.Id);
            }

            if (dbResource != null)
            {
                //Update the Resource and Content
                dbResource.Name           = resource.Name;
                dbResource.LastUpdateDate = DateTime.Now;
                dbResource.UpdateUserId   = context.CurrentUser.UserId;
            }
            else
            {
                //Else this is a new resource that was created or was delete from the respository.
                //So create the Resource ,the Content Store and add the resource to the ResourceManager's cache
                var resourceType = GetResourceType(resource);
                var folder       = GetFolder(resourceType);

                dbResource = _dbContext.Resources.Create();
                dbResource.CreateUserId   = context.CurrentUser.UserId;
                dbResource.UpdateUserId   = context.CurrentUser.UserId;
                dbResource.LastUpdateDate = DateTime.Now;
                dbResource.Name           = resource.Name;
                dbResource.ResourceTypeId = resourceType != null ? (int?)resourceType.ResourceTypeId : null;
                dbResource.FolderId       = folder.FolderId;

                dbResource = _dbContext.Resources.Add(dbResource);
                _dbContext.SaveChanges();

                resource.Id = dbResource.Id;

                //The resoures does'n has an Id until it was saved.
                //Then initialize the reference counter
                _resourceManager.Add(resource);
            }

            //mark as saved
            context.SetIsSaved(resource, true);

            //Check if the resource is saved with a context Scene
            if (context.Scene != null && !context.Scene.IsDesignOnly)
            {
                //IF true then link the resource to the context Scene
                if (!_dbContext.SceneResources.Any(x => x.ResourceId == dbResource.Id &&
                                                   x.SceneId == context.Scene.Id))
                {
                    //create the SceneResource
                    SceneResource sceneResource = new SceneResource()
                    {
                        ResourceId = dbResource.Id,
                        SceneId    = context.Scene.Id,
                    };
                    _dbContext.SceneResources.Add(sceneResource);
                }
            }

            //Creates the Reference
            ResourceReference reference;

            if (!_references.TryGetValue(dbResource.Id, out reference))
            {
                reference = new ResourceReference(dbResource.Id);
                _references.Add(dbResource.Id, reference);
            }

            if (dbResource.Content == null)
            {
                //Creates the resource content
                dbResource.Content = new Content();
            }

            //write the resource content data
            var data = CreateAsset(resource, context);

            Debug.Assert(data != null, "Invalid Resource " + resource.Name);
            dbResource.Content.Data = data;

            _dbContext.SaveChanges();

            return(reference);
        }