public void SynchronizeSchedules(ExecutionLogInterface log, bool useCurrentUser)
{
int count = 0, errorCount = 0, taskDeleted = 0;
StringBuilder errorSummary = new StringBuilder("");
Repository repository = Repository.Instance.CreateFast();
try
{
log.Log("Starting Report Schedules Synchronization\r\n");
if (!Helper.IsMachineAdministrator() && !useCurrentUser) log.Log("WARNING: For this tool, we recommend to execute the 'Server Manager' application with the option 'Run as administrator'\r\n");
SynchronizeSchedules(log, repository.ReportsFolder, repository, ref count, ref errorCount, errorSummary, useCurrentUser);
log.Log("Checking personal folders\r\n");
SynchronizeSchedules(log, repository.PersonalFolder, repository, ref count, ref errorCount, errorSummary, useCurrentUser);
log.Log("Checking for Orphan tasks\r\n");
TaskService taskService = new TaskService();
TaskFolder taskFolder = taskService.RootFolder.SubFolders.FirstOrDefault(i => i.Name == repository.Configuration.TaskFolderName);
if (taskFolder != null)
{
foreach (Task task in taskFolder.GetTasks())
{
log.Log("Checking task '{0}'", task.Name);
try
{
string reportPath = ReportSchedule.GetTaskSourceDetail(task.Definition.RegistrationInfo.Source, 0);
string reportGUID = ReportSchedule.GetTaskSourceDetail(task.Definition.RegistrationInfo.Source, 1);
string scheduleGUID = ReportSchedule.GetTaskSourceDetail(task.Definition.RegistrationInfo.Source, 3);
Report report = ReportExecution.GetScheduledReport(taskFolder, reportPath, reportGUID, scheduleGUID, repository);
if (report != null)
{
ReportSchedule schedule = ReportExecution.GetReportSchedule(taskFolder, report, scheduleGUID);
if (schedule == null)
{
taskDeleted++;
log.Log("WARNING: Unable to find schedule '{0}' in report '{1}'. Task has been deleted.", scheduleGUID, report.FilePath);
}
}
else
{
taskDeleted++;
log.Log("WARNING: Unable to find report '{0}' for schedule '{1}'. Report tasks have been deleted.", reportGUID, scheduleGUID);
}
}
catch (Exception ex)
{
errorCount++;
log.LogRaw("ERROR\r\n");
log.Log(ex.Message);
errorSummary.AppendFormat("\r\nTask '{0}': {1}\r\n", task.Name, ex.Message);
}
}
}
}
catch (Exception ex)
{
log.Log("\r\n[UNEXPECTED ERROR RECEIVED]\r\n{0}\r\n", ex.Message);
}
log.Log("Report Schedules Synchronization terminated\r\n");
log.Log("SUMMARY: {0} Report(s) checked, {1} Task(s) deleted, {2} Error(s) detected.\r\n{3}", count, taskDeleted, errorCount, errorSummary);
if (errorCount == 0) log.Log("Youpi, pas d'erreur !");
}