namespace Cas.IO
{
using System.IO;
/// <summary>
/// Summary for the Files class
/// <source>Subsonic 2.1 final</source>
/// </summary>
public static class FileUtility
{
/// <summary>
/// Read a text file and obtain it's contents.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <returns>String containing the content of the file.</returns>
public static string GetFileText(string absolutePath)
{
using (StreamReader sr = new StreamReader(absolutePath))
return sr.ReadToEnd();
}
/// <summary>
/// Creates or opens a file for writing and writes text to it.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <param name="fileText">A String containing text to be written to the file.</param>
public static void CreateToFile(string absolutePath, string fileText)
{
using (StreamWriter sw = File.CreateText(absolutePath))
sw.Write(fileText);
}
/// <summary>
/// Update text within a file by replacing a substring within the file.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <param name="lookFor">A String to be replaced.</param>
/// <param name="replaceWith">A String to replace all occurrences of lookFor.</param>
public static void UpdateFileText(string absolutePath, string lookFor, string replaceWith)
{
string newText = GetFileText(absolutePath).Replace(lookFor, replaceWith);
WriteToFile(absolutePath, newText);
}
/// <summary>
/// Writes out a string to a file.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <para0m name="fileText">A String containing text to be written to the file.</param>
public static void WriteToFile(string absolutePath, string fileText)
{
using (StreamWriter sw = new StreamWriter(absolutePath, false))
sw.Write(fileText);
}
} //eof clas
} //eof namespace
//END ================== File.cs
.
//START ===== MetaItem.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace MetaItem
{
class MetaItem
{
#region Props
/// <summary>
/// The id of this item as it is stored in the MetaItem table
/// </summary>
public long MetaItemId { get; set; }
/// <summary>
/// The generic id of this item - CustomerId , AccountId , OrderId etc.
/// </summary>
public long ItemId { get; set; }
/// <summary>
/// The name of the item - usually a name of a table - Customer , Order , Account etc.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Each item might have 0 or many master items
/// </summary>
public List <MetaItem> ListMasters { get ; set ; }
/// <summary>
/// Each item might have 0 or many Detail items
/// </summary>
public List <MetaItem> ListDetails { get ; set ; }
/// <summary>
/// A human language / semantic ways of describing the relationship to the Master
/// </summary>
public string RelashionchipToMaster { get; set; }
/// <summary>
/// A semantic description of the relationship to the a detail item
/// </summary>
public string RelashionchipToDetail { get; set; }
#endregion Props
#region Methods
/// <summary>
/// A dummy demo method to print all the link suffixes to a master
/// </summary>
/// <param name="objMetaItem"></param>
/// <returns></returns>
public string GenerateLinkSuffixToMaster ( MetaItem objMetaItem )
{
string strLink = String.Empty;
if (objMetaItem == null)
return null ;
if ( String.IsNullOrEmpty ( objMetaItem.Name ))
return null ;
if (objMetaItem.ItemId == null)
return null;
strLink = "m=" + objMetaItem.Name + "&id=" + objMetaItem.ItemId.ToString();
return strLink;
} //eof method
/// <summary>
/// A dummy demo method to print all the links to a detail item
/// </summary>
/// <param name="objMetaItem"></param>
/// <returns></returns>
public string GenerateLinkSuffixToDetail(MetaItem objMetaItem)
{
string strLink = String.Empty;
if (objMetaItem == null)
return null;
if (String.IsNullOrEmpty(objMetaItem.Name))
return null;
if (objMetaItem.ItemId == null)
return null;
strLink = "d=" + objMetaItem.Name + this.ItemId.ToString() + "id=" + objMetaItem.ItemId.ToString();
return strLink;
} //eof method
public string PrintLinksToMasters()
{
string strToPrint = String.Empty;
foreach (MetaItem item in this.ListMasters)
{
strToPrint = strToPrint + "The " + this.Name + this.ItemId.ToString() +
this.RelashionchipToMaster + item.Name + item.ItemId.ToString() + "\n";
strToPrint = strToPrint + "From " + this.Name + this.ItemId.ToString() + " to " +
item.Name + item.ItemId.ToString() + "the link is :" + item.GenerateLinkSuffixToMaster(item) + "\n";
} //eof foreach
return strToPrint;
} //eof method
public string PrintLinksToDetails()
{
string strToPrint = String.Empty;
foreach (MetaItem item in this.ListDetails)
{
strToPrint = strToPrint + "The " + this.Name + this.ItemId.ToString() +
this.RelashionchipToDetail + item.Name + item.ItemId.ToString() + "\n" ;
strToPrint = strToPrint + "From " + this.Name + this.ItemId.ToString () + " to " +
item.Name + item.ItemId.ToString() + " the link is: " + item.GenerateLinkSuffixToDetail(item) + "\n" ;
} //eof foreach
return strToPrint;
} //eof method
#endregion Melthods
} //eof class
}
//END ================== MetaItem.cs
.
//START ===== Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MetaItem
{
class Program
{
static void Main(string[] args)
{
int RollingFirstLevelId = 0; //start counting the second level
int intRollingSecondLevelId = 0; //start counting the second level
int intRollingThirdLeveId = 0; //start counting the third level
int intCurrentFirstLevel = 0;
string strToPrint = String.Empty ;
for (int intFirstLeveId = RollingFirstLevelId; intFirstLeveId <= intCurrentFirstLevel + 2; intFirstLeveId++)
{
MetaItem objMetaItem1 = new MetaItem();
objMetaItem1.Name = "Customer";
objMetaItem1.ItemId = intFirstLeveId ;
objMetaItem1.RelashionchipToMaster = " belongs to ";
objMetaItem1.RelashionchipToDetail = " has a ";
List<MetaItem> listFirstLevelMasters = new List<MetaItem>();
listFirstLevelMasters.Add(objMetaItem1);
List<MetaItem> listFirstLevelDetails = new List<MetaItem>();
int intCurrentSecondLevel = intRollingSecondLevelId;
for (int intSecondLeveId = intRollingSecondLevelId; intSecondLeveId <= intCurrentSecondLevel + 3; intSecondLeveId++)
{
MetaItem objMetaItem2 = new MetaItem();
objMetaItem2.Name = "Order";
objMetaItem2.ItemId = intSecondLeveId;
objMetaItem2.RelashionchipToMaster = " belongs to ";
objMetaItem2.RelashionchipToDetail = " has a ";
objMetaItem2.ListMasters = listFirstLevelMasters;
strToPrint = strToPrint + objMetaItem2.PrintLinksToMasters();
List<MetaItem> listSecondLevelMasters = new List<MetaItem>();
listSecondLevelMasters.Add(objMetaItem2);
List<MetaItem> listSecondLevelDetails = new List<MetaItem>();
int intCurrentThirdLevel = intRollingThirdLeveId;
for (int intThirdLeveId = intRollingThirdLeveId; intThirdLeveId <= intCurrentThirdLevel + 4; intThirdLeveId++)
{
MetaItem objMetaItem3 = new MetaItem();
objMetaItem3.Name = "Invoice";
objMetaItem3.ItemId = intThirdLeveId;
objMetaItem3.RelashionchipToMaster = " belongs to ";
objMetaItem3.RelashionchipToDetail = " has a ";
objMetaItem3.ListMasters = listSecondLevelMasters;
strToPrint = strToPrint + objMetaItem3.PrintLinksToMasters();
intRollingThirdLeveId++;
listSecondLevelDetails.Add(objMetaItem3);
objMetaItem2.ListDetails = listSecondLevelDetails;
strToPrint = strToPrint + objMetaItem2.PrintLinksToDetails();
}//eof for 3
intRollingSecondLevelId++;
//now add
listFirstLevelDetails.Add(objMetaItem2);
objMetaItem1.ListDetails = listFirstLevelDetails;
strToPrint = strToPrint + objMetaItem1.PrintLinksToDetails();
}//eof for 2
}//eof for 1
Console.WriteLine(strToPrint );
Console.WriteLine("Hit a key to exit " );
Console.ReadKey();
Cas.IO.FileUtility.WriteToFile( "C:\\temp\\file.txt" ,strToPrint );
} //eof Main
} //eof class
} //eof namesapce
//END ================== Program.cs
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!