Import data type in UI
[sdc.git] / catalog-ui / src / app / ng2 / pages / type-workspace / type-workspace-general / type-workspace-general.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, EventEmitter, Inject, Input, OnInit, Output} from '@angular/core';
23 import {FormControl, FormGroup, Validators} from "@angular/forms";
24 import {DataTypeModel} from "../../../../models/data-types";
25 import {DEFAULT_MODEL_NAME} from "app/utils/constants";
26 import {IWorkspaceViewModelScope} from "../../../../view-models/workspace/workspace-view-model";
27 import {ServiceDataTypeReader} from "../../../../utils/service-data-type-reader";
28 import {TranslateService} from "../../../shared/translator/translate.service";
29 import {SdcUiServices} from "onap-ui-angular/dist";
30 import {ModelService} from "../../../services/model.service";
31 import {Model} from "../../../../models/model";
32 import {DataTypesMap} from "../../../../models/data-types-map";
33 import {DataTypeService} from "../../../services/data-type.service";
34 import {Observable} from "rxjs/Observable";
35 import {IDropDownOption} from "onap-ui-angular/dist/form-elements/dropdown/dropdown-models";
36
37 @Component({
38   selector: 'app-type-workspace-general',
39   templateUrl: './type-workspace-general.component.html',
40   styleUrls: ['./type-workspace-general.component.less']
41 })
42 export class TypeWorkspaceGeneralComponent implements OnInit {
43
44   @Input() isViewOnly = true;
45   @Input() dataType: DataTypeModel = new DataTypeModel();
46   @Output() onImportedType = new EventEmitter<any>();
47   importedFile: File;
48   models: Array<Model>;
49   selectedModelName: string;
50   dataTypes: DataTypesMap;
51   derivedFromName: string;
52   dataTypeMap$: Observable<Map<string, DataTypeModel>>;
53   dataTypeMap: Map<string, DataTypeModel>;
54   typeOptions: Array<IDropDownOption>;
55   DEFAULT_MODEL_NAME = DEFAULT_MODEL_NAME;
56
57   type: FormControl = new FormControl(undefined, [Validators.required, Validators.minLength(1), Validators.maxLength(300)]);
58   derivedFrom: FormControl = new FormControl(undefined, [Validators.required, Validators.minLength(1)]);
59   description: FormControl = new FormControl(undefined, [Validators.required, Validators.minLength(1)]);
60   model: FormControl = new FormControl(undefined, [Validators.required]);
61   formGroup: FormGroup = new FormGroup({
62     'type': this.type,
63     'description': this.description,
64     'model': this.model,
65     'derivedFrom': this.derivedFrom
66   });
67
68   constructor(@Inject('$scope') private $scope: IWorkspaceViewModelScope,
69               @Inject('$state') private $state: ng.ui.IStateService,
70               protected dataTypeService: DataTypeService,
71               private modalServiceSdcUI: SdcUiServices.ModalService,
72               private modelService: ModelService,
73               private translateService: TranslateService) {
74       this.typeOptions = [];
75   }
76
77   ngOnInit(): void {
78       this.getImportedFile();
79       if (!this.isViewOnly) {
80           console.log("file size: " + this.importedFile.size);
81           console.log("file type: " + this.importedFile.type);
82           console.log("file lastModifiedDate: " + this.importedFile.lastModifiedDate);
83
84           new ServiceDataTypeReader().read(this.importedFile).then(
85               (serviceType) => {
86                   this.dataType = serviceType;
87                   this.dataType.modificationTime = this.importedFile.lastModifiedDate;
88                   this.dataType.creationTime = this.importedFile.lastModifiedDate;
89                   this.derivedFromName = serviceType.derivedFromName;
90                   this.dataType.uniqueId = this.dataType.model ? this.dataType.model + "." + this.dataType.name : this.dataType.name + ".datatype";
91                   this.$scope.dataType = this.dataType;
92                   this.onImportedType.emit(this.dataType);
93
94                   this.models = [];
95                   this.modelService.getDataTypeModels(this.derivedFromName).subscribe((modelsFound: any) => {
96                       modelsFound.sort().forEach(modelName => {
97                           let model:Model;
98                           if (modelName === null || "" === modelName) {
99                               model = new Model({"name": DEFAULT_MODEL_NAME, "derivedFrom": "", "modelType": "normative"});
100                           }
101                           else {
102                               model = new Model({"name": modelName, "derivedFrom": "", "modelType": "normative"});
103                           }
104                           this.models.push(model);
105                       });
106                       this.onModelChange();
107                       this.$scope.dataType = this.dataType;
108                   });
109
110               },
111               (error) => {
112                   const errorMsg = this.translateService.translate('IMPORT_DATA_TYPE_FAILURE_MESSAGE_TEXT');
113                   console.error(errorMsg, error);
114                   const errorDetails = {
115                       'Error': error.reason,
116                       'Details': error.message
117                   };
118                   console.error(error.reason);
119                   this.modalServiceSdcUI.openErrorDetailModal('Error', errorMsg,
120                       'error-modal', errorDetails);
121                   this.$state.go('dashboard');
122               });
123       }
124     this.initForm();
125   }
126
127   onModelChange(): void {
128     this.selectedModelName = this.models.filter(x => x.name == this.model.value).pop().name;
129     console.log("selected model: " + this.selectedModelName);
130     this.dataType.model = new Model({"name": this.selectedModelName, "derivedFrom": "", "modelType": "normative"});
131     this.dataType.uniqueId = this.dataType.model.name === DEFAULT_MODEL_NAME ?
132         this.dataType.name + ".datatype" : this.dataType.model.name + "." + this.dataType.name + ".datatype";
133     this.$scope.dataType.derivedFromName = this.derivedFromName;
134     this.$scope.dataType = this.dataType;
135     this.$scope.dataType.model = this.dataType.model;
136   }
137
138   private getImportedFile(): void {
139       let importedFile = this.$scope["$parent"]["$resolve"]["$stateParams"]["importedFile"];
140       this.importedFile = <File>importedFile;
141       this.$scope.importFile = this.importedFile;
142       if (this.importedFile) {
143           this.isViewOnly = false;
144       }
145   }
146
147   private initForm(): void {
148     if (!this.dataType) {
149       return;
150     }
151     this.type.setValue(this.dataType.name);
152     this.description.setValue(this.dataType.description);
153     this.model.setValue(this.dataType.model ? this.dataType.model : this.$scope.dataType && this.$scope.dataType.model ? this.$scope.dataType.model : DEFAULT_MODEL_NAME);
154     this.derivedFrom.setValue(this.dataType.derivedFromName);
155   }
156 }