commit 55d355610b4a922515ebdab1f1a4060d93e4c285
Author: Eliška Mourycová <eliska.mourycova@seznam.cz>
Date:   Wed Apr 28 08:17:50 2021 +0200

    Re #8691. Implemented downloaded data retrieving.

diff --git a/Server/ServerApp/DataDownload/DataDownloader.cs b/Server/ServerApp/DataDownload/DataDownloader.cs
index 565eda0..e76b3d6 100644
--- a/Server/ServerApp/DataDownload/DataDownloader.cs
+++ b/Server/ServerApp/DataDownload/DataDownloader.cs
@@ -60,6 +60,7 @@ namespace ServerApp.DataDownload
 		// a shortcut to writing Path.DirectorySeparatorChar
 		private char sep = Path.DirectorySeparatorChar;
 
+		// lists used for parsing the file names:
 		private List<string> separatedFileName;
 		private List<string> variablesInsertions;
 		private List<char> nameTurns;
@@ -68,8 +69,6 @@ namespace ServerApp.DataDownload
 		{
 			// initialize all needed variables:
 
-			DataSubDirectories = new Dictionary<DataType, string>();
-			
 			//Console.WriteLine(Directory.GetCurrentDirectory());
 			site = website;//"http://openstore.zcu.cz/";
 
@@ -79,9 +78,33 @@ namespace ServerApp.DataDownload
 			RootDataDirectory = rootDataDir;//$"..{sep}..{sep}..{sep}data{sep}auto";
 			OverwriteExisting = false;
 
+			DataSubDirectories = new Dictionary<DataType, string>();
+			CreateSubdirectories(); // todo should we do it like this?
+
 			webClient = new WebClient();
 		}
 
+		/// <summary>
+		/// Creates subdirectories for all data types
+		/// TBD if we want to do it this way
+		/// </summary>
+		private void CreateSubdirectories()
+		{
+			foreach (DataType type in (DataType[])Enum.GetValues(typeof(DataType)))
+			{
+				string subDirectory = RootDataDirectory + sep + type;
+				DirectoryInfo di = Directory.CreateDirectory(subDirectory);
+
+				// create subdirectory record if it doesn't exist:
+				if (!DataSubDirectories.ContainsKey(type))
+					DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
+			}
+		}
+
+		/// <summary>
+		/// Parses the naming convention to be later used for determining URLs for data download
+		/// </summary>
+		/// <param name="namingConvention">The configured naming convention</param>
 		private void ParseNaming(string namingConvention)
 		{
 			separatedFileName = new List<string>();
@@ -124,6 +147,7 @@ namespace ServerApp.DataDownload
 			Console.WriteLine();
 		}
 
+		// Adds to name parts
 		private void AddToNameParts(string s)
 		{
 			if (s.Length > 0)
@@ -134,6 +158,7 @@ namespace ServerApp.DataDownload
 				
 		}
 
+		// Adds to variable name parts
 		private void AddToVariables(string s)
 		{
 			if (s.Length > 0)
@@ -144,7 +169,14 @@ namespace ServerApp.DataDownload
 				
 		}
 
-
+		/// <summary>
+		/// Builds the name of the downloaded file. Takes naming convention into account
+		/// </summary>
+		/// <param name="type">The type of data</param>
+		/// <param name="format">The data format</param>
+		/// <param name="year">The year</param>
+		/// <param name="month">The month</param>
+		/// <returns></returns>
 		private string BuildDownloadedName(DataType type, DataFormat format, int year, int month)
 		{
 			string nameZip = "";
@@ -186,11 +218,6 @@ namespace ServerApp.DataDownload
 		}
 
 
-		private void ExtractDateFromFileName(string fileName, ref int month, ref int year)
-		{
-
-		}
-
 		/// <summary>
 		/// Downloads a specific archive.
 		/// </summary>
@@ -285,6 +312,11 @@ namespace ServerApp.DataDownload
 		/// Downloads selected type and time span of data in the desired format, returns a list of full paths to all successfully saved files. 
 		/// If some of the files already existed and were not overwritten, then the returned List contains paths to these files also.
 		/// </summary>
+		/// <param name="type">The requested data type</param>
+		/// <param name="format">The data format</param>
+		/// <param name="startDate">The start date</param>
+		/// <param name="endDate">The end date</param>
+		/// <returns></returns>
 		public List<string> DownloadData(DataType type, DataFormat format, Date startDate, Date endDate/*int startYear, int endYear, int startMonth, int endMonth*/)
 		{
 			if (startDate > endDate)
@@ -292,12 +324,12 @@ namespace ServerApp.DataDownload
 
 			// initialize:
 			List<string> savedFiles = new List<string>();
-			string subDirectory = RootDataDirectory + sep + type;
-			DirectoryInfo di = Directory.CreateDirectory(subDirectory);
+			//string subDirectory = RootDataDirectory + sep + type;
+			//DirectoryInfo di = Directory.CreateDirectory(subDirectory);
 
-			// create subdirectory record if it doesn't exist:
-			if (!DataSubDirectories.ContainsKey(type))
-				DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
+			//// create subdirectory record if it doesn't exist:
+			//if (!DataSubDirectories.ContainsKey(type))
+			//	DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
 
 
 			Date currentDate = startDate;
@@ -343,11 +375,35 @@ namespace ServerApp.DataDownload
 		}
 
 
-
+		/// <summary>
+		/// Retrieves all data files with dates falling within the specified range. If not all data for the specified range is found
+		/// then returns also file/s with month 0 if exists.
+		/// </summary>
+		/// <param name="subDirectory">The subdirectory to search</param>
+		/// <param name="startDate">The start date</param>
+		/// <param name="endDate">The end date</param>
+		/// <returns></returns>
 		public List<string> GetData(string subDirectory, Date startDate, Date endDate)
 		{
 			string[] files = Directory.GetFiles(subDirectory);
-			for(int i = 0; i < files.Length; i++)
+			List<string> found00Files = new List<string>();
+			List<string> relevantFiles = new List<string>();
+			List<Date> requestedDates = new List<Date>();
+
+			// prepare a list of requested dates:
+			Date currentDate = startDate;
+			do
+			{
+				requestedDates.Add(currentDate);
+				Date nextDate = currentDate.IncreaseMonthByOne();
+				// assign the increased date to the current date:
+				currentDate = nextDate;
+
+			} while (currentDate <= endDate);
+
+
+
+			for (int i = 0; i < files.Length; i++)
 			{
 				string currFileName = Path.GetFileName(files[i]);
 				Console.WriteLine("curr file: " + currFileName);
@@ -357,12 +413,46 @@ namespace ServerApp.DataDownload
 				int year = int.Parse(splits[1]);
 
 
+				
+				if (month == 0)
+				{
+					found00Files.Add(files[i]);
+					continue;
+				}
+
 				Date d = new Date((uint)month, (uint)year);
 
+				if (d >= startDate && d <= endDate)
+				{
+					// we want to add this
+					relevantFiles.Add(files[i]);
+					requestedDates.Remove(d);
+				}
+			}
+
+
+
+
+			// 00 will only appear once for every year?
+			foreach (string file00 in found00Files)
+			{
+				string fileName = Path.GetFileName(file00);
+				string[] splits = fileName.Split(new char[] { '-', '.' });
 
+				int month = int.Parse(splits[0]);
+				int year = int.Parse(splits[1]);
+
+				// now we have the year of one 00 file
+				// dates not found in the directory now remain in requested dates
+				if(requestedDates.Exists(d => d.Year == year))
+				{
+					// if the year of this 00 file remains in the list:
+					relevantFiles.Add(file00);
+				}
 			}
 
-			throw new NotImplementedException();
+
+			return relevantFiles;
 		}
 
 
diff --git a/Server/ServerApp/DataDownload/Date.cs b/Server/ServerApp/DataDownload/Date.cs
index d22019c..3d6147d 100644
--- a/Server/ServerApp/DataDownload/Date.cs
+++ b/Server/ServerApp/DataDownload/Date.cs
@@ -19,7 +19,10 @@ namespace ServerApp.DataDownload
 		}
 
 
-
+		/// <summary>
+		/// Increases the month by one. Increases year and sets month to 1 if need be.
+		/// </summary>
+		/// <returns>An increased date</returns>
 		public Date IncreaseMonthByOne()
 		{
 			uint newMonth = Month;
diff --git a/Server/ServerApp/Program.cs b/Server/ServerApp/Program.cs
index 84d25b9..c34d559 100644
--- a/Server/ServerApp/Program.cs
+++ b/Server/ServerApp/Program.cs
@@ -52,7 +52,7 @@ namespace ServerApp
 			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
 			dd.OverwriteExisting = false;
 			List<string> savedFiles = new List<string>();
-			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new Date(1, 2019), new Date(12, 2020)));
+			//savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new Date(1, 2019), new Date(12, 2020)));
 			//savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
 			//savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
 
@@ -70,7 +70,12 @@ namespace ServerApp
 				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
 			}
 
-			dd.GetData(dd.DataSubDirectories[DataType.JIS], new Date(1, 2019), new Date(12, 2020));
+			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new Date(10, 2019), new Date(12, 2020));
+			Console.WriteLine("Retrieved data: ");
+			foreach (string s in retrievedData)
+			{
+				Console.WriteLine(s);
+			}
 
 
 
