Advertisement
Sonic3R

TL+DL

Oct 21st, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.17 KB | None | 0 0
  1. /* TRANSFER LAYER */
  2.  
  3.  
  4.    public class LinksCtrlDto
  5.    {
  6.       /// <summary>
  7.       /// url link
  8.       /// </summary>
  9.       private string _url;
  10.  
  11.       /// <summary>
  12.       /// name of link
  13.       /// </summary>
  14.       private string _name;
  15.  
  16.       /// <summary>
  17.       /// description of string
  18.       /// </summary>
  19.       private string _description;
  20.  
  21.       /// <summary>
  22.       /// constructor for LinksCtrlDto
  23.       /// </summary>
  24.       /// <param name="url"></param>
  25.       /// <param name="name"></param>
  26.       /// <param name="description"></param>
  27.       public LinksCtrlDto( string url, string name, string description ) {
  28.          
  29.          //only if url si not null/empty, name is not null/empty, and description is not null/empty
  30.          if ( string.IsNullOrEmpty( url ) )
  31.             throw new ArgumentNullException( "url" );
  32.          else
  33.             this._url = url;
  34.  
  35.          if ( string.IsNullOrEmpty( name ) )
  36.             throw new ArgumentNullException( "name" );
  37.          else
  38.             this._name = name;
  39.  
  40.          if ( string.IsNullOrEmpty( description ) )
  41.             throw new ArgumentNullException( "description" );
  42.          else
  43.             this._description = description;
  44.       }
  45.  
  46.       /// <summary>
  47.       /// Url property as getter
  48.       /// </summary>
  49.       public string Url {
  50.          get {
  51.             if ( string.IsNullOrEmpty( Url ) )
  52.                throw new ArgumentNullException( "Url" );
  53.             else
  54.                return this._url;
  55.          }        
  56.       }
  57.  
  58.       /// <summary>
  59.       /// Name property as getter
  60.       /// </summary>
  61.       public string Name {
  62.          get {
  63.             if ( string.IsNullOrEmpty( Name ) )
  64.                throw new ArgumentNullException( "Name" );
  65.             else
  66.                return this._name;
  67.          }
  68.       }
  69.  
  70.       /// <summary>
  71.       /// Description property as getter
  72.       /// </summary>
  73.       public string Description {
  74.          get {
  75.             if ( string.IsNullOrEmpty( Description ) )
  76.                throw new ArgumentNullException( "Description" );
  77.             else
  78.                return this._description;
  79.          }
  80.       }
  81.    }
  82.  
  83.  
  84.  
  85.    public class CategoriesCtrlDto
  86.    {
  87.       /// <summary>
  88.       /// Name of category
  89.       /// </summary>
  90.       private string _name;
  91.  
  92.       /// <summary>
  93.       /// List of links
  94.       /// </summary>
  95.       private List<LinksCtrlDto> _ListLink;
  96.  
  97.       /// <summary>
  98.       /// constructor
  99.       /// </summary>
  100.       /// <param name="name"></param>
  101.       /// <param name="Link"></param>
  102.       public CategoriesCtrlDto( string name, List<LinksCtrlDto> Link) {
  103.          if ( string.IsNullOrEmpty( name ) )
  104.             throw new ArgumentNullException( "name" );
  105.          else
  106.             this._name = name;
  107.  
  108.          if ( _ListLink == null )
  109.             throw new ArgumentNullException( "_ListLink" );
  110.          else
  111.             this._ListLink = Link;
  112.          }
  113.  
  114.       /// <summary>
  115.       /// add link in List
  116.       /// </summary>
  117.       /// <param name="url"></param>
  118.       /// <param name="name"></param>
  119.       /// <param name="description"></param>
  120.       public List<LinksCtrlDto> AddLink( string url, string name ,string description ) {
  121.          if ( string.IsNullOrEmpty( url ) )
  122.             throw new ArgumentNullException( "AddLink TL Categories - url" );
  123.          if ( string.IsNullOrEmpty( name ) )
  124.             throw new ArgumentNullException( "Addlink TL Categories - name" );
  125.          if ( string.IsNullOrEmpty( description ) )
  126.             throw new ArgumentNullException( "Addlink TL Categories - description" );
  127.  
  128.          _ListLink.Add( new LinksCtrlDto( url, name, description ) );
  129.  
  130.          return _ListLink;
  131.       }
  132.  
  133.  
  134.       /// <summary>
  135.       /// delete a link from list if URL name is matched
  136.       /// </summary>
  137.       /// <param name="url"></param>
  138.       /// <param name="name"></param>
  139.       /// <param name="description"></param>
  140.       public void DeleteLink( string url ) {
  141.          if ( string.IsNullOrEmpty( url ) )
  142.             throw new ArgumentNullException( "deletelink TL url" );
  143.  
  144.          foreach ( LinksCtrlDto lk in _ListLink ) {
  145.             if ( lk.Url == url ) {
  146.                _ListLink.Remove( lk );
  147.                break;
  148.             }
  149.          }
  150.       }
  151.  
  152.       /// <summary>
  153.       /// Verify that link already exists in specified category
  154.       /// </summary>
  155.       /// <param name="url"></param>
  156.       /// <returns></returns>
  157.       public bool LinkExists(string url){
  158.          bool result = false;
  159.  
  160.          if ( string.IsNullOrEmpty( url ) )
  161.             throw new ArgumentNullException( "LinkExists name TL Categories" );
  162.  
  163.          foreach ( LinksCtrlDto lk in Links ) {
  164.             if ( lk.Url == url ) {
  165.                result = true;
  166.                break;
  167.             }
  168.          }
  169.  
  170.          return result;
  171.          
  172.       }
  173.  
  174.  
  175.       /// <summary>
  176.       /// Name property as getter
  177.       /// </summary>
  178.       public string Name {
  179.          get {
  180.             return this._name;
  181.          }
  182.       }
  183.  
  184.  
  185.  
  186.       /// <summary>
  187.       /// List property as getter
  188.       /// </summary>
  189.       public ReadOnlyCollection<LinksCtrlDto> Links {
  190.          get {            
  191.             return this._ListLink.AsReadOnly();
  192.          }
  193.       }
  194.    }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /* DATA LAYER */
  201.  
  202.    public class UrlStorage
  203.    {
  204.       /// <summary>
  205.       /// Message string
  206.       /// </summary>
  207.       public static string messages;
  208.  
  209.       /// <summary>
  210.       /// Path to XML File
  211.       /// </summary>
  212.       public const string FILEXML = @"D:\somewhere\example.xml";
  213.      
  214.      
  215.      
  216.       /// <summary>
  217.       /// private constructor of UrlStorage
  218.       /// useful to avoid creating instance of it
  219.       /// </summary>
  220.       private UrlStorage() {
  221.       }
  222.  
  223.       /// <summary>
  224.       /// singleton pattern
  225.       /// </summary>
  226.       public static readonly UrlStorage instance = new UrlStorage();
  227.  
  228.       /// <summary>
  229.       /// read XML file
  230.       /// </summary>
  231.       public List<CategoriesCtrlDto> readXMLtoLIST()
  232.       {
  233.          List<CategoriesCtrlDto> CategoryList = null;
  234.  
  235.          try {
  236.            
  237.             if ( File.Exists( FILEXML ) ) {
  238.  
  239.                XmlDocument document = new XmlDocument();
  240.                              
  241.              
  242.                document.Load( FILEXML );
  243.  
  244.                XmlNodeList list_of_categories = document.SelectNodes( "/parent/Categories" );
  245.  
  246.                CategoryList = new List<CategoriesCtrlDto>();
  247.  
  248.              
  249.                foreach ( XmlNode nodecategory in list_of_categories ) {                
  250.  
  251.                          
  252.                   if ( nodecategory.HasChildNodes ) {
  253.  
  254.                      List<LinksCtrlDto> List1 = new List<LinksCtrlDto>();
  255.                      
  256.                      //if yes then we can create a list of links
  257.                      foreach ( XmlNode nodelinks in nodecategory.ChildNodes ) {
  258.                         List1.Add( new LinksCtrlDto( nodelinks.Attributes["url"].Value, nodelinks.Attributes["name"].Value, nodelinks.Attributes["descr"].Value ) );
  259.                      }
  260.  
  261.                      CategoriesCtrlDto categ = new CategoriesCtrlDto( nodecategory.Attributes["Name"].Value, List1 );
  262.                      CategoryList.Add( categ );
  263.                   }
  264.                }
  265.             } else {
  266.                //if file not exists, return an empty list of categories
  267.                CategoryList = new List<CategoriesCtrlDto>( 0 );
  268.             }
  269.             //we close the document
  270.             //document.Save();
  271.          } catch ( Exception e ) {
  272.             //show exception message
  273.             messages = e.Message;
  274.          }
  275.  
  276.          return CategoryList;
  277.       }
  278.  
  279.  
  280.       /// <summary>
  281.       /// Find a category name
  282.       /// it is useful if you want to prevent adding duplicate category
  283.       /// or check if the category name exists for remove process
  284.       /// </summary>
  285.       /// <param name="source"></param>
  286.       /// <param name="search"></param>
  287.       /// <param name="categ_name"></param>
  288.       /// <param name="mode"></param>
  289.       /// <returns></returns>
  290.       private bool find_category(string source, string search) {
  291.          bool result = false;        
  292.          XmlDocument document = new XmlDocument();
  293.  
  294.          try {
  295.             if ( File.Exists( source ) ) {
  296.                document.Load( source );
  297.  
  298.                foreach ( XmlNode category_nodes in document ) {
  299.                   if ( ( category_nodes.Attributes["Name"].Value == search ) ) {
  300.                      result = true;
  301.                      break;
  302.                   }
  303.                }
  304.             }
  305.          } catch {
  306.             throw;
  307.          } finally {
  308.             document.Save( source );
  309.          }
  310.          return result;
  311.       }
  312.  
  313.  
  314.  
  315.       /// <summary>
  316.       /// Find a category name
  317.       /// it is useful if you want to prevent adding duplicate category
  318.       /// or check if the category name exists for remove process
  319.       /// </summary>
  320.       /// <param name="source"></param>
  321.       /// <param name="search"></param>
  322.       /// <param name="categ_name"></param>
  323.       /// <param name="mode"></param>
  324.       /// <returns></returns>
  325.       private bool find_link( string source, string search, string categ_name ) {
  326.          bool result = false;
  327.          XmlDocument document = new XmlDocument();
  328.  
  329.          try {
  330.             if ( File.Exists( source ) ) {
  331.                document.Load( source );
  332.  
  333.                foreach ( XmlNode categ_node in document ) {
  334.                   XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + categ_name + "']" );
  335.                   foreach ( XmlNode link_nodes in categ_node ) {
  336.                      if ( ( link_nodes.Attributes["url"].Value == search ) )
  337.                         result = true;
  338.                      break;
  339.                   }
  340.                }              
  341.             } else
  342.                result = false;
  343.          } catch {
  344.             throw;
  345.          } finally {
  346.             document.Save( source );
  347.          }
  348.          return result;
  349.       }
  350.  
  351.  
  352.       /// <summary>
  353.       /// add link to source file
  354.       /// to a specified category
  355.       /// </summary>
  356.       /// <param name="source"></param>
  357.       /// <param name="categoryname"></param>
  358.       /// <param name="links"></param>
  359.       /// <returns></returns>
  360.       public bool add_link( string category_name, string url, string name, string description ) {
  361.          bool result = false;
  362.  
  363.          XmlDocument document = new XmlDocument();
  364.          XmlTextWriter writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
  365.  
  366.          if ( string.IsNullOrEmpty( category_name ) )
  367.             throw new ArgumentNullException( "Add link to category name DL" );
  368.          if ( string.IsNullOrEmpty( url ) )
  369.             throw new ArgumentNullException( "Add link url name DL" );
  370.          if ( string.IsNullOrEmpty( name ) )
  371.             throw new ArgumentNullException( "Add link name DL" );
  372.          if ( string.IsNullOrEmpty( description ) )
  373.             throw new ArgumentNullException( "Add link description DL" );
  374.  
  375.          try {
  376.             if ( File.Exists( FILEXML ) ) {
  377.                document.Load( FILEXML );
  378.                document.PreserveWhitespace = false;
  379.  
  380.                XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + category_name + "']" );
  381.  
  382.                if ( xnlist.Count > 0 ) {
  383.  
  384.                   //if there are no duplicate then we can add link in XML file
  385.                   if ( !find_link( FILEXML, url, category_name ) ) {
  386.                      XmlElement element = document.CreateElement( "link" );
  387.                      element.SetAttribute( "url", url );
  388.                      element.SetAttribute( "name", name );
  389.                      element.SetAttribute( "descr", description );
  390.                      xnlist[0].AppendChild( element );
  391.                      result = true;
  392.                   } else {
  393.                      result = false;
  394.                   }
  395.  
  396.                } else
  397.                   throw new ArgumentNullException( "xnlist empty DataLayer" );
  398.             }
  399.             if ( result ) {
  400.                document.WriteTo( writer );
  401.             }
  402.  
  403.          } catch {
  404.             throw;
  405.          } finally {
  406.             document.Save( FILEXML );
  407.          }
  408.      
  409.          return result;
  410.       }
  411.  
  412.       /// <summary>
  413.       /// Add a category into XML File
  414.       /// First, we search it for duplicates
  415.       /// </summary>
  416.       /// <param name="source"></param>
  417.       /// <param name="category"></param>
  418.       /// <returns></returns>
  419.       public bool add_category( string category_name ) {
  420.          bool result = false;
  421.  
  422.          XmlDocument document = null;
  423.          XmlTextWriter writer = null;
  424.  
  425.          if ( string.IsNullOrEmpty(category_name) )
  426.             throw new ArgumentNullException( "categories" );
  427.  
  428.          try {
  429.             if ( File.Exists( FILEXML ) ) {
  430.  
  431.                document = new XmlDocument();
  432.                writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
  433.  
  434.                document.Load( FILEXML );
  435.                   if ( !find_category( FILEXML, category_name ) ) {
  436.                      XmlElement element = document.CreateElement( "Categories" );
  437.                      element.SetAttribute( "Name", category_name );
  438.                      result = true;
  439.                   }    
  440.             } else
  441.                result = false;
  442.  
  443.             //if is all ok then write data to XML file
  444.             if ( result ) {
  445.                document.WriteTo( writer );
  446.             }
  447.          } catch {
  448.             throw;
  449.          } finally {
  450.             document.Save( FILEXML );
  451.          }
  452.          return result;
  453.       }
  454.  
  455.       /// <summary>
  456.       /// delete link if it has been found
  457.       /// </summary>
  458.       /// <param name="source"></param>
  459.       /// <param name="category_name"></param>
  460.       /// <param name="url"></param>
  461.       /// <returns></returns>
  462.       public bool delete_link( string category_name, string link_url ) {
  463.          bool result = false;
  464.  
  465.          XmlDocument document = new XmlDocument();
  466.        
  467.          if ( string.IsNullOrEmpty(category_name) )
  468.             throw new ArgumentNullException( "delete_link categories" );
  469.          if ( string.IsNullOrEmpty( link_url ) )
  470.             throw new ArgumentNullException( "delete_link DL" );
  471.  
  472.          try {
  473.             if ( File.Exists( FILEXML ) ) {
  474.                document.Load( FILEXML );
  475.  
  476.                if ( find_link( FILEXML, link_url, category_name ) ) {
  477.                   XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "'/link[@url='" + link_url + "']" );
  478.                   if ( xnlist != null ) {
  479.                      xnlist.ParentNode.RemoveChild( xnlist );
  480.                      result = true;
  481.                   }
  482.                }
  483.             }
  484.          } catch {
  485.             throw;
  486.          } finally {
  487.             document.Save( FILEXML );
  488.          }
  489.  
  490.          return result;
  491.       }
  492.  
  493.  
  494.       /// <summary>
  495.       /// delete category if it has been found
  496.       /// </summary>
  497.       /// <param name="source"></param>
  498.       /// <param name="category_name"></param>
  499.       /// <param name="name"></param>
  500.       /// <returns></returns>
  501.       public bool delete_category( string category_name ) {
  502.          bool result = false;
  503.  
  504.          XmlDocument document = new XmlDocument();
  505.  
  506.          if ( string.IsNullOrEmpty(category_name) )
  507.             throw new ArgumentNullException("delete_category DL");
  508.  
  509.             try {
  510.                if ( File.Exists( FILEXML ) ) {
  511.                   document.Load( FILEXML );
  512.                
  513.                      if ( find_category( FILEXML, category_name ) ) {
  514.                         XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "']" );
  515.                         if ( xnlist != null ) {
  516.                            xnlist.ParentNode.RemoveChild( xnlist );
  517.                            result = true;
  518.                         }  
  519.                   }
  520.                }
  521.             } catch {
  522.                throw;
  523.             } finally {
  524.                document.Save( FILEXML );
  525.             }
  526.          
  527.          return result;
  528.       }
  529.    }
  530.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement