/// <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="file">
        /// The File 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.File file, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, file, container, out isHandled, valueTypeDictionaryAdditions);

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

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"File\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"LockedBy\", \"Owner\")");
                    sqlBuilder.AppendFormat(" = (:container, :lockedBy, :owner)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = file.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                    command.Parameters.Add("lockedBy", NpgsqlDbType.Uuid).Value  = !this.IsDerived(file, "LockedBy") ? Utils.NullableValue(file.LockedBy) : Utils.NullableValue(null);
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value     = !this.IsDerived(file, "Owner") ? file.Owner : Utils.NullableValue(null);

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, file, container));
        }
        /// <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.File"/>.
        /// </returns>
        public virtual CDP4Common.DTO.File MapToDto(NpgsqlDataReader reader)
        {
            string tempModifiedOn;
            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.File(iid, revisionNumber);

            dto.Category.AddRange(Array.ConvertAll((string[])reader["Category"], Guid.Parse));
            dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse));
            dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse));
            dto.FileRevision.AddRange(Array.ConvertAll((string[])reader["FileRevision"], Guid.Parse));
            dto.LockedBy = reader["LockedBy"] is DBNull ? (Guid?)null : Guid.Parse(reader["LockedBy"].ToString());
            dto.Owner    = Guid.Parse(reader["Owner"].ToString());

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

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

            return(dto);
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="file">
        /// The file 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.File file, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, file, container);

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

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"File\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"LockedBy\", \"Owner\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :container, :lockedBy, :owner)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = file.Iid;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                command.Parameters.Add("lockedBy", NpgsqlDbType.Uuid).Value  = !this.IsDerived(file, "LockedBy") ? Utils.NullableValue(file.LockedBy) : Utils.NullableValue(null);
                command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value     = !this.IsDerived(file, "Owner") ? file.Owner : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"Container\", \"LockedBy\", \"Owner\")");
                sqlBuilder.Append(" = (:container, :lockedBy, :owner);");

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

                this.ExecuteAndLogCommand(command);
            }
            file.Category.ForEach(x => this.UpsertCategory(transaction, partition, file.Iid, x));

            return(true);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void Verify_That_SetIterationId_Works_For_All_PartitionDependentContainmentClassType()
        {
            var dal = new TestDal(this.credentials);
            var uri = new Uri(@"http://localhost.com/EngineeringModel/694508eb-2730-488c-9405-6ca561df68dd/iteration/44647ff6-ffe3-44ff-9ed9-3256e2a97f9d?extent=deep&includeReferenceData=true&includeAllContainers=true");

            var folder       = new Folder();
            var file         = new CDP4Common.DTO.File();
            var fileRevision = new FileRevision();
            var list         = new Thing[] { folder, file, fileRevision };

            Assert.AreEqual(3, PartitionDependentContainmentClassType.EngineeringModelAndIterationClassKindArray.Length, "a ClassKind was added to or removed from PartitionDependentContainmentClassType.EngineeringModelAndIterationClassKindArray. Please make sure that this unit test, so that it tests all individual ClassKinds.");

            if (dal.TryExtractIterationIdfromUri(uri, out var iterationId))
            {
                dal.SetIterationContainer(list, iterationId);
            }

            Assert.AreEqual("44647ff6-ffe3-44ff-9ed9-3256e2a97f9d", folder.IterationContainerId.Value.ToString());
            Assert.AreEqual("44647ff6-ffe3-44ff-9ed9-3256e2a97f9d", file.IterationContainerId.Value.ToString());
            Assert.AreEqual("44647ff6-ffe3-44ff-9ed9-3256e2a97f9d", fileRevision.IterationContainerId.Value.ToString());
        }