public static void CapturePropertiesForAdd(ObjectContext context, OrgUnitV2 source, OrgUnit target) { target.SetName(source.Name); target.SetAddress1(source.Address1); target.SetAddress2(source.Address2); target.SetCity(source.City); target.SetPostalCode(source.PostalCode); target.SetPhone(source.Phone); target.SetFax(source.Fax); target.SetCustomUrl(source.CustomUrl); target.SetCustomLiveUrl(source.CustomLiveUrl); target.SetCustomStageUrl(source.CustomStageUrl); target.SetCustomQaUrl(source.CustomQaUrl); target.SetCustomDevUrl(source.CustomDevUrl); target.SetServiceLineUrl(source.ServiceLineUrl); target.IsEnabled = source.IsEnabledAsBool.HasValue ? source.IsEnabledAsBool.Value : false; LookupStateId(context, source.State, target); LookupCountryId(context, source.Country, target); target.Latitude = source.LatitudeAsDecimal; target.Longitude = source.LongitudeAsDecimal; int pictureId; if (int.TryParse(source.PictureId, out pictureId)) { target.ImageUrl = source.ImageUrl; target.PictureId = pictureId; } target.Keywords = source.Keywords; target.CustomKeywords = source.CustomKeywords; target.Custom1 = source.Custom1; target.Custom2 = source.Custom2; target.Custom3 = source.Custom3; target.PotentialEventLocation = source.IsPotentialEventLocationAsBool.HasValue ? source.IsPotentialEventLocationAsBool.Value : true; target.SeoPageTitle = source.SeoPageTitle; target.SeoPageDescription = source.SeoPageDescription; target.SeoCustomMetaTags = source.SeoCustomMetaTags; target.SeoH1tag = source.SeoH1Tag; target.SeoPrimaryKeyword = source.SeoPrimaryKeyword; target.SeoSecondaryKeyword = source.SeoSecondaryKeyword; target.SeoCanonicalUrl = source.SeoCanonicalUrl; }
public static void AddExternalIdMapping(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { if (!string.IsNullOrEmpty(source.OrgUnitExternalId)) { var existingMappings = context.CreateObjectSet<DataImportEntityIdMap>() .Where(m => m.EntityName == "OrgUnit" && m.InternalId == orgUnit.Id) .ToList(); if (existingMappings.Count == 1) { if (existingMappings[0].ExternalId != source.OrgUnitExternalId) { // Update ExternalId on existing mapping existingMappings[0].ExternalId = source.OrgUnitExternalId; } } else { // Remove ambiguous mappings (if any) if (existingMappings.Count > 1) { foreach (var mapping in existingMappings) { context.DeleteObject(mapping); } } // Create new mapping context.AddObject("DataImportEntityIdMaps", new DataImportEntityIdMap { EntityName = "OrgUnit", DataImportId = 1, InternalId = orgUnit.Id, ExternalId = source.OrgUnitExternalId }); } } }
public static void SetOrgUnitTypes(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { //ignore null values if (source.OrgUnitTypes == null) return; try { var existingOrgUnitTypes = orgUnit.OrgUnitTypeAssociations.ToArray(); foreach (var item in existingOrgUnitTypes) context.DeleteObject(item); var orgUnitTypes = context.CreateObjectSet<OrgUnitType>(); var orgUnitTypeAssociations = new List<OrgUnitTypeAssociation>(); foreach (var item in source.OrgUnitTypes) { //Ensure orgUnitTypes Exists var orgUnitType = orgUnitTypes.FirstOrDefault(i => i.Name == item.Name); if (orgUnitType == null) { orgUnitType = new OrgUnitType() { Name = item.Name, ImageUrl = item.ImageUrl, IsEnabled = true }; orgUnitTypes.AddObject(orgUnitType); } orgUnitTypeAssociations.Add(new OrgUnitTypeAssociation { OrgUnitType = orgUnitType, OrgUnit = orgUnit, IsPrimaryOrgUnitType = item.IsPrimaryOrgUnitType }); } orgUnit.OrgUnitTypeAssociations = orgUnitTypeAssociations; } catch (Exception ex) { throw new BusinessException("Error processing orgUnitTypes for orgUnit '" + orgUnit.Name + "' - " + ex.Message, ex); } }
public static void SetOrgUnitServices(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { //ignore null values if (source.OrgUnitServices == null) return; try { var existingServices = orgUnit.OrgUnitServices.ToArray(); foreach (var item in existingServices) context.DeleteObject(item); var services = context.CreateObjectSet<Service>(); var orgUnitServices = new List<OrgUnitService>(); foreach (var item in source.OrgUnitServices) { //Ensure service Exists var service = services.FirstOrDefault(i => i.Name == item.Name); if (service == null) { service = new Service() { Name = item.Name, IsEnabled = true }; services.AddObject(service); } orgUnitServices.Add(new OrgUnitService { Service = service, OrgUnit = orgUnit }); } orgUnit.OrgUnitServices = orgUnitServices; } catch (Exception ex) { throw new BusinessException("Error processing services for orgUnit '" + orgUnit.Name + "' - " + ex.Message, ex); } }
public static void SetOrgUnitSchedules(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { //ignore null values if (source.OrgUnitHours == null) return; //remove all schedules, will re-add below orgUnit.Schedules.ToList().ForEach(context.DeleteObject); foreach (var item in source.OrgUnitHours) { var timeSpan = new ScheduleTimeSpan(ResolveOpenTime(item), ResolveOpenHours(item)); orgUnit.AddSchedule(timeSpan); } }
public static void SetOrgUnitDiscountCodes(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { //ignore null values if (source.OrgUnitDiscountCodes == null) return; try { var existingOrgUnitDiscountCodes = orgUnit.OrgUnitDiscountCodes.ToArray(); foreach (var item in existingOrgUnitDiscountCodes) context.DeleteObject(item); var discountCodes = context.CreateObjectSet<DiscountCode>(); var orgUnitDiscountCodes = new List<OrgUnitDiscountCode>(); foreach (var item in source.OrgUnitDiscountCodes) { //Check if discount code exists var discountCode = discountCodes.FirstOrDefault(i => i.Code == item.Code); //If the discount code doesn't exist, create a new one if (discountCode == null) { discountCode = new DiscountCode() { Code = item.Code, Description = item.Description, DiscountTypeId = LookupDiscountType(context, item.DiscountType), IsEnabled = item.IsEnabled.ToString().ToUpperInvariant() == "TRUE" ? true : false, IsFromOrgUnit = true, IsGroupCode = item.IsGroupCode.ToString().ToUpperInvariant() == "TRUE" ? true : false, }; int requiredGroupSize; if (int.TryParse(item.RequiredGroupSize.ToString(), out requiredGroupSize)) discountCode.RequiredGroupSize = requiredGroupSize; decimal discountValue; if (decimal.TryParse(item.DiscountValue, out discountValue)) discountCode.DiscountValue = discountValue; DateTime startDate; if (DateTime.TryParse(item.StartDate, out startDate)) discountCode.StartDate = startDate; DateTime endDate; if (DateTime.TryParse(item.EndDate, out endDate)) discountCode.EndDate = endDate; discountCodes.AddObject(discountCode); } else { //Ensure existing discount code is not an Event discount code if (!discountCode.IsFromOrgUnit) { throw new BusinessException("Discount code already in use by an Event."); } UpdateExistingCode(context, discountCode, item); } var newOrgUnitDiscountCode = new OrgUnitDiscountCode(); var codeWithId = discountCodes.FirstOrDefault(d => d.Code == discountCode.Code); newOrgUnitDiscountCode.DiscountCodeId = codeWithId == null ? 0 : codeWithId.Id; newOrgUnitDiscountCode.DiscountCode = discountCode; int orgUnitId; if (int.TryParse(source.CopiedInternalId, out orgUnitId)) newOrgUnitDiscountCode.OrgUnitId = orgUnitId; orgUnitDiscountCodes.Add(newOrgUnitDiscountCode); } orgUnit.OrgUnitDiscountCodes = orgUnitDiscountCodes; } catch (Exception ex) { throw new BusinessException("Error processing OrgUnitDiscountCodes for orgUnit '" + orgUnit.Name + "' - " + ex.Message, ex); } }
public static void SetOrgUnitAssociation(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit, bool isAddingOrgUnit) { var secondaryId = orgUnit.Id; int? primaryId = null; if (source.ParentInternalIdAsInt.HasValue) { primaryId = source.ParentInternalIdAsInt.Value; } else if (source.ParentExternalId != null) { var mappings = context.CreateObjectSet<DataImportEntityIdMap>() .SingleOrDefault(m => m.ExternalId == source.ParentExternalId && m.EntityName == "OrgUnit"); if (mappings == null) throw new BusinessException(string.Format(CultureInfo.InvariantCulture, "OrgUnit with ParentExternalId of {0} not found", source.ParentExternalId)); primaryId = mappings.InternalId; } if (source.ParentInternalId != null || source.ParentExternalId != null || isAddingOrgUnit) { var orgUnitsObjectContext = context.CreateObjectSet<OrgUnit>(); var orgUnitAssociationsObjectContext = context.CreateObjectSet<OrgUnitAssociation>(); // Validate primary/parent org unit if (primaryId > 0) { var primaryOrgUnit = orgUnitsObjectContext.SingleOrDefault(o => o.Id == primaryId.Value); if (primaryOrgUnit == null) { throw new BusinessException(string.Format(CultureInfo.InvariantCulture, "OrgUnit with ParentInternalId of {0} not found", primaryId.Value.ToString(CultureInfo.InvariantCulture))); } } else { primaryId = null; } // Delete any existing association var oldAssocation = orgUnitAssociationsObjectContext.FirstOrDefault(a => a.SecondaryId == secondaryId); if (oldAssocation != null) { context.DeleteObject(oldAssocation); } // Add new association var association = new OrgUnitAssociation() { PrimaryId = primaryId, OrgUnitSecondary = orgUnit, IsEnabled = true }; orgUnitAssociationsObjectContext.AddObject(association); } }
public static void SetDynamicColumns(ObjectContext context, OrgUnitV2 source, OrgUnit orgUnit) { if (source.DynamicColumns == null) return; var existingDynamicColumns = context.CreateObjectSet<DynamicColumnEntity>() .First(d => d.EntityName == "OrgUnits") .DynamicColumnInstances; if (!existingDynamicColumns.Any()) orgUnit.DynamicColumnData = DynamicColumnUtility.ConvertFomList(new List<DynamicColumnDto>()); //Make sure column names specified are valid var invalidColumnNames = new List<string>(); foreach (var item in source.DynamicColumns) { var column = existingDynamicColumns.FirstOrDefault(d => d.Name == item.FieldName); if (column == null) invalidColumnNames.Add(item.FieldName); } if (invalidColumnNames.Any()) { var errorMessage = "Invalid Column Names Specified: " + string.Join(",", invalidColumnNames.ToArray()); throw new BusinessException("Error processing dynamic columns for organizational unit '" + orgUnit.Name + "' - " + errorMessage); } orgUnit.DynamicColumnData = DynamicColumnUtility.ConvertFomList(source.DynamicColumns); }
public static void CapturePropertiesForUpdate(ObjectContext context, OrgUnitV2 source, OrgUnit target) { //if source values are null, we do not update the field. if (source.Name != null) target.SetName(source.Name); if (source.Address1 != null) target.SetAddress1(source.Address1); if (source.Address2 != null) target.SetAddress2(source.Address2); if (source.City != null) target.SetCity(source.City); if (source.PostalCode != null) target.SetPostalCode(source.PostalCode); if (source.Phone != null) target.SetPhone(source.Phone); if (source.Email != null) target.Email = source.Email; if (source.Description != null) target.Description = source.Description; if (source.Fax != null) target.SetFax(source.Fax); if (source.CustomUrl != null) target.SetCustomUrl(source.CustomUrl); if (source.CustomLiveUrl != null) target.SetCustomLiveUrl(source.CustomLiveUrl); if (source.CustomStageUrl != null) target.SetCustomStageUrl(source.CustomStageUrl); if (source.CustomQaUrl != null) target.SetCustomQaUrl(source.CustomQaUrl); if (source.CustomDevUrl != null) target.SetCustomDevUrl(source.CustomDevUrl); if (source.ServiceLineUrl != null) target.SetServiceLineUrl(source.ServiceLineUrl); if (source.IsEnabled != null) target.IsEnabled = source.IsEnabledAsBool.HasValue ? source.IsEnabledAsBool.Value : false; if (source.State != null) { LookupStateId(context, source.State, target); } if (source.Country != null) { LookupCountryId(context, source.Country, target); } int pictureId; if (int.TryParse(source.PictureId, out pictureId)) { target.ImageUrl = source.ImageUrl ?? target.ImageUrl; target.PictureId = pictureId; } if (source.Keywords != null) target.Keywords = source.Keywords; if (source.CustomKeywords != null) target.CustomKeywords = source.CustomKeywords; if (source.Latitude != null) target.Latitude = source.LatitudeAsDecimal; if (source.Longitude != null) target.Longitude = source.LongitudeAsDecimal; if (source.Custom1 != null) target.Custom1 = source.Custom1; if (source.Custom2 != null) target.Custom2 = source.Custom2; if (source.Custom3 != null) target.Custom3 = source.Custom3; if (source.IsPotentialEventLocation != null) target.PotentialEventLocation = source.IsPotentialEventLocationAsBool.HasValue ? source.IsPotentialEventLocationAsBool.Value : true; if (source.SeoPageTitle != null) target.SeoPageTitle = source.SeoPageTitle; if (source.SeoPageDescription != null) target.SeoPageDescription = source.SeoPageDescription; if (source.SeoCustomMetaTags != null) target.SeoCustomMetaTags = source.SeoCustomMetaTags; if (source.SeoH1Tag != null) target.SeoH1tag = source.SeoH1Tag; if (source.SeoPrimaryKeyword != null) target.SeoPrimaryKeyword = source.SeoPrimaryKeyword; if (source.SeoSecondaryKeyword != null) target.SeoSecondaryKeyword = source.SeoSecondaryKeyword; if (source.SeoCanonicalUrl != null) target.SeoCanonicalUrl = source.SeoCanonicalUrl; }
private string GetLocation(ConvertServiceRequest request) { var orgUnitId = 0; if (request.Parameters.ContainsKey("externalId")) { orgUnitId = LookupDataEntityMapId(request.Parameters["externalId"], false); } else if (request.Parameters.ContainsKey("id")) { if (!int.TryParse(request.Parameters["id"], out orgUnitId)) throw new BusinessException("Org Unit Id must be a valid integer"); } var getOrgUnitRequest = new ReadOrgUnitPublishedRequestV2 { Id = orgUnitId }; var orgUnit = ProcessRequest<ReadOrgUnitPublishedResponseV2>(getOrgUnitRequest).OrgUnit; var viewModel = new OrgUnitV2 { OrgUnitInternalId = orgUnit.Id.ToString(CultureInfo.InvariantCulture), OrgUnitExternalId = orgUnit.ExternalId, Name = orgUnit.Name, IsEnabled = orgUnit.IsEnabled.ToString(CultureInfo.InvariantCulture), Address1 = orgUnit.Address1, Address2 = orgUnit.Address2, City = orgUnit.City, State = orgUnit.StateName, Country = orgUnit.CountryName, PostalCode = orgUnit.PostalCode, Latitude = orgUnit.Latitude.HasValue ? orgUnit.Latitude.Value.ToString(CultureInfo.InvariantCulture) : null, Longitude = orgUnit.Longitude.HasValue ? orgUnit.Longitude.Value.ToString(CultureInfo.InvariantCulture) : null, Phone = orgUnit.Phone, Fax = orgUnit.Fax, Email = orgUnit.Email, CustomUrl = orgUnit.CustomUrl, CustomLiveUrl = orgUnit.CustomLiveUrl, CustomStageUrl = orgUnit.CustomStageUrl, CustomQaUrl = orgUnit.CustomQaUrl, CustomDevUrl = orgUnit.CustomDevUrl, ServiceLineUrl = orgUnit.ServiceLineUrl, Description = orgUnit.Description, ImageUrl = orgUnit.ImageUrl, PictureId = orgUnit.PictureId.HasValue ? orgUnit.PictureId.Value.ToString(CultureInfo.InvariantCulture) : null, ParentInternalId = orgUnit.ParentId.HasValue ? orgUnit.ParentId.Value.ToString(CultureInfo.InvariantCulture) : null, ParentExternalId = orgUnit.ParentExternalId, CopiedInternalId = orgUnit.LinkedOrgUnitId.HasValue ? orgUnit.LinkedOrgUnitId.Value.ToString(CultureInfo.InvariantCulture) : null, CopiedExternalId = orgUnit.LinkedExternalId, CostCenter = orgUnit.CostCenter, Keywords = orgUnit.Keywords, CustomKeywords = orgUnit.CustomKeywords, Custom1 = orgUnit.Custom1, Custom2 = orgUnit.Custom2, Custom3 = orgUnit.Custom3, DynamicColumns = orgUnit.DynamicColumns, SeoPageTitle = orgUnit.SeoPageTitle, SeoPageDescription = orgUnit.SeoPageDescription, SeoCustomMetaTags = orgUnit.SeoCustomMetaTags, SeoH1Tag = orgUnit.SeoH1Tag, SeoPrimaryKeyword = orgUnit.SeoPrimaryKeyword, SeoSecondaryKeyword = orgUnit.SeoSecondaryKeyword, SeoCanonicalUrl = orgUnit.SeoCanonicalUrl, IsPotentialEventLocation = orgUnit.PotentialEventLocation.ToString(CultureInfo.InvariantCulture) }; //get schedule if (orgUnit.SchedulesOrgUnitId.HasValue) { var scheduleUnitRequest = new ReadOrgUnitRequest(orgUnit.SchedulesOrgUnitId.Value); var scheduleLocation = ProcessRequest<ReadOrgUnitResponse>(scheduleUnitRequest).Location; viewModel.OrgUnitHours = scheduleLocation.Schedules.Select(h => new WeekDayHoursV2 { DayOfWeek = h.Day, OpenTime = h.Open, CloseTime = h.Close }).ToList(); } //get services if (orgUnit.ServicesOrgUnitId.HasValue) { var servicesUnitRequest = new ReadOrgUnitRequest(orgUnit.ServicesOrgUnitId.Value); var servicesLocation = ProcessRequest<ReadOrgUnitResponse>(servicesUnitRequest).Location; viewModel.OrgUnitServices = servicesLocation.Services.Select(s => new OrgUnitServiceV2 { Name = s.ServiceName }).ToList(); } //get insurances if (orgUnit.InsurancesOrgUnitId.HasValue) { var insuranceUnitRequest = new ReadOrgUnitRequest(orgUnit.InsurancesOrgUnitId.Value); var insuranceLocation = ProcessRequest<ReadOrgUnitResponse>(insuranceUnitRequest).Location; viewModel.OrgUnitInsurances = insuranceLocation.Insurances.Select(i => new OrgUnitInsuranceV2 { Name = i.InsuranceName }).ToList(); } //get orgUnitTypes var listLocationOrgUnitTypesRequest = new ListOrgUnitOrgUnitTypesRequest(orgUnit.Id); var queryResponse = ProcessRequest<ListOrgUnitOrgUnitTypesResponse>(listLocationOrgUnitTypesRequest); viewModel.OrgUnitTypes = queryResponse.LocationOrgUnitTypes.Select(t => new OrgUnitTypeV2 { IsPrimaryOrgUnitType = t.IsPrimaryOrgUnitType, Name = t.OrgUnitTypeName }).ToList(); //get discount codes var discountCodeRequest = new ListOrgUnitDiscountCodesRequest(orgUnit.Id); var discountCodeResponse = ProcessRequest<ListOrgUnitDiscountCodesResponse>(discountCodeRequest); viewModel.OrgUnitDiscountCodes = discountCodeResponse.OrgUnitDiscountCodes.Select(d => new OrgUnitDiscountCodeV2 { Code = d.Code, Description = d.Description, DiscountType = d.DiscountTypeName.ToString(CultureInfo.InvariantCulture), DiscountValue = d.DiscountValue.ToString(CultureInfo.InvariantCulture), EndDate = d.EndDate == DateTime.MinValue ? null : d.EndDate.ToString(CultureInfo.InvariantCulture), StartDate = d.StartDate == DateTime.MinValue ? null : d.StartDate.ToString(CultureInfo.InvariantCulture), IsEnabled = d.IsEnabled.ToString(CultureInfo.InvariantCulture), IsGroupCode = d.IsGroupCode.ToString(CultureInfo.InvariantCulture), RequiredGroupSize = d.RequiredGroupSize.ToString(CultureInfo.InvariantCulture) }).ToList(); return CommonUtils.XmlSerialize(viewModel); }