From 1828f5acbbc2928d136da97bbff3f47297b07a9b Mon Sep 17 00:00:00 2001 From: Lunastra Date: Sat, 4 Dec 2021 18:23:15 -0800 Subject: [PATCH] Added a main to test stuffs Change return type for Datasource interface. Add file compression method. --- .../Contracts/IDataSource.cs | 8 +-- .../Contracts/IRelationArchivable.cs | 13 ++++ .../Implementations/SQLSource.cs | 68 +++++++++++++++---- ...tory.cs => RelationalDataSourceFactory.cs} | 4 +- ...Hobby.HobbyProjectGenerator.Archive.csproj | 4 ++ .../TeamHobby.HobbyProjectGenerator.sln | 8 ++- Source Code/TeamHobby.Main/HobbyMain.cs | 62 +++++++++++++++++ .../TeamHobby.Main/TeamHobby.Main.csproj | 19 ++++++ Source Code/TeamHobby.Main/archiving2.txt | 46 +++++++++++++ 9 files changed, 212 insertions(+), 20 deletions(-) create mode 100644 Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IRelationArchivable.cs rename Source Code/TeamHobby.HobbyProjectGenerator.Archive/{DataSourceFactory.cs => RelationalDataSourceFactory.cs} (88%) create mode 100644 Source Code/TeamHobby.Main/HobbyMain.cs create mode 100644 Source Code/TeamHobby.Main/TeamHobby.Main.csproj create mode 100644 Source Code/TeamHobby.Main/archiving2.txt diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs index d8bc317..f173ba6 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs @@ -10,15 +10,15 @@ namespace TeamHobby.HobbyProjectGenerator.Archive public interface IDataSource { // Method for reading data, return 0 for sucessful operation - int ReadData(string cmd); + Object ReadData(string cmd); // Method for Writing data to a data source - int WriteData(string cmd); + bool WriteData(string cmd); //Method for deleteing data from a data source, 0 for successful - int DeleteData(); + bool DeleteData(); // Method for updating data from a data source, 0 for sucessful - int UpdateData(); + bool UpdateData(); } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IRelationArchivable.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IRelationArchivable.cs new file mode 100644 index 0000000..96cabb9 --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IRelationArchivable.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TeamHobby.HobbyProjectGenerator.Archive +{ + public interface IRelationArchivable + { + bool CreateArchived(string fileName); + } +} diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs index 557f229..ab5b34a 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs @@ -1,26 +1,30 @@ using System; using System.Collections.Generic; using System.Data.SqlClient; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.IO.Compression; -namespace TeamHobby.HobbyProjectGenerator.Archive.Implementations + +namespace TeamHobby.HobbyProjectGenerator.Archive { - public class SQLSource : IDataSource + public class SQLSource : IDataSource, IRelationArchivable { - private SqlConnection conn; + //private SqlConnection conn; - public SQLSource(string info) - { - conn = new SqlConnection(info); - } - public int DeleteData() + //public SQLSource(string info) + //{ + // conn = new SqlConnection(info); + //} + + public bool DeleteData() { throw new NotImplementedException(); } - public int ReadData(string cmd) + public Object ReadData(string cmd) { try { @@ -32,23 +36,61 @@ namespace TeamHobby.HobbyProjectGenerator.Archive.Implementations // while (myReader) // Print to console // conn.close(); - return 0; + return null; } catch (Exception e) { Console.WriteLine(e.Message); - return -1; + return null; } } - public int UpdateData() + public bool UpdateData() { throw new NotImplementedException(); } - public int WriteData(string cmd) + public bool WriteData(string cmd) { throw new NotImplementedException(); } + + // Method to archive data + public bool CreateArchived(string fileName) + { + throw new NotImplementedException(); + } + + public bool CompressFile(string fileName) + { + try + { + // Get the stream of the original file + using FileStream origFile = File.Open(fileName, FileMode.Open); + + + // Get the attribute of the file + FileAttributes atrribute = File.GetAttributes(fileName); + + // Check to see if the file is hidden or already compressed before compressing the file. + if (atrribute != FileAttributes.Hidden && atrribute != FileAttributes.Compressed) + { + using FileStream outputFile = File.Create(fileName + ".gz"); + + using GZipStream compressor = new GZipStream(outputFile, CompressionMode.Compress); + origFile.CopyTo(compressor); + + return true; + } + + return true; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } + } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/DataSourceFactory.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/RelationalDataSourceFactory.cs similarity index 88% rename from Source Code/TeamHobby.HobbyProjectGenerator.Archive/DataSourceFactory.cs rename to Source Code/TeamHobby.HobbyProjectGenerator.Archive/RelationalDataSourceFactory.cs index 4bdb695..08f602a 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/DataSourceFactory.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/RelationalDataSourceFactory.cs @@ -3,11 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TeamHobby.HobbyProjectGenerator.Archive.Implementations; +using TeamHobby.HobbyProjectGenerator.Archive; namespace TeamHobby.HobbyProjectGenerator.Archive { - public class DataSourceFactory + public class RelationalDataSourceFactory { // Mehthod to create a specific data source suitable to the need public IDataSource? getDataSource(string name) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/TeamHobby.HobbyProjectGenerator.Archive.csproj b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/TeamHobby.HobbyProjectGenerator.Archive.csproj index 132c02c..ac029dc 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/TeamHobby.HobbyProjectGenerator.Archive.csproj +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/TeamHobby.HobbyProjectGenerator.Archive.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln index b76ca88..6ae835b 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.0.31919.166 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeamHobby.HobbyProjectGenerator", "TeamHobby.HobbyProjectGenerator.csproj", "{C75B6909-FD9E-4382-94B6-7CA2CE371C9A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamHobby.HobbyProjectGenerator.Archive", "..\TeamHobby.HobbyProjectGenerator.Archive\TeamHobby.HobbyProjectGenerator.Archive.csproj", "{F0E39503-E55A-4EB8-AA98-98D74A79D7D6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeamHobby.HobbyProjectGenerator.Archive", "..\TeamHobby.HobbyProjectGenerator.Archive\TeamHobby.HobbyProjectGenerator.Archive.csproj", "{F0E39503-E55A-4EB8-AA98-98D74A79D7D6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamHobby.Main", "..\TeamHobby.Main\TeamHobby.Main.csproj", "{6E9DCCDB-B7E3-4530-B20D-5B86BA86A905}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {F0E39503-E55A-4EB8-AA98-98D74A79D7D6}.Debug|Any CPU.Build.0 = Debug|Any CPU {F0E39503-E55A-4EB8-AA98-98D74A79D7D6}.Release|Any CPU.ActiveCfg = Release|Any CPU {F0E39503-E55A-4EB8-AA98-98D74A79D7D6}.Release|Any CPU.Build.0 = Release|Any CPU + {6E9DCCDB-B7E3-4530-B20D-5B86BA86A905}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6E9DCCDB-B7E3-4530-B20D-5B86BA86A905}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6E9DCCDB-B7E3-4530-B20D-5B86BA86A905}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6E9DCCDB-B7E3-4530-B20D-5B86BA86A905}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Source Code/TeamHobby.Main/HobbyMain.cs b/Source Code/TeamHobby.Main/HobbyMain.cs new file mode 100644 index 0000000..6d30a27 --- /dev/null +++ b/Source Code/TeamHobby.Main/HobbyMain.cs @@ -0,0 +1,62 @@ +// See https://aka.ms/new-console-template for more information +using System.Data.SqlClient; +using System.IO.Compression; +using TeamHobby.HobbyProjectGenerator.Archive; + +public class HobbyMain +{ + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + + // Testing Compressing a file + string fileName = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\TeamHobby.Main\archiving2.txt"; + //string fileName = @"arhiving2.txt"; + FileInfo fileInfo = new FileInfo(fileName); + + Console.WriteLine("File Name: {0}", fileInfo.FullName); + + //Console.WriteLine("Starting file compression: "); + //Compress(fileInfo); + //Console.WriteLine("Ending compression"); + //SqlConnection myconn = new SqlConnection(); + + + //IDataSource dataSource = new SQLSource(); + + SQLSource sqlSource = new SQLSource(); + bool res = sqlSource.CompressFile(fileName); + //bool res = CompressFile(fileName); + Console.WriteLine(res); + + + } + + + public static void Compress(FileInfo fi) + { + // Get the stream of the source file. + using (FileStream inFile = fi.OpenRead()) + { + // Prevent compressing hidden and + // already compressed files. + if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fi.Extension != ".gz") + { + // Create the compressed file. + using (FileStream outFile = + File.Create(fi.FullName + ".gz")) + { + using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) + { + // Copy the source file into + // the compression stream. + inFile.CopyTo(Compress); + + Console.WriteLine("Compressed {0} from {1} to {2} bytes.", + fi.Name, fi.Length.ToString(), outFile.Length.ToString()); + } + } + } + } + } +} diff --git a/Source Code/TeamHobby.Main/TeamHobby.Main.csproj b/Source Code/TeamHobby.Main/TeamHobby.Main.csproj new file mode 100644 index 0000000..00d7d1d --- /dev/null +++ b/Source Code/TeamHobby.Main/TeamHobby.Main.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + HobbyMain + + + + + + + + + + + diff --git a/Source Code/TeamHobby.Main/archiving2.txt b/Source Code/TeamHobby.Main/archiving2.txt new file mode 100644 index 0000000..a2ba694 --- /dev/null +++ b/Source Code/TeamHobby.Main/archiving2.txt @@ -0,0 +1,46 @@ +title Use Case: Archiving + +participant ArchiveController.cs +participant DataConnection +participant DataSourceFactory +participant SQLSource +participant MariaDB + +activate ArchiveController.cs +activate DataConnection +ArchiveController.cs ->DataConnection:static DataConnection getDataConnection() +DataConnection -->ArchiveController.cs:return DataConnection +deactivate DataConnection + +activate DataSourceFactory +ArchiveController.cs ->DataSourceFactory: new DataSourceFactory() +DataSourceFactory ->DataSourceFactory:DataSourceFactory()\nconstructor +ArchiveController.cs <-- DataSourceFactory:return DataSourceFactory +deactivate DataSourceFactory + +activate SQLSource +ArchiveController.cs->SQLSource: IDataSource DataSourceFact.GetDataSource(String sourceName, string info) +SQLSource->SQLSource: SQLSource(string info)\nconstructor +SQLSource --> ArchiveController.cs :return SQLSource + +loop #lightblue while true +alt #lightgreen date == 1 +ArchiveController.cs ->ArchiveController.cs: string fileName = string CreateFileName( ) +ArchiveController.cs -> SQLSource: int Compress(string fileName) +activate MariaDB + +SQLSource -> MariaDB: int createArchive(string fileLocation) +MariaDB -->SQLSource:return 0 + +SQLSource ->MariaDB: int RemoveEntries() +MariaDB-->SQLSource:return 0 +end + + +deactivate MariaDB + +SQLSource-->ArchiveController.cs:return 0 +end + +deactivate ArchiveController.cs +deactivate SQLSource \ No newline at end of file