public static LexileDto lexDto(LexileDump lex) { LexileDto lexileDto = new LexileDto(); lexileDto.Title = lex.title; lexileDto.Author = lex.author; lexileDto.Isbn = lex.isbn; lexileDto.Isbn13 = lex.isbn13; lexileDto.LexScore = short.Parse(lex.lexile); lexileDto.PageCount = (short?)short.Parse(lex.pages); lexileDto.LexUpdate = Convert.ToDateTime(lex.lastupdated); return lexileDto; }
////////////////////////////////////////////////////////////////////////////// public static LexileDto lexDto(LexileTitle lex) { LexileDto lexileDto = new LexileDto(); lexileDto.Title = lex.Title; lexileDto.Author = lex.Author; lexileDto.Isbn = lex.Isbn; lexileDto.Isbn13 = lex.Isbn13; lexileDto.LexScore = short.Parse(lex.Lexile); lexileDto.PageCount = (short?)lex.Pages; lexileDto.LexUpdate = (DateTime)lex.LastUpdated; return lexileDto; }
// Set the log source //private readonly ILogger log = LogManager.Instance().GetLogger(typeof(BookService)); private bool LexileRecordVerified(LexileDto lexileDto) { var isbn13Regex = @"(\d{13})"; //needs to be a 13 digit character var r = new Regex(isbn13Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline); var m = r.Match(lexileDto.Isbn13.ToString()); if (m.Success /*&& lexileDto.Isbn.Length == 10*/) { if (lexileDto.Isbn.Length != 10) lexileDto.Isbn = null; var notAllowed = new Hashtable(); notAllowed["DocType"] = new List<object> { "None" }; notAllowed["Series"] = new List<object> { "None" }; notAllowed["Awards"] = new List<object> { "" }; notAllowed["Author"] = new List<object> { "" }; notAllowed["LexCode"] = new List<object> { "" }; notAllowed["Summary"] = new List<object> { "" }; notAllowed["PageCount"] = new List<object> { 0 }; notAllowed["Isbn"] = new List<object> { "None" }; var dtoMembers = lexileDto.GetType().GetMembers(); foreach (var member in dtoMembers) { var memberName = member.Name; var property = lexileDto.GetType().GetProperty(memberName); if (property == null) continue; var propertyValue = property.GetValue(lexileDto); var badValueList = notAllowed[memberName]; if (badValueList == null) continue; if (((List<object>)badValueList).Contains(propertyValue)) property.SetValue(lexileDto, null); } return true; } return false; }
/// <summary> /// Adds or updates a single book in the books database /// </summary> /// <param name="lexileDto">book to add</param> /// <returns></returns> public ResultDto PostLexileData(LexileDto lexileDto) { if (!LexileRecordVerified(lexileDto)) return new ResultDto { ResultDescription = "bad object" }; var result = new ResultDto { ResultDescription = "" }; Mapper.CreateMap<LexileDto, SkillRecord>(); using (var context = new BookcaveEntities()) { //var recordQuery = context.SkillRecords.Where(book => book.Isbn13.Equals(lexileDto.Isbn13)); var recordQuery = context.Database.SqlQuery<SkillRecord>("Select * from SkillRecords where Isbn13 = {0}", lexileDto.Isbn13); //var currentSkillRecord = skillRecords.FirstOrDefault(); var skillRecords = new List<SkillRecord>(recordQuery); SkillRecord currentSkillRecord = null; if (skillRecords.Count > 0) currentSkillRecord = skillRecords.First(); if (currentSkillRecord != null) { var modifiedSkillRecord = Mapper.Map<LexileDto, SkillRecord>(lexileDto, currentSkillRecord); var aggregateSkill = DataFunctions.GetAverageSkillAge(modifiedSkillRecord); context.Database.ExecuteSqlCommand ( "update SkillRecords set LexScore={0},LexCode={1},LexUpdate={2},AverageSkillAge={3} where Isbn13={4}", lexileDto.LexScore, lexileDto.LexCode, lexileDto.LexUpdate, aggregateSkill, lexileDto.Isbn13 ); result.ResultDescription += lexileDto.Isbn13 + " skills updated "; } else { var newSkillRecord = Mapper.Map<LexileDto, SkillRecord>(lexileDto); newSkillRecord.AverageSkillAge = DataFunctions.GetAverageSkillAge(newSkillRecord); context.SkillRecords.Add(newSkillRecord); result.ResultDescription += lexileDto.Isbn13 + " skills added "; } Mapper.CreateMap<LexileDto, BookRecord>(); var bookQuery = context.Database.SqlQuery<BookRecord>("Select * from BookRecords where Isbn13 = {0}", lexileDto.Isbn13); //var bookQuery = context.BookRecords.Where(book => book.Isbn13.Equals(lexileDto.Isbn13)); var bookRecords = new List<BookRecord>(bookQuery); BookRecord currentBookRecord = null; if (bookRecords.Count > 0) currentBookRecord = bookRecords[0]; //var currentBookRecord = bookRecords.FirstOrDefault(); if (currentBookRecord != null) { //currentBookRecord = Mapper.Map<LexileDto, BookRecord>(lexileDto, currentBookRecord); context.Database.ExecuteSqlCommand ( "update BookRecords set Title={0},Author={1},Publisher={2},PageCount={3},DocType={4},Series={5},Awards={6},Summary={7} where Isbn13={8}", lexileDto.Title, lexileDto.Author, lexileDto.Publisher, lexileDto.PageCount, lexileDto.DocType, lexileDto.Series, lexileDto.Awards, lexileDto.Summary, lexileDto.Isbn13 ); context.SaveChanges(); result.ResultDescription += lexileDto.Isbn13 + " books added "; } else { var newBookRecord = Mapper.Map<LexileDto, BookRecord>(lexileDto); context.BookRecords.Add(newBookRecord); context.SaveChanges(); result.ResultDescription += lexileDto.Isbn13 + " books added "; } //try //{ //} //catch (DbEntityValidationException e) { Console.WriteLine(e.Message); } //catch (DbUpdateException e) { Console.WriteLine(e.Message); } } return result; }
public static StandardResult postNewLexile(LexileDto lexileDto) { using (WebClient webClient = new WebClient()) { webClient.Headers["Content-type"] = "application/json"; var memoryStream = new MemoryStream(); var serializedJson = new DataContractJsonSerializer(typeof(LexileDto)); serializedJson.WriteObject(memoryStream, lexileDto); var uri = new Uri(@"http://apps.apprendacloud.com/api/services/json/r/bookcavetest(v1)/BookService/IBook/books"); byte[] res1 = webClient.UploadData(uri.ToString(), "POST", memoryStream.ToArray()); return standardResult(res1, lexileDto.Isbn13); } }
public void PostLexileData() { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. //using (var sr = new StreamReader(@"C:\Users\tiliska\Documents\problematiclexile.txt")) using (var sr = new StreamReader(@"C:\Users\tiliska\Documents\lexiletitles.txt")) { sr.ReadLine();//eat first line string svLine; var line = 1; using (var webClient = new WebClient()) { var sourceCode = "lexile"; if (useCloud) uri = new Uri(cloudUrl + sourceCode); else uri = new Uri(localUrl + sourceCode); // Read and display lines from the file until the end of // the file is reached. while ((svLine = sr.ReadLine()) != null) { line++; if (svLine.StartsWith("#")) { Console.WriteLine("still a # on line " + line); continue; } var bookParams = svLine.Split('\t'); var lexileDto = new LexileDto(); lexileDto.Title = bookParams[0]; lexileDto.Author = bookParams[1]; try { lexileDto.Isbn = bookParams[2]; try { lexileDto.Isbn13 = bookParams[3]; if (bookParams[5].Length > 0) lexileDto.LexScore = Convert.ToInt16(bookParams[5]); if (bookParams[7].Length > 0) lexileDto.PageCount = Convert.ToInt16(bookParams[7]); lexileDto.LexUpdate = Convert.ToDateTime(bookParams[12]); } catch (FormatException) { Console.WriteLine("metametrics messed up the row in the text db on line " + line); } lexileDto.LexCode = bookParams[4]; lexileDto.Publisher = bookParams[6]; lexileDto.DocType = bookParams[8]; lexileDto.Series = bookParams[9]; lexileDto.Awards = bookParams[10]; lexileDto.Summary = bookParams[11]; if (post) { //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); webClient.Headers["Content-type"] = "application/json"; var memoryStream = new MemoryStream(); var serializedJson = new DataContractJsonSerializer(typeof(LexileDto)); serializedJson.WriteObject(memoryStream, lexileDto); byte[] res1 = webClient.UploadData(uri.ToString(), "POST", memoryStream.ToArray()); } else { var service = new BookService(); service.PostLexileData(lexileDto); } //else if (method.Equals("skillmetrics")) //{ // var service = new BookService(); // var skillAggregate = service.GetSkillMetrics(lexileDto.Isbn13); //} } catch (IndexOutOfRangeException) { Console.WriteLine("metametrics messed up the row in the text db on line " + line); } }//while }//using }//using }