Example #1
0
 public static bool LoadFromFile(string fileName, out page obj)
 {
     System.Exception exception = null;
     return LoadFromFile(fileName, out obj, out exception);
 }
Example #2
0
 /// <summary>
 /// Deserializes xml markup from file into an page object
 /// </summary>
 /// <param name="fileName">string xml file to load and deserialize</param>
 /// <param name="obj">Output page object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool LoadFromFile(string fileName, out page obj, out System.Exception exception)
 {
     exception = null;
     obj = default(page);
     try {
         obj = LoadFromFile(fileName);
         return true;
     }
     catch (System.Exception ex) {
         exception = ex;
         return false;
     }
 }
Example #3
0
 /// <summary>
 /// Deserializes workflow markup into an page object
 /// </summary>
 /// <param name="xml">string workflow markup to deserialize</param>
 /// <param name="obj">Output page object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool Deserialize(string xml, out page obj, out System.Exception exception)
 {
     exception = null;
     obj = default(page);
     try {
         obj = Deserialize(xml);
         return true;
     }
     catch (System.Exception ex) {
         exception = ex;
         return false;
     }
 }
Example #4
0
 public static bool Deserialize(string xml, out page obj)
 {
     System.Exception exception = null;
     return Deserialize(xml, out obj, out exception);
 }
Example #5
0
        private void readerToResult(SqlDataReader reader, Object Result, List <ProjectFlx.Schema.field> Fields, ProjectFlx.Schema.paging Paging)
        {
            int currentpage = Paging.pages.current == 0 ? 1 : Paging.pages.current;

            // TODO: fill available pages to dropdown

            switch (Paging.direction)
            {
            case ProjectFlx.Schema.pagingDirectionType.next:
                currentpage++;
                break;

            case ProjectFlx.Schema.pagingDirectionType.previous:
                currentpage--;
                break;

            case ProjectFlx.Schema.pagingDirectionType.top:
                currentpage = 1;
                break;

            case ProjectFlx.Schema.pagingDirectionType.last:
                currentpage = 99;          // TODO: last page
                break;
            }
            if (currentpage < 1)
            {
                currentpage = 1;
            }

            Paging.pages.current = currentpage;

            int resultfrom = ((currentpage * Paging.limit) - Paging.limit) + 1;
            int resultto   = resultfrom + Paging.limit;

            // keep copy of previous page results incase resultsfrom exceeds total pages
            // return last know page
            ProjectFlx.Schema.result prevresult = new ProjectFlx.Schema.result();

            int readercount = 0;

            while (reader.Read())
            {
                // enforce paging results
                readercount++;
                if ((readercount >= resultfrom && readercount < (resultto)) || Paging.limit.Equals(-1))
                {
                    ProjectFlx.Schema.row r;
                    if (Result.GetType().Equals(typeof(ProjectFlx.Schema.subresult)))
                    {
                        ((ProjectFlx.Schema.subresult)Result).row.Add(r = new ProjectFlx.Schema.row());
                    }
                    else
                    {
                        ((ProjectFlx.Schema.result)Result).row.Add(r = new ProjectFlx.Schema.row());
                    }

                    XmlDocument       xm         = new XmlDocument();
                    List <XmlElement> innerNodes = new List <XmlElement>();

                    // empty fieldset by default grab all
                    if (Fields.Count == 0)
                    {
                        for (int fld = 0; fld < reader.FieldCount; fld++)
                        {
                            string       fname = String.Format("field_{0:d3}", fld);
                            XmlAttribute att   = xm.CreateAttribute(fname);
                            att.Value = reader[fld].ToString();
                            r.AnyAttr.Add(att);
                        }
                    }


                    foreach (ProjectFlx.Schema.field f in Fields)
                    {
                        XmlAttribute att = xm.CreateAttribute(f.name);
                        if (f.type == Schema.fieldType.json || f.type == Schema.fieldType.tryjson)
                        {
                            string json = reader[f.name].ToString().TrimEnd();
                            try
                            {
                                if (String.IsNullOrEmpty(json))
                                {
                                    json = "{}";
                                }
                                else
                                {
                                    Newtonsoft.Json.Linq.JObject.Parse(json);
                                }

                                string jsonDetails = String.Format("{{\"{0}\":{1}}}", f.name, json);
                                var    innerXml    = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonDetails);
                                innerNodes.Add((XmlElement)innerXml.DocumentElement);
                            }
                            catch
                            {
                                if (f.type == Schema.fieldType.tryjson)
                                {
                                    json = reader[f.name].ToString().TrimEnd();
                                }
                                else
                                {
                                    json = "{\"error\":\"" + "Invalid Json Object field: " + f.name + "\"}";
                                }
                            }
                            att.Value = json;
                        }
                        else
                        {
                            try
                            {
                                string val = null;
                                if (f.encode.HasValue())
                                {
                                    val = reader[f.encode].ToString();
                                    val = System.Web.HttpUtility.UrlEncode(val.TrimEnd()).Replace("+", "%20");
                                }
                                else if (f.regx_field.HasValue())
                                {
                                    val = reader[f.regx_field].ToString();
                                    val = Regex.Replace(val, Schema.Helper.FlattenList(f.regx), f.regx_replace);
                                }
                                else
                                {
                                    val = reader[f.name].ToString();
                                }


                                if (val != null)
                                {
                                    att.Value = safeXmlCharacters(val);
                                }
                            }
                            catch (IndexOutOfRangeException handled)
                            {
                                att.Value = "-field not found-";
                            }
                        }

                        r.AnyAttr.Add(att);
                    }

                    // append inner nodes (usually from json field conversion)
                    for (int i = 0; i < innerNodes.Count; i++)
                    {
                        r.Any.Add(innerNodes[i]);
                    }
                }

                // save previous page results incase resultfrom > readercount, then return last page results
                int prevresultfrom = resultfrom - Paging.limit;
                int prevresultto   = resultto - Paging.limit;

                if (prevresultfrom > 0)
                {
                    if ((readercount >= prevresultfrom && readercount < (prevresultto)) && Paging.limit > 0)
                    {
                        ProjectFlx.Schema.row r;
                        prevresult.row.Add(r = new ProjectFlx.Schema.row());
                        XmlDocument xm = new XmlDocument();

                        foreach (ProjectFlx.Schema.field f in Fields)
                        {
                            XmlAttribute att = xm.CreateAttribute(f.name);
                            if (f.encode != null)
                            {
                                att.Value = System.Web.HttpUtility.UrlEncode(reader[f.encode].ToString().TrimEnd());
                            }
                            else
                            {
                                att.Value = reader[f.name].ToString().TrimEnd();
                            }

                            r.AnyAttr.Add(att);
                        }
                    }
                }
            }

            // check to see if we are trying to read records beyond recordset and
            // return last page results if we are
            if (resultfrom > readercount && Paging.limit != -1)
            {
                currentpage--;
                if (currentpage > 0)
                {
                    Paging.pages.current = currentpage;
                }

                // copy previous results to Result
                ProjectFlx.Schema.row r;
                foreach (ProjectFlx.Schema.row r2 in prevresult.row)
                {
                    if (Result.GetType().Equals(typeof(ProjectFlx.Schema.subresult)))
                    {
                        ((ProjectFlx.Schema.subresult)Result).row.Add(r = new ProjectFlx.Schema.row());
                    }
                    else
                    {
                        ((ProjectFlx.Schema.result)Result).row.Add(r = new ProjectFlx.Schema.row());
                    }

                    foreach (XmlAttribute att in r2.AnyAttr)
                    {
                        r.AnyAttr.Add(att);
                    }
                }
            }

            // get pages available for reader
            int pagescount = (readercount / Paging.limit) + 1;

            Paging.pages.totalpages   = pagescount;
            Paging.pages.totalrecords = readercount;

            for (int x = 1; x <= pagescount; x++)
            {
                ProjectFlx.Schema.page pg = new ProjectFlx.Schema.page();
                pg.value = x;
                pg.from  = ((Paging.limit * x) - Paging.limit) + 1;

                // upper limit page to value is difference of total reader count
                if (x.Equals(pagescount))
                {
                    pg.to = readercount;
                }
                else
                {
                    pg.to = (Paging.limit * x);
                }

                Paging.pages.page.Add(pg);
            }
        }