Exemplo n.º 1
0
        /// <summary>
        /// Toggle the document state in the search index.
        /// </summary>
        /// <param name="documentId"> </param>
        /// <returns>Base result containing the results of the operation</returns>
        public DataResult <IndexedDocument> AddDocumentToIndex(Guid documentId)
        {
            var result = new DataResult <IndexedDocument>();

            try
            {
                // use the repository to extarct the document for an uploaded document
                var document = _documentsRepository.Read(documentId);

                // create a lucene document from the given document
                var luceneDocument = GetLuceneDocumentFromDocument(document, true);

                // if there is no such document
                if (document == null)
                {
                    result.SetFailiure("There is no document with the selected document id");
                    return(result);
                }

                // check if the document is already indexed
                if (document.IsIndexed)
                {
                    result.SetSuccess("Document with the selected id is already indexed");
                    result.SetData(luceneDocument);

                    return(result);
                }

                var luceneIndexLocation = GetLuceneIndexLocation();

                // add it to the index
                var addToIndexResult = _luceneIndexService.AddToIndex(luceneIndexLocation, luceneDocument);

                if (addToIndexResult.GetStatus() == LuceneIndexingStatus.Success)
                {
                    SetDocumentIndexStatus(document, true);

                    // set success properties on the result
                    result.SetSuccess("Successs in adding the document to the lucene index");
                    result.SetData(luceneDocument);
                }
                else
                {
                    result.SetFailiure("Failed to add document to index");
                }
            }
            catch (Exception ex)
            {
                result.SetException(ex, "Exception while adding document to the lucene index");
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Query the index used by the DSS Services for the number of indexed documents
        /// </summary>
        /// <returns>Data Result wrapping the number of indexed documents</returns>
        public DataResult <int> GetNumberOfIndexedDocuments()
        {
            var result = new DataResult <int>();

            try
            {
                var directoryPaty = GetLuceneIndexLocation();

                var luceneResult = _luceneIndexService.GetDocumentsNumber(directoryPaty);

                if (luceneResult.GetStatus() == LuceneIndexingStatus.Success)
                {
                    result.SetSuccess("Succeeded in retrieving the lucene documents number");
                    result.SetData(luceneResult.ResponseObject);
                }
                else
                {
                    result.SetFailiure("Failed to retrieve lucene index documents number");
                }
            }
            catch (Exception ex)
            {
                result.SetException(ex, "Exception while checking the number of indexed documents");
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check and return the flat for the optimization state regarding the index used by
        /// </summary>
        /// <returns></returns>
        public DataResult <bool> IsIndexOptimized()
        {
            var result = new DataResult <bool>();

            try
            {
                var path         = GetLuceneIndexLocation();
                var luceneResult = _luceneIndexService.IsIndexedOptimized(path);

                if (luceneResult.GetStatus() == LuceneIndexingStatus.Success)
                {
                    result.SetSuccess("Retrieved index optimization state");
                    result.SetData(luceneResult.ResponseObject);
                }
                else
                {
                    result.SetFailiure("Could not get lucene index optimization status");
                }
            }
            catch (Exception ex)
            {
                result.SetException(ex, "Exception while checking index optimize status");
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Remove a document from the Lucene indexing, making it unavaiable for searching of general user
        /// downloading
        /// </summary>
        /// <param name="documentId">The id of the document to be removed from the index</param>
        /// <returns>Data result containing document objet that was just removed from the index</returns>
        public DataResult <IndexedDocument> RemoveDocumentFromIndex(Guid documentId)
        {
            var result = new DataResult <IndexedDocument>();

            try
            {
                var document       = _documentsRepository.Read(documentId);
                var luceneDocument = GetLuceneDocumentFromDocument(document);

                if (document == null)
                {
                    result.SetFailiure("There is no document with the given document id to remove from the index");
                    return(result);
                }

                if (!document.IsIndexed)
                {
                    result.SetSuccess("Document is already removed from the index");
                    result.SetData(luceneDocument);
                    return(result);
                }

                var lucenePath = GetLuceneIndexLocation();

                // try and remove the document from the index
                var removalResult = _luceneIndexService.RemoveFromIndex(lucenePath, luceneDocument, true);

                if (removalResult.GetStatus() == LuceneIndexingStatus.Success)
                {
                    SetDocumentIndexStatus(document, false);

                    result.SetSuccess("Document Successfully removed from index");
                    result.SetData(luceneDocument);
                }
                else
                {
                    result.SetFailiure("Failed to remove document from index");
                }

                return(result);
            }
            catch (Exception ex)
            {
                result.SetException(ex, "Exception while trying to remove document from index");
                return(result);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Authenticate user credential information, the username and password, and return an authenticated user object
        /// </summary>
        /// <param name="username">The Users, username property used to distuingshi</param>
        /// <param name="password">The Users, password property used to authenticate users in the system</param>
        /// <returns></returns>
        public DataResult <User> AuthenticateUser(string username, string password)
        {
            var result = new DataResult <User>();

            try
            {
                var usersForCredentials = _userRepository.GetUsersForCredentials(username, password);

                if (usersForCredentials.Count == 0)
                {
                    result.SetFailiure("User Authentication failed. No user found.");
                }
                else if (usersForCredentials.Count > 1)
                {
                    result.SetFailiure("User Authentication failed. More than one user found for given credentials");
                }
                else
                {
                    var user = usersForCredentials.FirstOrDefault();

                    if (user == null)
                    {
                        result.SetFailiure("First user result is null");
                    }
                    else
                    {
                        result.SetSuccess("Retrieved user information");
                        result.SetData(user);
                    }
                }
            }
            catch (Exception ex)
            {
                result.SetException(ex, "Exception while authenticating user");
            }

            return(result);
        }