Added a main to test stuffs

Change return type for Datasource interface.
Add file compression method.
This commit is contained in:
Lunastra 2021-12-04 18:23:15 -08:00
parent e0af27a177
commit 1828f5acbb
9 changed files with 212 additions and 20 deletions

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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
{
public class SQLSource : IDataSource
{
private SqlConnection conn;
public SQLSource(string info)
namespace TeamHobby.HobbyProjectGenerator.Archive
{
conn = new SqlConnection(info);
}
public int DeleteData()
public class SQLSource : IDataSource, IRelationArchivable
{
//private SqlConnection conn;
//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;
}
}
}
}

View File

@ -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)

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>
</Project>

View File

@ -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

View File

@ -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());
}
}
}
}
}
}

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>HobbyMain</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TeamHobby.HobbyProjectGenerator.Archive\TeamHobby.HobbyProjectGenerator.Archive.csproj" />
</ItemGroup>
</Project>

View File

@ -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