示例#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.Category"/>.
        /// </returns>
        public virtual CDP4Common.DTO.Category MapToDto(NpgsqlDataReader reader)
        {
            string tempIsAbstract;
            string tempIsDeprecated;
            string tempModifiedOn;
            string tempName;
            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.Category(iid, revisionNumber);

            dto.Alias.AddRange(Array.ConvertAll((string[])reader["Alias"], Guid.Parse));
            dto.Definition.AddRange(Array.ConvertAll((string[])reader["Definition"], Guid.Parse));
            dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse));
            dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse));
            dto.HyperLink.AddRange(Array.ConvertAll((string[])reader["HyperLink"], Guid.Parse));
            dto.PermissibleClass.AddRange(Utils.ParseEnumArray <CDP4Common.CommonData.ClassKind>((string[])reader["PermissibleClass"]));
            dto.SuperCategory.AddRange(Array.ConvertAll((string[])reader["SuperCategory"], Guid.Parse));

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

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

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

            if (valueDict.TryGetValue("Name", out tempName))
            {
                dto.Name = tempName.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 void SetUp()
        {
            this.uri = new Uri("http://www.rheagroup.com");

            this.testInput = new List <Dto.Thing>();

            //Top container
            this.siteDir = new Dto.SiteDirectory(Guid.NewGuid(), 1);

            this.siteRdl = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 1);
            this.siteDir.SiteReferenceDataLibrary.Add(this.siteRdl.Iid);

            var category1 = new Dto.Category(Guid.NewGuid(), 1);

            category1.PermissibleClass.Add(ClassKind.ParameterType);
            category1.PermissibleClass.Add(ClassKind.Person);
            var category2 = new Dto.Category(Guid.NewGuid(), 1);

            category2.PermissibleClass.Add(ClassKind.TelephoneNumber);
            category2.PermissibleClass.Add(ClassKind.EmailAddress);

            this.siteRdl.DefinedCategory.Add(category1.Iid);
            this.siteRdl.DefinedCategory.Add(category2.Iid);

            //topContainer
            var siteRDL2 = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 1);

            this.siteDir.SiteReferenceDataLibrary.Add(siteRDL2.Iid);

            var booleanParameterType = new Dto.BooleanParameterType(Guid.NewGuid(), 1);

            booleanParameterType.Category = new List <Guid> {
                category1.Iid, category2.Iid
            };
            var definition = new Dto.Definition(Guid.NewGuid(), 1);

            booleanParameterType.Definition.Add(definition.Iid);

            siteRDL2.ParameterType.Add(booleanParameterType.Iid);

            this.testInput.Add(this.siteDir);
            this.testInput.Add(this.siteRdl);
            this.testInput.Add(category1);
            this.testInput.Add(category2);

            this.testInput.Add(definition);
            this.testInput.Add(siteRDL2);
            this.testInput.Add(booleanParameterType);
        }
        public async Task VerifyThatAssemblerCanMoveThings()
        {
            var sitedir  = new Dto.SiteDirectory(Guid.NewGuid(), 0);
            var srdl1    = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 0);
            var srdl2    = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 0);
            var category = new Dto.Category(Guid.NewGuid(), 0);

            sitedir.SiteReferenceDataLibrary.Add(srdl1.Iid);
            sitedir.SiteReferenceDataLibrary.Add(srdl2.Iid);
            srdl1.DefinedCategory.Add(category.Iid);

            var dtos = new List <Dto.Thing>
            {
                sitedir,
                srdl1,
                srdl2,
                category
            };

            var assembler = new Assembler(this.uri);
            await assembler.Synchronize(dtos);

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

            srdl1.DefinedCategory.Clear();
            srdl2.DefinedCategory.Add(category.Iid);

            // move category
            var movedDtos = new List <Dto.Thing>
            {
                sitedir,
                srdl1,
                srdl2
            };

            await assembler.Synchronize(movedDtos);

            var srdl1poco = (SiteReferenceDataLibrary)assembler.Cache[new CacheKey(srdl1.Iid, null)].Value;
            var srdl2poco = (SiteReferenceDataLibrary)assembler.Cache[new CacheKey(srdl2.Iid, null)].Value;
            var catpoco   = assembler.Cache[new CacheKey(category.Iid, null)].Value;

            Assert.AreEqual(4, assembler.Cache.Count);
            Assert.IsEmpty(srdl1poco.DefinedCategory);
            Assert.IsTrue(srdl2poco.DefinedCategory.Contains(catpoco));
        }
示例#4
0
        /// <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="category">
        /// The category 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.Category category, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, category, container, out isHandled, valueTypeDictionaryAdditions);

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

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

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

                category.PermissibleClass.ForEach(x => this.AddPermissibleClass(transaction, partition, category.Iid, x));
                category.SuperCategory.ForEach(x => this.AddSuperCategory(transaction, partition, category.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, category, container));
        }
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="category">
        /// The category 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.Category category, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, category, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "IsAbstract", !this.IsDerived(category, "IsAbstract") ? category.IsAbstract.ToString() : string.Empty },
                    { "IsDeprecated", !this.IsDerived(category, "IsDeprecated") ? category.IsDeprecated.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("UPDATE \"{0}\".\"Category\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = category.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;

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

            return(this.AfterUpdate(beforeUpdate, transaction, partition, category, container));
        }
        public void VerifyThatMultipleIterationCanBeSynchronized()
        {
            var sitedir  = new Dto.SiteDirectory(Guid.NewGuid(), 0);
            var srdl1    = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 0);
            var srdl2    = new Dto.SiteReferenceDataLibrary(Guid.NewGuid(), 0);
            var category = new Dto.Category(Guid.NewGuid(), 0);

            var modeldto      = new Dto.EngineeringModel(Guid.NewGuid(), 1);
            var iteration1dto = new Dto.Iteration(Guid.NewGuid(), 1);
            var iteration2dto = new Dto.Iteration(Guid.NewGuid(), 1);

            var element1dto = new Dto.ElementDefinition(Guid.NewGuid(), 1);

            element1dto.IterationContainerId = iteration1dto.Iid;
            element1dto.Category.Add(category.Iid);

            var element2dto = new Dto.ElementDefinition(element1dto.Iid, 1);

            element2dto.IterationContainerId = iteration2dto.Iid;
            element2dto.Category.Add(category.Iid);

            var usage1dto = new Dto.ElementUsage(Guid.NewGuid(), 1);

            usage1dto.IterationContainerId = iteration1dto.Iid;
            usage1dto.Category.Add(category.Iid);

            var usage2dto = new Dto.ElementUsage(usage1dto.Iid, 1);

            usage2dto.IterationContainerId = iteration2dto.Iid;
            usage2dto.Category.Add(category.Iid);

            sitedir.SiteReferenceDataLibrary.Add(srdl1.Iid);
            sitedir.SiteReferenceDataLibrary.Add(srdl2.Iid);
            srdl1.DefinedCategory.Add(category.Iid);

            modeldto.Iteration.Add(iteration1dto.Iid);
            modeldto.Iteration.Add(iteration2dto.Iid);

            iteration1dto.Element.Add(element1dto.Iid);
            iteration2dto.Element.Add(element2dto.Iid);

            element1dto.ContainedElement.Add(usage1dto.Iid);
            element2dto.ContainedElement.Add(usage2dto.Iid);

            var dtos = new List <Dto.Thing>
            {
                sitedir,
                srdl1,
                srdl2,
                category
            };

            var assembler = new Assembler(this.uri);

            assembler.Synchronize(dtos);

            dtos = new List <Dto.Thing>
            {
                modeldto,
                iteration1dto,
                iteration2dto,
                element1dto,
                element2dto,
                usage1dto,
                usage2dto
            };

            assembler.Synchronize(dtos);

            Assert.AreEqual(11, assembler.Cache.Count);
        }