ContentEnumerator.cs for stored procedure sample
Here is a new version of ContentEnumerator.cs and Accessor.cs based off of John Kohz's PH c# sample to show you how you might utilize a custom protocol handler to crawl a database.
Just replace the origninal file in his sample code with this version and start from there.
This compiles but is not tested. I based it off of work I have done in the past that does work to give you an idea of some of the concepts. If you have any questions just ask and I will try to clarify or fix anything in this file.
One glaring omission for this sample is the ability to add custom properties to the index besides the basic Title and URL properties. It is not a far reach to figure out how though from I have provided.
More to come on the tricky stuff I discussed in my last post.
Snippets from the code:
public ContentEnumerator(string sUrl): base(sUrl)
{
if (sUrl.ToLower().Contains("fake.txt?id="))
m_type = UriType.Item;
else if (sUrl.ToLower().Contains("fake.txt"))
m_type = UriType.Container;
else
throw new ApplicationException("Unknown content type");
string tURL = sUrl.Substring(sUrl.IndexOf("//") + 2); // trim off the excess
string[] aURL = tURL.Split("/".ToCharArray());
m_server = aURL[0];
m_dbname = aURL[1];
m_enumstoredProc = aURL[2];
m_itemstoredProc = aURL[3];
string[] parms = this.Query.Split("&".ToCharArray());
Hashtable ht = new Hashtable();
for (int i = 0; i < parms.Length; i++)
{
string[] brk = parms[i].Split("=".ToCharArray());ht[Microsoft.JScript.GlobalObject.decodeURI(brk[0]).ToLower()] = Microsoft.JScript.GlobalObject.decodeURI(brk[1]);
}
if (Type == UriType.Item)
{
m_itemID = ht["id"].ToString();
m_itemType = ht["type"].ToString();
}
Logging.Trace("ContentEnumerator created");Logging.Information("Creating ContentEnumerator for: " + sUrl);
}
public void EnsureContent()
{
//need to initialize a stream here.
String contstr = m_dsContent.Tables[0].Rows[0]["CONTENT"].ToString(); // in this case your Stored proc combines all your searchable data into one field.
byte[] byteArray; UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(contstr);m_Stream = new MemoryStream(firstString);
}
public byte[] SecurityDescriptor
{
get
{
if (m_SecurityDescriptor == null)
{
DateTime startTime = Logging.Enter(typeof(ContentEnumerator), "SecurityDescriptor-create");
// lets say for the our example here that the stored proc will return a list of users that
//are allowed to access the items and they are in domain\user format already :)
String owner = m_dsContent.Tables[0].Rows[0]["OWNER"].ToString(); ArrayList al = new ArrayList();
if (m_dsContent.Tables.Count == 2) // second one is security list
for (int i = 0;i<m_dsContent.Tables[1].Rows.Count;i++)
al.Add (m_dsContent.Tables[1].Rows[i]["USER"]);
String[] users = (String[]) al.ToArray (typeof (String));
// need to include the Win32.Security libary. Do a search for it on Google
PHSecurityDescriptor sec = new PHSecurityDescriptor();
sec.LoadSecurity(owner,users);
m_SecurityDescriptor = sec.GetBuffer();
}
return m_SecurityDescriptor;
}
}
See the file link at the top of this post for the rest
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks