From af0089bf32ae9bd77d5cdff0b3ffd6c0292334f4 Mon Sep 17 00:00:00 2001
From: Roman Tolchenov <roman.tolchenov@stfc.ac.uk>
Date: Wed, 28 Jul 2010 12:11:48 +0000
Subject: [PATCH] Included the archive search service into finding files. re
 #1393

---
 .../Mantid/API/inc/MantidAPI/IArchiveSearch.h |  4 ++
 Code/Mantid/API/src/FileFinder.cpp            | 70 +++++++++++++++++--
 Code/Mantid/API/test/FileFinderTest.h         | 12 +++-
 Code/Mantid/Kernel/src/ConfigService.cpp      |  2 +-
 Code/Mantid/Properties/Mantid.properties      |  3 +
 5 files changed, 85 insertions(+), 6 deletions(-)

diff --git a/Code/Mantid/API/inc/MantidAPI/IArchiveSearch.h b/Code/Mantid/API/inc/MantidAPI/IArchiveSearch.h
index 490ce65470c..dc9229bc381 100644
--- a/Code/Mantid/API/inc/MantidAPI/IArchiveSearch.h
+++ b/Code/Mantid/API/inc/MantidAPI/IArchiveSearch.h
@@ -6,6 +6,7 @@
 //----------------------------------------------------------------------
 #include "MantidAPI/DllExport.h"
 
+#include <boost/shared_ptr.hpp>
 #include <string>
 
 #define DECLARE_ARCHIVESEARCH(classname,facility) \
@@ -58,6 +59,9 @@ namespace Mantid
       virtual std::string getPath(const std::string& fName)const = 0;
     };
 
+		///Typedef for a shared pointer to an IArchiveSearch
+		typedef boost::shared_ptr<IArchiveSearch> IArchiveSearch_sptr;
+
   }
 }
 
diff --git a/Code/Mantid/API/src/FileFinder.cpp b/Code/Mantid/API/src/FileFinder.cpp
index 8bdf697eb3c..f5cbf1b8250 100644
--- a/Code/Mantid/API/src/FileFinder.cpp
+++ b/Code/Mantid/API/src/FileFinder.cpp
@@ -2,9 +2,13 @@
 // Includes
 //----------------------------------------------------------------------
 #include "MantidAPI/FileFinder.h"
+#include "MantidAPI/IArchiveSearch.h"
+#include "MantidAPI/ArchiveSearchFactory.h"
 #include "MantidKernel/ConfigService.h"
 #include "MantidKernel/FacilityInfo.h"
 #include "MantidKernel/InstrumentInfo.h"
+#include "MantidKernel/LibraryManager.h"
+#include "MantidKernel/Glob.h"
 
 #include "Poco/Path.h"
 #include "Poco/File.h"
@@ -26,6 +30,12 @@ namespace Mantid
      */
     FileFinderImpl::FileFinderImpl()
     {
+      // Make sure plugins are loaded
+      std::string libpath = Kernel::ConfigService::Instance().getString("plugins.directory");
+      if( !libpath.empty() )
+      {
+        Kernel::LibraryManager::Instance().OpenAllLibraries(libpath);
+      }
     }
 
     /**
@@ -40,11 +50,25 @@ namespace Mantid
       std::vector<std::string>::const_iterator it = searchPaths.begin();
       for(;it != searchPaths.end(); ++it)
       {
-        Poco::Path path(*it,fName);
-        Poco::File file(path);
-        if (file.exists())
+        if (fName.find("*") != std::string::npos)
+        {
+          Poco::Path path(*it,fName);
+          Poco::Path pathPattern(path);
+          std::set<std::string> files;
+          Kernel::Glob::glob(pathPattern, files);
+          if ( !files.empty() )
+          {
+            return *files.begin();
+          }
+        }
+        else
         {
-          return path.toString();
+          Poco::Path path(*it,fName);
+          Poco::File file(path);
+          if (file.exists())
+          {
+            return path.toString();
+          }
         }
       }
       return "";
@@ -125,6 +149,44 @@ namespace Mantid
         std::string path = getFullPath(fName + "." + *ext);
         if ( !path.empty() ) return path;
       }
+
+      // Search the archive of the default facility
+      std::string archiveOpt = Kernel::ConfigService::Instance().getString("datasearch.searcharchive");
+      std::transform(archiveOpt.begin(),archiveOpt.end(),archiveOpt.begin(),tolower);
+      if ( !archiveOpt.empty() && archiveOpt != "off" )
+      {
+        IArchiveSearch_sptr arch = ArchiveSearchFactory::Instance().create(
+          Kernel::ConfigService::Instance().Facility().name()
+          );
+        if (arch)
+        {
+          std::string path = arch->getPath(fName);
+          if ( !path.empty() )
+          {
+            std::vector<std::string>::const_iterator ext = exts.begin();
+            for(;ext != exts.end(); ++ext)
+            {
+              Poco::Path pathPattern(path + "." + *ext);
+              if (ext->find("*") != std::string::npos)
+              {
+                continue;
+                std::set<std::string> files;
+                Kernel::Glob::glob(pathPattern, files);
+                std::cerr<<"Searching for:"<<pathPattern.toString()<<'\n';
+                std::cerr<<"Found:"<<files.size()<<'\n';
+              }
+              else
+              {
+                Poco::File file(pathPattern);
+                if (file.exists())
+                {
+                  return file.path();
+                }
+              }
+            }
+          }
+        }
+      }
       return "";
     }
 
diff --git a/Code/Mantid/API/test/FileFinderTest.h b/Code/Mantid/API/test/FileFinderTest.h
index 4d29264a7d3..b38577a2fb1 100644
--- a/Code/Mantid/API/test/FileFinderTest.h
+++ b/Code/Mantid/API/test/FileFinderTest.h
@@ -24,7 +24,7 @@ public:
 
     const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
       "<facilities>"
-      "  <facility name=\"ISIS\" zeropadding=\"5\" FileExtensions=\"nxs,raw,sav,n*,s*\">"
+      "  <facility name=\"ISIS\" zeropadding=\"5\" FileExtensions=\"nxs,raw,sav,n*,N*,s*,S*\">"
       "    <instrument name=\"HRPD\" shortname=\"HRP\">"
       "      <technique>Powder Diffraction</technique>"
       "    </instrument>"
@@ -84,10 +84,20 @@ public:
 
   void testFindFile()
   {
+    ConfigService::Instance().setString("datasearch.searcharchive","Off");
     std::string path = FileFinder::Instance().findFile("CSP78173");
     TS_ASSERT(path.find("CSP78173.raw") != std::string::npos);
     Poco::File file(path);
     TS_ASSERT(file.exists());
+    path = FileFinder::Instance().findFile("HRP37129");
+    std::cerr<<"Path: "<<path<<'\n';
+    TS_ASSERT(path.size() > 3);
+    TS_ASSERT_EQUALS(path.substr(path.size()-3),"S02");
+    //ConfigService::Instance().setString("datasearch.searcharchive","On");
+    //path = FileFinder::Instance().findFile("CSP77374");
+    //std::cerr<<"Path: "<<path<<'\n';
+    //path = FileFinder::Instance().findFile("CSP78174");
+    //std::cerr<<"Path: "<<path<<'\n';
   }
 
   void testFindFiles()
diff --git a/Code/Mantid/Kernel/src/ConfigService.cpp b/Code/Mantid/Kernel/src/ConfigService.cpp
index ac4106daa04..1443370183e 100644
--- a/Code/Mantid/Kernel/src/ConfigService.cpp
+++ b/Code/Mantid/Kernel/src/ConfigService.cpp
@@ -165,7 +165,7 @@ namespace Kernel
    */
   ConfigServiceImpl::~ConfigServiceImpl()
   {
-    std::cerr << "ConfigService destroyed." << std::endl;
+    //std::cerr << "ConfigService destroyed." << std::endl;
     Kernel::Logger::shutdown();
     delete m_pSysConfig;
     delete m_pConf;                // potential double delete???
diff --git a/Code/Mantid/Properties/Mantid.properties b/Code/Mantid/Properties/Mantid.properties
index 1b3f182fbb6..8bcb5286210 100644
--- a/Code/Mantid/Properties/Mantid.properties
+++ b/Code/Mantid/Properties/Mantid.properties
@@ -58,6 +58,9 @@ pythonalgorithms.directories = ../PythonAPI/PythonAlgorithms
 # Use forward slash / for all paths
 datasearch.directories = 
 
+# Setting this to On enables searching the facilitie's archive automatically
+datasearch.searcharchive = Off
+
 # A default directory to use for saving files
 # Use forward slash / for all paths
 defaultsave.directory = 
-- 
GitLab