/// <summary>
/// InvoiceRepositorySynch will keep two different invoices
repositories in synch.
/// </summary>
public class InvoiceRepositorySynch
: RepositorySynch
{
/// <summary>
/// Specific to this implementation and how the folders are
named.
/// </summary>
private
struct ProcessStruct
{
public
int Year;
public
int Month;
}
public
InvoiceRepositorySynch(string SourcePath, string DestinationPath)
: base(SourcePath,
DestinationPath)
{
}
/// <summary>
/// Override Synch, still calling the base so we don't have
to recall the validation function.
/// </summary>
public
override void
Synch()
{
base.Synch();
//
Gather Stats - Folder are named as year ex. 2010 and the subfolders are named
ex. 12-2010
// We
only want current month and next month.
int
currentYear = DateTime.Now.Year;
int
currentMonth = DateTime.Now.Month;
int
futureMonth = currentMonth + 1;
// Run
Current
Process(currentYear,
currentMonth);
//
Calculate Next
if
(currentMonth == 12)
{
futureMonth = 1;
currentYear += 1;
}
// Run
Future
Process(currentYear,
futureMonth);
}
/// <summary>
/// Synch the folders
/// </summary>
/// <param
name="year"></param>
/// <param
name="month"></param>
private
void Process(int
year, int month)
{
try
{
ProcessStruct
pStruct = new ProcessStruct()
{ Month = month, Year = year };
//
Get the year directory
DirectoryInfo
sourceDir =
SourceDirectory.GetDirectories().Where(d
=> d.Name == pStruct.Year.ToString()).FirstOrDefault();
DirectoryInfo
destinationDir =
DestinationDirectory.GetDirectories().Where(d => d.Name ==
pStruct.Year.ToString()).FirstOrDefault();
if
(sourceDir != null && destinationDir
!= null)
{
//
Get the month directory
DirectoryInfo
sourceMonth =
sourceDir.GetDirectories().Where(d => d.Name ==
ValidName(pStruct)).FirstOrDefault();
DirectoryInfo
destinationMonth =
destinationDir.GetDirectories().Where(d => d.Name ==
ValidName(pStruct)).FirstOrDefault();
if
(sourceMonth != null &&
destinationMonth != null)
{
// Get the arrays for file info in both directories
FileInfo[] sourceFiles = sourceMonth.GetFiles();
FileInfo[] destinationFiles =
destinationMonth.GetFiles();
// In this case we only want to perform our last check
when the source contains more files
if (sourceFiles.Length >
destinationFiles.Length)
{
// Get the filenames array
List<string>
sourceFileNames = (from f in sourceFiles
select f.Name).ToList<string>();
List<string>
destinationFileNames = (from f in destinationFiles
select f.Name).ToList<string>();
// Get all matches for removal
List<string>
matches = (from s in
sourceFileNames
join d in
destinationFileNames on s equals d
select s).ToList<string>();
// Remove matches to keep a list of all the missing
filenames
sourceFileNames.RemoveAll(a =>
{
return matches.Contains(a);
});
// Copy missing files
FileInfo[] filesToCopy = (from f in
sourceFiles
join c in
sourceFileNames on f.Name equals c
select f).ToArray<FileInfo>();
foreach (FileInfo
f in filesToCopy)
{
f.CopyTo(destinationMonth.FullName + @"\\"
+ f.Name);
}
}
}
}
}
catch
(Exception ex)
{
throw ex;
}
}
private
string ValidName(ProcessStruct
pStruct)
{
return
string.Format("{0}-{1}",
pStruct.Month < 10 ? "0" +
pStruct.Month.ToString() : pStruct.Month.ToString(),
pStruct.Year.ToString());
}
}
|