Updated Sparky to add ECOMP functionality Browse, Specialized Search, BYOQ, and the...
[aai/sparky-fe.git] / src / app / model / modelSearch / components / ModelRelationships.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import React, { Component } from 'react';
22 import { Link } from 'react-router-dom';
23 import Label from 'react-bootstrap/lib/Label';
24 import Panel from 'react-bootstrap/lib/Panel';
25 import {GlobalExtConstants} from 'utils/GlobalExtConstants.js';
26 import {ExportExcel} from 'utils/ExportExcel.js';
27 let buildAttrList = ExportExcel.buildAttrList;
28
29 let INVLIST = GlobalExtConstants.INVLIST;
30
31 class ModelRelationships extends Component {
32   constructor(props){
33       console.log(props);
34       super(props);
35       this.props = props;
36   }
37
38   render(){
39   console.log('[ModelRelationships.jsx] props : ', this.props);
40   let relationships = null;
41   let navigateByoq = null;
42   let relativesArray = [];
43   this.nodeDisplay = this.props.relatives.nodeType + ' : ' + (this.props.relatives.nodeUrl).split(this.props.relatives.nodeType + '\/').pop();
44   this.historyClick = () => {
45     this.props.openHistoryModal(this.nodeDisplay, this.props.relatives.nodeUrl,this.props.relatives.nodeType);
46   }
47
48   if (this.props.relatives.nodeRelatives && this.props.relatives.nodeRelatives.length > 0) {
49     relationships = this.props.relatives.nodeRelatives.sort(function(a, b) {
50                                                    var compareA = (a['node-type'] + (a.url).split(a['node-type']+'\/').pop()).toLowerCase();
51                                                    var compareB = (b['node-type'] + (b.url).split(a['node-type']+'\/').pop()).toLowerCase();
52                                                    if(compareA < compareB) return -1;
53                                                    if(compareA > compareB) return 1;
54                                                    return 0;
55                                                   }).map((relative, idx) => {
56                                                   if (relativesArray.includes(relative['node-type']) === false) relativesArray.push(relative['node-type']);
57       return (
58         <div>
59           <Link
60             key={idx}
61             to={{
62               pathname: '/model/' + relative['node-type'] + '/' + relative.id + '/'+  this.props.relatives.enableRealTime,
63               uri: relative.url,
64               historyStackString: this.props.historyStackString
65             }}>
66             {relative['node-type']}: {(decodeURI((relative.url).split(relative['node-type']+'\/').pop())).replace(/%2F/g,'/')}</Link> <Label bsStyle='default'>{relative['relationship-label'].slice(33)}</Label>
67         </div>
68       );
69     });
70   }
71     relativesArray = relativesArray.join('&');
72     var propKey = '';
73     var requiredParams = buildAttrList(this.props.relatives.nodeType,[],'mandatory');
74     var aliasColumnFilters = (this.props.relatives.aliasColumnList && this.props.relatives.aliasColumnList[this.props.relatives.nodeType])?this.props.relatives.aliasColumnList[this.props.relatives.nodeType][0]:[];
75     console.log('requiredParams>>>>>>>>>>>>',requiredParams);
76     Object.keys(this.props.relatives.nodeProps).map((prop, idx) => {           
77       for(var a in requiredParams){        
78         let alias='';
79         if(aliasColumnFilters && aliasColumnFilters[requiredParams[a].value]){
80           alias=requiredParams[a].value;
81           requiredParams[a].value=aliasColumnFilters[requiredParams[a].value];          
82         }
83         if(requiredParams[a].value === prop){
84           let tag= (alias!='')? alias: prop;
85           if(propKey === ''){
86             propKey = tag + ':' + btoa('<pre>' + this.props.relatives.nodeProps[prop].toString() + '</pre>');
87           }else{
88             propKey = propKey + ';' + tag + ':' + btoa('<pre>' + this.props.relatives.nodeProps[prop].toString() + '</pre>');
89           }        
90         }
91       }
92     });
93     let pathNameStr = (relativesArray.length>0) ? '/customDsl/' + this.props.relatives.nodeType + '/' + propKey + '/' + relativesArray : '/customDsl/' + this.props.relatives.nodeType + '/' + propKey;
94     navigateByoq = <Link
95                     to={{
96                       pathname: pathNameStr
97                      }}>
98                      <button type='button' className='btn btn-primary pull-right'>>>BYOQ</button>
99                    </Link>;
100   if (this.props.relatives.nodeRelatives && this.props.relatives.nodeRelatives.length > 0) {
101     return (
102       <Panel>
103         <Panel.Heading>
104           <Panel.Toggle>
105             <button type='button' className='btn btn-outline-primary'>
106               Relationships
107               </button>
108           </Panel.Toggle>
109             { INVLIST.isHistoryEnabled && (<button type='button' className='btn btn-outline-primary' onClick={this.historyClick}>
110               History
111             </button>)}
112             {navigateByoq}
113         </Panel.Heading>
114         <Panel.Collapse>
115           <Panel.Body className='cardwrap'>
116             {relationships}
117           </Panel.Body>
118         </Panel.Collapse>
119       </Panel>
120     );
121   } else {
122     return (
123       <div>
124         <button type='button' className='btn btn-outline-disabled'>
125           No relationships
126         </button>
127         { INVLIST.isHistoryEnabled && (<button type='button' className='btn btn-outline-primary' onClick={this.historyClick}>
128           History
129         </button>)}
130         {navigateByoq}
131       </div>
132     );
133   }
134 }
135 };
136
137 export default ModelRelationships;