| /******************************************************************************** |
| * Copyright (c) 2015-2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| |
| |
| import {Injectable} from '@angular/core'; |
| import {Http} from '@angular/http'; |
| import {map} from 'rxjs/operators'; |
| |
| import {PropertyService} from '../core/property.service'; |
| import {PreferenceService, Preference, Scope} from '../core/preference.service'; |
| import {Exclude, serialize, deserialize} from 'class-transformer'; |
| import { TRANSLATE } from '../core/mdm-core.module'; |
| |
| export enum Operator { |
| EQUALS, |
| LESS_THAN, |
| GREATER_THAN, |
| LIKE |
| } |
| |
| export namespace OperatorUtil { |
| export function values() { |
| return [ Operator.EQUALS, Operator.LESS_THAN, Operator.GREATER_THAN, Operator.LIKE ]; |
| } |
| export function toString(operator: Operator) { |
| switch (operator) { |
| case Operator.EQUALS: |
| return '='; |
| case Operator.LESS_THAN: |
| return '<'; |
| case Operator.GREATER_THAN: |
| return '>'; |
| case Operator.LIKE: |
| return 'like'; |
| default: |
| return undefined; |
| } |
| } |
| export function toFilterString(operator: Operator) { |
| switch (operator) { |
| case Operator.EQUALS: |
| return 'eq'; |
| case Operator.LESS_THAN: |
| return 'lt'; |
| case Operator.GREATER_THAN: |
| return 'gt'; |
| case Operator.LIKE: |
| return 'lk'; |
| default: |
| return undefined; |
| } |
| } |
| } |
| |
| export class Condition { |
| type: string; |
| attribute: string; |
| operator: Operator; |
| value: string[]; |
| valueType: string; |
| |
| @Exclude() |
| sortIndex: number; |
| |
| constructor(type: string, attribute: string, operator: Operator, value: string[], valueType?: string) { |
| this.type = type; |
| this.attribute = attribute; |
| this.operator = operator; |
| this.value = value; |
| if (valueType) { |
| this.valueType = valueType.toLowerCase(); |
| } else { |
| this.valueType = 'string'; |
| } |
| } |
| } |
| |
| export class SearchFilter { |
| name: string; |
| environments: string[]; |
| resultType: string; |
| fulltextQuery: string; |
| conditions: Condition[] = []; |
| |
| constructor(name: string, environments: string[], resultType: string, fulltextQuery: string, conditions: Condition[]) { |
| this.name = name; |
| this.environments = environments; |
| this.resultType = resultType; |
| this.fulltextQuery = fulltextQuery; |
| this.conditions = conditions; |
| } |
| } |
| |
| @Injectable() |
| export class FilterService { |
| public readonly NO_FILTER_NAME = TRANSLATE('search.filter.no-filter-selected'); |
| public readonly NEW_FILTER_NAME = TRANSLATE('search.filter.new-filter'); |
| public readonly EMPTY_FILTER: SearchFilter = new SearchFilter(this.NO_FILTER_NAME, [], 'Test', '', []); |
| |
| constructor(private http: Http, |
| private _prop: PropertyService, |
| private preferenceService: PreferenceService) { |
| } |
| |
| getFilters() { |
| return this.preferenceService.getPreferenceForScope(Scope.USER, 'filter.nodes.').pipe( |
| map(preferences => preferences.map(p => this.preferenceToFilter(p)))); |
| } |
| |
| saveFilter(filter: SearchFilter) { |
| return this.preferenceService.savePreference(this.filterToPreference(filter)); |
| } |
| |
| private preferenceToFilter(pref: Preference) { |
| return deserialize(SearchFilter, pref.value); |
| } |
| |
| private filterToPreference(filter: SearchFilter) { |
| let pref = new Preference(); |
| pref.value = serialize(filter); |
| pref.key = filter ? 'filter.nodes.' + filter.name : 'filter.nodes.'; |
| pref.scope = Scope.USER; |
| return pref; |
| } |
| |
| deleteFilter(name: string) { |
| return this.preferenceService.deletePreferenceByScopeAndKey(Scope.USER, 'filter.nodes.' + name); |
| } |
| } |