2017-07-07 15:46:32 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.Operations;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
namespace SafeExamBrowser.Core.Behaviour.Operations
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public class OperationSequence : IOperationSequence
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
private Queue<IOperation> operations = new Queue<IOperation>();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private Stack<IOperation> stack = new Stack<IOperation>();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public IProgressIndicator ProgressIndicator { private get; set; }
|
|
|
|
|
|
|
|
|
|
public OperationSequence(ILogger logger, Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.logger = logger;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
this.operations = new Queue<IOperation>(operations);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public bool TryPerform()
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-01-19 14:04:12 +01:00
|
|
|
|
var success = false;
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
Initialize();
|
|
|
|
|
success = Perform();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
Revert(true);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
logger.Error("Failed to perform operations!", e);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryRepeat()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var success = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Initialize();
|
|
|
|
|
success = Repeat();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to repeat operations!", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryRevert()
|
|
|
|
|
{
|
|
|
|
|
var success = false;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
Initialize(true);
|
|
|
|
|
success = Revert();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
logger.Error("Failed to revert operations!", e);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return success;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
private void Initialize(bool indeterminate = false)
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
if (indeterminate)
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
ProgressIndicator?.SetIndeterminate();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
ProgressIndicator?.SetValue(0);
|
|
|
|
|
ProgressIndicator?.SetMaxValue(operations.Count);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
private bool Perform()
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
foreach (var operation in operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
stack.Push(operation);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
operation.ProgressIndicator = ProgressIndicator;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operation.Perform();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
logger.Error($"Failed to perform operation '{operation.GetType().Name}'!", e);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (operation.Abort)
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
ProgressIndicator?.Progress();
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
private bool Repeat()
|
|
|
|
|
{
|
|
|
|
|
foreach (var operation in operations)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.ProgressIndicator = ProgressIndicator;
|
|
|
|
|
operation.Repeat();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to repeat operation '{operation.GetType().Name}'!", e);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operation.Abort)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProgressIndicator?.Progress();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private bool Revert(bool regress = false)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
var success = true;
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
while (stack.Any())
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
var operation = stack.Pop();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
operation.ProgressIndicator = ProgressIndicator;
|
2017-10-09 12:11:33 +02:00
|
|
|
|
operation.Revert();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
success = false;
|
2017-10-09 12:11:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (regress)
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
ProgressIndicator?.Regress();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
return success;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|