Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pipes / entity-filter.pipe.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 import {Pipe, PipeTransform} from "@angular/core";
22 import {Component, Resource} from "app/models";
23 import {ComponentType} from "app/utils/constants";
24
25 export interface ISearchFilter {
26     [key:string]: string;
27 }
28
29 export interface IEntityFilterObject {
30     // Types
31     selectedComponentTypes?:Array<string>;
32     selectedResourceSubTypes?:Array<string>;
33     // Categories
34     selectedCategoriesModel?:Array<string>;
35     // Statuses
36     selectedStatuses?:Array<string>;
37     // distributed
38     distributed?:Array<string>;
39     // search
40     search?:ISearchFilter;
41     
42 }
43
44 @Pipe({name: 'entity-filter'})
45 export class EntityFilterPipe implements PipeTransform{
46     constructor() {
47     }
48
49     public static transform(components:Array<Component>, filter:IEntityFilterObject) {
50         let filteredComponents:Array<Component> = components;
51
52         // filter by type
53         // --------------------------------------------------------------------------
54         if ((filter.selectedComponentTypes && filter.selectedComponentTypes.length > 0) || (filter.selectedResourceSubTypes && filter.selectedResourceSubTypes.length > 0)) {
55             let filteredTypes = [];
56             angular.forEach(components, (component:Component):void => {
57                 // Filter by component type
58                 let typeLower:string = component.componentType.toLowerCase();
59                 let typeFirstCapital:string = typeLower.charAt(0).toUpperCase() + typeLower.slice(1);
60                 if (filter.selectedComponentTypes.indexOf(typeFirstCapital) !== -1) {
61                     filteredTypes.push(component);
62                 }
63
64                 // Filter by resource sub type, only in case the resource checkbox was not selected (because in this case we already added all the components in above section).
65                 if (component.isResource() && filter.selectedComponentTypes.indexOf("Resource") === -1 && filter.selectedResourceSubTypes.length > 0) {
66                     //filteredComponents.pop(); // Remove the last inserted component.
67                     let resource:Resource = <Resource>component;
68                     if (filter.selectedResourceSubTypes.indexOf(resource.getComponentSubType()) !== -1) {
69                         filteredTypes.push(component);
70                     }
71                 }
72             });
73             filteredComponents = filteredTypes;
74         }
75
76         // filter by categories & subcategories & groupings
77         // --------------------------------------------------------------------------
78         if (filter.selectedCategoriesModel && filter.selectedCategoriesModel.length > 0) {
79             let filteredCategories = [];
80             angular.forEach(filteredComponents, (component:Component):void => {
81                 let componentCategory = component.categoryNormalizedName +
82                     ((component.subCategoryNormalizedName) ? '.' + component.subCategoryNormalizedName : '');
83                 if (component.componentType === ComponentType.RESOURCE) {
84                     componentCategory = 'resourceNewCategory.' + componentCategory;
85                 } else if (component.componentType === ComponentType.SERVICE) {
86                     componentCategory = 'serviceNewCategory.' + componentCategory;
87                 }
88                 if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {
89                     filteredCategories.push(component);
90                 }
91             });
92             filteredComponents = filteredCategories;
93         }
94
95         // filter by statuses
96         // --------------------------------------------------------------------------
97         if (filter.selectedStatuses && filter.selectedStatuses.length > 0) {
98
99             let filteredStatuses = [];
100             angular.forEach(filteredComponents, (component:Component):void => {
101                 if (filter.selectedStatuses.indexOf(component.lifecycleState) > -1) {
102                     filteredStatuses.push(component);
103                 }
104                 //if status DISTRIBUTED && CERTIFIED are selected the component will added in  CERTIFIED status , not need to add twice
105                 if (filter.selectedStatuses.indexOf('DISTRIBUTED') > -1 && !(filter.selectedStatuses.indexOf('CERTIFIED') > -1)) {
106                     if (component.distributionStatus && component.distributionStatus.indexOf('DISTRIBUTED') > -1 && component.lifecycleState.indexOf('CERTIFIED') > -1) {
107                         filteredStatuses.push(component);
108                     }
109                 }
110             });
111             filteredComponents = filteredStatuses;
112         }
113
114         // filter by statuses and distributed
115         // --------------------------------------------------------------------------
116         if (filter.distributed != undefined && filter.distributed.length > 0) {
117             let filterDistributed:Array<any> = filter.distributed;
118             let filteredDistributed = [];
119             angular.forEach(filteredComponents, (entity) => {
120                 filterDistributed.forEach((distribute) => {
121                     let distributeItem = distribute.split(',');
122                     distributeItem.forEach((item) => {
123                         if (item !== undefined && entity.distributionStatus === item) {
124                             filteredDistributed.push(entity);
125                         }
126                     })
127                 });
128             });
129             filteredComponents = filteredDistributed;
130         }
131
132         // filter by search
133         // --------------------------------------------------------------------------
134         if (filter.search != undefined) {
135             Object.keys(filter.search).forEach((searchKey) => {
136                 let searchVal = filter.search[searchKey];
137                 if (searchVal) {
138                     searchVal = searchVal.toLowerCase();
139                     filteredComponents = filteredComponents.filter((component:Component) =>
140                         component[searchKey].toLowerCase().indexOf(searchVal) !== -1);
141                 }
142             });
143         }
144
145         return filteredComponents;
146     }
147
148     public transform(components:Array<Component>, filter:IEntityFilterObject) {
149         return EntityFilterPipe.transform(components, filter);
150     }
151 }