2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import {Component, Resource} from "../models";
22 import {ComponentType} from "../utils/constants";
24 export class EntityFilter {
28 let filter = <EntityFilter>( (components:Array<Component>, filter:any) => {
30 let filteredComponents:Array<Component> = components;
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);
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);
53 filteredComponents = filteredTypes;
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;
68 if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {
69 filteredCategories.push(component);
72 filteredComponents = filteredCategories;
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);
81 let filteredStatuses = [];
82 angular.forEach(filteredComponents, (component:Component):void => {
83 if (selectedStatuses.indexOf(component.lifecycleState) > -1) {
84 filteredStatuses.push(component);
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);
93 filteredComponents = filteredStatuses;
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);
111 filteredComponents = filteredDistributed;
114 return filteredComponents;