/// <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.ActualFiniteState"/>. /// </returns> public virtual CDP4Common.DTO.ActualFiniteState MapToDto(NpgsqlDataReader reader) { string tempKind; string tempModifiedOn; string tempThingPreference; var valueDict = (Dictionary <string, string>)reader["ValueTypeSet"]; var iid = Guid.Parse(reader["Iid"].ToString()); var revisionNumber = int.Parse(valueDict["RevisionNumber"]); var dto = new CDP4Common.DTO.ActualFiniteState(iid, revisionNumber); dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse)); dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse)); dto.PossibleState.AddRange(Array.ConvertAll((string[])reader["PossibleState"], Guid.Parse)); if (valueDict.TryGetValue("Kind", out tempKind)) { dto.Kind = Utils.ParseEnum <CDP4Common.EngineeringModelData.ActualFiniteStateKind>(tempKind); } if (valueDict.TryGetValue("ModifiedOn", out tempModifiedOn)) { dto.ModifiedOn = Utils.ParseUtcDate(tempModifiedOn); } if (valueDict.TryGetValue("ThingPreference", out tempThingPreference) && tempThingPreference != null) { dto.ThingPreference = tempThingPreference.UnEscape(); } return(dto); }
/// <summary> /// Insert a new database record 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="actualFiniteState"> /// The actualFiniteState 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.ActualFiniteState actualFiniteState, CDP4Common.DTO.Thing container = null) { bool isHandled; var valueTypeDictionaryAdditions = new Dictionary <string, string>(); var beforeWrite = this.BeforeWrite(transaction, partition, actualFiniteState, container, out isHandled, valueTypeDictionaryAdditions); if (!isHandled) { beforeWrite = beforeWrite && base.Write(transaction, partition, actualFiniteState, container); var valueTypeDictionaryContents = new Dictionary <string, string> { { "Kind", !this.IsDerived(actualFiniteState, "Kind") ? actualFiniteState.Kind.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}\".\"ActualFiniteState\"", partition); sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\")"); sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container);"); command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = actualFiniteState.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); } actualFiniteState.PossibleState.ForEach(x => this.AddPossibleState(transaction, partition, actualFiniteState.Iid, x)); } return(this.AfterWrite(beforeWrite, transaction, partition, actualFiniteState, container)); }
public void Setup() { this.securityContext = new Mock <ISecurityContext>(); this.optionService = new Mock <IOptionService>(); this.actualFiniteStateListService = new Mock <IActualFiniteStateListService>(); this.parameterService = new Mock <ICompoundParameterTypeService>(); this.valueSetService = new Mock <IParameterValueSetService>(); this.iterationService = new Mock <IIterationService>(); this.parameterOverrideValueSetService = new Mock <IParameterOverrideValueSetService>(); this.parameterSubscriptionValueSetService = new Mock <IParameterSubscriptionValueSetService>(); this.parameterSubscriptionService = new Mock <IParameterSubscriptionService>(); this.parameterOverrideService = new Mock <IParameterOverrideService>(); this.parameterTypeComponentService = new Mock <IParameterTypeComponentService>(); this.parameterTypeService = new Mock <IParameterTypeService>(); this.elementUsageService = new Mock <IElementUsageService>(); this.defaultValueArrayFactory = new Mock <IDefaultValueArrayFactory>(); this.OldParameterContextProvider = new Mock <IOldParameterContextProvider>(); this.npgsqlTransaction = null; this.iteration = new Iteration(Guid.NewGuid(), 1); this.option1 = new Option(Guid.NewGuid(), 1); this.option2 = new Option(Guid.NewGuid(), 1); this.iteration.Option.Add(new OrderedItem { K = 1, V = this.option1.Iid }); this.iteration.Option.Add(new OrderedItem { K = 2, V = this.option2.Iid }); this.actualList = new ActualFiniteStateList(Guid.NewGuid(), 1); this.actualState1 = new ActualFiniteState(Guid.NewGuid(), 1); this.actualState2 = new ActualFiniteState(Guid.NewGuid(), 1); this.actualList.ActualState.Add(this.actualState1.Iid); this.actualList.ActualState.Add(this.actualState2.Iid); this.parameter = new Parameter(Guid.NewGuid(), 1); this.cptParameterType = new CompoundParameterType(Guid.NewGuid(), 1); this.boolPt = new BooleanParameterType(Guid.NewGuid(), 1); this.cpt1 = new ParameterTypeComponent(Guid.NewGuid(), 1) { ParameterType = this.boolPt.Iid }; this.cpt2 = new ParameterTypeComponent(Guid.NewGuid(), 1) { ParameterType = this.boolPt.Iid }; this.cptParameterType.Component.Add(new OrderedItem { K = 1, V = this.cpt1.Iid.ToString() }); this.cptParameterType.Component.Add(new OrderedItem { K = 2, V = this.cpt2.Iid.ToString() }); this.sideEffect = new ParameterSideEffect { IterationService = this.iterationService.Object, ActualFiniteStateListService = this.actualFiniteStateListService.Object, ParameterValueSetService = this.valueSetService.Object, ParameterOverrideValueSetService = this.parameterOverrideValueSetService.Object, ParameterSubscriptionValueSetService = this.parameterSubscriptionValueSetService.Object, ParameterOverrideService = this.parameterOverrideService.Object, ParameterSubscriptionService = this.parameterSubscriptionService.Object, ParameterTypeService = this.parameterTypeService.Object, ElementUsageService = this.elementUsageService.Object, ParameterTypeComponentService = this.parameterTypeComponentService.Object, OptionService = this.optionService.Object, DefaultValueArrayFactory = this.defaultValueArrayFactory.Object, ParameterValueSetFactory = new ParameterValueSetFactory(), ParameterOverrideValueSetFactory = new ParameterOverrideValueSetFactory(), ParameterSubscriptionValueSetFactory = new ParameterSubscriptionValueSetFactory(), OldParameterContextProvider = this.OldParameterContextProvider.Object }; // prepare mock data this.elementDefinition = new ElementDefinition(Guid.NewGuid(), 1); this.elementDefinition.Parameter.Add(this.parameter.Iid); this.parameterOverride = new ParameterOverride(Guid.NewGuid(), 1) { Parameter = this.parameter.Iid, }; this.elementUsage = new ElementUsage(Guid.NewGuid(), 1) { ElementDefinition = this.elementDefinition.Iid, ParameterOverride = { this.parameterOverride.Iid } }; this.parameterService.Setup(x => x.Get(It.IsAny <NpgsqlTransaction>(), "SiteDirectory", It.Is <IEnumerable <Guid> >(y => y.Contains(this.cptParameterType.Iid)), this.securityContext.Object)) .Returns(new List <Thing> { this.cptParameterType }); this.iterationService.Setup(x => x.GetActiveIteration(null, "partition", this.securityContext.Object)) .Returns(this.iteration); this.actualFiniteStateListService.Setup(x => x.GetShallow(It.IsAny <NpgsqlTransaction>(), "partition", It.Is <IEnumerable <Guid> >(y => y.Contains(this.actualList.Iid)), this.securityContext.Object)) .Returns(new List <Thing> { this.actualList }); this.parameterTypeService.Setup(x => x.GetShallow(this.npgsqlTransaction, "SiteDirectory", null, this.securityContext.Object)) .Returns(new List <Thing> { this.boolPt, this.cptParameterType }); this.parameterTypeService.Setup(x => x.GetShallow(this.npgsqlTransaction, "SiteDirectory", new List <Guid> { this.existingNotQuantityKindParameterTypeGuid }, this.securityContext.Object)) .Returns(new List <Thing> { new BooleanParameterType(this.existingNotQuantityKindParameterTypeGuid, 1) }); this.parameterTypeService.Setup(x => x.GetShallow(this.npgsqlTransaction, "SiteDirectory", new List <Guid> { this.existingQuantityKindParameterTypeGuid }, this.securityContext.Object)) .Returns(new List <Thing> { new SimpleQuantityKind(this.existingQuantityKindParameterTypeGuid, 1) }); this.parameterTypeService.Setup(x => x.GetShallow(this.npgsqlTransaction, "SiteDirectory", new List <Guid> { this.notExistingParameterTypeGuid }, this.securityContext.Object)) .Returns(new List <Thing>()); this.parameterTypeComponentService.Setup(x => x.GetShallow(this.npgsqlTransaction, "SiteDirectory", null, this.securityContext.Object)) .Returns(new List <Thing> { this.cpt1, this.cpt2 }); this.parameterOverrideService.Setup(x => x.GetShallow(this.npgsqlTransaction, "partition", null, this.securityContext.Object)) .Returns(new List <Thing> { this.parameterOverride }); this.elementUsageService.Setup(x => x.GetShallow(this.npgsqlTransaction, "partition", null, this.securityContext.Object)) .Returns(new List <Thing> { this.elementUsage }); this.scalarDefaultValueArray = new ValueArray <string>(new List <string>() { "-" }); this.compoundDefaultValueArray = new ValueArray <string>(new List <string>() { "-", "-" }); this.defaultValueArrayFactory.Setup(x => x.CreateDefaultValueArray(this.cptParameterType.Iid)) .Returns(this.compoundDefaultValueArray); this.defaultValueArrayFactory.Setup(x => x.CreateDefaultValueArray(this.boolPt.Iid)) .Returns(this.scalarDefaultValueArray); this.OldParameterContextProvider.Setup(x => x.GetsourceValueSet(It.IsAny <Guid?>(), It.IsAny <Guid?>())).Returns((ParameterValueSet)null); }
public void Setup() { this.securityContext = new Mock <ISecurityContext>(); this.optionService = new Mock <IOptionService>(); this.actualFiniteStateListService = new Mock <IActualFiniteStateListService>(); this.valueSetService = new Mock <IParameterValueSetService>(); this.iterationService = new Mock <IIterationService>(); this.parameterOverrideValueSetService = new Mock <IParameterOverrideValueSetService>(); this.parameterSubscriptionValueSetService = new Mock <IParameterSubscriptionValueSetService>(); this.parameterSubscriptionService = new Mock <IParameterSubscriptionService>(); this.parameterOverrideService = new Mock <IParameterOverrideService>(); this.elementUsageService = new Mock <IElementUsageService>(); this.defaultValueArrayFactory = new Mock <IDefaultValueArrayFactory>(); this.OldParameterContextProvider = new Mock <IOldParameterContextProvider>(); this.cachedReferenceDataService = new Mock <ICachedReferenceDataService>(); this.organizationalParticipationResolverService = new Mock <IOrganizationalParticipationResolverService>(); this.organizationalParticipationResolverService.Setup(x => x.ValidateCreateOrganizationalParticipation(It.IsAny <Thing>(), It.IsAny <Thing>(), It.IsAny <ISecurityContext>(), this.npgsqlTransaction, It.IsAny <string>())); this.npgsqlTransaction = null; this.iteration = new Iteration(Guid.NewGuid(), 1); this.option1 = new Option(Guid.NewGuid(), 1); this.option2 = new Option(Guid.NewGuid(), 1); this.iteration.Option.Add(new OrderedItem { K = 1, V = this.option1.Iid }); this.iteration.Option.Add(new OrderedItem { K = 2, V = this.option2.Iid }); this.actualList = new ActualFiniteStateList(Guid.NewGuid(), 1); this.actualState1 = new ActualFiniteState(Guid.NewGuid(), 1); this.actualState2 = new ActualFiniteState(Guid.NewGuid(), 1); this.actualList.ActualState.Add(this.actualState1.Iid); this.actualList.ActualState.Add(this.actualState2.Iid); this.parameter = new Parameter(Guid.NewGuid(), 1); this.cptParameterType = new CompoundParameterType(Guid.NewGuid(), 1); this.boolPt = new BooleanParameterType(Guid.NewGuid(), 1); this.cpt1 = new ParameterTypeComponent(Guid.NewGuid(), 1) { ParameterType = this.boolPt.Iid }; this.cpt2 = new ParameterTypeComponent(Guid.NewGuid(), 1) { ParameterType = this.boolPt.Iid }; this.cptParameterType.Component.Add(new OrderedItem { K = 1, V = this.cpt1.Iid.ToString() }); this.cptParameterType.Component.Add(new OrderedItem { K = 2, V = this.cpt2.Iid.ToString() }); this.sideEffect = new ParameterSideEffect { IterationService = this.iterationService.Object, ActualFiniteStateListService = this.actualFiniteStateListService.Object, ParameterValueSetService = this.valueSetService.Object, ParameterOverrideValueSetService = this.parameterOverrideValueSetService.Object, ParameterSubscriptionValueSetService = this.parameterSubscriptionValueSetService.Object, ParameterOverrideService = this.parameterOverrideService.Object, ParameterSubscriptionService = this.parameterSubscriptionService.Object, ElementUsageService = this.elementUsageService.Object, OptionService = this.optionService.Object, DefaultValueArrayFactory = this.defaultValueArrayFactory.Object, ParameterValueSetFactory = new ParameterValueSetFactory(), ParameterOverrideValueSetFactory = new ParameterOverrideValueSetFactory(), ParameterSubscriptionValueSetFactory = new ParameterSubscriptionValueSetFactory(), OldParameterContextProvider = this.OldParameterContextProvider.Object, OrganizationalParticipationResolverService = this.organizationalParticipationResolverService.Object, CachedReferenceDataService = this.cachedReferenceDataService.Object }; // prepare mock data this.elementDefinition = new ElementDefinition(Guid.NewGuid(), 1); this.elementDefinition.Parameter.Add(this.parameter.Iid); this.parameterOverride = new ParameterOverride(Guid.NewGuid(), 1) { Parameter = this.parameter.Iid }; this.elementUsage = new ElementUsage(Guid.NewGuid(), 1) { ElementDefinition = this.elementDefinition.Iid, ParameterOverride = { this.parameterOverride.Iid } }; this.iterationService.Setup(x => x.GetActiveIteration(null, "partition", this.securityContext.Object)) .Returns(this.iteration); this.actualFiniteStateListService.Setup(x => x.GetShallow(It.IsAny <NpgsqlTransaction>(), "partition", It.Is <IEnumerable <Guid> >(y => y.Contains(this.actualList.Iid)), this.securityContext.Object)) .Returns(new List <Thing> { this.actualList }); var parameterTypeDictionary = new Dictionary <Guid, ParameterType>(); parameterTypeDictionary.Add(this.cptParameterType.Iid, this.cptParameterType); parameterTypeDictionary.Add(this.boolPt.Iid, this.boolPt); this.cachedReferenceDataService.Setup(x => x.QueryParameterTypes(this.npgsqlTransaction, this.securityContext.Object)) .Returns(parameterTypeDictionary); var parameterTypeComponentDictionary = new Dictionary <Guid, ParameterTypeComponent>(); parameterTypeComponentDictionary.Add(this.cpt1.Iid, this.cpt1); parameterTypeComponentDictionary.Add(this.cpt2.Iid, this.cpt2); this.cachedReferenceDataService.Setup(x => x.QueryParameterTypeComponents(this.npgsqlTransaction, this.securityContext.Object)) .Returns(parameterTypeComponentDictionary); this.parameterOverrideService.Setup(x => x.GetShallow(this.npgsqlTransaction, "partition", null, this.securityContext.Object)) .Returns(new List <Thing> { this.parameterOverride }); this.elementUsageService.Setup(x => x.GetShallow(this.npgsqlTransaction, "partition", null, this.securityContext.Object)) .Returns(new List <Thing> { this.elementUsage }); this.scalarDefaultValueArray = new ValueArray <string>(new List <string> { "-" }); this.compoundDefaultValueArray = new ValueArray <string>(new List <string> { "-", "-" }); this.defaultValueArrayFactory.Setup(x => x.CreateDefaultValueArray(this.cptParameterType.Iid)) .Returns(this.compoundDefaultValueArray); this.defaultValueArrayFactory.Setup(x => x.CreateDefaultValueArray(this.boolPt.Iid)) .Returns(this.scalarDefaultValueArray); this.OldParameterContextProvider.Setup(x => x.GetsourceValueSet(It.IsAny <Guid?>(), It.IsAny <Guid?>())).Returns((ParameterValueSet)null); }