2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 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, EventEmitter, Output, ViewChild, Input } from '@angular/core';
23 import { Store } from '@ngrx/store';
24 import * as JSZip from 'jszip';
25 import { Observable } from 'rxjs';
27 import { IBlueprint } from '../../../../common/core/store/models/blueprint.model';
28 import { IBlueprintState } from '../../../../common/core/store/models/blueprintState.model';
29 import { IAppState } from '../../../../common/core/store/state/app.state';
30 import { LoadBlueprintSuccess, SET_BLUEPRINT_STATE, SetBlueprintState } from '../../../../common/core/store/actions/blueprint.action';
31 import { json } from 'd3';
32 import { SortPipe } from '../../../../common/shared/pipes/sort.pipe';
33 import { LoaderService } from '../../../../common/core/services/loader.service';
34 import { FormGroup, FormBuilder, Validators } from '@angular/forms';
35 import { MatAutocompleteTrigger } from '@angular/material';
39 selector: 'app-search-template',
40 templateUrl: './search-template.component.html',
41 styleUrls: ['./search-template.component.scss']
43 export class SearchTemplateComponent implements OnInit {
45 localBluePrintData: IBlueprint;
47 blueprintState: IBlueprintState;
48 bpState: Observable<IBlueprintState>;
49 validfile: boolean = false;
50 uploadedFileName: string;
51 @ViewChild('fileInput') fileInput;
53 @Input() optionSelected: string;
55 @ViewChild('resourceSelect', { read: MatAutocompleteTrigger }) resourceSelect: MatAutocompleteTrigger;
56 @Output() resourcesData = new EventEmitter();
58 searchText: string = '';
62 private zipFile: JSZip = new JSZip();
63 private fileObject: any;
64 private activationBlueprint: any;
65 private tocsaMetadaData: any;
66 private blueprintName: string;
67 private entryDefinition: string;
69 constructor(private store: Store<IAppState>, private loader: LoaderService,private formBuilder: FormBuilder) { }
72 this.myControl = this.formBuilder.group({
73 search_input: ['', Validators.required]
78 this.resourcesData.emit(value);
81 fetchResourceByName() {
82 // this.exsistingModelService.searchByTags(this.searchText)
83 // .subscribe(data => {
85 // data.forEach(element => {
86 // this.options.push(element)
88 // this.resourceSelect.openPanel();
90 // window.alert('error' + error);
95 this.file = e.target.files[0];
96 this.uploadedFileName = (this.file.name.split('.'))[0];
97 this.zipFile.files = {};
98 this.zipFile.loadAsync(this.file)
101 this.loader.showLoader();
102 this.buildFileViewData(zip);
107 updateBlueprintState() {
108 let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint;
109 let blueprintState = {
111 name: this.blueprintName,
113 filesData: this.paths,
114 uploadedFileName: this.uploadedFileName,
115 entryDefinition: this.entryDefinition
117 this.store.dispatch(new SetBlueprintState(blueprintState))
118 // this.store.dispatch(new LoadBlueprintSuccess(data));
121 async buildFileViewData(zip) {
122 this.validfile = false;
124 console.log(zip.files);
125 for (var file in zip.files) {
126 console.log("name: " +zip.files[file].name);
128 // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name,
129 // name: zip.files[file].name,
130 name: this.uploadedFileName + '/' + zip.files[file].name,
133 const value = <any>await zip.files[file].async('string');
134 this.fileObject.data = value;
135 this.paths.push(this.fileObject);
139 this.paths.forEach(path =>{
140 if(path.name.includes("TOSCA.meta")) {
141 this.validfile = true
145 alert('Please update proper file');
149 this.fetchTOSACAMetadata();
150 this.paths = new SortPipe().transform(this.paths, 'asc', 'name');
151 this.tree = this.arrangeTreeData(this.paths);
153 alert('Please update proper file with TOSCA metadata');
157 arrangeTreeData(paths) {
160 paths.forEach((path) => {
162 const pathParts = path.name.split('/');
163 // pathParts.shift();
164 let currentLevel = tree;
166 pathParts.forEach((part) => {
167 const existingPath = currentLevel.filter(level => level.name === part);
169 if (existingPath.length > 0) {
170 currentLevel = existingPath[0].children;
178 if(part.trim() == this.blueprintName.trim()) {
179 this.activationBlueprint = path.data;
180 newPart.data = JSON.parse(this.activationBlueprint.toString());
181 console.log('newpart', newPart);
182 this.entryDefinition = path.name.trim();
184 if(newPart.name !== '') {
185 currentLevel.push(newPart);
186 currentLevel = newPart.children;
191 this.loader.hideLoader();
195 fetchTOSACAMetadata() {
197 this.paths.forEach(file =>{
198 if(file.name.includes('TOSCA.meta')) {
199 let keys = file.data.split("\n");
200 keys.forEach((key)=>{
201 let propertyData = key.split(':');
202 toscaData[propertyData[0]] = propertyData[1];
206 this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
207 console.log(toscaData);