Skip to content

Instantly share code, notes, and snippets.

@skenderbeu
Created May 20, 2020 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skenderbeu/d0dd57fce10acc95fe0b41f9b71e5c46 to your computer and use it in GitHub Desktop.
Save skenderbeu/d0dd57fce10acc95fe0b41f9b71e5c46 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using log4net;
namespace Zoom.MailingExtracts
{
public class ZipModule : IDisposable
{
protected static readonly ILog Log = LogManager.GetLogger(typeof(ZipModule));
protected string BrandName;
protected string Prefix;
protected List<string> InstructionsList;
protected string SqlOutputPath;
protected IZip Zip;
protected string ZipFileName;
public ZipModule()
{
Zip = new ZipUtil();
}
protected virtual string GetZipFileMask()
{
return @"{0}\{1}{2}_{3}.zip";
}
protected virtual string GetFileNameMask()
{
return @"{0}\{1}_XXX_{2}.txt";
}
public void SetBrand(string brand)
{
BrandName = brand;
}
public void SetPrefix(string prefix)
{
Prefix = prefix;
}
public void SetOutputPath(string outputPath)
{
SqlOutputPath = outputPath;
}
public List<string> GetZipInstructions()
{
if (InstructionsList == null)
{
ZipInstructions();
}
return InstructionsList;
}
//This is for Testing with a mock
public void SetZip(IZip zip)
{
Zip = zip;
}
public string ZipFiles()
{
if (SetUpVariables())
{
Zip.SetZipFileName(ZipFileName);
ZipInstructions();
Zip.CreateArchive();
return ZipFileName;
}
return string.Empty;
}
protected virtual bool SetUpVariables()
{
ZipFileName = GetZipFileName();
RenameIfExists(ZipFileName);
return !string.IsNullOrEmpty(ZipFileName);
}
protected virtual string GetFileName()
{
return string.Format(GetFileNameMask(),
SqlOutputPath,
BrandName,
DateTime.Now.Date.ToString("dd-MM-yyyy"));
}
protected virtual string GetZipFileName()
{
return string.Format(GetZipFileMask(),
SqlOutputPath,
Prefix,
BrandName,
DateTime.Now.Date.ToString("yyyyMMdd"));
}
protected virtual void ZipInstructions()
{
var fileName = GetFileName();
InstructionsList = new List<string>
{
fileName.Replace("XXX", "Customers"),
fileName.Replace("XXX", "Prospects"),
fileName.Replace("XXX", "MailingOptions"),
fileName.Replace("XXX", "Orders"),
fileName.Replace("XXX", "OrderLines"),
fileName.Replace("XXX", "Products"),
fileName.Replace("XXX", "Returns")
};
Zip.SetZipInstructionList(InstructionsList);
}
private void RenameIfExists(string fileName)
{
Zip.RenameIfExists(fileName);
}
#region IDisposable Support
private bool _disposedValue; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment