/// <summary> /// Populate this <see cref="WspPostOperation"/> with the correct setup for the OCDT protocol /// </summary> /// <param name="operation"> /// The operation. /// </param> public override void ConstructFromOperation(Operation operation) { if (operation.ModifiedThing == null) { throw new ArgumentNullException("operation"); } if (operation.OperationKind == OperationKind.Update && operation.OriginalThing == null) { throw new ArgumentException( "When OperationKind is an Update the OriginalThing of the operation may not be null"); } switch (operation.OperationKind) { case OperationKind.Create: this.Create.Add(operation.ModifiedThing); break; case OperationKind.Delete: this.Delete.Add(ClasslessDtoFactory.FromThing(this.metaDataProvider, operation.ModifiedThing)); break; case OperationKind.Update: this.ResolveUpdate(operation); break; case OperationKind.Copy: throw new NotSupportedException("The WSP Post operations do not support a Copy"); case OperationKind.Move: throw new NotSupportedException("The WSP Post operations do not support a Move"); } }
/// <summary> /// Populate the current <see cref="PostOperation"/> with the content based on the /// provided <see cref="Operation"/> /// </summary> /// <param name="operation"> /// The <see cref="Operation"/> that contains all the <see cref="Thing"/>s that need to be /// updated to the data-source /// </param> public override void ConstructFromOperation(Operation operation) { if (operation.ModifiedThing == null) { throw new ArgumentNullException("operation", "The operation may not be null"); } switch (operation.OperationKind) { case OperationKind.Create: this.Create.Add(operation.ModifiedThing); break; case OperationKind.Delete: this.Delete.Add(ClasslessDtoFactory.FromThing(this.metaDataProvider, operation.ModifiedThing)); break; case OperationKind.Update: this.ResolveUpdate(operation); break; case OperationKind.Move: throw new NotImplementedException(); break; default: this.ResolveCopy(operation); break; } }
public void VerifyThatNullableIsTakenIntoAccount() { Thing thing = new ParameterGroup(); var classlessDTO = ClasslessDtoFactory.FullFromThing(this.metaDataProvider, thing); object containingGroup; classlessDTO.TryGetValue("ContainingGroup", out containingGroup); Assert.IsNull(containingGroup); object name; classlessDTO.TryGetValue("Name", out name); Assert.IsNotNull(name); Assert.AreEqual(string.Empty, name); }
public void TestFromThing() { var siteDir = new SiteDirectory(Guid.NewGuid(), 2); siteDir.DefaultPersonRole = Guid.NewGuid(); var properties = new List <string> { "DefaultPersonRole", "Iid" }; var returned = ClasslessDtoFactory.FromThing(this.metaDataProvider, siteDir, properties); Assert.AreEqual(3, returned.Count); object guid; Assert.IsTrue(returned.TryGetValue("DefaultPersonRole", out guid)); Assert.AreEqual(siteDir.DefaultPersonRole, (guid as Guid?).Value); }
public void TestFromThingFull() { var siteDir = new SiteDirectory(Guid.NewGuid(), 2); siteDir.DefaultPersonRole = Guid.NewGuid(); var returned = ClasslessDtoFactory.FullFromThing(this.metaDataProvider, siteDir); Assert.IsTrue(returned.ContainsKey("ClassKind")); Assert.IsTrue(returned.ContainsKey("Iid")); Assert.IsTrue(returned.ContainsKey("Organization")); Assert.IsTrue(returned.ContainsKey("Person")); Assert.IsTrue(returned.ContainsKey("ParticipantRole")); Assert.IsTrue(returned.ContainsKey("DefaultParticipantRole")); Assert.IsTrue(returned.ContainsKey("SiteReferenceDataLibrary")); Assert.IsTrue(returned.ContainsKey("Model")); Assert.IsTrue(returned.ContainsKey("PersonRole")); Assert.IsTrue(returned.ContainsKey("DefaultPersonRole")); Assert.IsTrue(returned.ContainsKey("LogEntry")); Assert.IsTrue(returned.ContainsKey("DomainGroup")); Assert.IsTrue(returned.ContainsKey("Domain")); Assert.IsTrue(returned.ContainsKey("NaturalLanguage")); Assert.IsTrue(returned.ContainsKey("CreatedOn")); Assert.IsTrue(returned.ContainsKey("Name")); Assert.IsTrue(returned.ContainsKey("ShortName")); Assert.IsTrue(returned.ContainsKey("LastModifiedOn")); Assert.IsTrue(returned.ContainsKey("ModifiedOn")); Assert.IsTrue(returned.ContainsKey("RevisionNumber")); Assert.IsTrue(returned.ContainsKey("ExcludedDomain")); Assert.IsTrue(returned.ContainsKey("ExcludedPerson")); Assert.IsTrue(returned.ContainsKey("Annotation")); Assert.IsTrue(returned.ContainsKey("ThingPreference")); Assert.AreEqual(24, returned.Count); object guid; Assert.IsTrue(returned.TryGetValue("DefaultPersonRole", out guid)); Assert.AreEqual(siteDir.DefaultPersonRole, (guid as Guid?).Value); }
public void TestNullThing2() { Thing thing = null; Assert.Throws <ArgumentNullException>(() => ClasslessDtoFactory.FullFromThing(this.metaDataProvider, thing)); }
public void VerifyThatSerializationofOperationsWorks() { this.serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 1, 0)); var postoperation = new TestPostOperation(); this.engModel = new EngineeringModel(Guid.NewGuid(), this.cache, this.uri); this.book1 = new Book(Guid.NewGuid(), this.cache, this.uri); this.book2 = new Book(Guid.NewGuid(), this.cache, this.uri); this.section1 = new Section(Guid.NewGuid(), this.cache, this.uri); this.section1.ShortName = "SS1"; this.engModel.Book.Add(this.book1); this.engModel.Book.Add(this.book2); this.book2.Section.Add(this.section1); var iteration = new Iteration(Guid.NewGuid(), this.cache, this.uri); var ed = new ElementDefinition(Guid.NewGuid(), this.cache, this.uri); var parameter = new Parameter(Guid.NewGuid(), this.cache, this.uri); var parameterSubscription = new ParameterSubscription(Guid.NewGuid(), this.cache, this.uri); var valueset = new ParameterValueSet(Guid.NewGuid(), this.cache, this.uri); var subscriptionValueset = new ParameterSubscriptionValueSet(Guid.NewGuid(), this.cache, this.uri); var valuearrayvalues = new[] { "123", "456", "789.0" }; valueset.Manual = new ValueArray <string>(valuearrayvalues); valueset.Reference = new ValueArray <string>(valuearrayvalues); valueset.Computed = new ValueArray <string>(valuearrayvalues); valueset.Formula = new ValueArray <string>(valuearrayvalues); valueset.Published = new ValueArray <string>(valuearrayvalues); subscriptionValueset.Manual = new ValueArray <string>(valuearrayvalues); subscriptionValueset.SubscribedValueSet = valueset; this.engModel.Iteration.Add(iteration); iteration.Element.Add(ed); ed.Parameter.Add(parameter); parameter.ValueSet.Add(valueset); parameter.ParameterSubscription.Add(parameterSubscription); parameterSubscription.ValueSet.Add(subscriptionValueset); postoperation.Update.Add(ClasslessDtoFactory.FullFromThing(this.metadataprovider, subscriptionValueset.ToDto())); postoperation.Update.Add(ClasslessDtoFactory.FullFromThing(this.metadataprovider, parameterSubscription.ToDto())); postoperation.Create.Add(valueset.ToDto()); postoperation.Delete.Add(ClasslessDtoFactory.FullFromThing(this.metadataprovider, ed.ToDto())); using (var memoryStream = new MemoryStream()) { this.serializer.SerializeToStream(postoperation, memoryStream); // necessary memoryStream.Position = 0; var result = this.serializer.Deserialize <TestPostOperation>(memoryStream); Assert.AreEqual(1, result.Delete.Count); Assert.AreEqual(1, result.Create.Count); Assert.AreEqual(2, result.Update.Count); var subscriptionValueSetClasslessDto = result.Update.Single(x => x["Iid"].ToString() == subscriptionValueset.Iid.ToString()); var valueArray = (ValueArray <string>)subscriptionValueSetClasslessDto["Manual"]; Assert.IsTrue(subscriptionValueSetClasslessDto["Iid"] is Guid); Assert.IsTrue(subscriptionValueSetClasslessDto["ClassKind"] is ClassKind); Assert.AreEqual(3, valueArray.Count()); Assert.AreEqual("123", valueArray[0]); Assert.AreEqual("456", valueArray[1]); Assert.AreEqual("789.0", valueArray[2]); } }
/// <summary> /// Resolves the Update container of the <see cref="Operation"/> /// </summary> /// <param name="operation"> /// The operation. /// </param> private void ResolveUpdate(Operation operation) { var original = ClasslessDtoFactory.FullFromThing(this.metaDataProvider, operation.OriginalThing); var modifiedFull = ClasslessDtoFactory.FullFromThing(this.metaDataProvider, operation.ModifiedThing); var modified = ClasslessDtoFactory.FullFromThing(this.metaDataProvider, operation.ModifiedThing); var listsToDelete = new Dictionary <string, IEnumerable <object> >(); var listsToAdd = new Dictionary <string, IEnumerable <object> >(); foreach (var key in original.Keys) { var originalIenumerable = original[key] as IEnumerable; if (originalIenumerable != null && originalIenumerable.GetType().IsGenericType) { var modifiedIenumerable = (IEnumerable)modifiedFull[key]; // value array case if (originalIenumerable is ValueArray <string> ) { var originalValue = (ValueArray <string>)originalIenumerable; var modifiedValue = (ValueArray <string>)modifiedIenumerable; if (originalValue.ToString() == modifiedValue.ToString()) { modified.Remove(key); } } else { var possibleAdditions = new List <object>(); List <object> originalProperty; List <object> modifiedProperty; var genericTypeArgument = original[key].GetType().GenericTypeArguments[0]; if (genericTypeArgument == typeof(Guid) || genericTypeArgument == typeof(ClassKind) || genericTypeArgument == typeof(VcardTelephoneNumberKind)) { originalProperty = originalIenumerable.Cast <object>().ToList(); modifiedProperty = modifiedIenumerable.Cast <object>().ToList(); } else if (genericTypeArgument == typeof(OrderedItem)) { originalProperty = originalIenumerable.Cast <object>().ToList(); modifiedProperty = modifiedIenumerable.Cast <object>().ToList(); var originalPropertyOrdered = ((List <OrderedItem>)original[key]).ToList(); var modifiedPropertyOrdered = ((List <OrderedItem>)modifiedFull[key]).ToList(); // move property using intersection var sameItems = originalPropertyOrdered.Intersect(modifiedPropertyOrdered); foreach (var sameItem in sameItems) { var orItem = originalPropertyOrdered.Find(o => o.V.Equals(sameItem.V)); var modItem = modifiedPropertyOrdered.Find(m => m.V.Equals(sameItem.V)); if (orItem.K != modItem.K) { modItem.MoveItem(orItem.K, modItem.K); possibleAdditions.Add(modItem); } } } else { // either way remove key in case the generic type is not one of the expected ones modified.Remove(key); continue; } possibleAdditions.AddRange(modifiedProperty.Except(originalProperty).ToList()); if (possibleAdditions.Count > 0) { // this part will be added to the update listsToAdd.Add(key, possibleAdditions); } var possibleDeletions = originalProperty.Except(modifiedProperty).ToList(); if (possibleDeletions.Count > 0) { // this part will be added to the delete listsToDelete.Add(key, possibleDeletions); } // either way remove key modified.Remove(key); } } else { // whatever outputs here has to be an update // remove the properties that have not changed if (key.Equals("Iid") || key.Equals("ClassKind")) { continue; } if (original[key] == null) { if (original[key] == modifiedFull[key]) { modified.Remove(key); } } else { if (original[key].Equals(modifiedFull[key])) { modified.Remove(key); } } } } if (listsToDelete.Count > 0) { var deleteDto = ClasslessDtoFactory.FromThing(this.metaDataProvider, operation.ModifiedThing); foreach (var kvp in listsToDelete) { deleteDto.Add(kvp.Key, kvp.Value); } // add a delete container // Add to the list of deleted thing, WSP will compute what references were removed for the current Thing // example: a category was removed from an ElementDefinition => add a operation in the Delete Operation with the removed category this.Delete.Add(deleteDto); } if (listsToAdd.Count > 0) { var updateDto = modified; foreach (var kvp in listsToAdd) { updateDto.Add(kvp.Key, kvp.Value); } } // if more than just Iid and Classkind have changed then add it to update if (modified.Count > 2) { this.Update.Add(modified); } }