CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / distribution / disribution-status-modal / disribution-status-modal-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {Distribution, DistributionComponent, ExportExcel} from "app/models";
24
25 interface IDistributionStatusModalViewModelScope {
26     distribution:Distribution;
27     status:string;
28     getStatusCount(distributionComponent:Array<DistributionComponent>):any;
29     getUrlName(url:string):string;
30     modalDitributionStatus:ng.ui.bootstrap.IModalServiceInstance;
31     footerButtons:Array<any>;
32     //exportExcelData:ExportExcel;
33     close():void;
34     initDataForExportExcel():ExportExcel;
35 }
36
37 export class DistributionStatusModalViewModel {
38
39     static '$inject' = ['$scope', '$uibModalInstance', 'data', '$filter'];
40
41     constructor(private $scope:IDistributionStatusModalViewModelScope,
42                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
43                 private data:any,
44                 private $filter:ng.IFilterService) {
45         this.initScope();
46     }
47
48     private generateMetaDataForExportExcel = ():Array<string>=> {
49         let metaData = [];
50         metaData[0] = 'Name:' + this.data.component.name + '| UUID:' + this.data.component.uuid + '|  Invariant UUID:' + this.data.component.invariantUUID;
51         metaData[1] = 'Distribution ID:' + this.$scope.distribution.distributionID +
52             '| USER ID:' + this.$scope.distribution.userId +
53             '| Time[UTC]:' + this.$filter('date')(this.$scope.distribution.timestamp, 'MM/dd/yyyy h:mma', 'UTC') +
54             '| Status:' + this.$scope.distribution.deployementStatus;
55         return metaData;
56     };
57
58     private generateDataObjectForExportExcel = ():any=> {
59         let correctFormatDataObj = [];
60         _.each(this.$scope.distribution.distributionComponents, (dComponent:DistributionComponent) => {
61             if (dComponent.status == this.$scope.status) {
62                 correctFormatDataObj.push({
63                     'omfComponentID': dComponent.omfComponentID,
64                     'artiFactName': this.$scope.getUrlName(dComponent.url),
65                     'url': dComponent.url,
66                     'timestamp': this.$filter('date')(dComponent.timestamp, 'MM/dd/yyyy h:mma', 'UTC'),
67                     'status': dComponent.status
68                 });
69             }
70         });
71         return correctFormatDataObj;
72     };
73
74     private initScope = ():void => {
75         this.$scope.distribution = this.data.distribution;
76         this.$scope.status = this.data.status;
77         this.$scope.modalDitributionStatus = this.$uibModalInstance;
78
79
80         this.$scope.getUrlName = (url:string):string => {
81             let urlName:string = _.last(url.split('/'));
82             return urlName;
83         };
84
85         this.$scope.initDataForExportExcel = ():ExportExcel => {
86             let exportExcelData = new ExportExcel();
87             exportExcelData.fileName = this.$scope.status;
88             exportExcelData.groupByField = "omfComponentID";
89             exportExcelData.tableHeaders = ["Component ID", "Artifact Name", "URL", "Time(UTC)", "Status"];
90             exportExcelData.metaData = this.generateMetaDataForExportExcel();
91             exportExcelData.dataObj = this.generateDataObjectForExportExcel();
92             return exportExcelData;
93         };
94
95         this.$scope.close = ():void => {
96             this.$uibModalInstance.close();
97         };
98
99         this.$scope.footerButtons = [
100             {'name': 'Close', 'css': 'blue', 'callback': this.$scope.close}
101         ];
102
103     };
104 }