Permalink
Browse files

Finished Search interface/implementation

  • Loading branch information...
1 parent 62403cd commit e99373d471857924b6e9b977334a5e8b7df64d4e Austin Montoya committed Oct 3, 2011
View
@@ -446,4 +446,25 @@ public void RemoveFromRepo()
/// </summary>
public bool Enabled { get; set; }
}
+
+ public class ContentObjectEqualityComparer : IEqualityComparer<ContentObject>
+ {
+
+ public bool Equals(ContentObject x, ContentObject y)
+ {
+ return x.PID.Equals(y.PID);
+ }
+
+ public int GetHashCode(ContentObject obj)
+ {
+ try
+ {
+ return int.Parse(obj.PID.Substring(obj.PID.LastIndexOf(':') + 1));
+ }
+ catch
+ {
+ return -1;
+ }
+ }
+ }
}
@@ -105,13 +105,15 @@ public IDataRepository CreateDataRepositorProxy()
return new FedoraCommonsRepo(FedoraUrl, FedoraUserName, FedoraPassword, FedoraAccessUrl, FedoraManagementUrl, ConnectionString, FedoraNamespace);
}
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public ITempContentManager CreateTempContentManager()
+
+ public IDataRepository CreateDataRepositorProxy(string uname)
+ {
+ return new FedoraCommonsRepo(FedoraUrl, FedoraUserName, FedoraPassword, FedoraAccessUrl, FedoraManagementUrl, ConnectionString, FedoraNamespace, uname);
+ }
+
+ public ISearchProxy CreateSearchProxy(string uname)
{
- return new TempWebContentManager(ConnectionString);
+ return new DefaultSearchProxy(ConnectionString, uname);
}
}
}
@@ -0,0 +1,190 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Linq;
+using System.Text;
+
+namespace vwarDAL
+{
+ class DefaultSearchProxy : ISearchProxy
+ {
+ private string _connectionString;
+ private IMetadataStore _dataStore;
+ private string _identity;
+ private ContentObjectEqualityComparer _compare = new ContentObjectEqualityComparer();
+
+ public DefaultSearchProxy(string connectionString, string identity)
+ {
+ _connectionString = connectionString;
+ _dataStore = new MySqlMetadataStore(_connectionString);
+ _identity = identity;
+ }
+
+ public IEnumerable<ContentObject> QuickSearch(string term)
+ {
+ IEnumerable<ContentObject> results = null;
+
+ results = combineResults(results, GetContentObjectsByTitle(term), SearchMethod.OR);
+ results = combineResults(results, GetContentObjectsByDescription(term), SearchMethod.OR);
+ results = combineResults(results, GetContentObjectsByKeyWords(term), SearchMethod.OR);
+
+ return results;
+ }
+
+ public IEnumerable<ContentObject> QuickSearch(IEnumerable<string> terms, SearchMethod method = SearchMethod.OR)
+ {
+ if (terms.Count() < 1)
+ return null;
+
+ IEnumerable<ContentObject> results = null;
+ foreach (string term in terms)
+ results = combineResults(results, QuickSearch(term), method);
+
+ return results;
+ }
+
+ public IEnumerable<ContentObject> SearchByField(string field, string term)
+ {
+ IEnumerable<ContentObject> results = null;
+ field = field.ToLowerInvariant();
+
+ switch (field)
+ {
+ case "title":
+ case "description":
+ case "developername":
+ case "sponsorname":
+ case "artistname":
+ results = _dataStore.GetContentObjectsByField(field, term, _identity);
+ break;
+
+ case "submitteremail":
+ results = _dataStore.GetContentObjectsByField("submitter", term, _identity);
+ break;
+
+ case "keywords":
+ results = this.GetContentObjectsByKeyWords(field);
+ break;
+
+ default:
+ throw new Exception("Referenced field not searchable in MySQL metadata");
+ }
+
+ return results;
+ }
+
+ public IEnumerable<ContentObject> SearchByFields(NameValueCollection fields, SearchMethod method = SearchMethod.OR)
+ {
+ if (fields.Count < 1)
+ return null;
+
+ IEnumerable<ContentObject> results = null;
+ for (int i = 0; i < fields.Count; i++)
+ results = combineResults(results, SearchByField(fields.Keys[i], fields[fields.Keys[i]]), method);
+
+ return results;
+ }
+
+ public IEnumerable<ContentObject> DeepSearch(string term)
+ {
+ NameValueCollection nvc = new NameValueCollection();
+ string[] fields = { "title", "description", "keywords", "developername", "sponsorname", "artistname" };
+ foreach (string field in fields)
+ nvc[field] = term;
+
+ return SearchByFields(nvc);
+ }
+
+ //This is horribly inefficient. Should only be used when absolutely necessary, like finding a troll
+ public IEnumerable<ContentObject> DeepSearch(IEnumerable<string> terms, SearchMethod method = SearchMethod.OR)
+ {
+ IEnumerable<ContentObject> results = null;
+ foreach (string term in terms)
+ results = combineResults(results, DeepSearch(term), method);
+
+ return results;
+ }
+
+ public IEnumerable<ContentObject> GetRecentlyViewed(int count, int start = 0)
+ {
+ return _dataStore.GetObjectsWithRange("{CALL GetMostRecentlyViewed(?,?,?)}", count, start, _identity);
+ }
+
+ public IEnumerable<ContentObject> GetHighestRated(int count, int start = 0)
+ {
+ return _dataStore.GetObjectsWithRange("{CALL GetHighestRated(?,?,?)}", count, start, _identity);
+ }
+
+ public IEnumerable<ContentObject> GetMostPopular(int count, int start = 0)
+ {
+ return _dataStore.GetObjectsWithRange("{CALL GetMostPopular(?,?,?)}", count, start, _identity);
+ }
+
+ public IEnumerable<ContentObject> GetRecentlyUpdated(int count, int start = 0)
+ {
+ return _dataStore.GetObjectsWithRange("{CALL GetMostRecentlyUpdated(?,?,?)}", count, start, _identity);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsByDeveloperName(string developerName)
+ {
+ return SearchByField("developername", developerName);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsBySponsorName(string sponsorName)
+ {
+ return SearchByField("sponsorname", sponsorName);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsByArtistName(string artistName)
+ {
+ return SearchByField("artistname", artistName);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsByKeyWords(string keywordsList)
+ {
+ return _dataStore.GetContentObjectsByKeywords(keywordsList, _identity);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsByDescription(string description)
+ {
+ return SearchByField("description", description);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsByTitle(string title)
+ {
+ return SearchByField("title", title);
+ }
+
+ public IEnumerable<ContentObject> GetContentObjectsBySubmitterEmail(string email)
+ {
+ var results = from obj in _dataStore.GetAllContentObjects()
+ where obj.SubmitterEmail.Equals(email, StringComparison.InvariantCultureIgnoreCase)
+ select obj;
+
+ return results;
+ }
+
+ /// <summary>
+ /// Cleanly combines two IEnumerables (possibly null)
+ /// </summary>
+ /// <param name="co1">The existing ContentObjects result</param>
+ /// <param name="co2">The new ContentObjects you would like to add to the result</param>
+ /// <param name="method">The method by which to join the results (union or intersect)</param>
+ /// <returns>An IEnumerable containing the combined inputs</returns>
+ private IEnumerable<ContentObject> combineResults(IEnumerable<ContentObject> existing, IEnumerable<ContentObject> toAdd, SearchMethod method)
+ {
+ if (toAdd.Count() > 0)
+ {
+ if (existing == null)
+ existing = toAdd;
+ else
+ {
+ existing = (method == SearchMethod.OR)
+ ? existing.Union(toAdd, _compare)
+ : existing.Intersect(toAdd, _compare);
+ }
+ }
+ return existing;
+ }
+ }
+}
@@ -33,6 +33,8 @@ public class FedoraCommonsRepo : IDataRepository
///
/// </summary>
private IFileStore _fileStore;
+
+ private string _identity;
/// <summary>
///
/// </summary>
@@ -43,10 +45,11 @@ public class FedoraCommonsRepo : IDataRepository
/// <param name="management"></param>
/// <param name="connectionString"></param>
/// <param name="fileNamespace"></param>
- internal FedoraCommonsRepo(string url, string userName, string password, string access, string management, string connectionString, string fileNamespace)
+ internal FedoraCommonsRepo(string url, string userName, string password, string access, string management, string connectionString, string fileNamespace, string identity="")
{
_metadataStore = new MySqlMetadataStore(connectionString);
_fileStore = new FedoraFileStore(url, userName, password, access, management, fileNamespace);
+ _identity = identity;
}
/// <summary>
///
@@ -64,9 +67,9 @@ public IEnumerable<ContentObject> GetAllContentObjects()
/// <returns></returns>
public IEnumerable<ContentObject> GetHighestRated(int count, int start = 0)
{
+ if (_identity == null) return null;
- return _metadataStore.GetObjectsWithRange("{CALL GetHighestRated(?,?)}", count, start);
-
+ return _metadataStore.GetObjectsWithRange("{CALL GetHighestRated(?,?,?)}", count, start, _identity);
}
/// <summary>
///
@@ -76,7 +79,9 @@ public IEnumerable<ContentObject> GetHighestRated(int count, int start = 0)
/// <returns></returns>
public IEnumerable<ContentObject> GetMostPopular(int count, int start = 0)
{
- return _metadataStore.GetObjectsWithRange("{CALL GetMostPopular(?,?)}", count, start);
+ if (_identity == null) return null;
+
+ return _metadataStore.GetObjectsWithRange("{CALL GetMostPopular(?,?,?)}", count, start, _identity);
}
/// <summary>
///
@@ -86,7 +91,9 @@ public IEnumerable<ContentObject> GetMostPopular(int count, int start = 0)
/// <returns></returns>
public IEnumerable<ContentObject> GetRecentlyUpdated(int count, int start = 0)
{
- return _metadataStore.GetObjectsWithRange("{CALL GetMostRecentlyUpdated(?,?)}", count, start);
+ if (_identity == null) return null;
+
+ return _metadataStore.GetObjectsWithRange("{CALL GetMostRecentlyUpdated(?,?,?)}", count, start, _identity);
}
/// <summary>
///
@@ -97,8 +104,6 @@ public IEnumerable<ContentObject> GetRecentlyUpdated(int count, int start = 0)
/// <param name="contentObjectId"></param>
public void InsertReview(int rating, string text, string submitterEmail, string contentObjectId)
{
-
-
_metadataStore.InsertReview(rating, text, submitterEmail, contentObjectId);
}
/// <summary>
@@ -119,7 +124,7 @@ public void UpdateContentObject(ContentObject co)
/// <returns></returns>
public IEnumerable<ContentObject> GetRecentlyViewed(int count, int start = 0)
{
- return _metadataStore.GetObjectsWithRange("{CALL GetMostRecentlyViewed(?,?)}", count, start);
+ return _metadataStore.GetObjectsWithRange("{CALL GetMostRecentlyViewed(?,?,?)}", count, start, _identity);
}
/// <summary>
///
@@ -213,8 +218,6 @@ public IEnumerable<ContentObject> GetContentObjectsByArtistName(string artistNam
select c;
return co;
-
-
}
/// <summary>
///
@@ -229,8 +232,6 @@ public IEnumerable<ContentObject> GetContentObjectsByKeyWords(string keyword)
select c;
return co;
-
-
}
/// <summary>
///
View
@@ -25,23 +25,10 @@ namespace vwarDAL
public interface IDataRepository
{
IEnumerable<ContentObject> GetAllContentObjects();
- //IEnumerable<ContentObject> GetContentObjectsByCollectionName(string collectionName);
- IEnumerable<ContentObject> GetHighestRated(int count, int start = 0);
- IEnumerable<ContentObject> GetMostPopular(int count, int start = 0);
- IEnumerable<ContentObject> GetRecentlyUpdated(int count, int start = 0);
- IEnumerable<ContentObject> GetContentObjectsByDeveloperName(string developerName);
- IEnumerable<ContentObject> GetContentObjectsBySponsorName(string sponsorName);
- IEnumerable<ContentObject> GetContentObjectsByArtistName(string artistName);
- IEnumerable<ContentObject> GetContentObjectsByKeyWords(string keyword);
- IEnumerable<ContentObject> GetContentObjectsByDescription(string description);
- IEnumerable<ContentObject> GetContentObjectsByTitle(string title);
void InsertReview(int rating, string text, string submitterEmail, string contentObjectId);
void UpdateContentObject(ContentObject co);
- IEnumerable<ContentObject> GetRecentlyViewed(int count, int start = 0);
- IEnumerable<ContentObject> SearchContentObjects(string searchTerm);
- IEnumerable<ContentObject> GetContentObjectsBySubmitterEmail(string email);
-
+
ContentObject GetContentObjectById(string pid, bool updateViews, bool getReviews = false, int revision = -1);
Stream GetContentFile(string pid, string file);
@@ -68,6 +55,7 @@ public interface IDataRepository
void IncrementDownloads(string id);
ContentObject GetNewContentObject();
-
+
+ IEnumerable<ContentObject> SearchContentObjects(string searchTerm);
}
}
Oops, something went wrong.

0 comments on commit e99373d

Please sign in to comment.