Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / packages.store.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. 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 {Injectable} from '@angular/core';
23 import {BluePrintPage} from './model/BluePrint.model';
24 import {Store} from '../../../common/core/stores/Store';
25 import {PackagesApiService} from './packages-api.service';
26 import {PackagesDashboardState} from './model/packages-dashboard.state';
27 import {Observable, of} from 'rxjs';
28 import {NgxUiLoaderService} from 'ngx-ui-loader';
29
30 @Injectable({
31     providedIn: 'root'
32 })
33 export class PackagesStore extends Store<PackagesDashboardState> {
34     // TDOD fixed for now as there is no requirement to change it from UI
35     public pageSize = 15;
36     private bluePrintContent: BluePrintPage = new BluePrintPage();
37
38     constructor(
39         private packagesServiceList: PackagesApiService,
40         private ngxLoader: NgxUiLoaderService
41     ) {
42         super(new PackagesDashboardState());
43     }
44
45     public getAll() {
46         console.log('getting all packages...');
47         this.getPagedPackages(0, this.pageSize);
48     }
49
50     public search(command: string) {
51         if (command) {
52             this.searchPagedPackages(command, 0, this.pageSize);
53         } else {
54             this.getPagedPackages(0, this.pageSize);
55         }
56     }
57
58     public filterByTags(tags: string[]) {
59         console.log(this.state.currentPage);
60         this.getPagedPackagesByTags(this.state.command, this.state.currentPage,
61             this.pageSize, this.state.sortBy, tags);
62
63     }
64
65     public getPage(pageNumber: number, pageSize: number) {
66         if (this.isCommandExist()) {
67             this.searchPagedPackages(this.state.command, pageNumber, pageSize);
68         } else {
69             this.getPagedPackages(pageNumber, pageSize);
70         }
71     }
72
73     public sortPagedPackages(sortBy: string) {
74         if (this.isCommandExist()) {
75             this.searchPagedPackages(this.state.command, this.state.currentPage, this.pageSize, sortBy);
76         } else {
77             this.getPagedPackages(this.state.currentPage, this.pageSize, sortBy);
78         }
79
80     }
81
82
83     protected getPagedPackages(pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
84
85         this.packagesServiceList.getPagedPackages(pageNumber, pageSize, sortBy)
86             .subscribe((pages: BluePrintPage[]) => {
87                 this.setState({
88                     ...this.state,
89                     page: pages[0],
90                     filteredPackages: pages[0],
91                     command: '',
92                     totalPackages: pages[0].totalElements,
93                     currentPage: pageNumber,
94                     // this param is set only in get all as it represents the total number of pacakges in the server
95                     totalPackagesWithoutSearchorFilters: pages[0].totalElements,
96                     tags: [],
97                     sortBy
98                 });
99             }, err => {
100                 console.log(err);
101             }, () => {
102                 this.ngxLoader.stop();
103             });
104     }
105
106     private searchPagedPackages(keyWord: string, pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
107         this.packagesServiceList.getPagedPackagesByKeyWord(keyWord, pageNumber, pageSize, sortBy)
108             .subscribe((pages: BluePrintPage[]) => {
109                 this.setState({
110                     ...this.state,
111                     page: pages[0],
112                     filteredPackages: pages[0],
113                     command: keyWord,
114                     totalPackages: pages[0].totalElements,
115                     currentPage: pageNumber,
116                     tags: [],
117                     sortBy
118                 });
119             }, err => {
120                 console.log(err);
121             }, () => {
122                 this.ngxLoader.stop(); // start master loader
123             });
124     }
125
126     private isCommandExist() {
127         return this.state.command;
128     }
129
130     private getPagedPackagesByTags(keyWord: string, currentPage1: number, pageSize: number, sortBy1: string, tagsSearchable: string[]) {
131         this.getPagedPackagesByKeyWordFilteredByTags(tagsSearchable)
132             .subscribe((pages: BluePrintPage) => {
133                 this.setState({
134                     ...this.state,
135                     page: this.state.page,
136                     filteredPackages: pages,
137                     command: keyWord,
138                     tags: tagsSearchable,
139                     //  totalPackages: pages.totalElements,
140                     currentPage: currentPage1,
141                     sortBy: sortBy1,
142                     totalPackages: this.state.page.totalElements,
143                 });
144             });
145     }
146
147     private getPagedPackagesByKeyWordFilteredByTags(tagsSearchable: string[]): Observable<any> {
148         this.bluePrintContent.content = [];
149         if (tagsSearchable && tagsSearchable.length !== 0 && !tagsSearchable.includes('All')) {
150             tagsSearchable.forEach(tag => {
151                 if (tag) {
152                     this.state.page.content.forEach(bluePrintModel => {
153                         if (tag.endsWith(',')) {
154                             tag = tag.replace(',', '');
155                         }
156                         bluePrintModel.tags.split(',').forEach(bluePrintModelTag => {
157                             if (tag.includes(bluePrintModelTag.trim())) {
158                                 this.bluePrintContent.content.push(bluePrintModel);
159                             }
160                         });
161                     });
162                 } else {
163                     this.getPagedPackages(this.state.currentPage, this.pageSize);
164                     return of(this.state.page);
165                 }
166             });
167             this.bluePrintContent.content = this.bluePrintContent.content.filter((value, index, self) => self.indexOf(value) === index);
168             console.log('the lenght is ' + this.bluePrintContent.content.length);
169             return of(this.bluePrintContent);
170         } else {
171             this.getPagedPackages(this.state.currentPage, this.pageSize);
172             return of(this.state.page);
173         }
174     }
175
176
177 }