namespace (Assoc Name).(site app is for)
{
public class RetsHelper : IDisposable
{
#region Properties
///
/// Contains the rets session connection.
///
internal RetsSession Session
{
get
{
return _sess;
}
set
{
_sess = value;
}
}
private RetsSession _sess;
#endregion
#region Constructors
public RetsHelper(string url, string userAgent, string userName,
string password)
{
this.Session = new RetsSession(url);
this.Session.SetUserAgent(userAgent);
try
{ //Log in to RETS
bool loginResult = this.Session.Login(userName, password);
if (!loginResult)
{
Console.WriteLine("\nLogin to RETS Failed at " + DateTime.Now);
}
else
{
Console.WriteLine("\nLogin to RETS Succeeded at " + DateTime.Now);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#endregion
#region Instance Methods
///
/// Gets search results for a given query.
///
/// Resource from rets metadata
/// Class from rets metadata
/// RETS query to run.
/// Record number to start at. This is 1-based, not zero-based.
/// Results of query.
internal SearchResultSet GetRetsSearchResults(string searchType, string searchClass, string query, int offset)
{
using (SearchRequest search = this.Session.CreateSearchRequest(
searchType, searchClass.ToString(), query))
{
search.SetQueryType(SearchRequest.QueryType.DMQL2);
search.SetStandardNames(false);
search.SetOffset(offset);
SearchResultSet results = this.Session.Search(search);
return results;
}
}
///
/// Downloads all listings, starting at the given offset. This method will recurse if needed.
///
///
/// Starting record of results. This is 1-based, not zero-based.
public void DownloadAllListings(string propType, int offset)
{
try
{
// get the records using System names.
using (SearchResultSet results = GetRetsSearchResults("Property", propType, "(StreetNumber=0+),(Status=|A,C,M)", offset))
{
// get the results as a list of objects.
ListingCollection list = Populate(results);
// save all records.
list.SaveAll();
Console.WriteLine("\nSaving listings to sql db by the batch. Offset = " + offset);
// process other info for the records.
//currently just pulls & writes image files for listing photos.
// Code in second example on RETS Wiki
//ProcessListings(list);
// check to see if the list finished to the end.
int totalProcessed = list.Count + offset;
if (results.GetCount() > totalProcessed)
{
// recurse if needed.
DownloadAllListings(propType, totalProcessed);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
///
/// Populates a ListingCollection from a RETS result set.
///
/// ResultSet to create the ListingCollection from.
/// ListingCollection representing the result set. Will return
/// an empty collection if no records.
private ListingCollection Populate(SearchResultSet results)
{
ListingCollection listings = new ListingCollection();
while (results.HasNext())
{
//sample db mapping
currentListing.AddrNumber = results.GetString("StreetNumber");
currentListing.AddrDirection = results.GetString("StreetDirection");
currentListing.AddrStreet = results.GetString("StreetName");
listings.Add(currentListing);
}
return listings;
}
#endregion
#region IDisposable Members
///
/// Logout of the RETS session.
///
public void Dispose()
{
try
{
Session.Logout();
}
finally
{
Session.Dispose();
}
}
#endregion
}
}