예제 #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.IterationSetup"/>.
        /// </returns>
        public virtual CDP4Common.DTO.IterationSetup MapToDto(NpgsqlDataReader reader)
        {
            string tempModifiedOn;
            string tempIterationIid;
            string tempIterationNumber;
            string tempDescription;
            string tempFrozenOn;
            string tempIsDeleted;
            string tempCreatedOn;

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

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

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

            if (valueDict.TryGetValue("IterationIid", out tempIterationIid))
            {
                dto.IterationIid = Guid.Parse(tempIterationIid);
            }

            if (valueDict.TryGetValue("IterationNumber", out tempIterationNumber))
            {
                dto.IterationNumber = int.Parse(tempIterationNumber);
            }

            if (valueDict.TryGetValue("Description", out tempDescription))
            {
                dto.Description = tempDescription.UnEscape();
            }

            if (valueDict.TryGetValue("FrozenOn", out tempFrozenOn) && tempFrozenOn != null)
            {
                dto.FrozenOn = Utils.ParseUtcDate(tempFrozenOn);
            }

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

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

            return(dto);
        }
        public async Task VerifyThatCloseModelWorks()
        {
            var siteDir     = new CDP4Common.SiteDirectoryData.SiteDirectory(Guid.NewGuid(), null, null);
            var modelRdlDto = new CDP4Common.DTO.ModelReferenceDataLibrary {
                Iid = Guid.NewGuid()
            };
            var siteDirDto = new CDP4Common.DTO.SiteDirectory {
                Iid = Guid.NewGuid()
            };
            var requiredPocoDto = new CDP4Common.DTO.SiteReferenceDataLibrary {
                Iid = Guid.NewGuid()
            };
            var containerEngModelSetupDto = new EngineeringModelSetup {
                Iid = Guid.NewGuid()
            };
            var containerEngModelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup {
                Iid = containerEngModelSetupDto.Iid
            };
            var iterationDto = new Iteration {
                Iid = Guid.NewGuid()
            };
            var iteration = new CDP4Common.EngineeringModelData.Iteration {
                Iid = iterationDto.Iid
            };
            var iterationSetupDto = new CDP4Common.DTO.IterationSetup {
                Iid = Guid.NewGuid(), IterationIid = iterationDto.Iid
            };

            iterationDto.IterationSetup = iterationSetupDto.IterationIid;
            siteDir.Model.Add(containerEngModelSetup);
            modelRdlDto.RequiredRdl = requiredPocoDto.Iid;

            var credentials = new Credentials("admin", "pass", new Uri("http://www.rheagroup.com"));
            var session2    = new Session(this.mockedDal.Object, credentials);

            var iterationSetup = new CDP4Common.SiteDirectoryData.IterationSetup {
                Iid = iterationSetupDto.Iid, Container = containerEngModelSetup, IterationIid = iteration.Iid
            };
            var thingsToAdd = new List <Thing> {
                siteDirDto, requiredPocoDto, containerEngModelSetupDto, modelRdlDto, iterationSetupDto
            };

            await session2.Assembler.Synchronize(thingsToAdd);

            var lazyiteration = new Lazy <CDP4Common.CommonData.Thing>(() => iteration);

            session2.Assembler.Cache.GetOrAdd(new CacheKey(iterationDto.Iid, null), lazyiteration);

            CDP4Common.CommonData.Thing changedObject = null;
            CDPMessageBus.Current.Listen <ObjectChangedEvent>(typeof(CDP4Common.EngineeringModelData.Iteration)).Subscribe(x => changedObject = x.ChangedThing);
            await session2.CloseIterationSetup(iterationSetup);

            Assert.NotNull(changedObject);
        }
예제 #3
0
        /// <summary>
        /// Create the first <see cref="IterationSetup"/> that is to be contained by the new <see cref="EngineeringModelSetup"/>
        /// </summary>
        /// <param name="engineeringModelSetup">
        /// The engineering Model Setup.
        /// </param>
        /// <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>
        /// <returns>
        /// The <see cref="IterationSetup"/>.
        /// </returns>
        private IterationSetup CreateIterationSetup(EngineeringModelSetup engineeringModelSetup, NpgsqlTransaction transaction, string partition)
        {
            var iterationNumber = this.QeuryIterationNumberForFirstIteration(engineeringModelSetup, transaction);

            // create iteration setup in sitedirectory (= partition)
            var iterationSetup = new IterationSetup(Guid.NewGuid(), 1)
            {
                IterationNumber = iterationNumber,
                IterationIid    = Guid.NewGuid(),
                Description     = "Iteration 1"
            };

            if (!this.IterationSetupService.CreateConcept(transaction, partition, iterationSetup, engineeringModelSetup))
            {
                throw new InvalidOperationException($"There was a problem creating the new IterationSetup: {iterationSetup.Iid} contained by EngineeringModelSetup: {engineeringModelSetup.Iid}");
            }

            return(iterationSetup);
        }
예제 #4
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="iterationSetup">
        /// The IterationSetup 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.IterationSetup iterationSetup, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, iterationSetup, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "CreatedOn", !this.IsDerived(iterationSetup, "CreatedOn") ? iterationSetup.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                    { "Description", !this.IsDerived(iterationSetup, "Description") ? iterationSetup.Description.Escape() : string.Empty },
                    { "FrozenOn", !this.IsDerived(iterationSetup, "FrozenOn") && iterationSetup.FrozenOn.HasValue ? iterationSetup.FrozenOn.Value.ToString(Utils.DateTimeUtcSerializationFormat) : null },
                    { "IsDeleted", !this.IsDerived(iterationSetup, "IsDeleted") ? iterationSetup.IsDeleted.ToString() : string.Empty },
                    { "IterationIid", !this.IsDerived(iterationSetup, "IterationIid") ? iterationSetup.IterationIid.ToString() : string.Empty },
                    { "IterationNumber", iterationSetup.IterationNumber.ToString() },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"IterationSetup\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"SourceIterationSetup\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :sourceIterationSetup)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

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

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, iterationSetup, container));
        }
        public void VerifyThatNullDatetimeSerializationWorks()
        {
            this.serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 0, 0));
            var iterationsetup = new Dto.IterationSetup(Guid.NewGuid(), 1);

            iterationsetup.FrozenOn = null;

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

                using (var reader = new StreamReader(memoryStream))
                {
                    var txt = reader.ReadToEnd();
                    Assert.IsTrue(txt.Contains("\"frozenOn\":null"));
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Build operation containes structure that will be serialized
        /// </summary>
        /// <returns>
        /// List of <see cref="OperationContainer"/>
        /// </returns>
        private IEnumerable <OperationContainer> BuildOperationContainers()
        {
            var cache = new ConcurrentDictionary <CacheKey, Lazy <Thing> >();

            // DomainOfExpertise
            var domain = new DomainOfExpertise(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                ShortName = "SYS"
            };

            this.siteDirectoryData.Domain.Add(domain);

            // PersonRole
            var role = new PersonRole(Guid.NewGuid(), null, null);

            this.siteDirectoryData.PersonRole.Add(role);
            this.siteDirectoryData.DefaultPersonRole = role;

            // ParticipantRole
            var participantRole = new ParticipantRole(Guid.Empty, null, null);

            this.siteDirectoryData.ParticipantRole.Add(participantRole);
            this.siteDirectoryData.DefaultParticipantRole = participantRole;

            // Organization
            var organization = new Organization(Guid.NewGuid(), null, null)
            {
                Container = this.siteDirectoryData
            };

            // Iteration
            var iterationIid       = new Guid("b58ea73d-350d-4520-b9d9-a52c75ac2b5d");
            var iterationSetup     = new IterationSetup(Guid.NewGuid(), 0);
            var iterationSetupPoco = new CDP4Common.SiteDirectoryData.IterationSetup(iterationSetup.Iid, cache, this.credentials.Uri);

            // EngineeringModel
            var model      = new EngineeringModel(Guid.NewGuid(), cache, this.credentials.Uri);
            var modelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup();

            modelSetup.ActiveDomain.Add(domain);

            var requiredRdl = new ModelReferenceDataLibrary();

            var person = new Person {
                ShortName = "admin", Organization = organization
            };
            var participant = new Participant(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                Person = person
            };

            participant.Person.Role = role;
            participant.Role        = participantRole;
            participant.Domain.Add(domain);
            modelSetup.Participant.Add(participant);

            var lazyPerson    = new Lazy <Thing>(() => person);
            var iterationPoco = new CDP4Common.EngineeringModelData.Iteration(iterationIid, cache, this.credentials.Uri)
            {
                IterationSetup = iterationSetupPoco
            };

            model.Iteration.Add(iterationPoco);
            var iteration = (Iteration)iterationPoco.ToDto();

            model.EngineeringModelSetup = modelSetup;
            this.siteDirectoryData.Model.Add(modelSetup);
            modelSetup.RequiredRdl.Add(requiredRdl);
            modelSetup.IterationSetup.Add(iterationSetupPoco);
            cache.TryAdd(new CacheKey(person.Iid, this.siteDirectoryData.Iid), lazyPerson);
            this.siteDirectoryData.Cache = cache;
            iteration.IterationSetup     = iterationSetup.Iid;
            var iterationClone = iteration.DeepClone <Iteration>();

            var operation           = new Operation(iteration, iterationClone, OperationKind.Update);
            var operationContainers = new[] { new OperationContainer("/EngineeringModel/" + model.Iid + "/iteration/" + iteration.Iid, 0) };

            operationContainers.Single().AddOperation(operation);

            return(operationContainers);
        }
예제 #7
0
        public async Task VerifyWriteEnumerableOperationContainer()
        {
            var cache = new ConcurrentDictionary <CacheKey, Lazy <Thing> >();

            var files = new[] { "file1" };

            Assert.Throws <ArgumentNullException>(() => this.dal.Write((IEnumerable <OperationContainer>)null, files));
            Assert.Throws <ArgumentException>(() => this.dal.Write(new List <OperationContainer>(), files));

            var operationContainers = new[] { new OperationContainer("/SiteDirectory/00000000-0000-0000-0000-000000000000", 0) };

            Assert.Throws <ArgumentException>(() => this.dal.Write(operationContainers, files));

            var domainSys = new DomainOfExpertise(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                ShortName = "SYS"
            };

            this.siteDirectoryData.Domain.Add(domainSys);

            var domainAer = new DomainOfExpertise(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                ShortName = "AER"
            };

            this.siteDirectoryData.Domain.Add(domainAer);

            var domainProp = new DomainOfExpertise(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                ShortName = "PROP"
            };

            this.siteDirectoryData.Domain.Add(domainProp);

            // PersonRole
            var role = new PersonRole(Guid.NewGuid(), null, null);

            this.siteDirectoryData.PersonRole.Add(role);
            this.siteDirectoryData.DefaultPersonRole = role;

            // ParticipantRole
            var participantRole = new ParticipantRole(Guid.Empty, null, null);

            this.siteDirectoryData.ParticipantRole.Add(participantRole);
            this.siteDirectoryData.DefaultParticipantRole = participantRole;

            // Organization
            var organization = new Organization(Guid.NewGuid(), null, null)
            {
                Container = this.siteDirectoryData
            };

            var sitedirectoryDto = (CDP4Common.DTO.SiteDirectory) this.siteDirectoryData.ToDto();
            var clone            = sitedirectoryDto.DeepClone <CDP4Common.DTO.SiteDirectory>();
            var operation        = new Operation(sitedirectoryDto, clone, OperationKind.Update);

            Assert.AreEqual(0, operationContainers.Single().Operations.Count());
            operationContainers.Single().AddOperation(operation);

            Assert.AreEqual(1, operationContainers.Single().Operations.Count());
            Assert.Throws <ArgumentException>(() => this.dal.Write(operationContainers, files));

            operationContainers.Single().RemoveOperation(operation);
            Assert.AreEqual(0, operationContainers.Single().Operations.Count());

            var iterationIid       = new Guid("b58ea73d-350d-4520-b9d9-a52c75ac2b5d");
            var iterationSetup     = new IterationSetup(Guid.NewGuid(), 0);
            var iterationSetupPoco = new CDP4Common.SiteDirectoryData.IterationSetup(iterationSetup.Iid, cache, this.credentials.Uri);
            var model      = new EngineeringModel(Guid.NewGuid(), cache, this.credentials.Uri);
            var modelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup();

            modelSetup.ActiveDomain.Add(domainSys);

            var source = new ReferenceSource(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                Publisher = new Organization(Guid.NewGuid(), cache, this.credentials.Uri)
                {
                    Container = this.siteDirectoryData
                }
            };

            var requiredRdl = new ModelReferenceDataLibrary
            {
                RequiredRdl     = new SiteReferenceDataLibrary(),
                ReferenceSource = { source }
            };

            var person = new Person
            {
                ShortName     = "admin",
                Organization  = organization,
                DefaultDomain = domainAer
            };

            var participant = new Participant(Guid.NewGuid(), cache, this.credentials.Uri)
            {
                Person = person
            };

            participant.Person.Role = role;
            participant.Role        = participantRole;
            participant.Domain.Add(domainSys);
            participant.Domain.Add(domainProp);
            modelSetup.Participant.Add(participant);

            var lazyPerson    = new Lazy <Thing>(() => person);
            var iterationPoco = new CDP4Common.EngineeringModelData.Iteration(iterationIid, cache, this.credentials.Uri)
            {
                IterationSetup = iterationSetupPoco
            };

            model.Iteration.Add(iterationPoco);
            var iteration = (Iteration)iterationPoco.ToDto();

            model.EngineeringModelSetup = modelSetup;
            this.siteDirectoryData.Model.Add(modelSetup);
            modelSetup.RequiredRdl.Add(requiredRdl);

            modelSetup.IterationSetup.Add(iterationSetupPoco);
            cache.TryAdd(new CacheKey(person.Iid, this.siteDirectoryData.Iid), lazyPerson);
            this.siteDirectoryData.Cache = cache;
            iteration.IterationSetup     = iterationSetup.Iid;

            var clone1 = iteration.DeepClone <Iteration>();

            operation = new Operation(iteration, clone1, OperationKind.Update);
            var operationContainer = new OperationContainer("/EngineeringModel/" + model.Iid + "/iteration/" + iteration.Iid, 0);

            operationContainer.AddOperation(operation);
            operationContainers = new[] { operationContainer, operationContainer };

            Assert.IsEmpty(await this.dal.Write(operationContainers, files));
        }