Beispiel #1
0
        public ResultDto PostScholasticData(ScholasticDto scholasticDto)
        {
            scholasticDto.GuidedReading = scholasticDto.GuidedReading.Trim();
            scholasticDto.Dra = scholasticDto.Dra.Trim();

            var verificationResult = VerifyScholasticDto(scholasticDto);

            //if (verificationResult.ResultCode < 0)
            //    return verificationResult;
            var result = new ResultDto { ResultDescription = "" };
            using (var context = new BookcaveEntities())
            {
                var bookRecords = context.Database.SqlQuery<BookRecord>("Select * from BookRecords where Isbn13 = {0}", scholasticDto.Isbn13);
                if (bookRecords.FirstOrDefault() == null)
                    return new ResultDto { ResultDescription = "no general info exists yet for " + scholasticDto.Isbn13 };

                var skillRecords = context.Database.SqlQuery<SkillRecord>("Select * from SkillRecords where Isbn13 = {0}", scholasticDto.Isbn13);
                var currentSkillRecord = skillRecords.FirstOrDefault();
                Mapper.CreateMap<ScholasticDto, SkillRecord>();

                if (currentSkillRecord != null)
                {
                    var modifiedSkillRecord = Mapper.Map<ScholasticDto, SkillRecord>(scholasticDto, currentSkillRecord);
                    var averageSkillAge = DataFunctions.GetAverageSkillAge(modifiedSkillRecord);
                    context.Database.ExecuteSqlCommand("update SkillRecords set ScholasticGrade={0},Dra={1},GuidedReading={2},AverageSkillAge={3} where Isbn13={4}",
                        scholasticDto.ScholasticGrade,
                        scholasticDto.Dra,
                        scholasticDto.GuidedReading,
                        averageSkillAge,
                        scholasticDto.Isbn13);
                    result.ResultDescription += scholasticDto.Isbn13 + " skills updated ";
                }
                else
                {
                    var newSkillRecord = Mapper.Map<ScholasticDto, SkillRecord>(scholasticDto);
                    newSkillRecord.AverageSkillAge = DataFunctions.GetAverageSkillAge(newSkillRecord);
                    context.SkillRecords.Add(newSkillRecord);
                    result.ResultDescription += scholasticDto.Isbn13 + " skills added ";
                }

                var contentRecords = context.Database.SqlQuery<ContentRecord>("Select * from ContentRecords where Isbn13 = {0}", scholasticDto.Isbn13);
                var currentContentRecord = contentRecords.FirstOrDefault();
                Mapper.CreateMap<ScholasticDto, ContentRecord>();

                if (currentContentRecord != null)
                {
                    var modifiedContentRecord = Mapper.Map<ScholasticDto, ContentRecord>(scholasticDto, currentContentRecord);
                    var averageContentAge = DataFunctions.GetAverageContentAge(modifiedContentRecord);
                    context.Database.ExecuteSqlCommand("update ContentRecords set ScholasticGradeLower={0}, ScholasticGradeHigher={1},AverageContentAge={2} where Isbn13={3}",
                        scholasticDto.ScholasticGradeLower,
                        scholasticDto.ScholasticGradeHigher,
                        averageContentAge,
                        scholasticDto.Isbn13);
                    context.SaveChanges();
                    result.ResultDescription += scholasticDto.Isbn13 + " contents updated";
                }
                else
                {
                    var newContentRecord = Mapper.Map<ScholasticDto, ContentRecord>(scholasticDto);

                    context.ContentRecords.Add(newContentRecord);
                    context.SaveChanges();
                    result.ResultDescription += scholasticDto.Isbn13 + " contents added";
                }
                //try
                //{
                //}
                //catch (DbEntityValidationException e) { Console.WriteLine(e.Message); }
                //catch (DbUpdateException e) { Console.WriteLine(e.Message); }
            }
            return result;
        }
Beispiel #2
0
        private ResultDto VerifyScholasticDto(ScholasticDto scholasticDto)
        {
            if (scholasticDto.GuidedReading.Length != 1 || !Char.IsLetter(Convert.ToChar(scholasticDto.GuidedReading)))
                //return new ResultDto { ResultDescription = "incorrect format for guided reading level property", ResultCode = -2 };
                scholasticDto.GuidedReading = null;

            var re1 = "(\\d+)";	// lower dra bound
            var re2 = "(-)";	// hyphen
            var re3 = "(\\d+)";	// upper dra bound

            var regexScoreRange = new Regex(re1 + re2 + re3, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var matchScoreRange = regexScoreRange.Match(scholasticDto.Dra);
            var regexScore = new Regex("(\\d+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var matchScore = regexScore.Match(scholasticDto.Dra);

            if (!matchScoreRange.Success && !matchScore.Success) //if the parameter is a range
                scholasticDto.Dra = null;
            //return new ResultDto { ResultDescription = "incorrect dra format ", ResultCode = -1 };

            var notAllowed = new Hashtable();
            notAllowed["ScholasticGradeLower"] = new List<object> { "" };

            var dtoMembers = scholasticDto.GetType().GetMembers();

            foreach (var member in dtoMembers)
            {
                var memberName = member.Name;
                var property = scholasticDto.GetType().GetProperty(memberName);

                if (property == null)
                    continue;
                var propertyValue = property.GetValue(scholasticDto);
                var badValueList = notAllowed[memberName];

                if (badValueList == null)
                    continue;
                if (((List<object>)badValueList).Contains(propertyValue))
                    property.SetValue(scholasticDto, null);
            }

            return new ResultDto { ResultDescription = "verified" };
        }
        public static string loadschdata(ScholasticDto schdata)
        {
            string msg = "(Unable to load to local db)";
            // receive schdata, load to database

            UTF8Encoding enc = new System.Text.UTF8Encoding();
            byte[] bs = new byte[1]{(byte)schdata.ScholasticGradeHigher};

            try
            {
                using (LexileTitlesEntities lr = new LexileTitlesEntities())
                {
                    List<Scholastic> existing = new List<Scholastic>(from s in lr.Scholastics
                                                                     where s.Isbn13 == schdata.Isbn13
                                                                     select s);

                    if (existing.Count == 0)
                    {
                        // create new BarnesAndNoble
                        Scholastic sch = new Scholastic();
                        sch.Isbn13 = schdata.Isbn13;
                        sch.InterestLowGrade = schdata.ScholasticGradeLower;

                        sch.InterestHighGrade = enc.GetString(bs);
                        sch.GradeEquiv = (decimal)schdata.ScholasticGrade;
                        sch.DRA = schdata.Dra;
                        sch.GuidedReading = schdata.GuidedReading;
                        // genre TBD

                        lr.Scholastics.Add(sch);
                        lr.SaveChanges();

                        msg = "(Loaded new SCH to DB)";
                    }
                    else
                    {
                        // update fields on existing BarnesAndNoble

                        existing[0].InterestLowGrade = schdata.ScholasticGradeLower;
                        existing[0].InterestHighGrade = enc.GetString(bs);
                        existing[0].GradeEquiv = (decimal)schdata.ScholasticGrade;
                        existing[0].DRA = schdata.Dra;
                        existing[0].GuidedReading = schdata.GuidedReading;

                        lr.SaveChanges();

                        msg = "(Updated existing SCH in DB)";
                    }
                }
            }
            catch { }

            return msg;
        }
        public static List<StandardResult> getschdata(List<TitleIsbn13> TitleIsbn13s, string action = "post")
        {
            List<StandardResult> resultlist = new List<StandardResult>();

            using (WebClient client = new WebClient())
            {
                //loop over each incoming isbn/title pair
                foreach (TitleIsbn13 ti in TitleIsbn13s)
                {

                    //instantiate new sch_data class
                    ScholasticDto schdata = new ScholasticDto();

                    schdata.Isbn13 = ti.isbn13;

                    //take a title string, return a schdata object
                    //title should be parsed into format title-of-book

                    string urltitle = lib.cleantitle(ti.title);

                    //create url for sch with incoming isbn
                    string url = "http://www.scholastic.com/teachers/book/" + urltitle;

                    //add user agent
                    client.Headers.Add("user-agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
                    string html = client.DownloadString(url);
                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(html);

                    //parse through the html
                    if (doc != null)
                    {
                        HtmlAgilityPack.HtmlNode detailnode = doc.DocumentNode.SelectSingleNode("//div[@id='bookdetail_attrib']");

                        string interestrange = detailnode.SelectSingleNode("//div[@class='col_1']//span[1]").InnerHtml;

                        string gradepattern = "Grade";
                        Match isgrade = Regex.Match(interestrange, gradepattern, RegexOptions.IgnoreCase);

                        string intPattern = "(\\d+)";
                        MatchCollection intMatches = Regex.Matches(interestrange, intPattern);

                        if (isgrade.Success)
                        {
                            string kpattern = "K";
                            Match kindergarten = Regex.Match(interestrange, kpattern);

                            if (intMatches.Count == 1 && kindergarten.Success)
                            {
                                schdata.ScholasticGradeLower = "K";
                                schdata.ScholasticGradeHigher = byte.Parse(intMatches[0].Value);
                            }
                            else if (intMatches.Count > 1)
                            {
                                schdata.ScholasticGradeLower = intMatches[0].Value;
                                schdata.ScholasticGradeHigher = byte.Parse(intMatches[1].Value);
                            }
                        }

                        else
                        {

                            MatchCollection iMatches = Regex.Matches(interestrange, intPattern);

                            if (iMatches.Count == 1)
                            {
                                int age = int.Parse(iMatches[0].Value);
                                if (age < 10) { schdata.ScholasticGradeHigher = byte.Parse(iMatches[0].Value); }
                                else { schdata.ScholasticGradeLower = iMatches[0].Value; }
                            }
                            else if (iMatches.Count > 1)
                            {
                                schdata.ScholasticGradeLower = iMatches[0].Value;
                                schdata.ScholasticGradeHigher = byte.Parse(iMatches[1].Value);
                            }
                        }

                        HtmlAgilityPack.HtmlNode levelnode = detailnode.SelectSingleNode(".//div[@class='col_2']");

                        //string gradestring = levelnode.SelectSingleNode(".//span[1]").InnerHtml;
                        HtmlAgilityPack.HtmlNode grade = doc.DocumentNode.SelectSingleNode("//div[@id='bookdetail_attrib']//div[@class='col_2']//span[1]");

                        HtmlAgilityPack.HtmlNode paradra = doc.DocumentNode.SelectSingleNode("//div[@id='bookdetail_attrib']//div[@class='col_2']//p[4]");

                        HtmlAgilityPack.HtmlNode dra = paradra.LastChild;

                        HtmlAgilityPack.HtmlNode paragr = doc.DocumentNode.SelectSingleNode("//div[@id='bookdetail_attrib']//div[@class='col_2']//p[5]");

                        HtmlAgilityPack.HtmlNode gr = paragr.LastChild;

                        try
                        {
                            string gradestring = grade.InnerHtml;
                            schdata.ScholasticGrade = double.Parse(gradestring);
                        }
                        catch { }

                        try
                        {
                            //schdata.dra = levelnode.SelectSingleNode(".//span[3]").InnerHtml;
                            schdata.Dra = dra.InnerHtml;
                        }
                        catch { }

                        try
                        {
                            //schdata.guidedreading = levelnode.SelectSingleNode(".//span[4]").InnerHtml;
                            schdata.GuidedReading = gr.InnerHtml;
                        }
                        catch { }

                    }

                    StandardResult result = new StandardResult();
                    if (action == "post" || action == "both")
                    {
                        //post new csmdata to BookCaveSvc
                        result = BookCaveSvc.postScholastic(schdata);

                    }

                    if (action == "loaddb" || action == "both")
                    {
                        string msg = loadschdata(schdata);

                        if (result.isbn13 == null)
                        {
                            result.isbn13 = schdata.Isbn13;
                            result.message = msg;
                        }

                        else
                        {
                            string postmsg = result.message;
                            result.message = postmsg + " " + msg;
                        }

                    }

                    resultlist.Add(result);
                }

            }
            return resultlist;
        }
        public static List<byte[]> postScholastic(List<Scholastic> schs)
        {
            List<byte[]> resultset = new List<byte[]>();
            foreach (Scholastic sch in schs)
            {
                // map bn to BarnesDto
                var schDto = new ScholasticDto();

                schDto.Isbn13 = sch.Isbn13;
                schDto.ScholasticGrade = (double)sch.GradeEquiv;
                schDto.ScholasticGradeLower = sch.InterestLowGrade;
                schDto.ScholasticGradeHigher = byte.Parse(sch.InterestHighGrade);
                schDto.Dra = sch.DRA;
                schDto.GuidedReading = sch.GuidedReading;

                using (WebClient webClient = new WebClient())
                {
                    webClient.Headers["Content-type"] = "application/json";

                    var memoryStream = new MemoryStream();
                    var serializedJson = new DataContractJsonSerializer(typeof(ScholasticDto));

                    serializedJson.WriteObject(memoryStream, schDto);

                    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());

                    resultset.Add(res1);
                }

            }

            return resultset;
        }
        public static StandardResult postScholastic(ScholasticDto schDto)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers["Content-type"] = "application/json";

                var memoryStream = new MemoryStream();
                var serializedJson = new DataContractJsonSerializer(typeof(ScholasticDto));

                serializedJson.WriteObject(memoryStream, schDto);

                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());

                StandardResult stdres = standardResult(res1, schDto.Isbn13);
                return stdres;
            }
        }
        public void PostScholasticData()
        {
            // 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\sch-output2.csv"))
            {
                var sourceCode = "scholastic";
                if (useCloud)
                    uri = new Uri(cloudUrl + sourceCode);
                else
                    uri = new Uri(localUrl + sourceCode);

                sr.ReadLine();
                string svLine;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((svLine = sr.ReadLine()) != null)
                {
                    var scholasticData = svLine.Split(',');
                    var scholasticDto = new ScholasticDto();

                    //isbn13
                    var isbn13 = scholasticData[0];
                    scholasticDto.Isbn13 = isbn13;

                    var service = new BookService();

                    //scholastic lower interest grade
                    var scholasticGradeLower = scholasticData[1];
                    scholasticDto.ScholasticGradeLower = scholasticGradeLower.Trim();

                    //scholastic higher interest grade
                    var scholasticGradeHigher = scholasticData[2];

                    if (scholasticGradeHigher.Trim().Length > 0) scholasticDto.ScholasticGradeHigher = (byte?)Convert.ToByte(scholasticGradeHigher);

                    //scholastic equivalent grade
                    try
                    {
                        var scholasticGrade = scholasticData[3];
                        scholasticDto.ScholasticGrade = Convert.ToDouble(scholasticGrade);
                    }
                    catch (FormatException) { Console.WriteLine("parameter not found"); }

                    //dra
                    var dra = scholasticData[4];
                    scholasticDto.Dra = dra;

                    //guided reading level
                    var guidedReading = scholasticData[5];
                    scholasticDto.GuidedReading = guidedReading;

                    if (post)
                    {
                        //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

                        var webClient = new WebClient();
                        webClient.Headers["Content-type"] = "application/json";

                        var memoryStream = new MemoryStream();
                        var serializedJson = new DataContractJsonSerializer(typeof(ScholasticDto));

                        serializedJson.WriteObject(memoryStream, scholasticDto);

                        try
                        {
                            byte[] res1 = webClient.UploadData(uri.ToString(), "POST", memoryStream.ToArray());
                        }
                        catch (WebException) { Console.WriteLine(scholasticDto.Isbn13 + " doesn't have general info"); }
                    }
                    else
                        service.PostScholasticData(scholasticDto);

                    //case "skillmetrics":
                    //    service.GetSkillMetrics(isbn13);
                    //    break;

                    //case "contentmetrics":
                    //    service.GetContentMetrics(isbn13);
                    //    break;
                }
            }
        }