Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / StatusChecker.cs @ 5bcf6d70

History | View | Annotate | Download (3.2 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.Composition;
4
using System.Diagnostics;
5
using System.Diagnostics.Contracts;
6
using System.IO;
7
using Castle.ActiveRecord;
8
using Castle.ActiveRecord.Framework.Config;
9
using Pithos.Interfaces;
10

    
11
namespace Pithos.Core
12
{
13
    [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
14
    public class StatusChecker:IStatusChecker,IStatusKeeper
15
    {
16
        [System.ComponentModel.Composition.Import]
17
        public IPithosSettings Settings { get; set; }
18

    
19

    
20
        public StatusChecker()
21
        {
22
            var source = new XmlConfigurationSource("DbConfig.xml");
23
            ActiveRecordStarter.Initialize(source,typeof(FileState));            
24
            
25
            if (!File.Exists("pithos.db"))
26
                ActiveRecordStarter.CreateSchema();            
27
            
28
        }
29

    
30
        public FileOverlayStatus GetFileOverlayStatus(string path)
31
        {
32
            try
33
            {
34
                var state = FileState.TryFind(path);
35
                return state == null ? FileOverlayStatus.NA : state.OverlayStatus;
36
            }
37
            catch (Exception exc)
38
            {
39
                Trace.TraceError(exc.ToString());
40
                return FileOverlayStatus.NA;
41
            }
42
        }
43

    
44

    
45
        private PithosStatus _pithosStatus=PithosStatus.InSynch;
46
        public void SetPithosStatus(PithosStatus status)
47
        {
48
            _pithosStatus = status;
49
        }
50

    
51
        public PithosStatus GetPithosStatus()
52
        {
53
            return _pithosStatus;
54
        }
55

    
56
        public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
57
        {
58
            var state = FileState.TryFind(path);
59
            if (state != null)
60
            {
61
                state.OverlayStatus = overlayStatus;
62
                state.Update();
63
            }
64
            else
65
            {
66
                state=new FileState{FilePath=path,OverlayStatus=overlayStatus};
67
                state.Save();
68
            }
69
        }
70

    
71
        public void RemoveFileOverlayStatus(string path)
72
        {
73
            FileState.DeleteAll(new[] {path});            
74
        }
75

    
76
        public void RenameFileOverlayStatus(string oldPath, string newPath)
77
        {
78
            var state = FileState.Find(oldPath);
79
            //TODO: This will cause problems if path is used as a key in relationships
80
            state.FilePath = newPath;
81
            state.Update();
82
        }
83

    
84
        public void SetFileStatus(string path, FileStatus status)
85
        {
86
            var state = FileState.Find(path);
87
            state.FileStatus = status;
88
        }
89

    
90
        public FileStatus GetFileStatus(string path)
91
        {
92
            var state = FileState.TryFind(path);
93
            return (state==null)?FileStatus.Missing:state.FileStatus ;
94
        }
95

    
96
        public void ClearFileStatus(string path)
97
        {
98
            //TODO:SHOULDN'T need both clear file status and remove overlay status
99
            FileState.DeleteAll(new[] { path });   
100
        }
101

    
102
        public void UpdateFileChecksum(string path, string checksum)
103
        {
104
            var state = FileState.Find(path);
105
            state.Checksum = checksum;
106
            state.Update();
107
        }
108
    }
109
}