Skip to content

Commit

Permalink
Issue #138 - Implement WriteError
Browse files Browse the repository at this point in the history
* Add-DSClientTimeRetentionOption: Implement WriteError when attempting to adding duplicate time retention option
  • Loading branch information
McGlovin1337 committed Nov 2, 2021
1 parent 521638c commit d0bb1cf
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions PSAsigraDSClient/AddDSClientTimeRetentionOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,25 @@ protected override void ProcessRetentionRule(RetentionRule[] retentionRules)
IntervalTimeRetentionOption intervalTimeRetention = DSClientRetentionRuleMgr.createIntervalTimeRetention();
intervalTimeRetention = IntervalTimeRetentionRule(intervalTimeRetention, IntervalTimeValue, IntervalTimeUnit, ValidForValue, ValidForUnit);

ErrorRecord intervalErrorRecord = null;
foreach (TimeRetentionOption option in timeRetentionOptions)
{
if (comparer.Equals(option, intervalTimeRetention))
{
intervalErrorRecord = new ErrorRecord(
new Exception("An Identical Time Retention Option already exists"),
"Exception",
ErrorCategory.ResourceExists,
option);
WriteError(intervalErrorRecord);

option.Dispose();
intervalTimeRetention.Dispose();
RetentionRule.Dispose();
DSClientRetentionRuleMgr.Dispose();

throw new Exception("An Identical Time Retention Option already exists");
}
}

RetentionRule.addTimeRetentionOption(intervalTimeRetention);
if (intervalErrorRecord == null)
RetentionRule.addTimeRetentionOption(intervalTimeRetention);
}

// Weekly based Time Retention
Expand All @@ -61,20 +66,25 @@ protected override void ProcessRetentionRule(RetentionRule[] retentionRules)
WeeklyTimeRetentionOption weeklyTimeRetention = DSClientRetentionRuleMgr.createWeeklyTimeRetention();
weeklyTimeRetention = WeeklyTimeRetentionRule(weeklyTimeRetention, WeeklyRetentionDay, RetentionTime, ValidForValue, ValidForUnit);

ErrorRecord weeklyErrorRecord = null;
foreach (TimeRetentionOption option in timeRetentionOptions)
{
if (comparer.Equals(option, weeklyTimeRetention))
{
option.Dispose();
weeklyTimeRetention.Dispose();
weeklyErrorRecord = new ErrorRecord(
new Exception("An Identical Time Retention Option already exists"),
"Exception",
ErrorCategory.ResourceExists,
option);
WriteError(weeklyErrorRecord);

RetentionRule.Dispose();
DSClientRetentionRuleMgr.Dispose();

throw new Exception("An Identical Time Retention Option already exists");
}
}

RetentionRule.addTimeRetentionOption(weeklyTimeRetention);
if (weeklyErrorRecord == null)
RetentionRule.addTimeRetentionOption(weeklyTimeRetention);
}

// Monthly based Time Retention
Expand All @@ -84,20 +94,25 @@ protected override void ProcessRetentionRule(RetentionRule[] retentionRules)
MonthlyTimeRetentionOption monthlyTimeRetention = DSClientRetentionRuleMgr.createMonthlyTimeRetention();
monthlyTimeRetention = MonthlyTimeRetentionRule(monthlyTimeRetention, RetentionDayOfMonth, RetentionTime, ValidForValue, ValidForUnit);

ErrorRecord monthlyErrorRecord = null;
foreach (TimeRetentionOption option in timeRetentionOptions)
{
if (comparer.Equals(option, monthlyTimeRetention))
{
option.Dispose();
monthlyTimeRetention.Dispose();
monthlyErrorRecord = new ErrorRecord(
new Exception("An Identical Time Retention Option already exists"),
"Exception",
ErrorCategory.ResourceExists,
option);
WriteError(monthlyErrorRecord);

RetentionRule.Dispose();
DSClientRetentionRuleMgr.Dispose();

throw new Exception("An Identical Time Retention Option already exists");
}
}

RetentionRule.addTimeRetentionOption(monthlyTimeRetention);
if (monthlyErrorRecord == null)
RetentionRule.addTimeRetentionOption(monthlyTimeRetention);
}

// Yearly based Time Retention
Expand All @@ -107,20 +122,25 @@ protected override void ProcessRetentionRule(RetentionRule[] retentionRules)
YearlyTimeRetentionOption yearlyTimeRetention = DSClientRetentionRuleMgr.createYearlyTimeRetention();
yearlyTimeRetention = YearlyTimeRetentionRule(yearlyTimeRetention, RetentionDayOfMonth, YearlyRetentionMonth, RetentionTime, ValidForValue, ValidForUnit);

ErrorRecord yearlyErrorRecord = null;
foreach (TimeRetentionOption option in timeRetentionOptions)
{
if (comparer.Equals(option, yearlyTimeRetention))
{
option.Dispose();
yearlyTimeRetention.Dispose();
yearlyErrorRecord = new ErrorRecord(
new Exception("An Identical Time Retention Option already exists"),
"Exception",
ErrorCategory.ResourceExists,
option);
WriteError(yearlyErrorRecord);

RetentionRule.Dispose();
DSClientRetentionRuleMgr.Dispose();

throw new Exception("An Identical Time Retention Option already exists");
}
}

RetentionRule.addTimeRetentionOption(yearlyTimeRetention);
if (yearlyErrorRecord == null)
RetentionRule.addTimeRetentionOption(yearlyTimeRetention);
}

RetentionRule.Dispose();
Expand Down

0 comments on commit d0bb1cf

Please sign in to comment.