Added log4net for client profile
[pithos-ms-client] / trunk / Pithos.Core / FileState.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="FileState.cs" company="Microsoft">
3 // TODO: Update copyright text.
4 // </copyright>
5 // -----------------------------------------------------------------------
6
7 using System.Diagnostics.Contracts;
8 using System.IO;
9 using System.Threading.Tasks;
10 using Castle.ActiveRecord;
11 using Castle.ActiveRecord.Framework;
12 using Castle.ActiveRecord.Queries;
13 using NHibernate.Engine;
14 using Pithos.Core.Agents;
15 using Pithos.Interfaces;
16 using Pithos.Network;
17
18 namespace Pithos.Core
19 {
20     using System;
21     using System.Collections.Generic;
22     using System.Linq;
23     using System.Text;
24
25     /// <summary>
26     /// TODO: Update summary.
27     /// </summary>
28     [ActiveRecord]
29     public class FileState:ActiveRecordLinqBase<FileState>
30     {
31         private string _filePath;
32         private IList<FileTag> _tags=new List<FileTag>();
33
34         [PrimaryKey(PrimaryKeyType.Guid)]
35         public Guid Id { get; set; }
36
37         [Property(Unique=true,UniqueKey="IX_FileState_FilePath")]
38         public string FilePath
39         {
40             get { return _filePath; }
41             set { _filePath = value.ToLower(); }
42         }
43
44         [Property]
45         public FileOverlayStatus OverlayStatus { get; set; }
46
47         [Property]
48         public FileStatus FileStatus { get; set; }
49
50         [Property]
51         public string Checksum { get; set; }
52
53 /*
54         [Property]
55         public string TopHash { get; set; }
56 */
57
58         [Property]
59         public long? Version { get; set; }
60
61         [Property]
62         public DateTime? VersionTimeStamp { get; set; }
63
64
65         [Property]
66         public bool IsShared { get; set; }
67
68         [Property]
69         public string SharedBy { get; set; }
70
71         [Property]
72         public bool ShareWrite { get; set; }
73
74
75        [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true,Inverse=true)]
76         public IList<FileTag> Tags
77         {
78             get { return _tags; }   
79             set { _tags=value;}
80         }
81
82 //        [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
83 //        public IList<FileHash> Hashes { get; set; }
84
85 //        [Property]
86 //        public byte[] HashmapHash { get; set; }
87        
88         public static FileState FindByFilePath(string absolutePath)
89         {
90             if (string.IsNullOrWhiteSpace(absolutePath))
91                 throw new ArgumentNullException("absolutePath");
92             Contract.EndContractBlock();
93             return Queryable.FirstOrDefault(s => s.FilePath == absolutePath.ToLower());
94         }
95
96         public static void DeleteByFilePath(string absolutePath)
97         {
98             if(string.IsNullOrWhiteSpace(absolutePath))
99                 throw new ArgumentNullException("absolutePath");
100             Contract.EndContractBlock();
101             
102             FileState.Execute((session, instance) =>
103                              {
104                                  const string hqlDelete = "delete FileState where FilePath = :path";                                 
105                                  var deletedEntities = session.CreateQuery(hqlDelete)
106                                          .SetString("path", absolutePath.ToLower())
107                                          .ExecuteUpdate();
108                                  return null;
109                              },null);
110             
111         }
112
113         public static Task<FileState> CreateForAsync(string filePath,int blockSize,string algorithm)
114         {
115             if (blockSize <= 0)
116                 throw new ArgumentOutOfRangeException("blockSize");
117             if (String.IsNullOrWhiteSpace(algorithm))
118                 throw new ArgumentNullException("algorithm");
119             Contract.EndContractBlock();
120
121
122             var fileState = new FileState
123                                 {
124                                     FilePath = filePath, 
125                                     OverlayStatus = FileOverlayStatus.Unversioned, 
126                                     FileStatus = FileStatus.Created,
127                                     Id=Guid.NewGuid()
128                                 };
129
130
131             return fileState.UpdateHashesAsync(blockSize,algorithm);            
132         }
133
134         public Task<FileState> UpdateHashesAsync(int blockSize,string algorithm)
135         {
136             if (blockSize<=0)
137                 throw new ArgumentOutOfRangeException("blockSize");
138             if (String.IsNullOrWhiteSpace(algorithm))
139                 throw new ArgumentNullException("algorithm");
140             Contract.EndContractBlock();
141             
142             //Skip updating the hash for folders
143             if (Directory.Exists(FilePath))
144                 return Task.Factory.FromResult(this);
145
146             var results = Task.Factory.StartNew(() =>
147             {
148                 var info = new FileInfo(FilePath);
149                 return info.CalculateHash(blockSize, algorithm);
150             });
151
152             var state=results.Then(hash =>
153             {
154                 Checksum = hash;
155                 return Task.Factory.FromResult(this);
156             });
157             
158             return state;
159         }
160     }
161
162     [ActiveRecord("Tags")]
163     public class FileTag : ActiveRecordLinqBase<FileTag>
164     {
165         [PrimaryKey]
166         public int Id { get; set; }
167
168         [Property]
169         public string Name { get; set; }
170
171         [Property]
172         public string Value { get; set; }
173
174         [BelongsTo("FileStateId")]
175         public FileState FileState { get; set; }
176
177     }
178     
179   /*  [ActiveRecord("hashes")]
180     public class FileHash : ActiveRecordLinqBase<FileHash>
181     {
182         [PrimaryKey]
183         public int Id { get; set; }
184
185         [Property]
186         public int Order { get; set; }
187
188         [Property]
189         public byte[] Value { get; set; }        
190
191         [BelongsTo("FileStateID")]
192         public FileState FileState { get; set; }
193
194     }*/
195
196
197 }