private static async Task <long> CreateAsset(string name)
        {
            // Create asset
            var asset = new EntityResourceWrapper(MConnector.Client);

            // Set title
            asset.SetProperty <string>(Constants.Properties.Title, name);

            // Set lifecycle
            var lifeCycleId = await EntityHelper.GetEntityId(Constants.Definitions.FinalLifeCycleStatus, Constants.Properties.StatusValue, "Approved");

            var lifeCycleRelation = await asset.GetRelation(Constants.Relations.FinalLifeCycleStatusToAsset);

            await lifeCycleRelation.SetParentId(lifeCycleId);

            // Set content repo
            var contentRepoId = await EntityHelper.GetEntityId(Constants.Definitions.ContentRepository, Constants.Properties.ClassificationName, "Standard");

            var contentRepoRelation = await asset.GetRelation(Constants.Relations.ContentRepositoryToAsset);

            await contentRepoRelation.SetParentId(contentRepoId);

            // save asset
            return(await MConnector.Client.Entities.Create(asset, Constants.Definitions.Asset));
        }
Пример #2
0
        private static async Task <long> CreateAsset(string title)
        {
            Console.WriteLine($"Creating asset {title}.");

            // Create entity
            var entity = new EntityResourceWrapper(MConnector.Client);

            // Set Title property
            entity.SetProperty <string>("Title", title);

            // Set FinalLifeCycleStatusToAsset
            var lifeCycleId = await GetEntityId("M.Final.LifeCycle.Status", "StatusValue", "Approved");

            var lifeCycleRelation = await entity.GetRelation("FinalLifeCycleStatusToAsset");

            await lifeCycleRelation.SetParentId(lifeCycleId);

            // Set ContentRepositoryToAsset
            var contentRepoId = await GetEntityId("M.Content.Repository", "ClassificationName", "Standard");

            var contentRepoRelation = await entity.GetRelation("ContentRepositoryToAsset");

            await contentRepoRelation.SetParentId(contentRepoId);

            // Save the asset
            return(await MConnector.Client.Entities.Create(entity, "M.Asset"));
        }
Пример #3
0
        /// <summary>
        /// Creates the asset.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="assetTypeId">The asset type identifier.</param>
        /// <param name="resourceUrl">The resource URL.</param>
        /// <returns></returns>
        public static async Task <long> CreateAsset(string title, string description, string status, long contentRepositoryId)
        {
            // Initialize the entity resource
            var entity = new EntityResourceWrapper(MConnector.Client);

            // Title
            entity.SetProperty <string>(Constants.EntityDefinitions.Asset.Properties.Title, title);

            // Description
            entity.SetProperty <string>(Constants.EntityDefinitions.Asset.Properties.Description, description, Constants.DefaultCulture);

            // Link to content repository
            var contentRepositoryRelation = await MConnector.Client.Entities.GetRelation(entity, Constants.EntityDefinitions.Asset.Relations.ContentRepositoryToAsset);

            await contentRepositoryRelation.SetParentId(contentRepositoryId);

            // Link the asset to final lifecycle
            var finalLifeCycleStatus = await MConnector.Client.Entities.Get($"M.Final.LifeCycle.Status.{status}");

            var finalLifeCycleRelation = await MConnector.Client.Entities.GetRelation(entity, Constants.EntityDefinitions.Asset.Relations.FinalLifeCycleStatusToAsset);

            await finalLifeCycleRelation.SetParentId(finalLifeCycleStatus.Resource.Id);

            // Process the relation updates
            return(await MConnector.Client.Entities.Create(entity, Constants.EntityDefinitions.Asset.DefinitionName));
        }
Пример #4
0
        public static async Task <long> CreateAsset(string title)
        {
            // Create the entity resource
            var asset = new EntityResourceWrapper(MConnector.Client);

            // Set the mandatory title property
            asset.SetProperty <string>(Constants.EntityDefinitions.Asset.Properties.Title, title);

            // Link the asset to content repository: standard
            var standardContentRepository = await MConnector.Client.Entities.Get(Constants.ContentRepositories.Standard);

            var contentRepositoryRelation = await MConnector.Client.Entities.GetRelation(asset, Constants.EntityDefinitions.Asset.Relations.ContentRepositoryToAsset);

            await contentRepositoryRelation.SetParentIds(new List <long>() { standardContentRepository.Resource.Id });

            // Link the asset to final lifecycle: created
            var finalLifeCycleCreated = await MConnector.Client.Entities.Get(Constants.LifeCycleStatus.Created);

            var finalLifeCycleRelation = await MConnector.Client.Entities.GetRelation(asset, Constants.EntityDefinitions.Asset.Relations.FinalLifeCycleStatusToAsset);

            await finalLifeCycleRelation.SetParentId(finalLifeCycleCreated.Resource.Id);

            // Create the asset
            var assetId = await MConnector.Client.Entities.Create(asset, Constants.EntityDefinitions.Asset.DefinitionName);

            // Return a reference to the newly created asset
            return(assetId);
        }
Пример #5
0
        /// <summary>
        /// Converts to json.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private static async Task <JObject> ConvertToJson(EntityResourceWrapper entity)
        {
            JObject result = new JObject();

            // System properties
            result.Add(new JProperty("id", entity.Resource.Id));
            result.Add(new JProperty("identifier", entity.Resource.Identifier));
            result.Add(new JProperty("definition", entity.Resource.EntityDefinition.Uri));
            result.Add(new JProperty("uri", entity.Resource.Self.Uri));
            result.Add(new JProperty("version", entity.Resource.Version));
            result.Add(new JProperty("createdon", entity.Resource.CreatedOn));
            result.Add(new JProperty("createdby", entity.Resource.CreatedBy.Uri));
            result.Add(new JProperty("modifiedon", entity.Resource.ModifiedOn));
            result.Add(new JProperty("modifiedby", entity.Resource.ModifiedBy.Uri));

            // Properties
            JArray properties = new JArray();

            foreach (var property in entity.Resource.Properties)
            {
                properties.Add(new JObject(new JProperty(property.Key, property.Value)));
            }
            result.Add(new JProperty("properties", properties));

            // Relations
            JArray relations = new JArray();

            foreach (var relation in entity.Resource.Relations)
            {
                var definitionName = entity.Resource.EntityDefinition.Uri.Split('/').LastOrDefault();
                var definition     = await MConnector.Client.EntityDefinitions.Get(definitionName);

                var memberGroup        = definition.MemberGroups.FirstOrDefault(mg => mg.MemberDefinitions.FirstOrDefault(md => md.Name.Equals(relation.Key)) != null);
                var relationDefinition = (RelationDefinition)memberGroup.MemberDefinitions.First(rd => rd.Name == relation.Key);

                if (relationDefinition.IsNested)
                {
                    var link = (RelationResource)relation.Value;
                    relations.Add(new JObject(new JProperty(relation.Key, link.Self.Uri)));
                }
                else
                {
                    var link = (Link)relation.Value;
                    relations.Add(new JObject(new JProperty(relation.Key, link.Uri)));
                }
            }

            result.Add(new JProperty("relations", relations));

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Gets or creates the user group with the specified name and returns the id.
        /// </summary>
        /// <param name="userGroupName">Name of the user group.</param>
        /// <returns></returns>
        public static async Task <long> GetOrCreateUserGroupId(string userGroupName)
        {
            // Build query to retrieve the given usergroup
            var query = new Query
            {
                Filter = new CompositeQueryFilter
                {
                    Children = new List <QueryFilter>()
                    {
                        new DefinitionQueryFilter
                        {
                            Name = Constants.EntityDefinitions.UserGroup.DefinitionName
                        },
                        new PropertyQueryFilter
                        {
                            Property = Constants.EntityDefinitions.UserGroup.Properties.GroupName,
                            Operator = ComparisonOperator.Equals,
                            DataType = FilterDataType.String,
                            Value    = userGroupName
                        }
                    }
                },
                Skip = 0,
                Take = 1,
                EntityLoadOptions = new EntityLoadOptions()
                {
                    LoadEntities = false
                }
            };

            // Attempt to retrieve the usergroup id
            var result = await MConnector.Client.Querying.Query(query);

            // Return the usergroup in case it already exists
            if (result.TotalItems.HasValue && result.TotalItems.Value > 0)
            {
                return(result.Ids.First());
            }

            // Create the usergroup if it does not exist yet
            var userGroup = new EntityResourceWrapper(MConnector.Client);

            userGroup.SetProperty(Constants.EntityDefinitions.UserGroup.Properties.GroupName, userGroupName);

            return(await MConnector.Client.Entities.Create(userGroup, Constants.EntityDefinitions.UserGroup.DefinitionName));
        }
Пример #7
0
        public static async Task <long> CreateAssetType(string name)
        {
            // Check if the asset type already exists
            var result = await MConnector.Client.Querying.Query(
                Query.CreateIdsQuery(entities =>
                                     from e in entities
                                     where e.Property(Constants.EntityDefinitions.AssetType.Properties.Label) == name
                                     select e));

            if (result.TotalItems > 0)
            {
                return(result.Ids.Single());
            }

            // Create a new asset type entity resource
            var assetType = new EntityResourceWrapper(MConnector.Client);

            assetType.Resource.EntityDefinition = Constants.EntityDefinitions.AssetType.DefinitionName;

            // Mark the asset type as a root taxonomy item
            assetType.Resource.IsRootTaxonomyItem = true;

            // Save the asset type
            var assetTypeId = await MConnector.Client.Entities.Create(assetType, Constants.EntityDefinitions.AssetType.DefinitionName);

            // Get our created resource
            assetType = await MConnector.Client.Entities.Get(assetTypeId);

            // Set the classification name
            assetType.SetProperty <string>(Constants.EntityDefinitions.AssetType.Properties.Label, name);

            // Update the resource
            await MConnector.Client.Entities.Update(assetType);

            return(assetTypeId);
        }
Пример #8
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "head", "post", Route = "create")] HttpRequestMessage req, TraceWriter log)
        {
            // Connectivity check
            if (req.Method == HttpMethod.Head)
            {
                return(req.CreateResponse(HttpStatusCode.OK));
            }

            // Parse request
            var body = await req.Content.ReadAsStringAsync();

            var token = JToken.Parse(body);

            var title = token["title"].Value <string>();
            var image = token["image"].Value <string>();

            // Create entity
            var entity = new EntityResourceWrapper(MConnector.Client);

            // Set Title property
            entity.SetProperty <string>(Constants.Properties.Title, title);

            // Set FinalLifeCycleStatusToAsset to 'Created'
            var lifeCycleId = await EntityHelper.GetEntityId(Constants.Definitions.FinalLifeCycleStatus, Constants.Properties.StatusValue, "Created");

            var lifeCycleRelation = await entity.GetRelation(Constants.Relations.FinalLifeCycleStatusToAsset);

            await lifeCycleRelation.SetParentId(lifeCycleId);

            // Set ContentRepositoryToAsset to 'Standard'
            var contentRepoId = await EntityHelper.GetEntityId(Constants.Definitions.ContentRepository, Constants.Properties.ClassificationName, "Standard");

            var contentRepoRelation = await entity.GetRelation(Constants.Relations.ContentRepositoryToAsset);

            await contentRepoRelation.SetParentId(contentRepoId);

            long assetId;

            try
            {
                // Save entity
                assetId = await MConnector.Client.Entities.Create(entity, Constants.Definitions.Asset);

                log.Info($"Entity {assetId} created.");

                var fetchJobId = await MConnector.Client.Jobs.CreateFetchJob(new WebFetchJobRequest("my fetch job", assetId)
                {
                    Urls = new List <string> {
                        image
                    }
                });

                log.Info($"Fetch job {fetchJobId} created.");
            }
            catch (WebApiValidationException ex)
            {
                // Handle validation errors
                foreach (var failure in ex.Failures)
                {
                    log.Error($"{failure.Key} - {failure.Message}");
                }
                throw;
            }

            // Return asset id in response
            var response = req.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(JsonConvert.SerializeObject(new { assetId }), Encoding.UTF8, "application/json");
            return(response);
        }