예제 #1
0
        /// <summary>
        /// Imports the specified text stream.
        /// </summary>
        /// <param name="stream">The text stream.</param>
        /// <exception cref="ArgumentNullException">txtStream or appStream</exception>
        public void ImportText(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            JsonDocument doc = JsonDocument.Parse(stream, _options);

            // for each item
            foreach (JsonElement itemElem in doc.RootElement.EnumerateArray())
            {
                // read its metadata
                IItem item = ReadItem(itemElem);

                // import it
                Logger?.LogInformation("Importing item {ItemId}: {Title}",
                                       item.Id, item.Title);
                if (!IsDry)
                {
                    _repository.AddItem(item);
                }

                // import its parts
                foreach (JsonElement partElem in itemElem.GetProperty("parts")
                         .EnumerateArray())
                {
                    if (!IsDry)
                    {
                        _repository.AddPartFromContent(partElem.ToString());
                    }
                }
            }
        }
예제 #2
0
        private Tuple <string, string> AddRawPart(string database, string raw)
        {
            ICadmusRepository repository =
                _repositoryProvider.CreateRepository(database);

            JObject doc = JObject.Parse(raw);

            // add the ID if new part
            JValue id = (JValue)doc["id"];
            string partId;
            bool   isNew = id == null || !_guidRegex.IsMatch(id.Value <string>());

            if (isNew)
            {
                partId = Guid.NewGuid().ToString("N");
                if (id != null)
                {
                    doc.Property("id").Remove();
                }
                doc.AddFirst(new JProperty("id", partId));
            }
            else
            {
                partId = id.Value <string>();
            }

            // set the creator ID if not specified
            JValue creatorId = (JValue)doc["creatorId"];

            if (string.IsNullOrEmpty(creatorId?.ToString()))
            {
                if (creatorId != null)
                {
                    doc.Property("creatorId").Remove();
                }
                doc.Add(new JProperty("creatorId", User.Identity.Name));
            }

            // override the user ID
            doc.Property("userId")?.Remove();
            doc.Add(new JProperty("userId", User.Identity.Name ?? ""));

            // add the part
            _logger.Information("User {UserName} saving part {PartId} from {IP}",
                                User.Identity.Name,
                                partId,
                                HttpContext.Connection.RemoteIpAddress);

            string json = doc.ToString(Formatting.None);

            repository.AddPartFromContent(json);
            return(Tuple.Create(partId, json));
        }
예제 #3
0
        private int ImportItemsWithParts(JsonDocument doc)
        {
            int count = 0;

            // for each item
            foreach (JsonElement itemElem in doc.RootElement.EnumerateArray())
            {
                // read its metadata
                IItem item = ReadItem(itemElem);

                // import it
                _repository.AddItem(item);

                // import its parts
                foreach (JsonElement partElem in itemElem.GetProperty("parts")
                         .EnumerateArray())
                {
                    _repository.AddPartFromContent(partElem.ToString());
                    count++;
                }
            }
            return(count);
        }