/// <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.TelephoneNumber"/>.
        /// </returns>
        public virtual CDP4Common.DTO.TelephoneNumber MapToDto(NpgsqlDataReader reader)
        {
            string tempModifiedOn;
            string tempThingPreference;
            string tempValue;

            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.TelephoneNumber(iid, revisionNumber);

            dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse));
            dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse));
            dto.VcardType.AddRange(Utils.ParseEnumArray <CDP4Common.SiteDirectoryData.VcardTelephoneNumberKind>((string[])reader["VcardType"]));

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

            if (valueDict.TryGetValue("ThingPreference", out tempThingPreference) && tempThingPreference != null)
            {
                dto.ThingPreference = tempThingPreference.UnEscape();
            }

            if (valueDict.TryGetValue("Value", out tempValue))
            {
                dto.Value = tempValue.UnEscape();
            }

            return(dto);
        }
        /// <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="telephoneNumber">
        /// The telephoneNumber 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.TelephoneNumber telephoneNumber, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, telephoneNumber, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Value", !this.IsDerived(telephoneNumber, "Value") ? telephoneNumber.Value.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}\".\"TelephoneNumber\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = telephoneNumber.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);
                }

                telephoneNumber.VcardType.ForEach(x => this.AddVcardType(transaction, partition, telephoneNumber.Iid, x));
            }

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

            var email = new Dto.TelephoneNumber(Guid.NewGuid(), 1);

            email.Value = "test";
            email.VcardType.Add((VcardTelephoneNumberKind)5);
            email.VcardType.Add((VcardTelephoneNumberKind)4);

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

                using (var reader = new StreamReader(memoryStream))
                {
                    var txt = reader.ReadToEnd();
                    Assert.IsTrue(txt.Contains("CELL"));
                    Assert.IsTrue(txt.Contains("FAX"));
                }
            }
        }