/// <summary>
        /// The mapping from a database record to data transfer object.
        /// </summary>
        /// <param name="reader">
        /// An instance of the SQL reader.
        /// </param>
        /// <returns>
        /// A deserialized instance of <see cref="CDP4Common.DTO.Definition"/>.
        /// </returns>
        public virtual CDP4Common.DTO.Definition MapToDto(NpgsqlDataReader reader)
        {
            string tempModifiedOn;
            string tempLanguageCode;
            string tempContent;

            var valueDict      = (Dictionary <string, string>)reader["ValueTypeSet"];
            var iid            = Guid.Parse(reader["Iid"].ToString());
            var revisionNumber = int.Parse(valueDict["RevisionNumber"]);

            var dto = new CDP4Common.DTO.Definition(iid, revisionNumber);

            dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse));
            dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse));
            dto.Note.AddRange(Utils.ParseOrderedList <string>(reader["Note"] as string[, ]));
            dto.Example.AddRange(Utils.ParseOrderedList <string>(reader["Example"] as string[, ]));
            dto.Citation.AddRange(Array.ConvertAll((string[])reader["Citation"], Guid.Parse));

            if (valueDict.TryGetValue("ModifiedOn", out tempModifiedOn))
            {
                dto.ModifiedOn = Utils.ParseUtcDate(tempModifiedOn);
            }

            if (valueDict.TryGetValue("LanguageCode", out tempLanguageCode))
            {
                dto.LanguageCode = tempLanguageCode.UnEscape();
            }

            if (valueDict.TryGetValue("Content", out tempContent))
            {
                dto.Content = tempContent.UnEscape();
            }

            return(dto);
        }
        public void SetUp()
        {
            this.uri = new Uri("http://www.rheagroup.com");

            this.testInput = new List <Dto.Thing>();

            //Top container
            this.siteDir = new Dto.SiteDirectory(Guid.NewGuid(), 1);

            this.siteRdl = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 1);
            this.siteDir.SiteReferenceDataLibrary.Add(this.siteRdl.Iid);

            var category1 = new Dto.Category(Guid.NewGuid(), 1);

            category1.PermissibleClass.Add(ClassKind.ParameterType);
            category1.PermissibleClass.Add(ClassKind.Person);
            var category2 = new Dto.Category(Guid.NewGuid(), 1);

            category2.PermissibleClass.Add(ClassKind.TelephoneNumber);
            category2.PermissibleClass.Add(ClassKind.EmailAddress);

            this.siteRdl.DefinedCategory.Add(category1.Iid);
            this.siteRdl.DefinedCategory.Add(category2.Iid);

            //topContainer
            var siteRDL2 = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 1);

            this.siteDir.SiteReferenceDataLibrary.Add(siteRDL2.Iid);

            var booleanParameterType = new Dto.BooleanParameterType(Guid.NewGuid(), 1);

            booleanParameterType.Category = new List <Guid> {
                category1.Iid, category2.Iid
            };
            var definition = new Dto.Definition(Guid.NewGuid(), 1);

            booleanParameterType.Definition.Add(definition.Iid);

            siteRDL2.ParameterType.Add(booleanParameterType.Iid);

            this.testInput.Add(this.siteDir);
            this.testInput.Add(this.siteRdl);
            this.testInput.Add(category1);
            this.testInput.Add(category2);

            this.testInput.Add(definition);
            this.testInput.Add(siteRDL2);
            this.testInput.Add(booleanParameterType);
        }
        public async Task VerifyThatCitationIsResolvedWhenRdlIsLoaded()
        {
            var sitedir    = new Dto.SiteDirectory(Guid.NewGuid(), 1);
            var domain     = new Dto.DomainOfExpertise(Guid.NewGuid(), 1);
            var definition = new Dto.Definition(Guid.NewGuid(), 1);
            var citation   = new Dto.Citation(Guid.NewGuid(), 1);

            var referenceSource = new Dto.ReferenceSource(Guid.NewGuid(), 1);
            var srdl            = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 1);

            srdl.ReferenceSource.Add(referenceSource.Iid);

            citation.Source = referenceSource.Iid;

            sitedir.Domain.Add(domain.Iid);
            domain.Definition.Add(definition.Iid);
            definition.Citation.Add(citation.Iid);

            var assembler = new Assembler(this.uri);
            var input     = new List <Dto.Thing>();

            input.Add(sitedir);
            input.Add(domain);
            input.Add(definition);
            input.Add(citation);

            await assembler.Synchronize(input);

            var citationPoco = (Citation)
                               assembler.Cache.Select(x => x.Value).Select(x => x.Value).Single(x => x.Iid == citation.Iid);

            Assert.AreEqual(citationPoco.Source.Iid, Guid.Empty);
            Assert.IsNotEmpty(citationPoco.ValidationErrors);

            sitedir.SiteReferenceDataLibrary.Add(srdl.Iid);
            input.Clear();

            input.Add(sitedir);
            input.Add(srdl);
            input.Add(referenceSource);

            await assembler.Synchronize(input);

            Assert.IsNotNull(citationPoco.Source);
        }
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current <see cref="NpgsqlTransaction"/> to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="definition">
        /// The definition DTO that is to be persisted.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be persisted.
        /// </param>
        /// <returns>
        /// True if the concept was successfully persisted.
        /// </returns>
        public virtual bool Write(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.Definition definition, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, definition, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, definition, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Content", !this.IsDerived(definition, "Content") ? definition.Content.Escape() : string.Empty },
                    { "LanguageCode", !this.IsDerived(definition, "LanguageCode") ? definition.LanguageCode.Escape() : string.Empty },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"Definition\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = definition.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }

                definition.Example.ForEach(x => this.AddExample(transaction, partition, definition.Iid, x));

                definition.Note.ForEach(x => this.AddNote(transaction, partition, definition.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, definition, container));
        }
        public void VerifyThatSpecialStringcharAreSerializedCorrectly()
        {
            this.serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 0, 0));

            var definition = new Dto.Definition(Guid.NewGuid(), 1);

            definition.Content = "abc \"hello world\"";

            using (var memoryStream = new MemoryStream())
            {
                this.serializer.SerializeToStream(new[] { definition }, memoryStream);
                memoryStream.Position = 0;

                using (var reader = new StreamReader(memoryStream))
                {
                    var txt = reader.ReadToEnd();
                    Assert.IsTrue(txt.Contains("abc \\\"hello world\\\""));
                }
            }
        }