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