import { OperatorService } from '../../../operator-service/operator.service';
import { LoggerService } from '../../../../logger/services/logger.service';
import { Operator } from '../../operator/operator';
import { Query } from '../../query/query';
import { OptionalDependencies } from '../../../category-factory/optional-dependencies';
import { SearchInputType } from '../search-input-type';
import { DatafieldMapKey } from '../../datafield-map-key';
import { SearchAutocompleteOption } from '../search-autocomplete-option';
import { BooleanOperator } from '../../boolean-operator';
import { CaseProcess } from './case-process';
import { EqualsDate } from '../../operator/equals-date';
import { Substring } from '../../operator/substring';
import { EqualsDateTime } from '../../operator/equals-date-time';
import { Equals } from '../../operator/equals';
import {BehaviorSubject, Observable, of, ReplaySubject, Subject, Subscription} from 'rxjs';
import { Category } from '../category';
import { NotEquals } from '../../operator/not-equals';
import { IsNull } from '../../operator/is-null';
import { Like } from '../../operator/like';
import { MoreThan } from '../../operator/more-than';
import { LessThan } from '../../operator/less-than';
import { InRange } from '../../operator/in-range';
import { NotEqualsDate } from '../../operator/not-equals-date';
import { MoreThanDate } from '../../operator/more-than-date';
import { LessThanDate } from '../../operator/less-than-date';
import { InRangeDate } from '../../operator/in-range-date';
import { MoreThanDateTime } from '../../operator/more-than-date-time';
import { LessThanDateTime } from '../../operator/less-than-date-time';
import { InRangeDateTime } from '../../operator/in-range-date-time';
import { AutocompleteOptions } from '../autocomplete-options';
import { ConfigurationInput } from '../../configuration-input';
import { Categories } from '../categories';
import { FormControl } from '@angular/forms';
import moment, { Moment } from 'moment';
import { MoreThanEqual } from '../../operator/more-than-equal';
import { LessThanEqual } from '../../operator/less-than-equal';
import { MoreThanEqualDate } from '../../operator/more-than-equal-date';
import { LessThanEqualDate } from '../../operator/less-than-equal-date';
import { MoreThanEqualDateTime } from '../../operator/more-than-equal-date-time';
import { LessThanEqualDateTime } from '../../operator/less-than-equal-date-time';
import { FilterTextSegment } from '../../persistance/filter-text-segment';
import { UserAutocomplete } from '../user-autocomplete';
import {ResourceTypeQueryPrefix} from "../resource-type-query-prefix";
import {FieldTypeResource} from "../../../../task-content/model/field-type-resource";
import {SimpleExpression} from "../../../../pfql/model/simple-expression";
import {DataSimpleExpression} from "../../../../pfql/model/data-simple-expression";
import {take, map, debounceTime} from "rxjs/operators";
interface Datafield {
netIdentifier: string;
fieldId: string;
fieldType: string;
}
export class CaseDataset extends Category<Datafield> implements AutocompleteOptions {
private static readonly _i18n = 'search.category.case.dataset';
protected static DISABLED_TYPES = [FieldTypeResource.BUTTON.valueOf(), FieldTypeResource.TASK_REF.valueOf(),
FieldTypeResource.CASE_REF.valueOf(), FieldTypeResource.CASE_FILTER.valueOf(), FieldTypeResource.TASK_FILTER.valueOf(),
FieldTypeResource.PROCESS_FILTER.valueOf()];
protected static readonly DATAFIELD_METADATA = 'datafield';
private static readonly AUTOCOMPLETE_ICON = 'account_circle';
protected readonly _DATAFIELD_INPUT: ConfigurationInput;
protected _processCategory: CaseProcess;
protected _configurationInputs$: BehaviorSubject<Array<ConfigurationInput>>;
protected _datafieldOptions: Map<string, Array<Datafield>>;
protected _datafieldOptionsInitialized$: ReplaySubject<void>;
protected _allowedNetsSub: Subscription;
protected _userAutocomplete: UserAutocomplete;
protected _isLoadedFromPfql: boolean = false;
protected _operatorWasLoadedFromPfql = false;
public static FieldTypeToInputType(fieldType: string): SearchInputType {
switch (fieldType) {
case FieldTypeResource.DATE.valueOf():
return SearchInputType.DATE;
case FieldTypeResource.DATE_TIME.valueOf():
return SearchInputType.DATE_TIME;
case FieldTypeResource.NUMBER.valueOf():
return SearchInputType.NUMBER;
case FieldTypeResource.BOOLEAN.valueOf():
return SearchInputType.BOOLEAN;
case FieldTypeResource.USER.valueOf():
return SearchInputType.AUTOCOMPLETE;
default:
return SearchInputType.TEXT;
}
}
constructor(operators: OperatorService, logger: LoggerService, protected _optionalDependencies: OptionalDependencies) {
super(undefined,
undefined,
`${CaseDataset._i18n}.name`,
undefined,
logger,
operators,
ResourceTypeQueryPrefix.CASES);
this._processCategory = this._optionalDependencies.categoryFactory.get(CaseProcess) as CaseProcess;
this._processCategory.selectDefaultOperator();
this._datafieldOptions = new Map<string, Array<Datafield>>();
this._userAutocomplete = new UserAutocomplete(this._optionalDependencies);
this.createDatafieldOptions();
let dataFieldInputDebounceTime = 0;
if (!!this._optionalDependencies.ignoreNetsOnAutocompleteCategories) {
this._DATAFIELD_INPUT = new ConfigurationInput(
SearchInputType.TEXT,
'search.category.case.dataset.placeholder.plainField',
true,
new Map<string, Array<unknown>>(),
() => { return [] }
);
dataFieldInputDebounceTime = 500;
} else {
this._DATAFIELD_INPUT = new ConfigurationInput(
SearchInputType.AUTOCOMPLETE,
'search.category.case.dataset.placeholder.field',
true,
this._datafieldOptions,
(mapKeys, newValue) => {
return mapKeys.map(serializedMapKey => DatafieldMapKey.parse(serializedMapKey))
.filter(mapKey => mapKey.title.toLocaleLowerCase().startsWith(newValue));
}
);
}
this._configurationInputs$ = new BehaviorSubject<Array<ConfigurationInput>>([this._DATAFIELD_INPUT]);
this._DATAFIELD_INPUT.valueChanges$().pipe(debounceTime(dataFieldInputDebounceTime)).subscribe(newValue => {
if (newValue === undefined
|| (this._DATAFIELD_INPUT.type === SearchInputType.AUTOCOMPLETE && typeof newValue === 'string')) {
this._configurationInputs$.next([this._DATAFIELD_INPUT]);
} else if (this._configurationInputs$.getValue().length === 1
|| (this._DATAFIELD_INPUT.type === SearchInputType.TEXT && typeof newValue === 'string')) {
this._configurationInputs$.next([this._DATAFIELD_INPUT, this._OPERATOR_INPUT]);
}
if (this._operatorWasLoadedFromPfql) {
this._operatorWasLoadedFromPfql = false;
} else {
this._operatorFormControl.setValue(undefined);
}
});
}
destroy() {
super.destroy();
this._configurationInputs$.complete();
this._datafieldOptionsInitialized$.complete();
if (this._allowedNetsSub && !this._allowedNetsSub.closed) {
this._allowedNetsSub.unsubscribe();
}
this._processCategory.destroy();
}
get configurationInputs$(): Observable<Array<ConfigurationInput>> {
return this._configurationInputs$.asObservable();
}
public get inputType(): SearchInputType {
if (!this.hasSelectedDatafields) {
throw new Error('Input type of arguments cannot be determined before selecting a data field during the configuration.');
}
if (!this._selectedDatafields) {
return SearchInputType.TEXT;
}
return CaseDataset.FieldTypeToInputType(this._selectedDatafields[0].fieldType);
}
/**
* The allowed operators are dependant on the selected data field.
*
* Beware that if you want to change the order of the allowed operators, then you must also update the
* [selectDefaultOperator()]{@link Category#selectDefaultOperator} method, so that default operator for each data field type matches
* the default operator of the {@link CaseSimpleDataset} search category. Otherwise the transition of header search into the search GUI
* won't work properly.
*/
public get allowedOperators(): Array<Operator<any>> {
if (!this.hasSelectedDatafields) {
return [];
}
const defaultOperators = [
this._operatorService.getOperator(Substring),
this._operatorService.getOperator(Equals),
this._operatorService.getOperator(NotEquals),
this._operatorService.getOperator(MoreThan),
this._operatorService.getOperator(MoreThanEqual),
this._operatorService.getOperator(LessThan),
this._operatorService.getOperator(LessThanEqual),
this._operatorService.getOperator(IsNull),
this._operatorService.getOperator(Like)
];
if (!this._selectedDatafields) {
return defaultOperators;
}
switch (this._selectedDatafields[0].fieldType) {
case FieldTypeResource.NUMBER.valueOf():
return [
this._operatorService.getOperator(Equals),
this._operatorService.getOperator(NotEquals),
this._operatorService.getOperator(MoreThan),
this._operatorService.getOperator(MoreThanEqual),
this._operatorService.getOperator(LessThan),
this._operatorService.getOperator(LessThanEqual),
this._operatorService.getOperator(InRange),
this._operatorService.getOperator(IsNull)
];
case FieldTypeResource.BOOLEAN.valueOf():
return [
this._operatorService.getOperator(Equals),
this._operatorService.getOperator(NotEquals)
];
case FieldTypeResource.USER.valueOf():
case FieldTypeResource.USER_LIST.valueOf():
return [
this._operatorService.getOperator(Equals),
this._operatorService.getOperator(NotEquals),
this._operatorService.getOperator(IsNull)
];
case FieldTypeResource.DATE.valueOf():
return [
this._operatorService.getOperator(EqualsDate),
this._operatorService.getOperator(NotEqualsDate),
this._operatorService.getOperator(MoreThanDate),
this._operatorService.getOperator(MoreThanEqualDate),
this._operatorService.getOperator(LessThanDate),
this._operatorService.getOperator(LessThanEqualDate),
this._operatorService.getOperator(InRangeDate),
this._operatorService.getOperator(IsNull)
];
case FieldTypeResource.DATE_TIME.valueOf():
return [
this._operatorService.getOperator(EqualsDateTime),
this._operatorService.getOperator(MoreThanDateTime),
this._operatorService.getOperator(MoreThanEqualDateTime),
this._operatorService.getOperator(LessThanDateTime),
this._operatorService.getOperator(LessThanEqualDateTime),
this._operatorService.getOperator(InRangeDateTime),
this._operatorService.getOperator(IsNull)
];
default:
return defaultOperators;
}
}
/**
* @returns `CaseDataset` category is not displayed in bold for better readability
*/
get displayBold(): boolean {
return false;
}
public get hasSelectedDatafields(): boolean {
return this._DATAFIELD_INPUT.isOptionSelected;
}
protected get _selectedDatafields(): Array<Datafield> {
return this._datafieldOptions.get(this._DATAFIELD_INPUT.formControl.value.value);
}
public reset() {
super.reset();
this._DATAFIELD_INPUT.clear();
}
duplicate(): CaseDataset {
return new CaseDataset(this._operatorService, this._log, this._optionalDependencies);
}
protected get pfqlKeywords(): Array<string> {
if (!this.hasSelectedDatafields) {
return [];
} else if (!this._selectedDatafields) {
const fieldId: string = this._DATAFIELD_INPUT.formControl.value
return [`data.${fieldId}.value`];
} else {
return this._selectedDatafields.map(dataField => `data.${dataField.fieldId}.value`);
}
}
get inputPlaceholder(): string {
if (!this.hasSelectedDatafields) {
return `${CaseDataset._i18n}.placeholder.field`;
}
return `${CaseDataset._i18n}.placeholder.value`;
}
protected get datafieldOptionsInitialized$(): Observable<void> {
return this._datafieldOptionsInitialized$.asObservable();
}
protected generateQuery(userInput: Array<unknown>): Query {
let queryGenerationStrategy;
if (this.isSelectedOperator(IsNull)) {
queryGenerationStrategy = (d, _) => this.isNullOperatorQueryGenerationStrategy(d);
} else if (this.inputType === SearchInputType.AUTOCOMPLETE) {
queryGenerationStrategy = (d, ui) => this.standardQueryGenerationStrategy(d, ui[0], false);
} else {
queryGenerationStrategy = (d, ui) => this.standardQueryGenerationStrategy(d, ui);
}
let queries;
if (!this._selectedDatafields) {
queries = [queryGenerationStrategy({
netIdentifier: undefined,
fieldId: this._DATAFIELD_INPUT.formControl.value,
fieldType: 'string'}, userInput)];
} else {
queries = this._selectedDatafields.map(datafield => queryGenerationStrategy(datafield, userInput));
}
return Query.combineQueries(queries, BooleanOperator.OR);
}
protected standardQueryGenerationStrategy(datafield: Datafield, userInput: Array<unknown>, escapeInput = true): Query {
const valueQuery = this.selectedOperator.createQuery(this.pfqlKeywords, userInput, escapeInput);
const netQuery = this.generateNetConstraint(datafield);
const query: Query = Query.combineQueries([valueQuery, netQuery], BooleanOperator.AND);
return query.ensurePrefixAndGet(ResourceTypeQueryPrefix.CASES);
}
protected isNullOperatorQueryGenerationStrategy(datafield: Datafield): Query {
const constraint = this.generateNetConstraint(datafield);
return (this._operatorService.getOperator(IsNull) as IsNull).createQueryWithConstraint(this.pfqlKeywords, constraint)
.ensurePrefixAndGet(ResourceTypeQueryPrefix.CASES);
}
protected generateNetConstraint(datafield: Datafield): Query {
return this._isLoadedFromPfql || this._DATAFIELD_INPUT.type === SearchInputType.TEXT ? new Query(undefined, ResourceTypeQueryPrefix.CASES, true)
: this._processCategory.generatePredicate([[datafield.netIdentifier]]).query;
}
protected createDatafieldOptions(): void {
this._datafieldOptionsInitialized$ = new ReplaySubject<void>(1);
this._allowedNetsSub = this._optionalDependencies.allowedNetsService.allowedNets$.subscribe(allowedNets => {
allowedNets.forEach(petriNet => {
petriNet.immediateData
.filter(immediateData => {
return immediateData.title !== undefined
&& immediateData.title.trim().length > 0
&& !CaseDataset.DISABLED_TYPES?.includes(immediateData.type);
})
.forEach(immediateData => {
let type = immediateData.type;
// for search purposes, enumeration and multichoice maps are equivalent to their simpler counterparts
if (type === FieldTypeResource.ENUMERATION_MAP.valueOf()) {
type = 'enumeration';
} else if (type === FieldTypeResource.MULTICHOICE_MAP.valueOf()) {
type = 'multichoice';
}
this.addToDatafieldOptionsMap(DatafieldMapKey.serializedForm(immediateData.type, immediateData.title), {
netIdentifier: petriNet.identifier,
fieldId: immediateData.stringId,
fieldType: immediateData.type,
});
});
});
this._datafieldOptionsInitialized$.next();
});
}
filterOptions(userInput: Observable<string | SearchAutocompleteOption<Array<string>>>):
Observable<Array<SearchAutocompleteOption<Array<string>>>> {
if (!this.hasSelectedDatafields) {
throw new Error('The category must be fully configured before attempting to get autocomplete options!');
}
if (this.inputType !== SearchInputType.AUTOCOMPLETE) {
throw new Error('Cannot filter options of non-autocomplete operands');
}
return this._userAutocomplete.filterOptions(userInput);
}
public selectDatafields(datafieldMapKey: string, selectDefaultOperator = true): void {
if (!this._datafieldOptions.has(datafieldMapKey)) {
this._log.warn(`The provided 'datafieldMapKey' (${datafieldMapKey}) does not exist.`);
return;
}
this._DATAFIELD_INPUT.formControl.setValue(DatafieldMapKey.parse(datafieldMapKey));
if (selectDefaultOperator) {
this.selectDefaultOperator();
}
}
public selectDataFieldsByPfqlExpression(expression: DataSimpleExpression): void {
let dataFieldMapKey: string | undefined;
for (const [key, dataFields] of this._datafieldOptions.entries()) {
if (dataFields.some(dataField => dataField.fieldId === expression.dataFieldId)) {
dataFieldMapKey = key;
break;
}
}
if (dataFieldMapKey === undefined) {
this._log.warn(`No data field option found for dataFieldId '${expression.dataFieldId}'`);
return;
}
this._DATAFIELD_INPUT.formControl.setValue(DatafieldMapKey.parse(dataFieldMapKey));
}
/**
* Adds a new entry or pushes value into an existing entry.
* When a new entry is created, it is created as an Array of one element.
* @param key where in the map should the value be added
* @param value the value that should be added to the map
*/
protected addToDatafieldOptionsMap(key: string, value: Datafield): void {
if (this._datafieldOptions.has(key)) {
this._datafieldOptions.get(key).push(value);
} else {
this._datafieldOptions.set(key, [value]);
}
}
protected isOperandValueSelected(newValue: any): boolean {
if (!this.isOperatorSelected()) {
return false;
}
let inputType: SearchInputType;
try {
inputType = this.inputType;
} catch (e) {
return false;
}
switch (inputType) {
case SearchInputType.NUMBER:
return typeof newValue === 'number';
case SearchInputType.BOOLEAN:
return typeof newValue === 'boolean';
case SearchInputType.AUTOCOMPLETE:
return !(!newValue || typeof newValue === 'string');
default:
return !!newValue;
}
}
/**
* Performs a transformation of the `FormControl` value before passing it into the selected `Operator` for query generation.
* It is mostly useful only for AutocompleteCategories, where the selected value of the FormControl is an object.
*
* @param value the FormControlValue
* @returns If the selected data field has input type `AUTOCOMPLETE` then returns the {@link SearchAutocompleteOption} `value`
* attribute. Otherwise performs an identity operation.
*/
protected transformCategoryValue(value: any): any {
if (this.inputType === SearchInputType.AUTOCOMPLETE) {
return (value as SearchAutocompleteOption<Array<number>>).value;
}
return value;
}
serializeClass(): Categories | string {
return Categories.CASE_DATASET;
}
protected serializeOperandValue(valueFormControl: FormControl): unknown {
switch (this.inputType) {
case SearchInputType.AUTOCOMPLETE:
return this._userAutocomplete.serializeOperandValue(valueFormControl);
case SearchInputType.DATE:
case SearchInputType.DATE_TIME:
return (valueFormControl.value as Moment).valueOf();
default:
return super.serializeOperandValue(valueFormControl);
}
}
public override loadFromPfqlExpression(expression: SimpleExpression): Observable<void> {
if (!(expression instanceof DataSimpleExpression)) {
this._log.error("Cannot load category CaseDataSet from wrong PFQL expression")
return of(undefined);
}
if (this._DATAFIELD_INPUT.type === SearchInputType.TEXT) {
this._DATAFIELD_INPUT.formControl.setValue(expression.dataFieldId);
this._DATAFIELD_INPUT.confirmInput();
if (!this.selectOperatorFromPfqlExpression(expression)) {
return of(undefined);
}
this._operatorWasLoadedFromPfql = true;
this.setOperands([expression.operandValue]);
return of(undefined);
}
return this._datafieldOptionsInitialized$.pipe(
take(1),
map(() => {
this._isLoadedFromPfql = true;
this.selectDataFieldsByPfqlExpression(expression);
if (!this.selectOperatorFromPfqlExpression(expression)) {
return;
}
this.setOperands(Array.isArray(expression.operandValue) ? expression.operandValue : [expression.operandValue]);
})
);
}
protected deserializeOperandValue(value: unknown): Observable<any> {
switch (this.inputType) {
case SearchInputType.AUTOCOMPLETE:
return this._userAutocomplete.deserializeOperandValue(value as SearchAutocompleteOption<Array<string>>);
case SearchInputType.DATE:
case SearchInputType.DATE_TIME:
return of(moment(value as string));
default:
return super.deserializeOperandValue(value);
}
}
protected createConfigurationFilterTextSegments(): Array<FilterTextSegment> {
const configInput = this._configurationInputs$.value[0];
let dataFieldId: string = configInput.formControl.value.text;
if (!dataFieldId) {
dataFieldId = configInput.formControl.value
}
return [{segment: dataFieldId, bold: true}];
}
}