/
PrimitiveCommandExecutionHelper.cs
56 lines (51 loc) · 1.81 KB
/
PrimitiveCommandExecutionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using model = org.kevoree;
using Org.Kevoree.Core.Api;
using Org.Kevoree.Core.Api.Adaptation;
using System;
using System.Collections.Generic;
using System.Linq;
using Org.Kevoree.Core.Api.IMarshalled;
namespace Org.Kevoree.Core
{
class PrimitiveCommandExecutionHelper
{
public static bool execute(KevoreeCoreBean originCore, IContainerNodeMarshalled rootNode, AdaptationModel adaptionModel, NodeType nodeInstance, Func<bool> afterUpdateFunc, Func<bool> preRollBack, Func<bool> postRollback)
{
var processedActions = new HashSet<AdaptationPrimitive>();
bool success = true;
foreach (AdaptationPrimitive action in adaptionModel.ToArray())
{
processedActions.Add(action);
var resultAction = processAction(action, nodeInstance);
if (!resultAction)
{
success = false;
break;
}
}
if (!success)
{
originCore.getLogger().Error("Adaptation failed");
foreach (var act in processedActions.Reverse())
{
processUndoAction(act, nodeInstance);
}
}
else
{
originCore.getLogger().Debug("Adaptation succeeded");
}
return success;
}
private static bool processAction(AdaptationPrimitive action, NodeType nodeInstance)
{
var primitive = nodeInstance.getPrimitive(action);
return primitive.Execute();
}
private static void processUndoAction(AdaptationPrimitive action, NodeType nodeInstance)
{
var primitive = nodeInstance.getPrimitive(action);
primitive.Undo();
}
}
}