protected override void ExecuteModify(ModuleService oldService, ModuleService newService)
{
string changeFileSufix = "_CopyForRollBack";
switch (transContext)
{
case "BeginTransaction":
{
//we copy old file to file with changeFileSufix sufix, and change file
string rootFolder = this.resource["RootFolder"].ToString();
if (rootFolder == "")
{
rootFolder = "c:\\ProvisioningModuleTestFolder\\";
}
if (rootFolder.Substring(rootFolder.Length - 1, 1) != "\\")
{
rootFolder += "\\";
}
string newFileName = string.Empty;
string oldFileName = string.Empty;
newFileName = newService.Properties.Find(p => p.Name == "Name").Value;
oldFileName = oldService.Properties.Find(p => p.Name == "Name").Value;
string parentFolder = newService.Properties.Find(p => p.Name == "ParentFolder").Value;
string newFileFullPath = rootFolder + parentFolder + "\\" + newFileName;
string oldFileFullPath = rootFolder + parentFolder + "\\" + oldFileName;
string oldFileContent = oldService.Properties.Find(p => p.Name == "Content").Value;
string newFileContent = newService.Properties.Find(p => p.Name == "Content").Value;
File.Copy(oldFileFullPath, oldFileFullPath + changeFileSufix);
if (oldFileContent != newFileContent)
{
File.WriteAllText(oldFileFullPath, newFileContent);
}
if (oldFileName != newFileName)
{
File.Move(oldFileFullPath, newFileFullPath);
File.Delete(oldFileFullPath);
}
}
break;
case "CommitTransaction":
{
//change content of file, rename it to new name and delete copy with changeFileSufix
string rootFolder = this.resource["RootFolder"].ToString();
if (rootFolder == "")
{
rootFolder = "c:\\ProvisioningModuleTestFolder\\";
}
if (rootFolder.Substring(rootFolder.Length - 1, 1) != "\\")
{
rootFolder += "\\";
}
string newFileName = string.Empty;
string oldFileName = string.Empty;
newFileName = newService.Properties.Find(p => p.Name == "Name").Value;
oldFileName = oldService.Properties.Find(p => p.Name == "Name").Value;
string parentFolder = newService.Properties.Find(p => p.Name == "ParentFolder").Value;
string newFileFullPath = rootFolder + parentFolder + "\\" + newFileName;
string oldFileFullPath = rootFolder + parentFolder + "\\" + oldFileName;
File.Delete(oldFileFullPath + changeFileSufix);
}
break;
case "RollBackTransaction":
{
//we should revert renamed file name, and file content
string rootFolder = this.resource["RootFolder"].ToString();
if (rootFolder == "")
{
rootFolder = "c:\\ProvisioningModuleTestFolder\\";
}
if (rootFolder.Substring(rootFolder.Length - 1, 1) != "\\")
{
rootFolder += "\\";
}
string newFileName = string.Empty;
string oldFileName = string.Empty;
newFileName = newService.Properties.Find(p => p.Name == "Name").Value;
oldFileName = oldService.Properties.Find(p => p.Name == "Name").Value;
string parentFolder = newService.Properties.Find(p => p.Name == "ParentFolder").Value;
string newFileFullPath = rootFolder + parentFolder + "\\" + newFileName;
string oldFileFullPath = rootFolder + parentFolder + "\\" + oldFileName;
if (oldFileName == newFileName)
{
File.Delete(oldFileFullPath);
}
File.Copy(oldFileFullPath + changeFileSufix, oldFileFullPath);
}
break;
}
}