2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 IBM Intellectual Property. All rights reserved.
5 ===================================================================
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
22 import { Component, OnInit, ViewChild, EventEmitter, Output } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { Store } from '@ngrx/store';
25 import { SearchTemplateService } from '../search-template.service';
26 import { MatAutocompleteTrigger } from '@angular/material';
27 import { NotificationHandlerService } from 'src/app/common/core/services/notification-handler.service';
28 import { SearchPipe } from 'src/app/common/shared/pipes/search.pipe';
29 import * as JSZip from 'jszip';
30 import { SortPipe } from '../../../../../common/shared/pipes/sort.pipe';
31 import { LoaderService } from '../../../../../common/core/services/loader.service';
32 import { IBlueprint } from '../../../../../common/core/store/models/blueprint.model';
33 import { IBlueprintState } from '../../../../../common/core/store/models/blueprintState.model';
34 import { IAppState } from '../../../../../common/core/store/state/app.state';
35 import { SetBlueprintState } from '../../../../../common/core/store/actions/blueprint.action';
36 import { SelectTemplateService } from '../../select-template.service';
38 selector: 'app-search-from-database',
39 templateUrl: './search-from-database.component.html',
40 styleUrls: ['./search-from-database.component.scss']
42 export class SearchFromDatabaseComponent implements OnInit {
45 @Output() resourcesData = new EventEmitter();
47 // @ViewChild('resourceSelect', { read: MatAutocompleteTrigger }) resourceSelect: MatAutocompleteTrigger;
49 validfile: boolean = false;
52 private zipFile: JSZip = new JSZip();
55 private fileObject: any;
56 private activationBlueprint: any;
57 private tocsaMetadaData: any;
58 private blueprintName: string;
59 private entryDefinition: string;
60 uploadedFileName: string;
62 searchText: string = '';
63 constructor(private _formBuilder: FormBuilder,
64 private searchService: SearchTemplateService, private alertService: NotificationHandlerService,
65 private loader: LoaderService, private store: Store<IAppState>, private cbEditOption: SelectTemplateService) { }
68 this.myControl = this._formBuilder.group({
69 search_input: ['', Validators.required]
73 this.resourcesData.emit(value);
76 fetchResourceByName() {
78 this.searchService.searchByTags(this.searchText)
80 data.forEach(element => {
81 this.options.push(element)
84 this.alertService.error('Blueprint not matching the search tag' + error);
88 editCBA(artifactName: string, artifactVersion: string, option: string) {
89 this.cbEditOption.setCbaOption(option);
90 this.zipFile.generateAsync({ type: "blob" })
92 const formData = new FormData();
93 formData.append("file", blob);
94 // this.editorService.enrich("/enrich-blueprint/", formData)
95 this.searchService.getBlueprintZip(artifactName + "/" + artifactVersion)
98 // console.log(response);
99 this.zipFile.files = {};
100 this.zipFile.loadAsync(response)
103 this.buildFileViewData(zip);
104 // console.log("processed");
105 let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint;
106 let blueprintState = {
108 name: this.blueprintName,
110 filesData: this.paths,
111 uploadedFileName: this.blueprintName,
112 entryDefinition: this.entryDefinition
114 this.store.dispatch(new SetBlueprintState(blueprintState));
115 // console.log(blueprintState);
118 // this.alertService.success('Blueprint enriched successfully');
121 this.alertService.error('Blue print error' + error.message);
127 this.filesData.forEach((path) => {
128 let index = path.name.indexOf("/");
129 let name = path.name.slice(index + 1, path.name.length);
130 this.zipFile.file(name, path.data);
134 async buildFileViewData(zip) {
135 this.validfile = false;
137 // console.log(zip.files);
138 for (var file in zip.files) {
139 console.log("name: " + zip.files[file].name);
141 // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name,
142 // name: zip.files[file].name,
143 name: this.uploadedFileName + '/' + zip.files[file].name,
146 const value = <any>await zip.files[file].async('string');
147 this.fileObject.data = value;
148 this.paths.push(this.fileObject);
152 this.paths.forEach(path => {
153 if (path.name.includes("TOSCA.meta")) {
154 this.validfile = true
158 alert('Please update proper file');
161 if (this.validfile) {
162 this.fetchTOSACAMetadata();
163 this.paths = new SortPipe().transform(this.paths, 'asc', 'name');
164 this.tree = this.arrangeTreeData(this.paths);
166 alert('Please update proper file with TOSCA metadata');
170 arrangeTreeData(paths) {
173 paths.forEach((path) => {
175 const pathParts = path.name.split('/');
176 // pathParts.shift();
177 let currentLevel = tree;
179 pathParts.forEach((part) => {
180 const existingPath = currentLevel.filter(level => level.name === part);
182 if (existingPath.length > 0) {
183 currentLevel = existingPath[0].children;
191 if (part.trim() == this.blueprintName.trim()) {
192 this.activationBlueprint = path.data;
193 newPart.data = JSON.parse(this.activationBlueprint.toString());
194 // console.log('newpart', newPart);
195 this.entryDefinition = path.name.trim();
197 if (newPart.name !== '') {
198 currentLevel.push(newPart);
199 currentLevel = newPart.children;
204 this.loader.hideLoader();
208 fetchTOSACAMetadata() {
210 this.paths.forEach(file => {
211 if (file.name.includes('TOSCA.meta')) {
212 let keys = file.data.split("\n");
213 keys.forEach((key) => {
214 let propertyData = key.split(':');
215 toscaData[propertyData[0]] = propertyData[1];
219 this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
220 // console.log(toscaData);