File

projects/netgrif-components-core/src/lib/filter/models/merged-filter.ts

Description

Represents an union or an intersection of SimpleFilters and allows limited manipulation with it.

Extends

Filter

Index

Methods
Accessors

Constructor

constructor(id: string, type: FilterType, filterBodies: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>, _operator: MergeOperator, title?: string, isPfql: boolean)
Parameters :
Name Type Optional Description
id string No

identifier of the filter

type FilterType No

type of resources that the filter can query

filterBodies Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody> No

Array of search requests matching this filter's type

_operator MergeOperator No

boolean operator that is used to combine the filters

title string Yes

human readable filter name

isPfql boolean No

true if the Filter should cooperate with PFQL API

Methods

Public addFullTextFields
addFullTextFields(fullTextFields: literal type)
Inherited from Filter
Defined in Filter:140
Parameters :
Name Type Optional
fullTextFields literal type No
Returns : void
bodyContainsQuery
bodyContainsQuery()
Inherited from Filter
Defined in Filter:121
Returns : boolean
Public clearFullTextFields
clearFullTextFields()
Inherited from Filter
Defined in Filter:147
Returns : void
clone
clone()
Inherited from Filter
Defined in Filter:80
Returns : Filter
Public containsFullTextFields
containsFullTextFields()
Inherited from Filter
Defined in Filter:156
Returns : boolean
Static fromCaseQuery
fromCaseQuery(filterBody: Array<CaseSearchRequestBody>, operator: MergeOperator)

Creates a case filter with the provided body

Parameters :
Name Type Optional Description
filterBody Array<CaseSearchRequestBody> No

the body of the new filter

operator MergeOperator No

the merge operator that is used to combine the filters

Returns : Filter

a case filter with the given body and operator. id of the new filter is set to an empty string.

Static fromQuery
fromQuery(filterBody: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>, operator: MergeOperator, type: FilterType)

Creates a filter with the provided body and the provided type

Parameters :
Name Type Optional Description
filterBody Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody> No

the body of the new filter

operator MergeOperator No

the merge operator that is used to combine the filters

type FilterType No

the type of the new filter. Type must match the type of the body.

Returns : Filter

a filter with the given body, operator and type. id of the new filter is set to an empty string.

Static fromTaskQuery
fromTaskQuery(filterBody: Array, operator: MergeOperator)

Creates a task filter with the provided body

Parameters :
Name Type Optional Description
filterBody Array<TaskSearchRequestBody> No

the body of the new filter

operator MergeOperator No

the merge operator that is used to combine the filters

Returns : Filter

a task filter with the given body and operator. id of the new filter is set to an empty string.

Public getFullTextFields
getFullTextFields()
Inherited from Filter
Defined in Filter:163
Returns : literal type
getRequestBody
getRequestBody()
Inherited from Filter
Defined in Filter:114
Returns : Array | Array
getRequestParams
getRequestParams()
Inherited from Filter
Defined in Filter:131

Returns the necessary request params for the filter.

See MergedFilterParams for more information.

merge
merge(filter: Filter, operator: MergeOperator)
Inherited from Filter
Defined in Filter:89

Filters that are merged must share the same type and the same merge operator.

See Filter.merge() for information about parameters.

Parameters :
Name Type Optional
filter Filter No
operator MergeOperator No
Returns : MergedFilter
Protected deepCopy
deepCopy(obj: object)
Inherited from Filter
Defined in Filter:118

Creates a deep copy of a simple object.

Parameters :
Name Type Optional Description
obj object No

object that should be copied

Returns : object

a deep copy of the argument

Accessors

operator
getoperator()
setoperator(operator: MergeOperator)
Parameters :
Name Type Optional
operator MergeOperator No
Returns : void
import {Filter} from './filter';
import {MergeOperator} from './merge-operator';
import {TaskSearchRequestBody} from './task-search-request-body';
import {CaseSearchRequestBody} from './case-search-request-body';
import {FilterType} from './filter-type';
import {Params} from '../../resources/resource-provider.service';
import {MergedFilterParams} from './merged-filter-params';

/**
 * Represents an union or an intersection of {@link SimpleFilter}s and allows limited manipulation with it.
 */
export class MergedFilter extends Filter {

    /**
     * @ignore
     * Stores the underlying filter data
     */
    private readonly _filters: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>;

    /**
     * Creates a filter with the provided body and the provided type
     * @param filterBody the body of the new filter
     * @param operator the merge operator that is used to combine the filters
     * @param type the type of the new filter. Type must match the type of the body.
     * @returns a filter with the given body, operator and type. `id` of the new filter is set to an empty string.
     */
    public static fromQuery(filterBody: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>, operator: MergeOperator,
                            type: FilterType): Filter {
        return new MergedFilter('', type, filterBody, operator);
    }

    /**
     * Creates a case filter with the provided body
     * @param filterBody the body of the new filter
     * @param operator the merge operator that is used to combine the filters
     * @returns a case filter with the given body and operator. `id` of the new filter is set to an empty string.
     */
    public static fromCaseQuery(filterBody: Array<CaseSearchRequestBody>, operator: MergeOperator): Filter {
        return MergedFilter.fromQuery(filterBody, operator, FilterType.CASE);
    }

    /**
     * Creates a task filter with the provided body
     * @param filterBody the body of the new filter
     * @param operator the merge operator that is used to combine the filters
     * @returns a task filter with the given body and operator. `id` of the new filter is set to an empty string.
     */
    public static fromTaskQuery(filterBody: Array<TaskSearchRequestBody>, operator: MergeOperator): Filter {
        return MergedFilter.fromQuery(filterBody, operator, FilterType.TASK);
    }

    /**
     * @param id identifier of the filter
     * @param type type of resources that the filter can query
     * @param filterBodies `Array` of search requests matching this filter's type
     * @param _operator boolean operator that is used to combine the filters
     * @param title human readable filter name
     * @param isPfql true if the Filter should cooperate with PFQL API
     */
    constructor(id: string, type: FilterType, filterBodies: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>,
                protected _operator: MergeOperator, title?: string, isPfql: boolean = false) {
        super(id, type, title, isPfql);
        this._filters = [];
        filterBodies.forEach(body => {
            this._filters.push(this.deepCopy(body));
        });
    }

    public set operator(operator: MergeOperator) {
        this._operator = operator;
    }

    public get operator(): MergeOperator {
        return this._operator;
    }

    /**
     * See [Filter.clone()]{@link Filter#clone}
     */
    clone(): Filter {
        return new MergedFilter(this.id, this.type, this._filters, this._operator, this.title, this.isPfql);
    }

    /**
     * Filters that are merged must share the same type and the same merge operator.
     *
     * See [Filter.merge()]{@link Filter#merge} for information about parameters.
     */
    merge(filter: Filter, operator: MergeOperator): MergedFilter {
        if (filter.type !== this.type) {
            throw new Error('Can\'t merge filters with different types');
        }
        if (this._operator !== operator || (filter instanceof MergedFilter && filter._operator !== this._operator)) {
            throw new Error('Can\'t merge filters with different merge operators');
        }

        const id = `${this.id}${operator === MergeOperator.AND ? '&' : '|'}${filter.id}`;

        const combinedFilters: Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody> = [];
        combinedFilters.push(...this._filters);
        const otherFilters = filter.getRequestBody();
        if (Array.isArray(otherFilters)) {
            combinedFilters.push(...otherFilters);
        } else {
            combinedFilters.push(otherFilters);
        }

        return new MergedFilter(id, this.type, combinedFilters, this._operator, undefined, this.isPfql);
    }

    /**
     * See [Filter.getRequestBody()]{@link Filter#getRequestBody}
     */
    getRequestBody(): Array<TaskSearchRequestBody> | Array<CaseSearchRequestBody> {
        return this.deepCopy(this._filters) as Array<CaseSearchRequestBody> | Array<TaskSearchRequestBody>;
    }

    /**
     * See [Filter.bodyContainsQuery()]{@link Filter#bodyContainsQuery}
     */
    bodyContainsQuery(): boolean {
        return this._filters.some(f => f.query !== undefined && f.query !== null);
    }

    /**
     * Returns the necessary request params for the filter.
     * @returns params with `operation` set to either `AND` or `OR` based on this object's `_operator` property.
     *
     * See {@link MergedFilterParams} for more information.
     */
    getRequestParams(): MergedFilterParams & Params {
        return {
            operation: this._operator
        };
    }

    /**
     * See [Filter.addFullTextFields()]{@link Filter#addFullTextFields}
     */
    public addFullTextFields(fullTextFields: { [fieldId: string]: number; }): void {
        this._filters.forEach(filter => filter.fullTextFields = fullTextFields);
    }

    /**
     * See [Filter.clearFullTextFields()]{@link Filter#clearFullTextFields}
     */
    public clearFullTextFields(): void {
        this._filters.forEach(filter => {
            filter.fullTextFields = undefined;
        });
    }

    /**
     * See [Filter.containsFullTextFields()]{@link Filter#containsFullTextFields}
     */
    public containsFullTextFields(): boolean {
        return this._filters.some(filter => !!filter.fullTextFields && Object.keys(filter.fullTextFields).length > 0);
    }

    /**
     * See [Filter.getFullTextFields()]{@link Filter#getFullTextFields}
     */
    public getFullTextFields(): { [fieldId: string]: number; } {
        const mergedFullTextFields: { [fieldId: string]: number; } = {};
        this._filters.forEach(filter => {
            if (!filter.fullTextFields || Object.keys(filter.fullTextFields).length === 0) {
                return;
            }
            for (const [fieldId, weight] of Object.entries(filter.fullTextFields)) {
                if (!(fieldId in mergedFullTextFields) || (weight as number) < mergedFullTextFields[fieldId]) {
                    mergedFullTextFields[fieldId] = weight as number;
                }
            }
        });
        return mergedFullTextFields;
    }
}

results matching ""

    No results matching ""