private dynamic ConvertReaXmlToJson(IEnumerable<KeyValuePair<string, string>> contents) { string lastFile = null; try { var results = new Dictionary<string, ConvertToResult>(); foreach (var content in contents) { lastFile = content.Key; var convertToResult = _reaXmlTransmorgrifier.ConvertTo(content.Value, true); if (convertToResult != null) { results.Add(content.Key, convertToResult); } } var viewModel = new ConvertViewModel(); foreach (var convertToResult in results) { CopyToViewModel(convertToResult, viewModel); } return Response.AsJson(viewModel); } catch (Exception exception) { return Response.AsText( string.Format("Failed to convert the ReaXml to OpenRealEstate json. File: {0}. Error message: {1}.", string.IsNullOrEmpty(lastFile) ? "--no filename--" : lastFile, exception.InnerException != null ? exception.InnerException.Message : exception.Message)) .WithStatusCode(HttpStatusCode.InternalServerError); } }
private static void CopyToViewModel(KeyValuePair<string, ConvertToResult> convertToResultKeyValuePair, ConvertViewModel viewModel) { if (convertToResultKeyValuePair.Value == null) { throw new ArgumentNullException("convertToResultKeyValuePair"); } if (viewModel == null) { throw new ArgumentNullException("viewModel"); } var errors = new List<ValidationError>(); var listings = convertToResultKeyValuePair.Value.Listings != null ? convertToResultKeyValuePair.Value.Listings.Select(x => x.Listing).ToList() : null; var invalidData = convertToResultKeyValuePair.Value.Errors != null ? convertToResultKeyValuePair.Value.Errors.Select(x => x.ExceptionMessage).ToList() : null; var unhandledData = convertToResultKeyValuePair.Value.UnhandledData != null ? convertToResultKeyValuePair.Value.UnhandledData.Select(x => x).ToList() : null; if (listings != null && listings.Any()) { foreach (var listing in listings) { // We only do strict validation (the bare minimum needed to store a listing) if this is current. var validationResults = ValidatorMediator.Validate(listing, listing.StatusType == StatusType.Current); if (validationResults.Errors != null && validationResults.Errors.Any()) { errors.AddRange(ValidationError.ConvertToValidationErrors(listing.ToString(), validationResults.Errors)); } } if (viewModel.Listings == null) { viewModel.Listings = new List<Listing>(); } viewModel.Listings.AddRange(listings); } if (viewModel.ValidationErrors == null) { viewModel.ValidationErrors = new Dictionary<string, string>(); } if (invalidData != null && invalidData.Any()) { CreateError(viewModel.ValidationErrors, convertToResultKeyValuePair.Key, invalidData); } if (unhandledData != null && unhandledData.Any()) { var errorList = new [] {string.Format("Found: {0} unhandled data segments", unhandledData.Count)}; CreateError(viewModel.ValidationErrors, convertToResultKeyValuePair.Key, errorList); } if (errors.Any()) { var convertedErrors = ConvertErrorsToDictionary(convertToResultKeyValuePair.Key, errors); foreach (var convertedError in convertedErrors) { viewModel.ValidationErrors.Add(convertedError); } } }