/// <summary> /// Create a property object. /// </summary> public static Property CreateProperty(PropertyType propertyType, string value, Registration registration) { return new Property { PropertyTypeId = propertyType.Id, PropertyType = propertyType, Value = value, Registrations = new Collection<Registration> { registration } }; }
/// <summary> /// Add a property to a registration. /// </summary> public void AddProperty(PropertyType propertyType, KeyValuePair<string, string> property, Registration registration) { var registrationProperty = this.CheckIfPropertyIsRegistered(propertyType, property.Value); if (registrationProperty.Id == 0) { this.AddPropertyToDbSet(propertyType, property.Value, registration); } else { this.AddRegistrationToProperty(registrationProperty, registration); } }
/// <summary> /// Create registration dto from registration. /// </summary> public static RegistrationDto CreateRegistrationDto(Registration registration) { return new RegistrationDto { Id = registration.Id, Name = registration.Name, Created = registration.Created, Updated = registration.Updated, OriginalWriteEventId = registration.OriginalWriteEventId, RegistrationTypeId = registration.RegistrationType.Id, RegistrationTypeName = registration.RegistrationType.Name, RegistrationProperties = CreateRegistrationPropertiesDtos(registration.Properties) }; }
/// <summary> /// Add property to db set. /// </summary> public Property AddPropertyToDbSet(PropertyType type, string value, Registration registration) { try { var property = Property.CreateProperty(type, value, registration); this.readContext.Properties.Add(property); registration.Properties.Add(property); return property; } catch (Exception ex) { this.logger.Error(ex); throw; } }
/// <summary> /// Add both property type and property to a registration. /// </summary> private void AddPropertyTypeAndProperty(Registration registration, KeyValuePair<string, string> gdtoProperty) { var propertyType = this.GetPropertyType(gdtoProperty.Key); if (propertyType.Id == 0) { propertyType = this.AddPropertyTypeToDbSet(gdtoProperty.Key); } if (propertyType.Id > -1) { this.AddProperty(propertyType, gdtoProperty, registration); } }
/// <summary> /// Add new properties to a registration. /// </summary> private Registration AddNewProperties(Registration registration, Gdto gdto) { if (registration.Properties == null || gdto.Properties == null) { return registration; } foreach (var keyValuePair in gdto.Properties) { bool exists = CheckIfPropertyExistInRegistration(registration, keyValuePair); if (!exists) { this.AddPropertyTypeAndProperty(registration, keyValuePair); } } return registration; }
/// <summary> /// Update the values of existing properties with new values from the Gdto. /// </summary> private static Registration UpdateExistingProperties(Registration registration, Gdto gdto) { if (registration.Properties == null || gdto.Properties == null) { return registration; } foreach (var property in registration.Properties) { UpdateProperty(gdto, property); } return registration; }
/// <summary> /// Get the properties that should be deleted. /// </summary> private static IList<Property> GetPropertiesToDelete(Registration registration, Gdto gdto) { var toDelete = new List<Property>(); foreach (var property in registration.Properties) { bool exists = CheckIfPropertyExistInGdto(gdto.Properties, property); if (!exists) { toDelete.Add(property); } } return toDelete; }
/// <summary> /// Delete properties that does not exist in the GDTO. /// </summary> private static Registration DeleteNonExistingProperties(Registration registration, Gdto gdto) { if (registration.Properties == null || gdto.Properties == null) { return registration; } var toDelete = GetPropertiesToDelete(registration, gdto); foreach (var property in toDelete) { registration.Properties.Remove(property); } return registration; }
/// <summary> /// Check if a property exists in a registration. /// </summary> private static bool CheckIfPropertyExistInRegistration(Registration registration, KeyValuePair<string, string> gdtoProperty) { bool exists = false; foreach (var property in registration.Properties) { if (string.Equals(property.PropertyType.Name, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase) || string.Equals(Name, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase) || string.Equals(OriginalWriteEventId, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase)) { exists = true; } } return exists; }
/// <summary> /// Update a registration. /// </summary> public Registration UpdateRegistration(Registration registration, RegistrationType type, DateTime timestamp, string name) { try { return Registration.UpdateRegistration(registration, type, timestamp, name); } catch (Exception ex) { this.logger.Error(ex); throw; } }
/// <summary> /// Update all properties for a registration. Also inserting new and removing old properties. /// </summary> public Registration UpdateProperties(Registration registration, Gdto gdto) { try { if (registration == null || gdto == null) { return registration; } registration = UpdateExistingProperties(registration, gdto); registration = this.AddNewProperties(registration, gdto); registration = DeleteNonExistingProperties(registration, gdto); } catch (Exception ex) { this.logger.Error(ex); throw; } return registration; }
/// <summary> /// Delete a registration. /// </summary> public bool DeleteRegistration(Registration registration) { try { var properties = new List<Property>(); properties.AddRange(registration.Properties); foreach (var property in properties) { this.readContext.Properties.Remove(property); } this.readContext.Registrations.Remove(registration); return true; } catch (Exception ex) { this.logger.Error(ex); return false; } }
/// <summary> /// Add a registration to a property. /// </summary> public bool AddRegistrationToProperty(Property property, Registration registration) { try { if (property.Registrations == null) { property.Registrations = new Collection<Registration>(); } property.Registrations.Add(registration); } catch (Exception ex) { this.logger.Error(ex); throw; } return true; }
/// <summary> /// Add properties to a registration. /// </summary> public void AddRegistrationProperties(Gdto gdto, Registration registration) { foreach (var property in gdto.Properties) { this.AddPropertyTypeAndProperty(registration, property); } }
/// <summary> /// Update a registration. /// </summary> public static Registration UpdateRegistration(Registration registration, RegistrationType type, DateTime timestamp, string name) { registration.RegistrationType = type; registration.RegistrationTypeId = type.Id; registration.Name = name; registration.Updated = timestamp; return registration; }