public object Post(VehicleEntity vehicle) { try { var stringBuilder = new StringBuilder(); if(vehicle.photoIds != null ) { vehicle.photoIds.ToList().ForEach(p => stringBuilder.Append(p + ",")); } var message = string.Format("Post a new Vehicle {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}.", vehicle.Vin, vehicle.MakeId, vehicle.Make, vehicle.ModelId, vehicle.Model, vehicle.OEMCode, (vehicle.Options == null) ? "0" : vehicle.Options.Count().ToString(), vehicle.StockNumber, vehicle.Style, vehicle.StyleId, vehicle.Trim, vehicle.Year, stringBuilder.ToString()); _log.Log(message, System.Diagnostics.EventLogEntryType.Information); } catch { // Logging - so don't want this code to crash original worflow } return _vehicleFacade.CreateVehicle(vehicle); }
private void TryGetYear(dynamic jsonObject, VehicleEntity vehicle) { try { if (vehicle == null) vehicle = new VehicleEntity(); if (jsonObject.year != null && jsonObject.year.Value != null) { int year; Int32.TryParse(jsonObject.year.Value.ToString(), out year); vehicle.Year = year; } } catch { //TODO: Logging } }
private void TryGetTrimAndStyle(dynamic jsonObject, VehicleEntity vehicle) { try { if (vehicle == null) vehicle = new VehicleEntity(); if (jsonObject.style != null && jsonObject.style.label != null && jsonObject.style.id != null && jsonObject.style.trim != null && jsonObject.style.label.Value != null && jsonObject.style.id.Value != null && jsonObject.style.trim.Value!= null) { vehicle.Style = jsonObject.style.label.Value.ToString(); vehicle.StyleId = jsonObject.style.id.Value.ToString(); vehicle.Trim = jsonObject.style.trim.Value.ToString(); } } catch { //TODO: take care of logging. } }
private void TryGetOemModelCode(dynamic jsonObject, VehicleEntity vehicle) { try { if (vehicle == null) vehicle = new VehicleEntity(); if (jsonObject.oemModelCode != null && jsonObject.oemModelCode.Value != null) { vehicle.OEMCode = jsonObject.oemModelCode.Value; } } catch { //TODO: Logging } }
private void TryGetModel(dynamic jsonObject, VehicleEntity vehicle) { try { if (vehicle == null) vehicle = new VehicleEntity(); if (jsonObject.model != null && jsonObject.model.label != null && jsonObject.model.id != null && jsonObject.model.label.Value != null && jsonObject.model.id.Value != null) { vehicle.Model = jsonObject.model.label.Value.ToString(); vehicle.ModelId = jsonObject.model.id.Value.ToString(); } } catch { //TODO: Logging } }
private void TryGetMake(dynamic jsonObject, VehicleEntity vehicle) { try { if (vehicle == null) vehicle = new VehicleEntity(); if (jsonObject.make != null && jsonObject.make.label != null && jsonObject.make.id != null && jsonObject.make.label.Value != null && jsonObject.make.id.Value != null) { vehicle.Make = jsonObject.make.label.Value.ToString(); vehicle.MakeId = jsonObject.make.id.Value.ToString(); } } catch { //TODO: take care of logging. } }
/// <summary> /// Mapper for transforming incoming object to the one service returns /// </summary> /// <param name="responseJson">JSON to be parsed</param> /// <returns>Resultant object structure of vehicle</returns> private VehicleEntity GetVehicleEntityByParsingJSON(JObject responseJson) { var response = responseJson.ToObject<dynamic>(); var returnEntity = new VehicleEntity(); if(response !=null) { if(response.vehicles != null && response.vehicles.GetType() == typeof(JArray) && response.vehicles[0].vehicle != null) { TryGetMake(response.vehicles[0].vehicle, returnEntity); TryGetModel(response.vehicles[0].vehicle, returnEntity); TryGetYear(response.vehicles[0].vehicle, returnEntity); } } return returnEntity; }
private IEnumerable<VehicleEntity> GetTaxonomyListByParsingJson(JObject json) { var taxonomyResults = new List<VehicleEntity>(); var responseObject = json.ToObject<dynamic>(); if (responseObject != null) { if (responseObject.searchResult != null) { if(responseObject.searchResult.taxonomy != null && responseObject.searchResult.taxonomy.GetType() == typeof(JArray)) { foreach (var taxonomyObject in responseObject.searchResult.taxonomy) { if(taxonomyObject.taxonomyRecord != null) { var vehicleEntity = new VehicleEntity(); TryGetMake(taxonomyObject.taxonomyRecord, vehicleEntity); TryGetModel(taxonomyObject.taxonomyRecord, vehicleEntity); TryGetYear(taxonomyObject.taxonomyRecord, vehicleEntity); TryGetOemModelCode(taxonomyObject.taxonomyRecord, vehicleEntity); TryGetTrimAndStyle(taxonomyObject.taxonomyRecord, vehicleEntity); taxonomyResults.Add(vehicleEntity); } } } } } return taxonomyResults; }
public VehicleEntity CreateVehicle(VehicleEntity newVehicle) { var resultVehicleEntity = new VehicleEntity(); EventLogEntryType eventLogType = EventLogEntryType.Information; try { #region Detailer Call var requestUri = ConfigurationManager.AppSettings["detailerUrl"]; dynamic detailerResponse = RestClient.PostData(requestUri, GetDetailerRequestPayload(newVehicle)); _logger.AppendMessages("Successfully completed Detailer call before Create Vehicle."); #endregion Detailer call #region Create Vehicle Call if (detailerResponse.vehicles != null && detailerResponse.vehicles.GetType() == typeof(JArray) && detailerResponse.vehicles[0] != null && detailerResponse.vehicles[0].vehicle != null) { JObject vehicleEntity = detailerResponse.vehicles[0].vehicle; vehicleEntity.Add("stockNumber", newVehicle.StockNumber ?? string.Empty); _logger.AppendMessages("Detailer found atleast one vehicle in response."); if (newVehicle.photoIds != null) { //?? _logger.AppendMessages("Number of PhotoIds: " + newVehicle.photoIds.Count()); //vehicleEntity.Add("assets", JObject.Parse("{\"dealerPhotos\": []}")); foreach (var id in newVehicle.photoIds) { if (id != null && !id.Equals(string.Empty) && !id.Equals(" ") && vehicleEntity.GetValue("assets") == null) { //?? _logger.AppendMessages("L"); vehicleEntity.Add("assets", JObject.Parse("{\"dealerPhotos\": [{\"id\":" + id + "}]}")); } } } if (vehicleEntity.GetValue("source") == null) { vehicleEntity.Add("source", "M"); } var createVehicleRequestPayload = string.Format("{{\"criteria\":{{\"vehicleContexts\":[{{\"vehicleContext\":{{\"vehicle\":{0},\"modifiedFields\":[\"assets\",\"bodyStyle\",\"bodyType\",\"certified\",\"colors.exterior.base\",\"colors.exterior.code\",\"colors.exterior.name\",\"colors.interior.code\",\"colors.interior.name\",\"createdDate\",\"descriptions\",\"doors\",\"drivetrain\",\"engine.aspiration\",\"engine.cylinders\",\"engine.description\",\"engine.displacement\",\"engine.fuelType\",\"engine.power\",\"id\",\"inventoryOwner\",\"lastModifiedDate\",\"lotDate\",\"make.Id\",\"make.label\",\"model.Id\",\"model.label\",\"odometer\",\"oemModelCode\",\"options.dealerOptions\",\"options.factoryOptions\",\"preOwned\",\"prices.discountPrice\",\"prices.internetPrice\",\"prices.invoicePrice\",\"prices.msrp\",\"prices.retailPrice\",\"prices.vendedPrice\",\"stockNumber\",\"style.Id\",\"style.trim\",\"transmission.speeds\",\"transmission.text\",\"transmission.type\",\"unmodifiable\",\"vin\",\"warranties\",\"year\"]}}}}],\"inventoryOwner\":\"gmps-kindred\",\"useSource\": true}}}}", vehicleEntity); _logger.AppendMessages(string.Format("Request payload for create vehicle call - {0}", createVehicleRequestPayload)); dynamic result = RestClient.PostData(ConfigurationManager.AppSettings["createVehicleUrl"], createVehicleRequestPayload); if (result != null && result.result != null) { result = result.result; if (result != null && result.status != null && result.status.GetType() == typeof(JArray) && result.status[0].vehicle != null) { _logger.AppendMessages(string.Format("Create Vehicle call successful . result {0} ", result)); resultVehicleEntity.Vin = result.status[0].vehicle.vin ?? string.Empty; resultVehicleEntity.StockNumber = result.status[0].vehicle.stockNumber ?? string.Empty; TryGetMake(result.status[0].vehicle, resultVehicleEntity); TryGetModel(result.status[0].vehicle, resultVehicleEntity); TryGetOemModelCode(result.status[0].vehicle, resultVehicleEntity); TryGetTrimAndStyle(result.status[0].vehicle, resultVehicleEntity); TryGetYear(result.status[0].vehicle, resultVehicleEntity); #region Get Color from response var refStyles = result.status[0].vehicle; if (refStyles.colors != null && refStyles.colors.GetType() == typeof(JArray)) { foreach (var iColor in refStyles.colors) { var color = iColor.color; var colorRefObject = new ColorReferenceEntity(); if (color.category != null && color.category == "Exterior") { resultVehicleEntity.ExternalColor = new Color { Code = color.code ?? string.Empty, //Base = color.exterior.base ?? string.Empty, Name = color.name ?? string.Empty, RgbHexCode = color.RGBHexCode ?? string.Empty }; } if (color.category != null && color.category == "Interior") { resultVehicleEntity.InternalColor = new Color { Code = color.code ?? string.Empty, //Base = color.exterior.base ?? string.Empty, Name = color.name ?? string.Empty, RgbHexCode = color.RGBHexCode ?? string.Empty }; } } } #endregion Get Color from response } } if(result.error != null && result.error.message != null) { _logger.AppendMessages("Create Vehicle call unsuccessful"); throw new Exception(result.error.message); } } #endregion Create Vehicle Call } catch(Exception ex) { eventLogType = EventLogEntryType.Error; _logger.AppendMessages(string.Format("Error - {0}.", ex)); } finally { _logger.LogAppendedMessages(eventLogType); } return resultVehicleEntity; }
private string GetDetailerRequestPayload(VehicleEntity newVehicle) { string colorPayload = "\"colors\":[{0}]", optionsPayload = null; var basicVehiclePayload = "\"vin\":\"" + (newVehicle.Vin ?? string.Empty) + "\",\"year\":" + (newVehicle.Year.ToString() ?? string.Empty) + ",\"make\":{\"id\":" + (newVehicle.MakeId ?? string.Empty) + ",\"label\":\"" + (newVehicle.Make ?? string.Empty) + "\"},\"model\":{\"id\":" + (newVehicle.ModelId ?? string.Empty) + ",\"label\":\"" + (newVehicle.Model ?? string.Empty) + "\"},\"style\":{\"id\":" + (newVehicle.StyleId ?? string.Empty) + ",\"label\":\"" + (newVehicle.Style ?? string.Empty) + "\",\"trim\":\"" + (newVehicle.Trim ?? string.Empty) + "\"},\"oemModelCode\":\"" + (newVehicle.OEMCode ?? string.Empty) + "\"";//}]}"; if (newVehicle.ExternalColor != null && newVehicle.InternalColor != null) { //TODO: using name for base color too. might need to fix it. var twoColorsOfTheVehicle = string.Format("{{\"color\":{{\"category\":\"Exterior\",\"name\":\"{0}\",\"base\":\"{0}\",\"code\":\"{1}\"}} }},{{\"color\":{{\"code\":\"{2}\",\"name\":\"{2}\",\"category\":\"Interior\"}} }}", newVehicle.ExternalColor.Name ?? string.Empty, newVehicle.ExternalColor.Code ?? string.Empty, newVehicle.InternalColor.Code ?? string.Empty, newVehicle.InternalColor.Name ?? string.Empty); colorPayload = string.Format(colorPayload, twoColorsOfTheVehicle); } else { colorPayload = string.Format(colorPayload, string.Empty); } string factoryOptionsArray = string.Empty; if (newVehicle.Options != null) { factoryOptionsArray = string.Empty; foreach (var option in newVehicle.Options) { var factoryOptionEntity = string.Format("{{\"id\":4,\"optionCode\":\"{0}\",\"description\":\"{1}\"}}", option.OptionCode, option.Description); if(factoryOptionsArray == string.Empty) { factoryOptionsArray = factoryOptionEntity; } else { factoryOptionsArray = string.Format("{0},{1}", factoryOptionsArray, factoryOptionEntity); } } factoryOptionsArray = string.Format("\"factoryOptions\":[{0}]", factoryOptionsArray); } optionsPayload = string.Format("\"options\":{{{0}}}", factoryOptionsArray); return string.Format("{{\"vehicles\":[{{\"vehicle\":{{{0},{1},{2}}} }}]}}", basicVehiclePayload, colorPayload, optionsPayload); }
public VehicleEntity CreateVehicle(VehicleEntity newVehicle) { var resultVehicleEntity = new VehicleEntity(); EventLogEntryType eventLogType = EventLogEntryType.Information; try { #region Detailer Call var requestUri = ConfigurationManager.AppSettings["detailerUrl"]; dynamic detailerResponse = RestClient.PostData(requestUri, GetDetailerRequestPayload(newVehicle)); _logger.AppendMessages("Successfully completed Detailer call before Create Vehicle."); #endregion Detailer call #region Create Vehicle Call if (detailerResponse.vehicles != null && detailerResponse.vehicles.GetType() == typeof(JArray) && detailerResponse.vehicles[0] != null && detailerResponse.vehicles[0].vehicle != null) { JObject vehicleEntity = detailerResponse.vehicles[0].vehicle; vehicleEntity.Add("stockNumber", newVehicle.StockNumber ?? string.Empty); _logger.AppendMessages("Detailer found atleast one vehicle in response."); if (newVehicle.photoIds != null) { //?? _logger.AppendMessages("Number of PhotoIds: " + newVehicle.photoIds.Count()); //vehicleEntity.Add("assets", JObject.Parse("{\"dealerPhotos\": []}")); foreach (var id in newVehicle.photoIds) { if (id != null && !id.Equals(string.Empty) && !id.Equals(" ") && vehicleEntity.GetValue("assets") == null) { //?? _logger.AppendMessages("L"); vehicleEntity.Add("assets", JObject.Parse("{\"dealerPhotos\": [{\"id\":" + id + "}]}")); } } } if (vehicleEntity.GetValue("source") == null) { vehicleEntity.Add("source", "M"); } var createVehicleRequestPayload = string.Format("{{\"criteria\":{{\"vehicleContexts\":[{{\"vehicleContext\":{{\"vehicle\":{0},\"modifiedFields\":[\"assets\",\"bodyStyle\",\"bodyType\",\"certified\",\"colors.exterior.base\",\"colors.exterior.code\",\"colors.exterior.name\",\"colors.interior.code\",\"colors.interior.name\",\"createdDate\",\"descriptions\",\"doors\",\"drivetrain\",\"engine.aspiration\",\"engine.cylinders\",\"engine.description\",\"engine.displacement\",\"engine.fuelType\",\"engine.power\",\"id\",\"inventoryOwner\",\"lastModifiedDate\",\"lotDate\",\"make.Id\",\"make.label\",\"model.Id\",\"model.label\",\"odometer\",\"oemModelCode\",\"options.dealerOptions\",\"options.factoryOptions\",\"preOwned\",\"prices.discountPrice\",\"prices.internetPrice\",\"prices.invoicePrice\",\"prices.msrp\",\"prices.retailPrice\",\"prices.vendedPrice\",\"stockNumber\",\"style.Id\",\"style.trim\",\"transmission.speeds\",\"transmission.text\",\"transmission.type\",\"unmodifiable\",\"vin\",\"warranties\",\"year\"]}}}}],\"inventoryOwner\":\"gmps-kindred\",\"useSource\": true}}}}", vehicleEntity); _logger.AppendMessages(string.Format("Request payload for create vehicle call - {0}", createVehicleRequestPayload)); dynamic result = RestClient.PostData(ConfigurationManager.AppSettings["createVehicleUrl"], createVehicleRequestPayload); if (result != null && result.result != null) { result = result.result; if (result != null && result.status != null && result.status.GetType() == typeof(JArray) && result.status[0].vehicle != null) { _logger.AppendMessages(string.Format("Create Vehicle call successful . result {0} ", result)); resultVehicleEntity.Vin = result.status[0].vehicle.vin ?? string.Empty; resultVehicleEntity.StockNumber = result.status[0].vehicle.stockNumber ?? string.Empty; TryGetMake(result.status[0].vehicle, resultVehicleEntity); TryGetModel(result.status[0].vehicle, resultVehicleEntity); TryGetOemModelCode(result.status[0].vehicle, resultVehicleEntity); TryGetTrimAndStyle(result.status[0].vehicle, resultVehicleEntity); TryGetYear(result.status[0].vehicle, resultVehicleEntity); #region Get Color from response var refStyles = result.status[0].vehicle; if (refStyles.colors != null && refStyles.colors.GetType() == typeof(JArray)) { foreach (var iColor in refStyles.colors) { var color = iColor.color; var colorRefObject = new ColorReferenceEntity(); if (color.category != null && color.category == "Exterior") { resultVehicleEntity.ExternalColor = new Color { Code = color.code ?? string.Empty, //Base = color.exterior.base ?? string.Empty, Name = color.name ?? string.Empty, RgbHexCode = color.RGBHexCode ?? string.Empty }; } if (color.category != null && color.category == "Interior") { resultVehicleEntity.InternalColor = new Color { Code = color.code ?? string.Empty, //Base = color.exterior.base ?? string.Empty, Name = color.name ?? string.Empty, RgbHexCode = color.RGBHexCode ?? string.Empty }; } } } #endregion Get Color from response } } if (result.error != null && result.error.message != null) { _logger.AppendMessages("Create Vehicle call unsuccessful"); throw new Exception(result.error.message); } } #endregion Create Vehicle Call } catch (Exception ex) { eventLogType = EventLogEntryType.Error; _logger.AppendMessages(string.Format("Error - {0}.", ex)); } finally { _logger.LogAppendedMessages(eventLogType); } return(resultVehicleEntity); }