bd6500bd964aa1c1767a8328ad58e5dc60e8b1fc
[portal.git] / portal-FE-common / src / app / pages / dashboard-application-catalog / dashboard-application-catalog.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 import { Component, OnInit } from '@angular/core';
39 import { GridsterConfig, GridsterItem } from 'angular-gridster2';
40 import { ApplicationCatalogService } from '../../shared/services/application-catalog/application-catalog.service';
41 import { IApplicationCatalog } from '../../shared/model/application-catalog.model';
42 import { IWidgetCatalog } from '../../shared/model/widget-catalog.model';
43 import { environment } from 'src/environments/environment';
44 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
45 import { CatalogModalComponent } from '../catalog-modal/catalog-modal.component';
46 import { ExternalRequestAccessService } from 'src/app/shared/services/external-request-access-service/external-request-access.service';
47 import { UsersService } from 'src/app/shared/services/users/users.service';
48 import { AddTabFunctionService } from 'src/app/shared/services/tab/add-tab-function.service';
49 import { AuditLogService } from 'src/app/shared/services/auditLog/audit-log.service';
50
51 @Component({
52   selector: 'app-dashboard-application-catalog',
53   templateUrl: './dashboard-application-catalog.component.html',
54   styleUrls: ['./dashboard-application-catalog.component.scss']
55 })
56 export class DashboardApplicationCatalogComponent implements OnInit {
57
58   widgetCatalogData: IWidgetCatalog[];
59   appCatalogData: IApplicationCatalog[];
60   resultAccessValue: string;
61   orgUserId: string;
62   firstName: string;
63   lastName: string;
64   selectedSortType: any;
65   sortOptions: Array<any>;
66
67   get options(): GridsterConfig {
68     return this.applicationCatalogService.options;
69   } get layout(): GridsterItem[] {
70     return this.applicationCatalogService.layout;
71   } constructor(private applicationCatalogService: ApplicationCatalogService, private externalRequestAccessService: ExternalRequestAccessService, private userService: UsersService,private addTabFuntionService: AddTabFunctionService, private auditLogService: AuditLogService) {
72     this.sortOptions = [{
73       index: 0,
74       value: 'N',
75       title: 'Name'
76     },
77     {
78       index: 1,
79       value: 'L',
80       title: 'Last used'
81     },
82     {
83       index: 2,
84       value: 'F',
85       title: 'Most used'
86     },
87     {
88       index: 3,
89       value: 'M',
90       title: 'Manual'
91     }
92     ];
93     this.selectedSortType = {};
94    }
95
96   ngOnInit() {
97     this.applicationCatalogService.clearCatalog();
98     //this.selectedSortType = this.sortOptions[0];
99     this.getUserAppsSortTypePreference();
100     //
101     // //this.getUserAppsSortTypePreference();
102     //this.getAppCatalogService('N');
103   }
104
105   getUserAppsSortTypePreference() {
106     this.applicationCatalogService.getUserAppsSortTypePreference().subscribe(data => {
107       //console.log("getUserAppsSortTypePreference data"+data);
108       if (data) {
109         var resJson: any = {};
110         resJson.value = data;
111         if (resJson.value === "N" || resJson.value === "") {
112           resJson.index = 0;
113
114         } else if (resJson.value === "L") {
115           resJson.index = 1;
116
117         } else if (resJson.value === "F") {
118           resJson.index = 2;
119
120         } else {
121           resJson.index = 3;
122
123         } 
124           this.selectedSortType = this.sortOptions[resJson.index];
125         //console.log(this.selectedSortType);
126       this.getAppCatalogService(data);          
127       }
128           else {
129                   this.sortTypeChanged('N');
130           }
131           
132                   
133     }, error => {
134       console.log('getUserAppsSortTypePreference Error Object' + error.message);
135     });
136
137   }
138
139   sortTypeChanged(userAppSortTypePref: string) {
140     //console.log("check whether get into the method");
141     if (!userAppSortTypePref) {
142       this.selectedSortType = this.sortOptions[0];
143     }
144     else {
145       this.sortOptions.forEach(obj => {
146         if (obj.value == userAppSortTypePref) {
147           this.selectedSortType = obj;
148         }
149       })
150
151     }
152
153     this.getAppCatalogService(userAppSortTypePref);
154     this.saveAppsSortTypePreference(this.selectedSortType);
155
156   }
157
158   getAppCatalogService(userAppSortTypePref: string) {
159     //console.log("getAppCatalogServices called");
160         if(!userAppSortTypePref)
161     {
162       userAppSortTypePref = "N";
163           this.selectedSortType = this.sortOptions[0];
164       //console.log("userAppSortTypePref"+userAppSortTypePref);
165     }
166     this.applicationCatalogService.getAppsOrderBySortPref(userAppSortTypePref).subscribe(data => {
167       //console.log("Response data" + data);
168       this.appCatalogData = data;
169       if (data) {
170         this.applicationCatalogService.layout = [];
171         for (let entry of data) {
172           //console.log("Check the URL" + environment.api.appThumbnail);
173                   if(entry.applicationType != '3'){
174           var appCatalog = {
175             x: -1,
176             y: -1,
177             id: entry.id,
178             name: entry.name,
179             subHeaderText: entry.notes,
180             imageLink: environment.api.appThumbnail.replace(':appId', <string><any>entry.id),
181             applicationType: entry.applicationType,
182             select: entry.select,
183             access: entry.access,
184             pending: entry.pending,
185             order: entry.order,
186             url: entry.url,
187             appid: entry.id
188           };
189           this.applicationCatalogService.addItem(appCatalog);
190         }
191                 }
192       }
193     }, error => {
194       console.log('getAppCatalogServices Error Object' + error);
195     });
196   };
197
198   saveAppsSortTypePreference(selectedSortType: any) {
199     this.applicationCatalogService.saveAppsSortTypePreference(selectedSortType).subscribe();
200
201   }
202
203   openAddRoleModal(item: any) {
204     //console.log("OpenModal check" + item.id+" "+item.url);
205     if (item.applicationType =='2') {
206       // Link-based apps open in their own browser tab
207       window.open(item.url, '_blank');
208     } else{
209       var tabContent = {
210         id: new Date(),
211         title: item.name,
212         url: item.url,
213         appId: item.appid
214     };
215       this.addTabFuntionService.filter(tabContent);
216     }
217   }
218
219   auditLog(app:any) {
220     this.auditLogService.storeAudit(app.appId, 'app', app.url).subscribe(data => {
221       console.log('App action Saved');
222     }, error => {
223       console.log('auditLog Save Error' + error);
224     });
225   }
226 }