public void map_OrgUnitPublished_to_LocationDto() { var location = new OrgUnitPublished { Id = 1, Description = "test desc", Name = "test", IsEnabled = true }; var dto = Mapper.Map<OrgUnitPublished, OrgUnitDto>(location); Assert.AreEqual(location.Id, dto.Id); }
private void FixupOrgUnitPublished(OrgUnitPublished previousValue) { if (previousValue != null && ReferenceEquals(previousValue.OrgUnit, this)) { previousValue.OrgUnit = null; } if (OrgUnitPublished != null) { OrgUnitPublished.OrgUnit = this; } }
/// <summary> /// Publishes the specified org unit. /// </summary> /// <param name="objectContext">The object context.</param> /// <param name="orgUnitId">The org unit ID.</param> /// <remarks>Calls SaveChanges on the object context.</remarks> public static void PublishOrgUnits(ObjectContext objectContext, int? orgUnitId) { var disabledOrgUnits = new Collection<int>(); // Create object sets var orgUnitObjectSet = objectContext.CreateObjectSet<OrgUnit>() .AsNoTracking() .Include("Schedules") .Include("OrgUnitInsurances") .Include("OrgUnitTypeAssociations") .Include("OrgUnitADGroups") .Include("OrgUnitServices"); var orgUnitPubObjectSet = objectContext.CreateObjectSet<OrgUnitPublished>(); var orgUnitAssocObjectSet = objectContext.CreateObjectSet<OrgUnitAssociation>(); var orgUnitAssocPubObjectSet = objectContext.CreateObjectSet<OrgUnitAssociationPublished>(); // Retrieving all existing org units var orgUnits = (from o in orgUnitObjectSet select o).ToArray(); // Retrieving all existing org units associations var orgUnitAssocs = (from a in orgUnitAssocObjectSet select a).ToArray(); // Get org units sub-set to publish IEnumerable<OrgUnit> orgUnitsToPublish = GetOrgUnitsToPublish(orgUnitObjectSet, orgUnitAssocPubObjectSet, orgUnitId); // For each org unit foreach (OrgUnit orgUnit in orgUnitsToPublish) { // Create a new published org unit from original and set non-inheritable information var orgUnitPub = new OrgUnitPublished { Id = orgUnit.Id, Name = orgUnit.Name, IsEnabled = orgUnit.IsEnabled, LinkedOrgUnitId = orgUnit.LinkedOrgUnitId, Keywords = orgUnit.Keywords, CustomKeywords = orgUnit.CustomKeywords, Custom1 = orgUnit.Custom1, Custom2 = orgUnit.Custom2, Custom3 = orgUnit.Custom3, DynamicColumnData = orgUnit.DynamicColumnData, SeoPageTitle = orgUnit.SeoPageTitle, SeoPageDescription = orgUnit.SeoPageDescription, SeoCustomMetaTags = orgUnit.SeoCustomMetaTags, SeoH1tag = orgUnit.SeoH1tag, SeoPrimaryKeyword = orgUnit.SeoPrimaryKeyword, SeoSecondaryKeyword = orgUnit.SeoSecondaryKeyword, SeoCanonicalUrl = orgUnit.SeoCanonicalUrl, HasCustomCoordinates = orgUnit.HasCustomCoordinates, PotentialEventLocation = orgUnit.PotentialEventLocation, Description = orgUnit.Description }; // Resolve all remaining information for published org unit orgUnitPub = ResolvePublishedOrgUnit(orgUnit, orgUnitPub, orgUnits, orgUnitAssocs, disabledOrgUnits); // Track disabled published org units for future reference if (orgUnitPub.IsEnabled != null && !orgUnitPub.IsEnabled.Value) disabledOrgUnits.Add(orgUnit.Id); // Save changes to object set SavePublishedOrgUnit(orgUnitPubObjectSet, orgUnitPub); } objectContext.SaveChanges(); }
/// <summary> /// Saves the specified published org unit to the object set. /// </summary> /// <param name="orgUnitPubObjectSet">The published org unit object set.</param> /// <param name="orgUnitPub">The published org unit.</param> private static void SavePublishedOrgUnit(ObjectSet<OrgUnitPublished> orgUnitPubObjectSet, OrgUnitPublished orgUnitPub) { // Check if org unit already has a published result OrgUnitPublished existingOrgUnitPub = orgUnitPubObjectSet.SingleOrDefault(p => p.Id == orgUnitPub.Id); // Either add or update published org unit if (existingOrgUnitPub != null) orgUnitPubObjectSet.ApplyCurrentValues(orgUnitPub); else orgUnitPubObjectSet.AddObject(orgUnitPub); }
/// <summary> /// Resolves all the information for a published org unit entity. /// </summary> /// <param name="orgUnit">The org unit.</param> /// <param name="orgUnitPub">The published org unit.</param> /// <param name="orgUnits">Collection of all org units.</param> /// <param name="orgUnitAssocs">Collection of all org unit associations.</param> /// <param name="disabledOrgUnits">A list of org units determined to be disabled.</param> /// <returns></returns> private static OrgUnitPublished ResolvePublishedOrgUnit(OrgUnit orgUnit, OrgUnitPublished orgUnitPub, OrgUnit[] orgUnits, IEnumerable<OrgUnitAssociation> orgUnitAssocs, Collection<int> disabledOrgUnits) { // Fill in information from org unit (self) ApplyOrgUnitInfo(orgUnit, orgUnitPub, disabledOrgUnits); // Fill in information from linked org unit if (orgUnit.LinkedOrgUnitId != null) { OrgUnit linkedOrgUnit = orgUnits.SingleOrDefault(o => o.Id == orgUnit.LinkedOrgUnitId); ApplyOrgUnitInfo(linkedOrgUnit, orgUnitPub, disabledOrgUnits); } // Repeat process for any ascendants OrgUnitAssociation orgUnitAssoc = orgUnitAssocs.SingleOrDefault(a => a.SecondaryId == orgUnit.Id); int? ascendantId = orgUnitAssoc != null ? orgUnitAssoc.PrimaryId : null; if (ascendantId.HasValue) { OrgUnit ascendantOrgUnit = orgUnits.SingleOrDefault(o => o.Id == ascendantId); ResolvePublishedOrgUnit(ascendantOrgUnit, orgUnitPub, orgUnits, orgUnitAssocs, disabledOrgUnits); } // Ensure schedules/insurances/types/services org unit IDs were set before exiting if (orgUnitPub.SchedulesOrgUnitId == null) orgUnitPub.SchedulesOrgUnitId = orgUnitPub.Id; if (orgUnitPub.InsurancesOrgUnitId == null) orgUnitPub.InsurancesOrgUnitId = orgUnitPub.Id; if (orgUnitPub.TypesOrgUnitId == null) orgUnitPub.TypesOrgUnitId = orgUnitPub.Id; if (orgUnitPub.ServicesOrgUnitId == null) orgUnitPub.ServicesOrgUnitId = orgUnitPub.Id; return orgUnitPub; }
/// <summary> /// Uses the specified org unit to fill in missing information for the specified /// published org unit. /// </summary> /// <param name="orgUnit">The org unit.</param> /// <param name="orgUnitPub">The published org unit.</param> /// <param name="disabledOrgUnits">A list of org units determined to be disabled.</param> private static void ApplyOrgUnitInfo(OrgUnit orgUnit, OrgUnitPublished orgUnitPub, Collection<int> disabledOrgUnits) { bool orgUnitPubAddress1Exist = !string.IsNullOrEmpty(orgUnitPub.Address1); // Fill in missing information from specified org unit orgUnitPub.Address1 = ResolveValue(orgUnit.Address1, orgUnitPub.Address1) as string; if (!orgUnitPubAddress1Exist) { orgUnitPub.Address2 = ResolveValue(orgUnit.Address2, orgUnitPub.Address2) as string; } orgUnitPub.City = ResolveValue(orgUnit.City, orgUnitPub.City) as string; orgUnitPub.StateId = ResolveValue(orgUnit.StateId, orgUnitPub.StateId) as int?; orgUnitPub.PostalCode = ResolveValue(orgUnit.PostalCode, orgUnitPub.PostalCode) as string; orgUnitPub.Phone = ResolveValue(orgUnit.Phone, orgUnitPub.Phone) as string; orgUnitPub.Fax = ResolveValue(orgUnit.Fax, orgUnitPub.Fax) as string; orgUnitPub.CountryId = ResolveValue(orgUnit.CountryId, orgUnitPub.CountryId) as int?; orgUnitPub.Metadata = ResolveValue(orgUnit.Metadata, orgUnitPub.Metadata) as string; orgUnitPub.Keyword = ResolveValue(orgUnit.Keyword, orgUnitPub.Keyword) as string; orgUnitPub.CustomFields = ResolveValue(orgUnit.CustomFields, orgUnitPub.CustomFields) as string; orgUnitPub.Latitude = ResolveValue(orgUnit.Latitude, orgUnitPub.Latitude) as decimal?; orgUnitPub.Longitude = ResolveValue(orgUnit.Longitude, orgUnitPub.Longitude) as decimal?; orgUnitPub.Email = ResolveValue(orgUnit.Email, orgUnitPub.Email) as string; orgUnitPub.PaymentProcessorConfigurationId = ResolveValue(orgUnit.PaymentProcessorConfigurationId, orgUnitPub.PaymentProcessorConfigurationId) as int?; // Image and Url information should be published but not inherited. if (orgUnit.Id == orgUnitPub.Id) { orgUnitPub.ImageUrl = ResolveValue(orgUnit.ImageUrl, orgUnitPub.ImageUrl) as string; orgUnitPub.PictureId = ResolveValue(orgUnit.PictureId, orgUnitPub.PictureId) as int?; orgUnitPub.CustomUrl = ResolveValue(orgUnit.CustomUrl, orgUnitPub.CustomUrl) as string; orgUnitPub.CustomLiveUrl = ResolveValue(orgUnit.CustomLiveUrl, orgUnitPub.CustomLiveUrl) as string; orgUnitPub.CustomStageUrl = ResolveValue(orgUnit.CustomStageUrl, orgUnitPub.CustomStageUrl) as string; orgUnitPub.CustomQaUrl = ResolveValue(orgUnit.CustomQaUrl, orgUnitPub.CustomQaUrl) as string; orgUnitPub.CustomDevUrl = ResolveValue(orgUnit.CustomDevUrl, orgUnitPub.CustomDevUrl) as string; orgUnitPub.ServiceLineUrl = ResolveValue(orgUnit.ServiceLineUrl, orgUnitPub.ServiceLineUrl) as string; } if (disabledOrgUnits.Contains(orgUnit.Id)) orgUnitPub.IsEnabled = false; else orgUnitPub.IsEnabled = orgUnitPub.IsEnabled.Value && orgUnit.IsEnabled; if (!orgUnitPub.SchedulesOrgUnitId.HasValue) { if (orgUnit.Schedules.Any()) orgUnitPub.SchedulesOrgUnitId = orgUnit.Id; } if (!orgUnitPub.InsurancesOrgUnitId.HasValue) { if (orgUnit.OrgUnitInsurances.Any()) orgUnitPub.InsurancesOrgUnitId = orgUnit.Id; } // Fill in missing information from specified org unit only if specified org unit is self or linked if (orgUnitPub.Id == orgUnit.Id || orgUnitPub.LinkedOrgUnitId == orgUnit.Id) { if (!orgUnitPub.ServicesOrgUnitId.HasValue) { if (orgUnit.OrgUnitServices.Any()) orgUnitPub.ServicesOrgUnitId = orgUnit.Id; } if (!orgUnitPub.TypesOrgUnitId.HasValue) { if (orgUnit.OrgUnitTypeAssociations.Any()) orgUnitPub.TypesOrgUnitId = orgUnit.Id; } } }