Initial OpenECOMP SDC commit
[sdc.git] / catalog-ui / app / scripts / view-models / workspace / tabs / activity-log / activity-log.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 /// <reference path="../../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23
24     export interface IActivityLogViewModelScope extends IWorkspaceViewModelScope {
25         activityDateArray: Array<any>; //this is in order to sort the dates
26         activityLog: Array<Models.Activity>;
27         preVersion:string;
28
29         tableHeadersList: Array<any>;
30         reverse: boolean;
31         sortBy:string;
32         searchBind:string;
33
34         getActivityLog(uniqueId:string):void;
35         onVersionChanged(version:any) : void;
36         parseAction(action:string):string;
37         sort(sortBy:string): void;
38     }
39
40     export class ActivityLogViewModel {
41
42         static '$inject' = [
43             '$scope',
44             '$state',
45             'Sdc.Services.ActivityLogService'
46         ];
47
48         constructor(private $scope:IActivityLogViewModelScope,
49                     private $state:ng.ui.IStateService,
50                     private activityLogService:Services.ActivityLogService
51             ) {
52
53             this.initScope();
54             this.$scope.setValidState(true);
55             this.initSortedTableScope();
56             this.$scope.updateSelectedMenuItem();
57
58             // Set default sorting
59             this.$scope.sortBy = 'logDate';
60         }
61
62         private initScope():void {
63
64             this.$scope.preVersion = this.$scope.component.version;
65
66             this.$scope.onVersionChanged = (version:any):void => {
67                 if (version.versionNumber != this.$scope.component.version) {
68                     this.$scope.isLoading = true;
69                     this.$scope.getActivityLog(version.versionId);
70                 }
71             };
72
73             this.$scope.getActivityLog = (uniqueId:any):void => {
74
75                 let onError = (response) => {
76                     this.$scope.isLoading = false;
77                     console.info('onFaild', response);
78
79                 };
80
81                 let onSuccess = (response:Array<Models.Activity>) => {
82                     this.$scope.activityLog = _.sortBy(response, function(o) { return o.TIMESTAMP; }); //response; //
83                     this.$scope.isLoading = false;
84                 };
85
86                 this.$scope.isLoading = true;
87                 if (this.$scope.component.isResource()) {
88                     this.activityLogService.getActivityLogService('resources', uniqueId).then(onSuccess, onError);
89                 }
90                 if (this.$scope.component.isService()) {
91                     this.activityLogService.getActivityLogService('services', uniqueId).then(onSuccess, onError);
92                 }
93
94             };
95
96             if (!this.$scope.activityLog || this.$scope.preVersion != this.$scope.component.version) {
97                 this.$scope.getActivityLog(this.$scope.component.uniqueId);
98             }
99
100             this.$scope.parseAction = (action:string) => {
101                 return action ? action.split(/(?=[A-Z])/).join(' ') : '';
102             };
103
104         }
105
106         private initSortedTableScope = ():void => {
107             this.$scope.tableHeadersList = [
108                 {title: 'Date', property: 'logDate'},
109                 {title: 'Action', property: 'logAction'},
110                 {title: 'Comment', property: 'logComment'},
111                 {title: 'Username', property: 'logUsername'},
112                 {title: 'Status', property: 'logStatus'}
113             ];
114
115             this.$scope.sort = (sortBy:string):void => {
116                 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
117                 this.$scope.sortBy = sortBy;
118             };
119         };
120
121     }
122 }