예제 #1
0
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="domainFileStore">
        /// The domainFileStore 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.DomainFileStore domainFileStore, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, domainFileStore, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "IsHidden", !this.IsDerived(domainFileStore, "IsHidden") ? domainFileStore.IsHidden.ToString() : 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}\".\"DomainFileStore\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = domainFileStore.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);
                }
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, domainFileStore, container));
        }
예제 #2
0
        public async Task AssertThatIterationIdsForDomainFileStoreRelatedDtosStayFilled()
        {
            var assembler       = new Assembler(this.uri);
            var iterationIid    = Guid.NewGuid();
            var domainFileStore = new Dto.DomainFileStore(Guid.NewGuid(), 1);
            var folder          = new Dto.Folder(Guid.NewGuid(), 1)
            {
                IterationContainerId = iterationIid
            };
            var file = new Dto.File(Guid.NewGuid(), 1)
            {
                IterationContainerId = iterationIid
            };
            var fileRevision = new Dto.FileRevision(Guid.NewGuid(), 1)
            {
                IterationContainerId = iterationIid, ContainingFolder = folder.Iid
            };

            file.FileRevision.Add(fileRevision.Iid);
            domainFileStore.File.Add(file.Iid);
            domainFileStore.Folder.Add(folder.Iid);

            var initialList = new List <Dto.Thing> {
                domainFileStore, folder, file, fileRevision
            };

            // 1st call of Synchronize, uses incoming DTO's
            await assembler.Synchronize(initialList);

            Assert.IsNotEmpty(assembler.Cache);
            Assert.AreEqual(4, assembler.Cache.Count);

            Assert.AreEqual(iterationIid, folder.IterationContainerId);
            Assert.AreEqual(iterationIid, file.IterationContainerId);
            Assert.AreEqual(iterationIid, fileRevision.IterationContainerId);

            //Newly added FileRevision
            var addedFileRevision = new Dto.FileRevision(Guid.NewGuid(), 2)
            {
                IterationContainerId = iterationIid, ContainingFolder = folder.Iid
            };

            file.FileRevision.Add(addedFileRevision.Iid);
            file.RevisionNumber       = 2;
            file.IterationContainerId = iterationIid;

            var addFileRevisionList = new List <Dto.Thing> {
                file, addedFileRevision
            };

            // 2nd call of Synchronize, uses Cache and incoming DTO's
            await assembler.Synchronize(addFileRevisionList);

            Assert.IsNotEmpty(assembler.Cache);
            Assert.AreEqual(5, assembler.Cache.Count);

            Assert.AreEqual(iterationIid, file.IterationContainerId);
            Assert.AreEqual(iterationIid, addedFileRevision.IterationContainerId);
        }
        /// <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.DomainFileStore"/>.
        /// </returns>
        public virtual CDP4Common.DTO.DomainFileStore MapToDto(NpgsqlDataReader reader)
        {
            string tempCreatedOn;
            string tempIsHidden;
            string tempModifiedOn;
            string tempName;
            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.DomainFileStore(iid, revisionNumber);

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

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

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

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

            if (valueDict.TryGetValue("Name", out tempName))
            {
                dto.Name = tempName.UnEscape();
            }

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

            return(dto);
        }