Esempio n. 1
0
        /// <summary>
        /// Creates ParameterSubscriptionValueSets for the supplied ParameterValueSets.
        /// </summary>
        /// <param name="thing">
        ///     The <see cref="Thing"/> instance that will be inspected.
        /// </param>
        /// <param name="container">
        ///     The container instance of the <see cref="Thing"/> that is inspected.
        /// </param>
        /// <param name="transaction">
        ///     The current transaction to the database.
        /// </param>
        /// <param name="partition">
        ///     The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="securityContext">
        ///     The security-context
        /// </param>
        private void CreateParameterSubscriptionValueSets(
            ParameterSubscription thing,
            Thing container,
            NpgsqlTransaction transaction,
            string partition,
            ISecurityContext securityContext)
        {
            if (!(container is CDP4Common.DTO.ParameterOrOverrideBase parameterOrOverrideBase))
            {
                throw new InvalidOperationException("The container of a ParameterSubscription can only be a ParameterOrOverrideBase.");
            }

            var parameterValueSets = parameterOrOverrideBase is CDP4Common.DTO.Parameter
                ? this.ParameterValueSetService.GetShallow(transaction, partition, parameterOrOverrideBase.ValueSets, securityContext).OfType <CDP4Common.DTO.ParameterValueSetBase>().ToArray()
                : this.ParameterOverrideValueSetService.GetShallow(transaction, partition, parameterOrOverrideBase.ValueSets, securityContext).OfType <CDP4Common.DTO.ParameterValueSetBase>().ToArray();

            foreach (var parameterValueSet in parameterValueSets)
            {
                var parameterSubscriptionValueSet =
                    new ParameterSubscriptionValueSet(Guid.NewGuid(), 0)
                {
                    SubscribedValueSet = parameterValueSet.Iid,
                    ValueSwitch        = ParameterSwitchKind.COMPUTED
                };

                var valueArray = new ValueArray <string>(this.DefaultValueArrayFactory.CreateDefaultValueArray(parameterValueSet.Manual.Count));
                parameterSubscriptionValueSet.Manual = valueArray;

                this.ParameterSubscriptionValueSetService.CreateConcept(
                    transaction,
                    partition,
                    parameterSubscriptionValueSet,
                    thing);
            }
        }
Esempio n. 2
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.ParameterSubscriptionValueSet"/>.
        /// </returns>
        public virtual CDP4Common.DTO.ParameterSubscriptionValueSet MapToDto(NpgsqlDataReader reader)
        {
            string tempModifiedOn;
            string tempManual;
            string tempValueSwitch;

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

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

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

            if (valueDict.TryGetValue("Manual", out tempManual))
            {
                dto.Manual = tempManual.FromHstoreToValueArray <string>();
            }

            if (valueDict.TryGetValue("ValueSwitch", out tempValueSwitch))
            {
                dto.ValueSwitch = Utils.ParseEnum <CDP4Common.EngineeringModelData.ParameterSwitchKind>(tempValueSwitch);
            }

            return(dto);
        }
        /// <summary>
        /// Create a new <see cref="ParameterSubscriptionValueSet"/> from the updated <see cref="ParameterValueSetBase"/>
        /// </summary>
        /// <param name="oldValue">The old <see cref="ParameterSubscriptionValueSet"/></param>
        /// <param name="valueSetBase">The subscribed <see cref="ParameterValueSetBase"/></param>
        /// <param name="container">The container</param>
        /// <param name="transaction">The current transaction</param>
        /// <param name="partition">The current partition</param>
        private void CreateParameterSubscriptionValueSet(ParameterSubscriptionValueSet oldValue, ParameterValueSetBase valueSetBase, ParameterSubscription container, NpgsqlTransaction transaction, string partition)
        {
            var isOldValueNull = oldValue == null;
            var newValueSet    = new ParameterSubscriptionValueSet(Guid.NewGuid(), 1)
            {
                Manual             = new ValueArray <string>((oldValue == null) ? valueSetBase.Manual : oldValue.Manual),
                ValueSwitch        = isOldValueNull ? CDP4Common.EngineeringModelData.ParameterSwitchKind.MANUAL : oldValue.ValueSwitch,
                SubscribedValueSet = valueSetBase.Iid
            };

            this.ParameterSubscriptionValueSetService.CreateConcept(transaction, partition, newValueSet, container);
        }
        public void VerifyThatCreateValueSetsUpdateOperationsWorks()
        {
            var valueset = new ParameterValueSet(Guid.NewGuid(), this.assembler.Cache, this.uri);

            valueset.Manual = new ValueArray <string>(manual);
            this.parameter1.ValueSet.Add(valueset);

            var subscriptionvalueset = new ParameterSubscriptionValueSet(Guid.NewGuid(), this.assembler.Cache, this.uri);

            subscriptionvalueset.Manual = new ValueArray <string>(manual);
            this.subscription1.ValueSet.Add(subscriptionvalueset);

            var modeldto     = this.model2.ToDto();
            var iterationDto = (Dto.Iteration) this.iteration2.ToDto();
            var def2Dto      = new Dto.ElementDefinition(this.def2Copy.Iid, 2);
            var parameterDto = new Dto.Parameter(this.parameter1Copy.Iid, 2);
            var newValueSet  = new Dto.ParameterValueSet(Guid.NewGuid(), 2);

            var subscription         = new Dto.ParameterSubscription(this.subscriptionCopy.Iid, 1);
            var subscriptionValueSet = new Dto.ParameterSubscriptionValueSet(Guid.NewGuid(), 1);

            iterationDto.Element.Add(def2Dto.Iid);
            def2Dto.Parameter.Add(parameterDto.Iid);
            parameterDto.ValueSet.Add(newValueSet.Iid);
            parameterDto.ParameterSubscription.Add(subscription.Iid);
            subscription.ValueSet.Add(subscriptionValueSet.Iid);

            var returnedDto = new List <Dto.Thing>
            {
                modeldto,
                iterationDto,
                def2Dto,
                parameterDto,
                newValueSet,
                subscription,
                subscriptionValueSet
            };

            var transactionContext = TransactionContextResolver.ResolveContext(this.iteration2);
            var context            = transactionContext.ContextRoute();

            var operationCreator   = new ValueSetOperationCreator(this.session.Object);
            var operationContainer = operationCreator.CreateValueSetsUpdateOperations(context, returnedDto, this.map);

            Assert.AreEqual(2, operationContainer.Operations.Count());
            var operation   = operationContainer.Operations.Single(x => x.OriginalThing.ClassKind == ClassKind.ParameterValueSet);
            var originalPvs = (Dto.ParameterValueSet)operation.OriginalThing;
            var modifiedPvs = (Dto.ParameterValueSet)operation.ModifiedThing;

            Assert.AreNotEqual(originalPvs.Manual, modifiedPvs.Manual);
        }
Esempio n. 5
0
        /// <summary>
        /// Obfuscates an <see cref="CDP4Common.DTO.ParameterSubscriptionValueSet" /> object
        /// </summary>
        /// <param name="thing">The <see cref="CDP4Common.DTO.ParameterValueSetBase" /> to obfuscate.</param>
        private void ObfuscateSubscriptionValueSet(ParameterSubscriptionValueSet thing)
        {
            var valueArrayCount = thing.Manual.Count;

            var newList = new List <string>();

            for (var i = 0; i < valueArrayCount; i++)
            {
                newList.Add("-");
            }

            var newArray = new ValueArray <string>(newList);

            thing.Manual      = newArray;
            thing.ValueSwitch = ParameterSwitchKind.MANUAL;
            this.obfuscatedCache.Add(thing.Iid);
        }
        /// <summary>
        /// Creates ParameterSubscriptionValueSets for the supplied ParameterValueSets.
        /// </summary>
        /// <param name="thing">
        ///     The <see cref="Thing"/> instance that will be inspected.
        /// </param>
        /// <param name="container">
        ///     The container instance of the <see cref="Thing"/> that is inspected.
        /// </param>
        /// <param name="transaction">
        ///     The current transaction to the database.
        /// </param>
        /// <param name="partition">
        ///     The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="securityContext">
        ///     The security-context
        /// </param>
        private void CreateParameterSubscriptionValueSets(
            ParameterSubscription thing,
            Thing container,
            NpgsqlTransaction transaction,
            string partition,
            ISecurityContext securityContext)
        {
            if (!(container is CDP4Common.DTO.ParameterOrOverrideBase parameterOrOverrideBase))
            {
                throw new InvalidOperationException("The container of a ParameterSubscription can only be a ParameterOrOverrideBase.");
            }

            var paramContainer = container is CDP4Common.DTO.Parameter
                    ? (ParameterOrOverrideBase)this.ParameterService.GetShallow(transaction, partition, new[] { container.Iid }, securityContext).OfType <CDP4Common.DTO.Parameter>().SingleOrDefault()
                    : this.ParameterOverrideService.GetShallow(transaction, partition, new[] { container.Iid }, securityContext).OfType <CDP4Common.DTO.ParameterOverride>().SingleOrDefault();


            if (paramContainer == null || !paramContainer.ValueSets.Any())
            {
                throw new InvalidOperationException($"Could not determine the value-set to subscribe on for the parameter-subscription {thing.Iid} to create on parameter/override {container.Iid}");
            }

            var parameterValueSets = parameterOrOverrideBase is CDP4Common.DTO.Parameter
                ? this.ParameterValueSetService.GetShallow(transaction, partition, paramContainer.ValueSets, securityContext).OfType <CDP4Common.DTO.ParameterValueSetBase>().ToArray()
                : this.ParameterOverrideValueSetService.GetShallow(transaction, partition, paramContainer.ValueSets, securityContext).OfType <CDP4Common.DTO.ParameterValueSetBase>().ToArray();

            foreach (var parameterValueSet in parameterValueSets)
            {
                var parameterSubscriptionValueSet =
                    new ParameterSubscriptionValueSet(Guid.NewGuid(), 0)
                {
                    SubscribedValueSet = parameterValueSet.Iid,
                    ValueSwitch        = ParameterSwitchKind.COMPUTED
                };

                var valueArray = new ValueArray <string>(this.DefaultValueArrayFactory.CreateDefaultValueArray(parameterValueSet.Manual.Count));
                parameterSubscriptionValueSet.Manual = valueArray;

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

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

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Manual", !this.IsDerived(parameterSubscriptionValueSet, "Manual") ? parameterSubscriptionValueSet.Manual.ToHstoreString() : string.Empty },
                    { "ValueSwitch", !this.IsDerived(parameterSubscriptionValueSet, "ValueSwitch") ? parameterSubscriptionValueSet.ValueSwitch.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}\".\"ParameterSubscriptionValueSet\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"SubscribedValueSet\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :subscribedValueSet)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

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

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

                    this.ExecuteAndLogCommand(command);
                }
            }

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

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

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Manual", !this.IsDerived(parameterSubscriptionValueSet, "Manual") ? parameterSubscriptionValueSet.Manual.ToHstoreString() : string.Empty },
                { "ValueSwitch", !this.IsDerived(parameterSubscriptionValueSet, "ValueSwitch") ? parameterSubscriptionValueSet.ValueSwitch.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}\".\"ParameterSubscriptionValueSet\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"SubscribedValueSet\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :subscribedValueSet)");

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

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

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
 /// <summary>
 /// Set the values of the <see cref="Dto.ParameterSubscriptionValueSet"/> with the values of a original <see cref="ParameterSubscriptionValueSet"/>
 /// </summary>
 /// <param name="valueSet">The <see cref="Dto.ParameterSubscriptionValueSet"/></param>
 /// <param name="originalValueSet">The <see cref="ParameterSubscriptionValueSet"/></param>
 private void SetValueSetValues(Dto.ParameterSubscriptionValueSet valueSet, ParameterSubscriptionValueSet originalValueSet)
 {
     valueSet.Manual      = originalValueSet.Manual;
     valueSet.ValueSwitch = originalValueSet.ValueSwitch;
 }
Esempio n. 10
0
        public void VerifyThatAfterUpdateUpdateTheOVerrideAndSubscription()
        {
            var valueset = new ParameterValueSet(Guid.NewGuid(), 0);

            valueset.Manual      = new ValueArray <string>(new[] { "set" });
            valueset.Published   = new ValueArray <string>(new[] { "set" });
            valueset.Computed    = new ValueArray <string>(new[] { "set" });
            valueset.ValueSwitch = ParameterSwitchKind.REFERENCE;

            this.parameter.ValueSet.Add(valueset.Iid);
            this.OldParameterContextProvider.Setup(x => x.GetsourceValueSet(null, It.IsAny <Guid?>())).Returns(valueset);

            var overrideValueSet = new ParameterOverrideValueSet(Guid.NewGuid(), 1);

            overrideValueSet.ParameterValueSet = valueset.Iid;
            overrideValueSet.Manual            = new ValueArray <string>(new[] { "override" });
            overrideValueSet.Published         = new ValueArray <string>(new[] { "override" });
            overrideValueSet.Computed          = new ValueArray <string>(new[] { "override" });
            overrideValueSet.ValueSwitch       = ParameterSwitchKind.REFERENCE;

            this.parameterOverride.ValueSet.Add(overrideValueSet.Iid);

            var subscription1 = new ParameterSubscription(Guid.NewGuid(), 1);
            var subscription2 = new ParameterSubscription(Guid.NewGuid(), 2);

            var subscription1ValueSet = new ParameterSubscriptionValueSet(Guid.NewGuid(), 1);

            subscription1ValueSet.Manual             = new ValueArray <string>(new [] { "sub1" });
            subscription1ValueSet.SubscribedValueSet = valueset.Iid;

            var subscription2ValueSet = new ParameterSubscriptionValueSet(Guid.NewGuid(), 1);

            subscription2ValueSet.Manual             = new ValueArray <string>(new[] { "sub2" });
            subscription2ValueSet.SubscribedValueSet = overrideValueSet.Iid;

            subscription1.ValueSet.Add(subscription1ValueSet.Iid);
            subscription2.ValueSet.Add(subscription2ValueSet.Iid);

            var originalThing = this.parameter.DeepClone <Thing>();

            this.parameterOverride.ParameterSubscription.Add(subscription2.Iid);
            this.parameter.ParameterSubscription.Add(subscription1.Iid);

            this.parameterOverrideService.Setup(
                x => x.GetShallow(this.npgsqlTransaction, "partition", null, this.securityContext.Object))
            .Returns(new List <Thing> {
                this.parameterOverride
            });

            this.parameterSubscriptionService.Setup(x =>
                                                    x.GetShallow(this.npgsqlTransaction, "partition",
                                                                 It.Is <IEnumerable <Guid> >(g => g.Contains(subscription1.Iid) && g.Contains(subscription2.Iid)),
                                                                 this.securityContext.Object))
            .Returns(new List <Thing> {
                subscription1, subscription2
            });

            this.valueSetService.Setup(x => x.DeleteConcept(null, "partition", It.IsAny <ParameterValueSet>(), this.parameter))
            .Returns(true);

            this.parameterOverrideValueSetService
            .Setup(x => x.GetShallow(null, "partition", It.Is <IEnumerable <Guid> >(y => y.Contains(overrideValueSet.Iid)), this.securityContext.Object))
            .Returns(new[] { overrideValueSet });

            this.parameterSubscriptionValueSetService
            .Setup(x => x.GetShallow(null, "partition", It.Is <IEnumerable <Guid> >(y => y.Contains(subscription1ValueSet.Iid)), this.securityContext.Object))
            .Returns(new[] { subscription1ValueSet });

            this.parameterSubscriptionValueSetService
            .Setup(x => x.GetShallow(null, "partition", It.Is <IEnumerable <Guid> >(y => y.Contains(subscription2ValueSet.Iid)), this.securityContext.Object))
            .Returns(new[] { subscription2ValueSet });

            this.parameter.ParameterType   = this.cptParameterType.Iid;
            this.parameter.StateDependence = null;

            var updatedParameter = new Parameter(this.parameter.Iid, 0)
            {
                StateDependence = this.actualList.Iid
            };

            updatedParameter.ValueSet.Add(valueset.Iid);
            updatedParameter.ParameterType = this.cptParameterType.Iid;
            updatedParameter.ParameterSubscription.Add(subscription1.Iid);

            this.sideEffect.AfterUpdate(updatedParameter, this.elementDefinition, originalThing, this.npgsqlTransaction, "partition", this.securityContext.Object);

            this.valueSetService.Verify(x => x.CreateConcept(this.npgsqlTransaction, "partition", It.Is <ParameterValueSet>(y => y.Manual.Contains("set")), updatedParameter, -1),
                                        Times.Exactly(2));

            this.parameterOverrideValueSetService.Verify(x => x.CreateConcept(this.npgsqlTransaction, "partition", It.Is <ParameterOverrideValueSet>(y => y.Manual.Contains("override")), this.parameterOverride, -1),
                                                         Times.Exactly(2));

            this.parameterSubscriptionValueSetService.Verify(x => x.CreateConcept(this.npgsqlTransaction, "partition", It.Is <ParameterSubscriptionValueSet>(y => y.Manual.Contains("sub1")), subscription1, -1),
                                                             Times.Exactly(2));

            this.parameterSubscriptionValueSetService.Verify(x => x.CreateConcept(this.npgsqlTransaction, "partition", It.Is <ParameterSubscriptionValueSet>(y => y.Manual.Contains("sub2")), subscription2, -1),
                                                             Times.Exactly(2));
        }