Esempio n. 1
0
        /// <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.Citation"/>.
        /// </returns>
        public virtual CDP4Common.DTO.Citation MapToDto(NpgsqlDataReader reader)
        {
            string tempIsAdaptation;
            string tempLocation;
            string tempModifiedOn;
            string tempRemark;
            string tempShortName;
            string tempThingPreference;

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

            dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse));
            dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse));
            dto.Source = Guid.Parse(reader["Source"].ToString());

            if (valueDict.TryGetValue("IsAdaptation", out tempIsAdaptation))
            {
                dto.IsAdaptation = bool.Parse(tempIsAdaptation);
            }

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

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

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

            if (valueDict.TryGetValue("ShortName", out tempShortName))
            {
                dto.ShortName = tempShortName.UnEscape();
            }

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

            return(dto);
        }
        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);
        }
Esempio n. 3
0
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="citation">
        /// The Citation DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.Citation citation, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, citation, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, citation, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "IsAdaptation", !this.IsDerived(citation, "IsAdaptation") ? citation.IsAdaptation.ToString() : string.Empty },
                    { "Location", !this.IsDerived(citation, "Location") ? citation.Location.Escape() : null },
                    { "Remark", !this.IsDerived(citation, "Remark") ? citation.Remark.Escape() : null },
                    { "ShortName", !this.IsDerived(citation, "ShortName") ? citation.ShortName.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("UPDATE \"{0}\".\"Citation\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"Source\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :source)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = citation.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("source", NpgsqlDbType.Uuid).Value = !this.IsDerived(citation, "Source") ? citation.Source : Utils.NullableValue(null);

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, citation, container));
        }