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

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "EngineeringModelIid", !this.IsDerived(engineeringModelSetup, "EngineeringModelIid") ? engineeringModelSetup.EngineeringModelIid.ToString() : string.Empty },
                    { "Kind", !this.IsDerived(engineeringModelSetup, "Kind") ? engineeringModelSetup.Kind.ToString() : string.Empty },
                    { "SourceEngineeringModelSetupIid", !this.IsDerived(engineeringModelSetup, "SourceEngineeringModelSetupIid") && engineeringModelSetup.SourceEngineeringModelSetupIid.HasValue ? engineeringModelSetup.SourceEngineeringModelSetupIid.Value.ToString() : null },
                    { "StudyPhase", !this.IsDerived(engineeringModelSetup, "StudyPhase") ? engineeringModelSetup.StudyPhase.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}\".\"EngineeringModelSetup\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"DefaultOrganizationalParticipant\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :defaultOrganizationalParticipant)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

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

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, engineeringModelSetup, container));
        }
Example #2
0
        /// <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="fileRevision">
        /// The fileRevision 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.FileRevision fileRevision, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, fileRevision, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ContentHash", fileRevision.ContentHash.Escape() },
                    { "CreatedOn", !this.IsDerived(fileRevision, "CreatedOn") ? fileRevision.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                    { "Name", !this.IsDerived(fileRevision, "Name") ? fileRevision.Name.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}\".\"FileRevision\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"Creator\", \"ContainingFolder\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, :creator, :containingFolder, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = fileRevision.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("creator", NpgsqlDbType.Uuid).Value               = !this.IsDerived(fileRevision, "Creator") ? fileRevision.Creator : Utils.NullableValue(null);
                    command.Parameters.Add("containingFolder", NpgsqlDbType.Uuid).Value      = !this.IsDerived(fileRevision, "ContainingFolder") ? Utils.NullableValue(fileRevision.ContainingFolder) : Utils.NullableValue(null);
                    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, fileRevision, container));
        }
        /// <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="hyperLink">
        /// The HyperLink 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.HyperLink hyperLink, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, hyperLink, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Content", !this.IsDerived(hyperLink, "Content") ? hyperLink.Content.Escape() : string.Empty },
                    { "LanguageCode", !this.IsDerived(hyperLink, "LanguageCode") ? hyperLink.LanguageCode.Escape() : string.Empty },
                    { "Uri", !this.IsDerived(hyperLink, "Uri") ? hyperLink.Uri.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}\".\"HyperLink\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = hyperLink.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.AfterUpdate(beforeUpdate, transaction, partition, hyperLink, container));
        }
Example #4
0
        /// <summary>
        /// The create file structure on disk.
        /// </summary>
        /// <param name="thing">
        /// The thing to get file data for.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource is stored.
        /// </param>
        /// <param name="folderPath">
        /// The folder path where the structure will be created.
        /// </param>
        /// <param name="routeSegments">
        /// The route segments.
        /// </param>
        private void CreateFileStructureOnDisk(Thing thing, string partition, string folderPath, dynamic routeSegments)
        {
            var logMessage = string.Format(
                "File structure creation is started into the temporary folder {0}.",
                folderPath);

            Logger.Info(logMessage);

            var credentials       = this.Cdp4Context.Context.CurrentUser as Credentials;
            var authorizedContext = new RequestSecurityContext {
                ContainerReadAllowed = true
            };

            NpgsqlConnection  connection  = null;
            NpgsqlTransaction transaction = this.TransactionManager.SetupTransaction(ref connection, credentials);

            try
            {
                List <Thing> things = this.GetFileStoreThings(
                    thing,
                    routeSegments,
                    partition,
                    transaction,
                    authorizedContext);

                if (things.Any())
                {
                    // Get all folders
                    var folders = things.Where(i => i.GetType() == typeof(Folder)).Cast <Folder>().ToList();

                    // Get all files
                    var files = things.Where(i => i.GetType() == typeof(File))
                                .Cast <File>().ToList();

                    // Get all files
                    var fileRevisions = things.Where(i => i.GetType() == typeof(FileRevision)).Cast <FileRevision>()
                                        .ToList();

                    // Get all fileType iids
                    var fileTypeOrderedItems = new List <OrderedItem>();
                    foreach (var fileRevision in fileRevisions)
                    {
                        fileTypeOrderedItems.AddRange(fileRevision.FileType);
                    }

                    // Get fileTypes iids
                    var fileTypeIids = new List <Guid>();
                    foreach (var fileTypeOrderedItem in fileTypeOrderedItems)
                    {
                        fileTypeIids.Add(Guid.Parse(fileTypeOrderedItem.V.ToString()));
                    }

                    // Get all fileTypes
                    var fileTypes = this.FileTypeService
                                    .GetShallow(transaction, "SiteDirectory", fileTypeIids.Distinct(), authorizedContext)
                                    .OfType <FileType>().ToList();

                    if (thing.ClassKind != ClassKind.Folder)
                    {
                        // Get all root folders
                        var rootFolders = folders.Where(folder => folder.ContainingFolder == null);

                        // Iterate all root folders and find child folders and files for the file store
                        foreach (var rootFolder in rootFolders)
                        {
                            this.GetFoldersAndFiles(rootFolder, folders, files, fileRevisions, folderPath, fileTypes);
                        }

                        // Get root files
                        this.GetFiles(null, files, fileRevisions, folderPath, fileTypes);
                    }
                    else
                    {
                        // Find child folders and files for the folder
                        this.GetFoldersAndFiles(thing as Folder, folders, files, fileRevisions, folderPath, fileTypes);
                    }
                }

                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                Logger.Error(ex, "Failed to create a file structure on the disk.");
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }

                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }
        /// <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="booleanParameterType">
        /// The booleanParameterType 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.BooleanParameterType booleanParameterType, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

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

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"BooleanParameterType\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\")");
                sqlBuilder.AppendFormat(" VALUES (:iid)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = booleanParameterType.Iid;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO NOTHING; ");

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

                this.ExecuteAndLogCommand(command);
            }

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

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "CloseOutDate", !this.IsDerived(actionItem, "CloseOutDate") && actionItem.CloseOutDate.HasValue ? actionItem.CloseOutDate.Value.ToString(Utils.DateTimeUtcSerializationFormat) : null },
                    { "CloseOutStatement", !this.IsDerived(actionItem, "CloseOutStatement") ? actionItem.CloseOutStatement.Escape() : null },
                    { "DueDate", !this.IsDerived(actionItem, "DueDate") ? actionItem.DueDate.ToString(Utils.DateTimeUtcSerializationFormat) : 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}\".\"ActionItem\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Actionee\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :actionee)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = actionItem.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("actionee", NpgsqlDbType.Uuid).Value = !this.IsDerived(actionItem, "Actionee") ? actionItem.Actionee : Utils.NullableValue(null);

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, actionItem, container));
        }
        /// <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="contractChangeNotice">
        /// The contractChangeNotice 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.ContractChangeNotice contractChangeNotice, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, contractChangeNotice, container, out isHandled, valueTypeDictionaryAdditions);

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

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

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"ContractChangeNotice\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ChangeProposal\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :changeProposal);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value            = contractChangeNotice.Iid;
                    command.Parameters.Add("changeProposal", NpgsqlDbType.Uuid).Value = !this.IsDerived(contractChangeNotice, "ChangeProposal") ? contractChangeNotice.ChangeProposal : Utils.NullableValue(null);

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

            return(this.AfterWrite(beforeWrite, transaction, partition, contractChangeNotice, container));
        }
Example #8
0
        /// <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="quantityKindFactor">
        /// The quantityKindFactor DTO that is to be persisted.
        /// </param>
        /// <param name="sequence">
        /// The order sequence used to persist this instance.
        /// </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.QuantityKindFactor quantityKindFactor, long sequence, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Exponent", !this.IsDerived(quantityKindFactor, "Exponent") ? quantityKindFactor.Exponent.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("INSERT INTO \"{0}\".\"QuantityKindFactor\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Sequence\", \"Container\", \"QuantityKind\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :sequence, :container, :quantityKind)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = quantityKindFactor.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("sequence", NpgsqlDbType.Bigint).Value            = sequence;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("quantityKind", NpgsqlDbType.Uuid).Value          = !this.IsDerived(quantityKindFactor, "QuantityKind") ? quantityKindFactor.QuantityKind : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"QuantityKind\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :quantityKind);");

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

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
Example #9
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="quantityKindFactor">
        /// The QuantityKindFactor 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.QuantityKindFactor quantityKindFactor, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, quantityKindFactor, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Exponent", !this.IsDerived(quantityKindFactor, "Exponent") ? quantityKindFactor.Exponent.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}\".\"QuantityKindFactor\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"QuantityKind\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :quantityKind)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

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

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

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, quantityKindFactor, container));
        }
Example #10
0
        /// <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="stakeholder">
        /// The stakeholder 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.Stakeholder stakeholder, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

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

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"Stakeholder\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Container\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :container)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = stakeholder.Iid;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET \"Container\"");
                sqlBuilder.Append(" = :container;");

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

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

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

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Value", !this.IsDerived(simpleParameterValue, "Value") ? simpleParameterValue.Value.ToHstoreString() : 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}\".\"SimpleParameterValue\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"ParameterType\", \"Scale\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, :parameterType, :scale, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = simpleParameterValue.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("parameterType", NpgsqlDbType.Uuid).Value         = !this.IsDerived(simpleParameterValue, "ParameterType") ? simpleParameterValue.ParameterType : Utils.NullableValue(null);
                    command.Parameters.Add("scale", NpgsqlDbType.Uuid).Value                 = !this.IsDerived(simpleParameterValue, "Scale") ? Utils.NullableValue(simpleParameterValue.Scale) : Utils.NullableValue(null);
                    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, simpleParameterValue, container));
        }
        /// <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="topContainer">
        /// The TopContainer 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.TopContainer topContainer, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, topContainer, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "LastModifiedOn", !this.IsDerived(topContainer, "LastModifiedOn") ? topContainer.LastModifiedOn.ToString(Utils.DateTimeUtcSerializationFormat) : 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}\".\"TopContainer\"", partition);
                    sqlBuilder.AppendFormat(" SET \"ValueTypeDictionary\"");
                    sqlBuilder.AppendFormat(" = :valueTypeDictionary");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = topContainer.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, topContainer, container));
        }
        /// <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="topContainer">
        /// The topContainer 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.TopContainer topContainer, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "LastModifiedOn", !this.IsDerived(topContainer, "LastModifiedOn") ? topContainer.LastModifiedOn.ToString(Utils.DateTimeUtcSerializationFormat) : 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}\".\"TopContainer\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = topContainer.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET \"ValueTypeDictionary\"");
                sqlBuilder.Append(" = :valueTypeDictionary;");

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

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
        /// <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="parameterBase">
        /// The parameterBase 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.ParameterBase parameterBase, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, parameterBase, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "IsOptionDependent", !this.IsDerived(parameterBase, "IsOptionDependent") ? parameterBase.IsOptionDependent.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}\".\"ParameterBase\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"ParameterType\", \"Scale\", \"StateDependence\", \"Group\", \"Owner\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :parameterType, :scale, :stateDependence, :group, :owner);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = parameterBase.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("parameterType", NpgsqlDbType.Uuid).Value         = !this.IsDerived(parameterBase, "ParameterType") ? parameterBase.ParameterType : Utils.NullableValue(null);
                    command.Parameters.Add("scale", NpgsqlDbType.Uuid).Value           = !this.IsDerived(parameterBase, "Scale") ? Utils.NullableValue(parameterBase.Scale) : Utils.NullableValue(null);
                    command.Parameters.Add("stateDependence", NpgsqlDbType.Uuid).Value = !this.IsDerived(parameterBase, "StateDependence") ? Utils.NullableValue(parameterBase.StateDependence) : Utils.NullableValue(null);
                    command.Parameters.Add("group", NpgsqlDbType.Uuid).Value           = !this.IsDerived(parameterBase, "Group") ? Utils.NullableValue(parameterBase.Group) : Utils.NullableValue(null);
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value           = !this.IsDerived(parameterBase, "Owner") ? parameterBase.Owner : Utils.NullableValue(null);

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

            return(this.AfterWrite(beforeWrite, transaction, partition, parameterBase, container));
        }
        /// <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="glossary">
        /// The glossary 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.Glossary glossary, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

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

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = glossary.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container);");

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

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

            return(true);
        }
Example #16
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="personPermission">
        /// The personPermission 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.PersonPermission personPermission, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, personPermission, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "AccessRight", !this.IsDerived(personPermission, "AccessRight") ? personPermission.AccessRight.ToString() : string.Empty },
                    { "ObjectClass", !this.IsDerived(personPermission, "ObjectClass") ? personPermission.ObjectClass.ToString() : string.Empty },
                    { "IsDeprecated", !this.IsDerived(personPermission, "IsDeprecated") ? personPermission.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}\".\"PersonPermission\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = personPermission.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, personPermission, container));
        }
        /// <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="diagramThingBase">
        /// The diagramThingBase 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.DiagramThingBase diagramThingBase, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, diagramThingBase, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Name", !this.IsDerived(diagramThingBase, "Name") ? diagramThingBase.Name.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("INSERT INTO \"{0}\".\"DiagramThingBase\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = diagramThingBase.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.AfterWrite(beforeWrite, transaction, partition, diagramThingBase, container));
        }
Example #19
0
        /// <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="externalIdentifierMap">
        /// The externalIdentifierMap 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.ExternalIdentifierMap externalIdentifierMap, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, externalIdentifierMap, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ExternalModelName", !this.IsDerived(externalIdentifierMap, "ExternalModelName") ? externalIdentifierMap.ExternalModelName.Escape() : string.Empty },
                    { "ExternalToolName", !this.IsDerived(externalIdentifierMap, "ExternalToolName") ? externalIdentifierMap.ExternalToolName.Escape() : string.Empty },
                    { "ExternalToolVersion", !this.IsDerived(externalIdentifierMap, "ExternalToolVersion") ? externalIdentifierMap.ExternalToolVersion.Escape() : null },
                    { "Name", !this.IsDerived(externalIdentifierMap, "Name") ? externalIdentifierMap.Name.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}\".\"ExternalIdentifierMap\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"ExternalFormat\", \"Owner\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, :externalFormat, :owner, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = externalIdentifierMap.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("externalFormat", NpgsqlDbType.Uuid).Value        = !this.IsDerived(externalIdentifierMap, "ExternalFormat") ? Utils.NullableValue(externalIdentifierMap.ExternalFormat) : Utils.NullableValue(null);
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value                 = !this.IsDerived(externalIdentifierMap, "Owner") ? externalIdentifierMap.Owner : Utils.NullableValue(null);
                    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, externalIdentifierMap, container));
        }
        /// <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="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 Write(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.File file, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, file, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(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);

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

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

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

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

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

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"SimpleParameterizableThing\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Owner\")");
                    sqlBuilder.AppendFormat(" = (:owner)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value   = simpleParameterizableThing.Iid;
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value = !this.IsDerived(simpleParameterizableThing, "Owner") ? simpleParameterizableThing.Owner : Utils.NullableValue(null);

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

            return(this.AfterUpdate(beforeUpdate, transaction, partition, simpleParameterizableThing, container));
        }
        /// <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="ordinalScale">
        /// The ordinalScale 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.OrdinalScale ordinalScale, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "UseShortNameValues", !this.IsDerived(ordinalScale, "UseShortNameValues") ? ordinalScale.UseShortNameValues.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}\".\"OrdinalScale\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = ordinalScale.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET \"ValueTypeDictionary\"");
                sqlBuilder.Append(" = :valueTypeDictionary;");

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

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
        /// <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="booleanParameterType">
        /// The booleanParameterType 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.BooleanParameterType booleanParameterType, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, booleanParameterType, container, out isHandled, valueTypeDictionaryAdditions);

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

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

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"BooleanParameterType\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = booleanParameterType.Iid;

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

                    this.ExecuteAndLogCommand(command);
                }
            }

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

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "UseShortNameValues", !this.IsDerived(ordinalScale, "UseShortNameValues") ? ordinalScale.UseShortNameValues.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}\".\"OrdinalScale\"", partition);
                    sqlBuilder.AppendFormat(" SET \"ValueTypeDictionary\"");
                    sqlBuilder.AppendFormat(" = :valueTypeDictionary");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = ordinalScale.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, ordinalScale, container));
        }
        /// <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="booleanParameterType">
        /// The BooleanParameterType 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.BooleanParameterType booleanParameterType, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, booleanParameterType, container, out isHandled, valueTypeDictionaryAdditions);

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

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

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

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

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"EngineeringModelDataNote\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\")");
                    sqlBuilder.AppendFormat(" = (:container)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = engineeringModelDataNote.Iid;
                    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.AfterUpdate(beforeUpdate, transaction, partition, engineeringModelDataNote, container));
        }
        /// <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="hyperLink">
        /// The hyperLink 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.HyperLink hyperLink, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Content", !this.IsDerived(hyperLink, "Content") ? hyperLink.Content.Escape() : string.Empty },
                { "LanguageCode", !this.IsDerived(hyperLink, "LanguageCode") ? hyperLink.LanguageCode.Escape() : string.Empty },
                { "Uri", !this.IsDerived(hyperLink, "Uri") ? hyperLink.Uri.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("INSERT INTO \"{0}\".\"HyperLink\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = hyperLink.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container);");

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

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
Example #28
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="genericAnnotation">
        /// The genericAnnotation 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.GenericAnnotation genericAnnotation, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, genericAnnotation, container, out isHandled, valueTypeDictionaryAdditions);

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Content", !this.IsDerived(genericAnnotation, "Content") ? genericAnnotation.Content.Escape() : string.Empty },
                    { "CreatedOn", !this.IsDerived(genericAnnotation, "CreatedOn") ? genericAnnotation.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                    { "LanguageCode", !this.IsDerived(genericAnnotation, "LanguageCode") ? genericAnnotation.LanguageCode.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("INSERT INTO \"{0}\".\"GenericAnnotation\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = genericAnnotation.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.AfterWrite(beforeWrite, transaction, partition, genericAnnotation, container));
        }
 /// <summary>
 /// The Nothing class does not support the ResolveProperties method
 /// </summary>
 /// <exception cref="NotSupportedException">The methos is not supported by this class</exception>
 internal override void ResolveProperties(DTO.Thing dto)
 {
     throw new NotSupportedException();
 }
        /// <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="engineeringModelSetup">
        /// The engineeringModelSetup 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.EngineeringModelSetup engineeringModelSetup, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "EngineeringModelIid", !this.IsDerived(engineeringModelSetup, "EngineeringModelIid") ? engineeringModelSetup.EngineeringModelIid.ToString() : string.Empty },
                { "Kind", !this.IsDerived(engineeringModelSetup, "Kind") ? engineeringModelSetup.Kind.ToString() : string.Empty },
                { "SourceEngineeringModelSetupIid", !this.IsDerived(engineeringModelSetup, "SourceEngineeringModelSetupIid") && engineeringModelSetup.SourceEngineeringModelSetupIid.HasValue ? engineeringModelSetup.SourceEngineeringModelSetupIid.Value.ToString() : null },
                { "StudyPhase", !this.IsDerived(engineeringModelSetup, "StudyPhase") ? engineeringModelSetup.StudyPhase.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}\".\"EngineeringModelSetup\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"DefaultOrganizationalParticipant\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :defaultOrganizationalParticipant)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = engineeringModelSetup.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value            = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value                        = container.Iid;
                command.Parameters.Add("defaultOrganizationalParticipant", NpgsqlDbType.Uuid).Value = !this.IsDerived(engineeringModelSetup, "DefaultOrganizationalParticipant") ? Utils.NullableValue(engineeringModelSetup.DefaultOrganizationalParticipant) : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"DefaultOrganizationalParticipant\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :defaultOrganizationalParticipant);");

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

                this.ExecuteAndLogCommand(command);
            }
            engineeringModelSetup.ActiveDomain.ForEach(x => this.UpsertActiveDomain(transaction, partition, engineeringModelSetup.Iid, x));

            return(true);
        }