[AAI-92 Amsterdam] Update license
[aai/sparky-fe.git] / src / generic-components / graph / NodeFactory.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import React from 'react';
24
25 import NodeVisualElementConstants from './NodeVisualElementConstants.js';
26 import NodeVisualElementFactory from './NodeVisualElementFactory.js';
27
28 class NodeFactory {
29
30   constructor() {
31     this.graphMeta = {};
32     this.visualElementFactory = new NodeVisualElementFactory();
33
34     this.setNodeMeta = this.setNodeMeta.bind(this);
35   }
36
37   setNodeMeta(metaObject) {
38     this.graphMeta = metaObject;
39     this.visualElementFactory.setVisualElementMeta(metaObject);
40   }
41
42   buildNode(nodeType, nodeProps) {
43
44     let translate = `translate(
45                               ${nodeProps.renderProps.x},
46                               ${nodeProps.renderProps.y})`;
47     let finalProps = {
48       ...nodeProps.renderProps,
49       className: this.graphMeta.aaiEntityNodeDescriptors[nodeType].class,
50       transform: translate
51     };
52
53     let nodeVisualElementsData = this.extractVisualElementArrayFromMeta(
54       nodeType);
55     let nodeVisualElements = undefined;
56     if (nodeVisualElementsData) {
57       nodeVisualElements = [];
58       nodeVisualElementsData.map((elementData, index) => {
59         if (elementData.type === NodeVisualElementConstants.BUTTON) {
60           if (nodeProps.buttons) {
61             let isButtonSelected = true;
62             elementData = {
63               ...elementData,
64               isSelected: isButtonSelected
65             };
66           }
67         }
68         nodeVisualElements.push(
69           this.visualElementFactory.buildVisualElement(nodeProps.meta,
70             elementData.type, elementData, index));
71       });
72       //Draw overlay only if the node is validated
73       if (nodeProps.meta.nodeMeta.nodeValidated) {
74
75         if (nodeProps.meta.nodeMeta.nodeIssue) {
76           let warningOverlayProps = {
77             name: NodeVisualElementConstants.ICON_WARNING,
78           };
79           nodeVisualElements.push(
80             this.visualElementFactory.buildVisualElement(nodeProps,
81               NodeVisualElementConstants.ICON, warningOverlayProps,
82               nodeVisualElementsData.length + 1));
83         } else {
84           let tickOverlayProps = {
85             name: NodeVisualElementConstants.ICON_TICK,
86           };
87           nodeVisualElements.push(
88             this.visualElementFactory.buildVisualElement(nodeProps,
89               NodeVisualElementConstants.ICON, tickOverlayProps,
90               nodeVisualElementsData.length + 1));
91         }
92       }
93     }
94
95     if (nodeVisualElements) {
96       return React.createElement('g', finalProps, nodeVisualElements);
97     }
98
99     return React.createElement('g', finalProps);
100   }
101
102   extractVisualElementArrayFromMeta(nodeClassName) {
103     let nodeVisualElements = undefined;
104     if (this.graphMeta.aaiEntityNodeDescriptors) {
105       nodeVisualElements =
106         this.graphMeta.aaiEntityNodeDescriptors[nodeClassName].visualElements;
107     }
108     return nodeVisualElements;
109   }
110 }
111
112 export default NodeFactory;