Modified loggers to use their enclosing class
[pithos-ms-client] / trunk / Pithos.Core / FileState.cs
index 90182d3..5fbacbd 100644 (file)
@@ -1,55 +1,61 @@
-// -----------------------------------------------------------------------
-// <copyright file="FileState.cs" company="GRNet">
-// Copyright 2011 GRNET S.A. All rights reserved.
-// 
-// Redistribution and use in source and binary forms, with or
-// without modification, are permitted provided that the following
-// conditions are met:
-// 
-//   1. Redistributions of source code must retain the above
-//      copyright notice, this list of conditions and the following
-//      disclaimer.
-// 
-//   2. Redistributions in binary form must reproduce the above
-//      copyright notice, this list of conditions and the following
-//      disclaimer in the documentation and/or other materials
-//      provided with the distribution.
-// 
-// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
-// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
-// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
-// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-// POSSIBILITY OF SUCH DAMAGE.
-// 
-// The views and conclusions contained in the software and
-// documentation are those of the authors and should not be
-// interpreted as representing official policies, either expressed
-// or implied, of GRNET S.A.
-// </copyright>
-// -----------------------------------------------------------------------
-
-
+#region
+/* -----------------------------------------------------------------------
+ * <copyright file="FileState.cs" company="GRNet">
+ * 
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ * </copyright>
+ * -----------------------------------------------------------------------
+ */
+#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;
 using log4net;
 
 namespace Pithos.Core
 {
     using System;
-    using System.Collections.Generic;
-    using System.Linq;
+    using System.Collections.Generic;    
 
     /// <summary>
     /// TODO: Update summary.
@@ -57,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>();
 
@@ -104,7 +111,30 @@ namespace Pithos.Core
             set { _tags = value; }
         }
 
+        [Property]
+        public DateTime Modified { get; set; }
+
 
+        public FileSystemInfo GetFileSystemInfo()
+        {
+            if (String.IsNullOrWhiteSpace(FilePath))
+                throw new InvalidOperationException();
+            Contract.EndContractBlock();
+
+            return Directory.Exists(FilePath) ?
+                (FileSystemInfo)new DirectoryInfo(FilePath)
+                : new FileInfo(FilePath);
+        }
+
+        public string GetRelativeUrl(AccountInfo accountInfo)
+        {
+            if (accountInfo==null)
+                throw new ArgumentNullException("accountInfo");
+            Contract.EndContractBlock();
+
+            var fsi=GetFileSystemInfo();
+            return fsi.AsRelativeUrlTo(accountInfo.AccountPath);
+        }
         /*public static FileState FindByFilePath(string absolutePath)
         {
             if (string.IsNullOrWhiteSpace(absolutePath))
@@ -390,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")]