diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs deleted file mode 100644 index e76171b..0000000 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs +++ /dev/null @@ -1,39 +0,0 @@ - -using TeamHobby.HobbyProjectGenerator.DataAccess; - -namespace TeamHobby.HobbyProjectGenerator.Archive -{ - public class ArchiveController - { - private IDataSource _conn; - - // Constructor to take in the connection. - public ArchiveController(IDataSource dataSource) { - _conn = dataSource; - } - - public IDataSource GetDataSource(){ - return _conn; - } - - // Create the folder where the compress file will be stored - public bool CreateArchiveFolder(){ - return true; - } - - // Return the name of the new output file with Path - public string CreateOutFileName(){ - return "hello"; - } - - // put everything toget here - public bool Controller(){ - return true; - } - - - - - - } -} \ No newline at end of file diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs new file mode 100644 index 0000000..256ce31 --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs @@ -0,0 +1,161 @@ + +using TeamHobby.HobbyProjectGenerator.DataAccess; + +namespace TeamHobby.HobbyProjectGenerator.Archive +{ + public class ArchiveManager + { + private IDataSource _conn; + + // Constructor to take in the connection. + public ArchiveManager(IDataSource dataSource) { + _conn = dataSource; + } + + + // Create the folder where the compress file will be stored + public bool CreateArchiveFolder(){ + //string curPath = Directory.GetCurrentDirectory(); + string curPath = @"C:\"; + string folderName = "\\HobbyArchive"; + + // Find the root drive of the program + string drive = Path.GetPathRoot(path: curPath); + // Check if Drive exists or not + if (!Directory.Exists(drive)) + { + Console.WriteLine("Drive {0} not found", drive); + return false; + } + + string archiveFolder = curPath + folderName; + //Console.WriteLine("Creating a new folder at: {0}", archiveFolder); + if (!Directory.Exists(archiveFolder)) + { + Console.WriteLine("Creating a new folder at: {0}", archiveFolder); + Directory.CreateDirectory(archiveFolder); + } + return true; + } + + + // Return the name of the new output file with Path + public string CreateOutFileName() { + try { + // Creating a file name: + string path = @"C:\HobbyArchive"; + //string path = Directory.GetCurrentDirectory() + "\\HobbyArchive"; + Console.WriteLine("The current directory is {0}", path); + string date = DateTime.Now.ToString("M_d_yyyy"); + string fileName = date + "_archive.csv"; + string filePath = System.IO.Path.Combine(path, fileName); + + Console.WriteLine("Date: {0}", date); + Console.WriteLine("Filepath: {0}", filePath); + return filePath; + } + catch + { + Console.WriteLine("ArchiveCon: Creating a file name failed. "); + return ""; + } + } + + // Method overload to take in a specific path + public string CreateOutFileName(string archivePath) + { + try + { + //string path = archivePath + "\\HobbyArchive"; + Console.WriteLine("The current directory is {0}", archivePath); + //string date = DateTime.Now.ToString("M_d_yyyy_H:mm:ss"); + string date = DateTime.Now.ToString("M_d_yyyy"); + string fileName = date + "_archive.csv"; + string filePath = Path.Combine(archivePath, fileName); + + Console.WriteLine("Date: {0}", date); + Console.WriteLine("Filepath: {0}", filePath); + return filePath; + } + catch + { + Console.WriteLine("ArchiveCon: Creating a file name failed. "); + return ""; + } + } + + // put everything toget here + public bool Controller(){ + + SqlDAO sqlDS = null; + if (_conn.GetType() == typeof(SqlDAO)) + { + sqlDS = (SqlDAO)_conn; + + } + // TODO: Remember to add try catch block here to make sure it is completed. + + //Creating the folder Archive + Console.WriteLine("Creating a new folder ..."); + CreateArchiveFolder(); + Console.WriteLine("Creating a new folder completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + // Creating a file name: + string filePath = @"C:\HobbyArchive"; + //Console.WriteLine("Creating file name ... "); + //string curPath = archive.CreateOutFileName(); + Console.WriteLine("Creating Output file name ..."); + string curPath = CreateOutFileName(); + Console.WriteLine("Output file name created ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + //string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive"; + //string pathTemp = "C:/Temp/oldlogs10.txt"; + //string pathTempBack = @"C:\Temp\oldlogs10.txt"; + + //Output SQL to a text file + Console.WriteLine("Copying to a text file ..."); + sqlDS.CopyToFile(curPath); + Console.WriteLine("Copying completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + // Compress the file + Console.WriteLine("Copressing the text file ..."); + sqlDS.CompressFile(curPath); + Console.WriteLine("Copression completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + //Remove output file + Console.WriteLine("Removing the text file ..."); + sqlDS.RemoveOutputFile(curPath); + Console.WriteLine("Text File removal completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + // Remove entries fromt the database + Console.WriteLine("Removing the text file ..."); + sqlDS.RemoveEntries(); + Console.WriteLine("Entries removal completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + // Testing date time patters + //string date = DateTime.Now.ToString("d"); + //Console.WriteLine(date); + //Console.WriteLine(DateTime.Now.ToString("M_d_yyyy_H:mm:ss")); + + return true; + + } + + + + + + } +} \ No newline at end of file diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index d68b6bf..1843b50 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -36,7 +36,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess return _conn; } - // Closing a connection here will cause a problem + // TODO: Closing a connection here will cause a problem // Make sure whoever called this need to close the connection until we can fixed it. public Object? ReadData(string cmd) { @@ -175,17 +175,68 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess // Copy the Sql data to the a text file public bool CopyToFile(string filePath){ - return true; + try + { + _conn.Open(); + // Conver backward slash in to forward slash + filePath = filePath.Replace("\\", "/"); + string sqlQuery = "SELECT 'LtimeStamp', 'logID', 'LvName', 'catName', 'userOP', 'logMessage' " + + "UNION ALL " + + "SELECT* FROM log " + + "WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30 " + + "INTO OUTFILE '" + filePath + "'" + + "FIELDS ENCLOSED BY ''" + + "TERMINATED BY ',' " + + "LINES TERMINATED BY '\r\n';"; + + Console.WriteLine(sqlQuery); + + OdbcCommand command = new OdbcCommand(sqlQuery, _conn); + //command.Parameters.AddWithValue("@filePath", filePath); + //command.Prepare(); + + Console.WriteLine("Output to a file started. "); + command.ExecuteNonQuery(); + Console.WriteLine("Output to a file completed. "); + + + _conn.Close(); + return true; + + } + catch + { + _conn.Close(); + Console.WriteLine("Error when copying query to a file !!"); + throw; + } + finally + { + _conn.Close(); + } } public bool RemoveOutputFile(string filePath){ - return true; + try + { + if (File.Exists(filePath)) + { + Console.WriteLine("File at: {0} exist, proceed to remove.", filePath); + File.Delete(filePath); + return true; + } + return false; + } + catch { + Console.WriteLine("Removing file failed!!"); + return false; + } } // Remove data from the database, if failed, let the controller handle it. public bool RemoveEntries() { - string sqlCmd = "DELETE FROM log WHERE DATE_DIFF(current_timestamp, log.LtimeStamp) > 30"; + string sqlCmd = "DELETE FROM log WHERE DATEDIFF(current_timestamp, log.LtimeStamp) > 30"; try{ DeleteData(sqlCmd); diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 956702e..c02a6f4 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -1,6 +1,7 @@ using main; using System; using System.Data.Odbc; +using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; @@ -63,10 +64,57 @@ namespace TeamHobby.HobbyProjectGenerator.Main Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]); } SqlDAO sqlDS = (SqlDAO)datasource; + Console.WriteLine(""); // Closing the connection sqlDS.getConnection().Close();*/ + // While loop to keep this running forever with UserManagement + // Testing archive Manager + //while (true) + //{ + string currentDate = DateTime.Now.ToString("dd"); + string currentTime = DateTime.Now.ToString("T"); + Console.WriteLine("Current date: {0}, Current Time: {1}", currentDate, currentTime); + if (String.Equals(currentDate, "1") && String.Equals(currentTime, "00:00:00 AM")){ + ArchiveManager archive = new ArchiveManager(datasource); + archive.Controller(); + } + //} + + + + //Creating the folder Archive + //Console.WriteLine("Creating a new folder ..."); + //archive.CreateArchiveFolder(); + //Console.WriteLine(""); + + //// Creating a file name: + //string filePath = @"C:\HobbyArchive"; + ////Console.WriteLine("Creating file name ... "); + ////string curPath = archive.CreateOutFileName(); + //string curPath = archive.CreateOutFileName(filePath); + + ////string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive"; + ////string pathTemp = "C:/Temp/oldlogs10.txt"; + ////string pathTempBack = @"C:\Temp\oldlogs10.txt"; + //Console.WriteLine("----------------"); + + ////Output SQL to a text file + //sqlDS.CopyToFile(curPath); + + //// Compress the file + //sqlDS.CompressFile(curPath); + + ////Remove output file + //sqlDS.RemoveOutputFile(curPath); + + //// Remove entries fromt the database + //sqlDS.RemoveEntries(); + + + + // 2.Inserting Data into the database: //string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " + // "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');"; @@ -82,9 +130,6 @@ namespace TeamHobby.HobbyProjectGenerator.Main //Console.WriteLine("Writing completed. "); - - - /* ExampleDAO z = new ExampleDAO(); z.UserData("Tomato"); Console.Read();*/ diff --git a/Source Code/main/main.csproj b/Source Code/main/main.csproj index 63f5952..8853c02 100644 --- a/Source Code/main/main.csproj +++ b/Source Code/main/main.csproj @@ -12,6 +12,7 @@ + diff --git a/doc/Business Docs/Test_Plan.pdf b/doc/Business Docs/Test_Plan.pdf index a6ffa3a..9c06802 100644 Binary files a/doc/Business Docs/Test_Plan.pdf and b/doc/Business Docs/Test_Plan.pdf differ