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 {Pipe, PipeTransform} from "@angular/core";
22 import {Component, Resource} from "app/models";
23 import {ComponentType} from "app/utils/constants";
25 export interface ISearchFilter {
29 export interface IEntityFilterObject {
31 selectedComponentTypes?:Array<string>;
32 selectedResourceSubTypes?:Array<string>;
34 selectedCategoriesModel?:Array<string>;
36 selectedStatuses?:Array<string>;
38 distributed?:Array<string>;
40 search?:ISearchFilter;
44 @Pipe({name: 'entity-filter'})
45 export class EntityFilterPipe implements PipeTransform{
49 public static transform(components:Array<Component>, filter:IEntityFilterObject) {
50 let filteredComponents:Array<Component> = components;
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);
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);
73 filteredComponents = filteredTypes;
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;
88 if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {
89 filteredCategories.push(component);
92 filteredComponents = filteredCategories;
96 // --------------------------------------------------------------------------
97 if (filter.selectedStatuses && filter.selectedStatuses.length > 0) {
99 let filteredStatuses = [];
100 angular.forEach(filteredComponents, (component:Component):void => {
101 if (filter.selectedStatuses.indexOf(component.lifecycleState) > -1) {
102 filteredStatuses.push(component);
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);
111 filteredComponents = filteredStatuses;
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);
129 filteredComponents = filteredDistributed;
133 // --------------------------------------------------------------------------
134 if (filter.search != undefined) {
135 Object.keys(filter.search).forEach((searchKey) => {
136 let searchVal = filter.search[searchKey];
138 searchVal = searchVal.toLowerCase();
139 filteredComponents = filteredComponents.filter((component:Component) =>
140 component[searchKey].toLowerCase().indexOf(searchVal) !== -1);
145 return filteredComponents;
148 public transform(components:Array<Component>, filter:IEntityFilterObject) {
149 return EntityFilterPipe.transform(components, filter);