Modified loggers to use their enclosing class
[pithos-ms-client] / trunk / Pithos.Core / FileState.cs
index 5e05c35..5fbacbd 100644 (file)
 #endregion
 using System.Diagnostics.Contracts;
 using System.IO;
+using System.Reflection;
 using System.Threading.Tasks;
 using Castle.ActiveRecord;
 using Castle.ActiveRecord.Framework;
+using Castle.ActiveRecord.Queries;
+using NHibernate.Criterion;
 using Pithos.Core.Agents;
 using Pithos.Interfaces;
 using Pithos.Network;
@@ -52,8 +55,7 @@ using log4net;
 namespace Pithos.Core
 {
     using System;
-    using System.Collections.Generic;
-    using System.Linq;
+    using System.Collections.Generic;    
 
     /// <summary>
     /// TODO: Update summary.
@@ -61,7 +63,8 @@ namespace Pithos.Core
     [ActiveRecord]
     public class FileState : ActiveRecordLinqBase<FileState>
     {
-        private static readonly ILog Log = LogManager.GetLogger("FileState");
+        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
 
         private IList<FileTag> _tags = new List<FileTag>();
 
@@ -417,6 +420,48 @@ namespace Pithos.Core
                         throw;
                 }
         }
+
+        /// <summary>
+        /// Mark Unversioned all FileState rows from the database whose path
+        /// starts with one of the removed paths
+        /// </summary>
+        /// <param name="removed"></param>
+        public static void UnversionPaths(List<string> removed)
+        {
+            if (removed == null)
+                return;
+            if (removed.Count == 0)
+                return;
+
+            //Create a disjunction (list of OR statements
+            var disjunction = new Disjunction();            
+            foreach (var path in removed)
+            {
+                //with the restriction FileState.FilePath like '@path%'
+                disjunction.Add(Restrictions.On<FileState>(s => s.FilePath)
+                    .IsLike(path, MatchMode.Start));
+            }
+
+            //Generate a query from the disjunction
+            var query=QueryOver.Of<FileState>().Where(disjunction);
+                        
+            ExecuteWithRetry((session,instance)=>
+                                 {
+                                     using (var t=session.BeginTransaction())
+                                     {
+                                         var states = query.GetExecutableQueryOver(session).List();
+                                         foreach (var state in states)
+                                         {
+                                             state.FileStatus = FileStatus.Unversioned;
+                                             state.OverlayStatus = FileOverlayStatus.Unversioned;
+                                             state.Update();
+                                         }
+                                         t.Commit();
+                                     }
+                                     return null;
+                                 },null);
+        }
+
     }
 
     [ActiveRecord("Tags")]