Sync Integ to Master
[sdc.git] / catalog-ui / src / app / filters / entity-filter.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 {Component, Resource} from "../models";
22 import {ComponentType} from "../utils/constants";
23
24 export class EntityFilter {
25
26     constructor() {
27
28         let filter = <EntityFilter>( (components:Array<Component>, filter:any) => {
29
30             let filteredComponents:Array<Component> = components;
31
32             // filter by type
33             // --------------------------------------------------------------------------
34             if ((filter.selectedComponentTypes && filter.selectedComponentTypes.length > 0) || (filter.selectedResourceSubTypes && filter.selectedResourceSubTypes.length > 0)) {
35                 let filteredTypes = [];
36                 angular.forEach(components, (component:Component):void => {
37                     // Filter by component type
38                     let typeLower:string = component.componentType.toLowerCase();
39                     let typeFirstCapital:string = typeLower.charAt(0).toUpperCase() + typeLower.slice(1);
40                     if (filter.selectedComponentTypes.indexOf(typeFirstCapital) !== -1) {
41                         filteredTypes.push(component);
42                     }
43
44                     // 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).
45                     if (component.isResource() && filter.selectedComponentTypes.indexOf("Resource") === -1 && filter.selectedResourceSubTypes.length > 0) {
46                         //filteredComponents.pop(); // Remove the last inserted component.
47                         let resource:Resource = <Resource>component;
48                         if (filter.selectedResourceSubTypes.indexOf(resource.getComponentSubType()) !== -1) {
49                             filteredTypes.push(component);
50                         }
51                     }
52                 });
53                 filteredComponents = filteredTypes;
54             }
55
56             // filter by categories & subcategories & groupings
57             // --------------------------------------------------------------------------
58             if (filter.selectedCategoriesModel && filter.selectedCategoriesModel.length > 0) {
59                 let filteredCategories = [];
60                 angular.forEach(filteredComponents, (component:Component):void => {
61                     let componentCategory = component.categoryNormalizedName +
62                         ((component.subCategoryNormalizedName) ? '.' + component.subCategoryNormalizedName : '');
63                     if (component.componentType === ComponentType.RESOURCE) {
64                         componentCategory = 'resourceNewCategory.' + componentCategory;
65                     } else if (component.componentType === ComponentType.SERVICE) {
66                         componentCategory = 'serviceNewCategory.' + componentCategory;
67                     }
68                     if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {
69                         filteredCategories.push(component);
70                     }
71                 });
72                 filteredComponents = filteredCategories;
73             }
74
75             // filter by statuses
76             // --------------------------------------------------------------------------
77             if (filter.selectedStatuses && filter.selectedStatuses.length > 0) {
78                 //convert array of array to string array
79                 let selectedStatuses:Array<string> = [].concat.apply([], filter.selectedStatuses);
80
81                 let filteredStatuses = [];
82                 angular.forEach(filteredComponents, (component:Component):void => {
83                     if (selectedStatuses.indexOf(component.lifecycleState) > -1) {
84                         filteredStatuses.push(component);
85                     }
86                     //if status DISTRIBUTED && CERTIFIED are selected the component will added in  CERTIFIED status , not need to add twice
87                     if (selectedStatuses.indexOf('DISTRIBUTED') > -1 && !(selectedStatuses.indexOf('CERTIFIED') > -1)) {
88                         if (component.distributionStatus && component.distributionStatus.indexOf('DISTRIBUTED') > -1 && component.lifecycleState.indexOf('CERTIFIED') > -1) {
89                             filteredStatuses.push(component);
90                         }
91                     }
92                 });
93                 filteredComponents = filteredStatuses;
94             }
95
96             // filter by statuses and distributed
97             // --------------------------------------------------------------------------
98             if (filter.distributed != undefined && filter.distributed.length > 0) {
99                 let filterDistributed:Array<any> = filter.distributed;
100                 let filteredDistributed = [];
101                 angular.forEach(filteredComponents, (entity) => {
102                     filterDistributed.forEach((distribute) => {
103                         let distributeItem = distribute.split(',');
104                         distributeItem.forEach((item) => {
105                             if (item !== undefined && entity.distributionStatus === item) {
106                                 filteredDistributed.push(entity);
107                             }
108                         })
109                     });
110                 });
111                 filteredComponents = filteredDistributed;
112             }
113
114             return filteredComponents;
115         });
116
117         return filter;
118     }
119 }