aboutsummaryrefslogtreecommitdiffstats
path: root/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js
blob: 538688dac0d02670d6895f386f20bdd1a56b7a47 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */


import React, { forwardRef } from 'react';
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import styled from 'styled-components';
import TemplateMenuService from '../../../api/TemplateService';
import MaterialTable, {MTableToolbar} from "material-table";
import IconButton from '@material-ui/core/IconButton';
import Tooltip from '@material-ui/core/Tooltip';
import Grid from '@material-ui/core/Grid';
import AddBox from '@material-ui/icons/AddBox';
import ArrowUpward from '@material-ui/icons/ArrowUpward';
import Check from '@material-ui/icons/Check';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import VerticalAlignTopIcon from '@material-ui/icons/VerticalAlignTop';
import VerticalAlignBottomIcon from '@material-ui/icons/VerticalAlignBottom';
import ChevronRight from '@material-ui/icons/ChevronRight';
import Clear from '@material-ui/icons/Clear';
import DeleteOutline from '@material-ui/icons/DeleteOutline';
import Edit from '@material-ui/icons/Edit';
import FilterList from '@material-ui/icons/FilterList';
import FirstPage from '@material-ui/icons/FirstPage';
import LastPage from '@material-ui/icons/LastPage';
import Remove from '@material-ui/icons/Remove';
import Search from '@material-ui/icons/Search';
import ViewColumn from '@material-ui/icons/ViewColumn';


const ModalStyled = styled(Modal)`
	background-color: transparent;
`
const cellStyle = { border: '1px solid black' };
const headerStyle = { backgroundColor: '#ddd',	border: '2px solid black'	};
const rowHeaderStyle = {backgroundColor:'#ddd',  fontSize: '15pt', text: 'bold', border: '1px solid black'};
var dictList = [];

function SelectSubDictType(props) {
	const {onChange} = props;
	const selectedValues = (e) => {
		var options = e.target.options;
		var SelectedDictTypes = '';
		for (var dictType = 0, values = options.length; dictType < values; dictType++) {
			if (options[dictType].selected) {
				SelectedDictTypes = SelectedDictTypes.concat(options[dictType].value);
				SelectedDictTypes = SelectedDictTypes.concat('|');
			}
		}
		SelectedDictTypes = SelectedDictTypes.slice(0,-1);
		onChange(SelectedDictTypes);
	}
	return(
		<div>
			<select multiple={true}  onChange={selectedValues}>
				<option value="string">string</option>
				<option value="number">number</option>
				<option value="datetime">datetime</option>
				<option value="map">map</option>
				<option value="json">json</option>
			</select>
		</div>
	)
}

function SubDict(props) {
	const {onChange} = props;
	const subDicts = [];
	subDicts.push('Default');
	for(var item in dictList) {
		if(dictList[item].secondLevelDictionary === 1) {
			subDicts.push(dictList[item].name);
		}
	};
	subDicts.push('');
 	var optionItems = subDicts.map(
		(item) => <option key={item}>{item}</option>
	  );
 	function selectedValue (e) {
		onChange(e.target.value);
	}
	return(
		<select onChange={selectedValue} >
			{optionItems}
		</select>
	)
}

export default class ManageDictionaries extends React.Component {
	constructor(props, context) {
		super(props, context);
		this.handleClose = this.handleClose.bind(this);
		this.getDictionary = this.getDictionary.bind(this);
		this.getDictionaryElements = this.getDictionaryElements.bind(this);
		this.clickHandler = this.clickHandler.bind(this);
		this.addDictionary = this.addDictionary.bind(this);
		this.deleteDictionary = this.deleteDictionary.bind(this);
		this.fileSelectedHandler = this.fileSelectedHandler.bind(this);
		this.state = {
			show: true,
			selectedFile: '',
			dictNameFlag: false,
			exportFilename: '',
			content: null,
			newDict: '',
			newDictItem: '',
			delDictItem: '',
			addDict: false,
			delData: '',
			delDict: false,
			validImport: false,
			dictionaryNames: [],
			dictionaryElements: [],
      tableIcons: {
		Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
        Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
        Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
        Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
        DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
        Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
        Export: forwardRef((props, ref) => <VerticalAlignBottomIcon {...props} ref={ref} />),
        Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
        FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
        LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
        NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
        PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
        ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
        Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
        SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />),
        ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
        ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
      },
			dictColumns: [
				{
					title: "Dictionary Name", field: "name",editable: 'onAdd',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Sub Dictionary ?", field: "secondLevelDictionary", lookup: {0: 'No', 1: 'Yes'},
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Dictionary Type", field: "subDictionaryType",lookup: {string: 'string', number: 'number'},
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Updated By", field: "updatedBy", editable: 'never',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Last Updated Date", field: "updatedDate", editable: 'never',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				}
			],
			dictElementColumns: [
				{
					title: "Element Short Name", field: "shortName",editable: 'onAdd',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
        {
					title: "Element Name", field: "name",
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Element Description", field: "description",
					cellStyle: cellStyle,
					headerStyle: headerStyle
				 },
				 {
					title: "Element Type", field: "type",
					editComponent: props => (
						<div>
							<SelectSubDictType  value={props.value} onChange={props.onChange} />
						</div>
					),
					cellStyle: cellStyle,
					headerStyle: headerStyle
				 },
				 {
				    title: "Sub-Dictionary", field: "subDictionary",
				      editComponent: props => (
						 <div>
							 <SubDict  value={props.value} onChange={props.onChange} />
						 </div>
				      ),
				    cellStyle: cellStyle,
				    headerStyle: headerStyle
				 },
				{   
					title: "Updated By", field: "updatedBy", editable: 'never',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				},
				{
					title: "Updated Date", field: "updatedDate", editable: 'never',
					cellStyle: cellStyle,
					headerStyle: headerStyle
				}
			]
		}
	}

	componentWillMount() {
        this.getDictionary();
    }

    getDictionary() {
        TemplateMenuService.getDictionary().then(dictionaryNames => {
            this.setState({ dictionaryNames: dictionaryNames })
        });
    }

    getDictionaryElements(dictionaryName) {
        TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => {
            dictList = this.state.dictionaryNames;
            this.setState({ dictionaryElements: dictionaryElements.dictionaryElements});
        });
    }

    clickHandler(rowData)   {
        this.setState({
            dictNameFlag: false,
            addDict: false,
    });
    }

    handleClose() {
        this.setState({ show: false });
        this.props.history.push('/');
    }

    addDictionary() {
        var modifiedData = [];
        if(this.state.newDict !== '') {
            modifiedData = this.state.newDict;
        } else {
            modifiedData = {"name": this.state.dictionaryName, 'dictionaryElements': this.state.newDictItem};
        }
        if(this.state.newDictItem === '') {
            TemplateMenuService.insDictionary(modifiedData).then(resp => {
            });
        } else {
            TemplateMenuService.insDictionaryElements(modifiedData).then(resp => {
            });
        }
    }

    deleteDictionary() {
        var modifiedData = [];
        if(this.state.delData !== '') {
            modifiedData = this.state.delData.name;
        } else {
            modifiedData = {"name": this.state.dictionaryName, "shortName": this.state.delDictItem.shortName};
        }
        if(this.state.delDictItem === '') {
            TemplateMenuService.deleteDictionary(modifiedData).then(resp => {
            });
        } else {
            TemplateMenuService.deleteDictionaryElements(modifiedData).then(resp => {
            });
        }
    }

    fileSelectedHandler = (event) => {
        const text = this;
        var dictionaryElements = [];
        if (event.target.files[0].type === 'text/csv' ) {
            if (event.target.files && event.target.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    var dictElems = reader.result.split('\n');
                    var jsonObj = [];
                    var headers = dictElems[0].split(',');
                    for(var i = 0; i < dictElems.length; i++) {
                        var data = dictElems[i].split(',');
                        var obj = {};
                        for(var j = 0; j < data.length; j++) {
                            obj[headers[j].trim()] = data[j].trim();
                        }
                        jsonObj.push(obj);
                    }
                    JSON.stringify(jsonObj);
                    const dictKeys = ['Element Short Name','Element Name','Element Description','Element Type','Sub-Dictionary'];
                    const mandatoryKeys = [ 'Element Short Name', 'Element Name', 'Element Type' ];
                    const validTypes = ['string','number','datetime','json','map'];
                    if (!dictElems){
                        text.setState({validData: false});
                    } else if (headers.length !== dictKeys.length){
                        text.setState({validImport: false});
                    } else {
                        var subDictionaries = [];
                        for(var item in dictList) {
                            if(dictList[item].secondLevelDictionary === 1) {
                                subDictionaries.push(dictList[item].name);
                            }
                        };
                        subDictionaries = subDictionaries.toString();
                        var row = 0;
                        for (var dictElem of jsonObj){
                            ++row;
                            for (var itemKey in dictElem){
                                var value = dictElem[itemKey].trim();
                                if (dictKeys.indexOf(itemKey) < 0){
                                    var errorMessage = 'unknown field name of, ' + itemKey + ', found in CSV header';
                                    text.setState({validImport: false});
                                    alert(errorMessage);
                                    break;
                                } else if (value === "" && mandatoryKeys.indexOf(itemKey) >= 0){
                                    errorMessage = 'value for ' + itemKey + ', at row #, ' + row + ', is empty but required';
                                    text.setState({validImport: false});
                                    alert(errorMessage);
                                    break;
                                } else if (itemKey === 'Element Type' && validTypes.indexOf(value) < 0 && row > 1) {
                                    errorMessage = 'invalid dictElemenType of ' + value + ' at row #' + row;
                                    text.setState({validImport: false});
                                    alert(errorMessage);
                                    break;
                                } else if (value !== "" && itemKey === 'Sub-Dictionary' && subDictionaries.indexOf(value) < 0 && row > 1) {
                                    errorMessage = 'invalid subDictionary of ' + value + ' at row #' + row;
                                    text.setState({validImport: false});
                                    alert(errorMessage);
                                }
                            }
                        }
                    }
                    const headerKeys = ['shortName','name','description','type','subDictionary'];

                    for(i = 1; i < dictElems.length; i++) {
                        data = dictElems[i].split(',');
                        obj = {};
                        for(j = 0; j < data.length; j++) {
                            obj[headerKeys[j].trim()] = data[j].trim();
                        }
                        dictionaryElements.push(obj);
                    }
                    text.setState({newDictItem: dictionaryElements, addDict: true});
                }
                reader.readAsText(event.target.files[0]);
            }
            this.setState({selectedFile: event.target.files[0]})
        } else {
            text.setState({validImport: false});
            alert('Please upload .csv extention files only.');
        }

    }
   
    render() {
        return (
            <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
                <Modal.Header closeButton>
                    <Modal.Title>Manage Dictionaries</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    {!this.state.dictNameFlag? <MaterialTable
                        title={"Dictionary List"}
                        data={this.state.dictionaryNames}
                        columns={this.state.dictColumns}
                        icons={this.state.tableIcons}
                        onRowClick={(event, rowData) => {this.getDictionaryElements(rowData.name);this.setState({dictNameFlag: true, exportFilename: rowData.name, dictionaryName: rowData.name})}}
                        options={{
                            headerStyle: rowHeaderStyle,
                        }}
                        editable={{
                            onRowAdd: newData =>
                            new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        const dictionaryNames = this.state.dictionaryNames;
                                        var validData =  true;
                                        if(/[^a-zA-Z0-9-_.]/.test(newData.name)) {
                                            validData = false;
                                            alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)');
                                        }
                                        for (var i = 0; i < this.state.dictionaryNames.length; i++) {
                                            if (this.state.dictionaryNames[i].name === newData.name) {
                                                validData = false;
                                                alert(newData.name + ' dictionary name already exists')
                                            }
                                        }
                                        if(validData){
                                            dictionaryNames.push(newData);
                                            this.setState({ dictionaryNames }, () => resolve());
                                            this.setState({addDict: true, newDict: newData});
                                        }
                                    }
                                    resolve();
                                }, 1000);
                            }),
                            onRowUpdate: (newData, oldData) =>
                            new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        const dictionaryNames = this.state.dictionaryNames;
                                        var validData =  true;
                                        if(/[^a-zA-Z0-9-_.]/.test(newData.name)) {
                                            validData = false;
                                            alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)');
                                        }
                                        if(validData){
                                            const index = dictionaryNames.indexOf(oldData);
                                            dictionaryNames[index] = newData;
                                            this.setState({ dictionaryNames }, () => resolve());
                                            this.setState({addDict: true, newDict: newData});
                                        }
                                    }
                                    resolve();
                                }, 1000);
                            }),
                            onRowDelete: oldData =>
                new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        const data = this.state.dictionaryNames;
                    const index = data.indexOf(oldData);
                    data.splice(index, 1);
                    this.setState({ data }, () => resolve());
                                        this.setState({delDict: true, delData: oldData})
                    }
                    resolve()
                }, 1000)
                })
                        }}
                        />:""
                    }
                    {this.state.dictNameFlag? <MaterialTable
                        title={"Dictionary Elements List"}
                        data={this.state.dictionaryElements}
                        columns={this.state.dictElementColumns}
                        icons={this.state.tableIcons}
                        options={{
                            exportButton: true,
                            exportFileName: this.state.exportFilename,
                            headerStyle:{backgroundColor:'white',  fontSize: '15pt', text: 'bold', border: '1px solid black'}
                        }}
                        components={{
                            Toolbar: props => (
                                <div>
                                    <MTableToolbar {...props} />
                                <div>
                                    <Grid item container xs={12} alignItems="flex-end" direction="column" justify="flex-end">
                                        <Tooltip title="Import" placement = "bottom">
                                            <IconButton aria-label="import" onClick={() => this.fileUpload.click()}>
                                                <VerticalAlignTopIcon />
                                            </IconButton>
                                        </Tooltip>
                                    </Grid>
                                </div>
                                <input type="file" ref={(fileUpload) => {this.fileUpload = fileUpload;}} style={{ visibility: 'hidden'}} onChange={this.fileSelectedHandler} />
                                </div>
                            )
                        }}
                        editable={{
                            onRowAdd: newData =>
                            new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        const dictionaryElements = this.state.dictionaryElements;
                                        var validData =  true;
                                        for (var i = 0; i < this.state.dictionaryElements.length; i++) {
                                            if (this.state.dictionaryElements[i].shortName === newData.shortName) {
                                                validData = false;
                                                alert(newData.shortname + 'short name already exists')
                                            }
                                        }
                                        if(/[^a-zA-Z0-9-_.]/.test(newData.shortName)) {
                                            validData = false;
                                            alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)');
                                        }
                                        if(!newData.type){
                                            validData = false;
                                            alert('Element Type cannot be null');
                                        }
                                        if(validData){
                                            dictionaryElements.push(newData);
                                            this.setState({ dictionaryElements }, () => resolve());
                                            this.setState({addDict: true, newDictItem: [newData]});
                                        }
                                    }
                                    resolve();
                                }, 1000);
                            }),
                            onRowUpdate: (newData, oldData) =>
                            new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        const dictionaryElements = this.state.dictionaryElements;
                                        var validData =  true;
                                        if(!newData.type){
                                            validData = false;
                                            alert('Element Type cannot be null');
                                        }
                                        if(validData){
                                            const index = dictionaryElements.indexOf(oldData);
                                            dictionaryElements[index] = newData;
                                            this.setState({ dictionaryElements }, () => resolve());
                                            this.setState({addDict: true, newDictItem: [newData]});
                                        }
                                    }
                                    resolve();
                                }, 1000);
                            }),
                            onRowDelete: oldData =>
                new Promise((resolve, reject) => {
                                setTimeout(() => {
                                    {
                                        let data = this.state.dictionaryElements;
                    const index = data.indexOf(oldData);
                    data.splice(index, 1);
                    this.setState({ data }, () => resolve());
                                        this.setState({delDict: true, delDictItem: oldData})
                    }
                    resolve()
                }, 1000)
                })
                        }}
                        />:""
                    }
                    {this.state.dictNameFlag?<button onClick={this.clickHandler} style={{marginTop: '25px'}}>Go Back to Dictionaries List</button>:""}
                    {this.state.addDict && this.addDictionary()}
                    {this.state.delDict && this.deleteDictionary()}
                </Modal.Body>
                <Modal.Footer>
                    <Button variant="secondary" type="null" onClick={this.handleClose}>Close</Button>
                </Modal.Footer>
            </ModalStyled>
        );
    }
}