From 936362609f1d881188922e408416f58dba270d10 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Tue, 19 Dec 2017 17:57:42 +0100
Subject: [PATCH] Move validation of status into the ServiceStatus class.

---
 AutoTx/AutoTx.cs                   | 24 ++++++-----------------
 AutoTx/XmlWrapper/ServiceStatus.cs | 31 ++++++++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index a9c0123..4a5b80b 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -146,7 +146,7 @@ namespace AutoTx
         private void LoadStatusXml() {
             try {
                 writeLogDebug("Trying to load status from " + _statusPath);
-                _status = ServiceStatus.Deserialize(_statusPath);
+                _status = ServiceStatus.Deserialize(_statusPath, _config);
                 writeLogDebug("Loaded status from " + _statusPath);
             }
             catch (Exception ex) {
@@ -170,22 +170,6 @@ namespace AutoTx
                     writeLog("ERROR checking spooling directories (incoming / managed)!");
                     configInvalid = true;
                 }
-
-                // CurrentTransferSrc
-                if (_status.CurrentTransferSrc.Length > 0
-                    && !Directory.Exists(_status.CurrentTransferSrc)) {
-                    writeLog("WARNING: status file contains non-existing source path of an " +
-                             "unfinished transfer: " + _status.CurrentTransferSrc);
-                    _status.CurrentTransferSrc = "";
-                }
-
-                // CurrentTargetTmp
-                if (_status.CurrentTargetTmp.Length > 0
-                    && !Directory.Exists(ExpandCurrentTargetTmp())) {
-                    writeLog("WARNING: status file contains non-existing temporary path of an " +
-                             "unfinished transfer: " + _status.CurrentTargetTmp);
-                    _status.CurrentTargetTmp = "";
-                }
             }
             catch (Exception ex) {
                 writeLog("Error in CheckConfiguration(): " + ex.Message + " " + ex.StackTrace);
@@ -285,7 +269,11 @@ namespace AutoTx
 
             if (!string.IsNullOrEmpty(_config.ValidationWarnings)) {
                 writeLog("WARNING: some configuration settings might not be optimal:\n" +
-                    _config.ValidationWarnings);
+                         _config.ValidationWarnings);
+            }
+            if (!string.IsNullOrEmpty(_status.ValidationWarnings)) {
+                writeLog("WARNING: some status parameters were invalid and have been reset:\n" +
+                         _status.ValidationWarnings);
             }
         }
 
diff --git a/AutoTx/XmlWrapper/ServiceStatus.cs b/AutoTx/XmlWrapper/ServiceStatus.cs
index 4cd705a..de48e5c 100644
--- a/AutoTx/XmlWrapper/ServiceStatus.cs
+++ b/AutoTx/XmlWrapper/ServiceStatus.cs
@@ -7,7 +7,9 @@ namespace AutoTx.XmlWrapper
     [Serializable]
     public class ServiceStatus
     {
-        [NonSerialized] string _storageFile; // remember where we came from
+        [XmlIgnore] string _storageFile; // remember where we came from
+        [XmlIgnore] private ServiceConfig _config; 
+        [XmlIgnore] public string ValidationWarnings;
         
         private DateTime _lastStatusUpdate;
         private DateTime _lastStorageNotification;
@@ -154,8 +156,9 @@ namespace AutoTx.XmlWrapper
              */
         }
 
-        public static ServiceStatus Deserialize(string file) {
+        public static ServiceStatus Deserialize(string file, ServiceConfig config) {
             ServiceStatus status;
+
             var xs = new XmlSerializer(typeof(ServiceStatus));
             try {
                 var reader = File.OpenText(file);
@@ -166,9 +169,33 @@ namespace AutoTx.XmlWrapper
                 // if reading the status XML fails, we return an empty (new) one
                 status = new ServiceStatus();
             }
+            status._config = config;
+            ValidateStatus(status);
             // now set the storage filename:
             status._storageFile = file;
             return status;
         }
+
+        private static void ValidateStatus(ServiceStatus s) {
+            // CurrentTransferSrc
+            if (s.CurrentTransferSrc.Length > 0
+                && !Directory.Exists(s.CurrentTransferSrc)) {
+                s.ValidationWarnings += " - found non-existing source path of an unfinished " +
+                                        "transfer: " + s.CurrentTransferSrc + "\n";
+                s.CurrentTransferSrc = "";
+            }
+
+            // CurrentTargetTmp
+            var currentTargetTmpPath = Path.Combine(s._config.DestinationDirectory,
+                s._config.TmpTransferDir,
+                s.CurrentTargetTmp);
+            if (s.CurrentTargetTmp.Length > 0
+                && !Directory.Exists(currentTargetTmpPath)) {
+                s.ValidationWarnings += " - found non-existing temporary path of an " +
+                                        "unfinished transfer: " + currentTargetTmpPath+ "\n";
+                s.CurrentTargetTmp = "";
+            }
+
+        }
     }
 }
-- 
GitLab