summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Modification/NodeCreateAction.php
blob: 167d3bc1c9917c24337c0640835181c2c2aefe66 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace Icinga\Module\Businessprocess\Modification;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\Node;

class NodeCreateAction extends NodeAction
{
    /** @var string */
    protected $parentName;

    /** @var array */
    protected $properties = array();

    /** @var array */
    protected $preserveProperties = array('parentName', 'properties');

    /**
     * @param Node $name
     */
    public function setParent(Node $name)
    {
        $this->parentName = $name->getName();
    }

    /**
     * @return bool
     */
    public function hasParent()
    {
        return $this->parentName !== null;
    }

    /**
     * @return string
     */
    public function getParentName()
    {
        return $this->parentName;
    }

    /**
     * @param string $name
     */
    public function setParentName($name)
    {
        $this->parentName = $name;
    }

    /**
     * @return array
     */
    public function getProperties()
    {
        return $this->properties;
    }

    /**
     * @param array $properties
     * @return $this
     */
    public function setProperties($properties)
    {
        $this->properties = (array) $properties;
        return $this;
    }

    /**
     * @inheritdoc
     */
    public function appliesTo(BpConfig $config)
    {
        $name = $this->getNodeName();
        if ($config->hasNode($name)) {
            $this->error('A node with name "%s" already exists', $name);
        }

        $parent = $this->getParentName();
        if ($parent !== null && !$config->hasBpNode($parent)) {
            $this->error('Parent process "%s" missing', $parent);
        }

        return true;
    }

    /**
     * @inheritdoc
     */
    public function applyTo(BpConfig $config)
    {
        $name = $this->getNodeName();

        $properties = array(
            'name'        => $name,
            'operator'    => $this->properties['operator'],
        );
        if (array_key_exists('childNames', $this->properties)) {
            $properties['child_names'] = $this->properties['childNames'];
        } else {
            $properties['child_names'] = array();
        }
        $node = new BpNode((object) $properties);
        $node->setBpConfig($config);

        foreach ($this->getProperties() as $key => $val) {
            if ($key === 'parentName') {
                $config->getBpNode($val)->addChild($node);
                continue;
            }
            $func = 'set' . ucfirst($key);
            $node->$func($val);
        }

        if ($node->getDisplay() > 1) {
            $i = $node->getDisplay();
            foreach ($config->getRootNodes() as $_ => $rootNode) {
                if ($rootNode->getDisplay() >= $node->getDisplay()) {
                    $rootNode->setDisplay(++$i);
                }
            }
        }

        $config->addNode($name, $node);

        return $node;
    }
}