1ce33f6ab63dc81a666b9f58b884491b1a5fb0c3
[ccsdk/cds.git] /
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
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
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
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============================================
20 */
21
22 import { Component, OnInit, EventEmitter, Output, ViewChild } from '@angular/core';
23 import { Store } from '@ngrx/store';
24 import * as JSZip from 'jszip';
25 import { Observable } from 'rxjs';
26
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
33 @Component({
34   selector: 'app-search-template',
35   templateUrl: './search-template.component.html',
36   styleUrls: ['./search-template.component.scss']
37 })
38 export class SearchTemplateComponent implements OnInit {
39   file: File;
40   localBluePrintData: IBlueprint;
41   fileText: object[];
42   blueprintState: IBlueprintState;
43   bpState: Observable<IBlueprintState>;
44
45   @ViewChild('fileInput') fileInput;
46   result: string = '';
47
48   private paths = [];
49   private tree;
50   private zipFile: JSZip = new JSZip();
51   private fileObject: any;
52   private activationBlueprint: any;
53   private tocsaMetadaData: any;
54   private blueprintName: string;
55
56   constructor(private store: Store<IAppState>) { }
57
58   ngOnInit() {
59   }
60
61   fileChanged(e: any) {
62     this.file = e.target.files[0];
63
64     this.zipFile.loadAsync(this.file)
65       .then((zip) => {
66         if(zip) {            
67           this.buildFileViewData(zip);
68         }
69       });
70   }
71
72   updateBlueprintState() {
73     // let fileReader = new FileReader();
74     // fileReader.readAsText(this.file);
75     // var me = this;
76     // fileReader.onload = function () {
77     //   var data: IBlueprint = JSON.parse(fileReader.result.toString());
78     //   me.store.dispatch(new LoadBlueprintSuccess(data));
79     // }
80
81     let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint;
82     let blueprintState = {
83       blueprint: data,
84       name: this.blueprintName,
85       files: this.tree,
86       filesData: this.paths
87     }
88     this.store.dispatch(new SetBlueprintState(blueprintState))
89     // this.store.dispatch(new LoadBlueprintSuccess(data));
90   }
91
92   async buildFileViewData(zip) {
93     for (var file in zip.files) {
94       this.fileObject = {
95         name: zip.files[file].name,
96         data: ''
97       };
98       const value = <any>await  zip.files[file].async('string');
99       this.fileObject.data = value;
100       this.paths.push(this.fileObject); 
101     }
102     this.fetchTOSACAMetadata();
103    this.tree = this.arrangeTreeData(this.paths);
104   }
105
106   arrangeTreeData(paths) {
107     const tree = [];
108
109     paths.forEach((path) => {
110
111       const pathParts = path.name.split('/');
112       pathParts.shift();
113       let currentLevel = tree;
114
115       pathParts.forEach((part) => {
116         const existingPath = currentLevel.filter(level => level.name === part);
117
118         if (existingPath.length > 0) {
119           currentLevel = existingPath[0].children;
120         } else {
121           const newPart = {
122             name: part,
123             children: [],
124             data: path.data
125           };
126           if(part.trim() == this.blueprintName.trim()) { 
127             this.activationBlueprint = path.data; 
128             newPart.data = JSON.parse(this.activationBlueprint.toString());            
129             console.log('newpart', newPart);
130           }
131           if(newPart.name !== '') {            
132               currentLevel.push(newPart);
133               currentLevel = newPart.children;
134           }
135         }
136       });
137     });
138     console.log('tree', tree);
139     return tree;
140   }
141
142   fetchTOSACAMetadata() {
143     let toscaData = {};
144     this.paths.forEach(file =>{
145       if(file.name.includes('TOSCA.meta')) {
146         let keys = file.data.split("\n");
147         keys.forEach((key)=>{
148           let propertyData = key.split(':');
149           toscaData[propertyData[0]] = propertyData[1];
150         });
151       }
152     });
153     this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
154     console.log(toscaData);
155   }
156 }