2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
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';
32 export class PackagesStore extends Store<PackagesDashboardState> {
33 // TDOD fixed for now as there is no requirement to change it from UI
35 private bluePrintContent: BluePrintPage = new BluePrintPage();
37 constructor(private packagesServiceList: PackagesApiService) {
38 super(new PackagesDashboardState());
42 console.log('getting all packages...');
43 this.getPagedPackages(0, this.pageSize);
46 public search(command: string) {
48 this.searchPagedPackages(command, 0, this.pageSize);
50 this.getPagedPackages(0, this.pageSize);
54 public filterByTags(tags: string[]) {
55 console.log(this.state.currentPage);
56 this.getPagedPackagesByTags(this.state.command, this.state.currentPage,
57 this.pageSize, this.state.sortBy, tags);
61 public getPage(pageNumber: number, pageSize: number) {
62 if (this.isCommandExist()) {
63 this.searchPagedPackages(this.state.command, pageNumber, pageSize);
65 this.getPagedPackages(pageNumber, pageSize);
69 public sortPagedPackages(sortBy: string) {
70 if (this.isCommandExist()) {
71 this.searchPagedPackages(this.state.command, this.state.currentPage, this.pageSize, sortBy);
73 this.getPagedPackages(this.state.currentPage, this.pageSize, sortBy);
79 protected getPagedPackages(pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
81 this.packagesServiceList.getPagedPackages(pageNumber, pageSize, sortBy)
82 .subscribe((pages: BluePrintPage[]) => {
86 filteredPackages: pages[0],
88 totalPackages: pages[0].totalElements,
89 currentPage: pageNumber,
90 // this param is set only in get all as it represents the total number of pacakges in the server
91 totalPackagesWithoutSearchorFilters: pages[0].totalElements,
98 private searchPagedPackages(keyWord: string, pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
99 this.packagesServiceList.getPagedPackagesByKeyWord(keyWord, pageNumber, pageSize, sortBy)
100 .subscribe((pages: BluePrintPage[]) => {
104 filteredPackages: pages[0],
106 totalPackages: pages[0].totalElements,
107 currentPage: pageNumber,
114 private isCommandExist() {
115 return this.state.command;
118 private getPagedPackagesByTags(keyWord: string, currentPage1: number, pageSize: number, sortBy1: string, tagsSearchable: string[]) {
119 this.getPagedPackagesByKeyWordFilteredByTags(tagsSearchable)
120 .subscribe((pages: BluePrintPage) => {
123 page: this.state.page,
124 filteredPackages: pages,
126 tags: tagsSearchable,
127 // totalPackages: pages.totalElements,
128 currentPage: currentPage1,
130 totalPackages: this.state.page.totalElements,
135 private getPagedPackagesByKeyWordFilteredByTags(tagsSearchable: string[]): Observable<any> {
136 this.bluePrintContent.content = [];
137 if (tagsSearchable && tagsSearchable.length !== 0 && !tagsSearchable.includes('All')) {
138 tagsSearchable.forEach(tag => {
140 this.state.page.content.forEach(bluePrintModel => {
141 if (tag.endsWith(',')) {
142 tag = tag.replace(',', '');
144 bluePrintModel.tags.split(',').forEach(bluePrintModelTag => {
145 if (bluePrintModelTag === tag) {
146 this.bluePrintContent.content.push(bluePrintModel);
151 this.getPagedPackages(this.state.currentPage, this.pageSize);
152 return of(this.state.page);
155 this.bluePrintContent.content = this.bluePrintContent.content.filter((value, index, self) => self.indexOf(value) === index);
156 console.log('the lenght is ' + this.bluePrintContent.content.length);
157 return of(this.bluePrintContent);
159 this.getPagedPackages(this.state.currentPage, this.pageSize);
160 return of(this.state.page);