src/lib/service/stored-queries/stored-queries.service.ts
Service managing stored queries.
To use it just call getStoredQueries$
which will return the current
state of the list of stored queries. This one is a long term observable, so bare in mind
to unsubscribe from it when you don't need it any more (or your component gets destroyed).
Every change to the list of stored queries is done using this service. So don't change the retrieved list locally inside of your component. Otherwise other components that subscribe to the stored query list will not be notified.
Properties |
|
Methods |
clear |
clear()
|
Clear list of stored queries.
Returns :
void
|
fetchStoredQueries |
fetchStoredQueries()
|
Fetches the stored queries from the backend and propagates a new value to the main subject.
Returns :
Observable<StoredQuery[]>
|
getFulltextFormElement | ||||||||||||
getFulltextFormElement(label: string, value: string)
|
||||||||||||
Creates a pseudo form element for a stored queries fulltext input field to be used inside of a stored query form.
Parameters :
Returns :
{ name: string; qname: string; label: string; value: string; type: string; }
|
getStoredQueries$ |
getStoredQueries$()
|
Provides you with a
Returns :
Observable<StoredQueriesSubscription>
|
getStoredQuery | ||||||||
getStoredQuery(id: string)
|
||||||||
Get StoredQuery with the provided Id
Parameters :
Returns :
Observable<StoredQuery>
|
refreshStoredQueries |
refreshStoredQueries()
|
Will re-fetch the data from the backend. This will propagate a new value to your subscription.
Returns :
void
|
removeStoredQuery | ||||||||
removeStoredQuery(id: string)
|
||||||||
Remove Stored Query be provided ID
Parameters :
Returns :
Observable<any>
|
saveStoredQuery | ||||||||
saveStoredQuery(storedQuery: StoredQuery)
|
||||||||
Save a stored query. Depending on whether or not the provided query is a new one or not this will either update or create a new query.
Parameters :
Returns :
Observable<StoredQuery>
|
Static FULLTEXT |
FULLTEXT:
|
Type : string
|
Default value : 'fulltext'
|
qname of fulltext params |
Static ID_NEW |
ID_NEW:
|
Default value : StoredQuery.ID_NEW
|
Temporary ID for new stored queries |
Public storedQueries$ |
storedQueries$:
|
Type : Observable<StoredQueriesSubscription>
|
Default value : this.storedQueriesSource.asObservable()
|
Stream of stored queries |
import {tap, map} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Observable, BehaviorSubject} from 'rxjs';
import {BackendService} from '../backend/backend.service';
import {SystemService} from '../system/system.service';
import {StoredQueriesSubscription} from './stored-queries-subscription.interface';
import {StoredQuery} from './stored-query.model';
import {SearchService} from '../search/search.service';
import {EventService} from '../events/event.service';
import {EnaioEvent} from '../events/events';
/**
* Service managing stored queries.
* To use it just call `getStoredQueries$` which will return the current
* state of the list of stored queries. This one is a long term observable, so bare in mind
* to unsubscribe from it when you don't need it any more (or your component gets destroyed).
*
* Every change to the list of stored queries is done using this service. So don't change the
* retrieved list locally inside of your component. Otherwise other components that subscribe
* to the stored query list will not be notified.
*/
@Injectable({
providedIn: 'root'
})
export class StoredQueriesService {
/**
* Temporary ID for new stored queries
*/
static ID_NEW = StoredQuery.ID_NEW;
/**
* qname of fulltext params
*/
static FULLTEXT = 'fulltext';
private storedQueries: StoredQueriesSubscription = {
queries: []
};
private storedQueriesSource = new BehaviorSubject<StoredQueriesSubscription>(this.storedQueries);
/**
* Stream of stored queries
*/
public storedQueries$: Observable<StoredQueriesSubscription> = this.storedQueriesSource.asObservable();
/**
* @ignore
*/
constructor(private backend: BackendService,
private system: SystemService,
private searchService: SearchService,
private eventService: EventService) {
eventService
.on(EnaioEvent.LOGOUT)
.subscribe(() => this.clear());
}
/**
* Clear list of stored queries.
*/
clear() {
this.storedQueries = {queries: []};
this.storedQueriesSource.next(this.storedQueries);
}
/**
* Provides you with a `long term observable` holding the stored queries.
*/
getStoredQueries$(): Observable<StoredQueriesSubscription> {
if (!this.storedQueries.queries.length) {
this.refreshStoredQueries();
}
return this.storedQueries$;
}
/**
* Will re-fetch the data from the backend. This will propagate a
* new value to your subscription.
*/
refreshStoredQueries(): void {
this.backend
.getJson('/storedqueries?size=9999', this.backend.getSearchBase())
.pipe(
map(sQueries => {
return (sQueries || [])
.map(s => {
let sq = new StoredQuery(s);
/**
* set up search query properties for the stored query
*/
sq.setQuery(this.searchService.buildQuery(s.query));
return sq;
});
})
)
.subscribe(res => {
this.storedQueries = {queries: res};
this.storedQueriesSource.next(this.storedQueries);
});
}
/**
* Fetches the stored queries from the backend and propagates a new
* value to the main subject.
*/
fetchStoredQueries(): Observable<StoredQuery[]> {
return this.backend
.getJson('/storedqueries?size=9999', this.backend.getSearchBase())
.pipe(
map(sQueries => {
return (sQueries || [])
.map(s => {
let sq = new StoredQuery(s);
/**
* set up search query properties for the stored query
*/
sq.setQuery(this.searchService.buildQuery(s.query));
return sq;
});
}),
tap(res => {
this.storedQueries = {queries: res};
this.storedQueriesSource.next(this.storedQueries);
})
);
}
/**
* Save new Stored Query
*
* @param StoredQuery storedQuery
* @return Observable<StoredQuery>
*/
private saveNewStoredQuery(storedQuery: StoredQuery): Observable<StoredQuery> {
return this.backend
.post('/storedqueries', storedQuery.toPersistableStoredQuery(), this.backend.getSearchBase())
.pipe(
map(res => {
let updatedQuery = new StoredQuery(res);
updatedQuery.setQuery(this.searchService.buildQuery(res.query));
this.storedQueries.select = updatedQuery.id;
this.storedQueries.queries.push(updatedQuery);
this.storedQueriesSource.next(this.storedQueries);
return updatedQuery;
}));
}
/**
* Update Stored Query
*
* @param StoredQuery storedQuery
* @return Observable<StoredQuery>
*/
private updateStoredQuery(storedQuery: StoredQuery): Observable<StoredQuery> {
return this.backend
.put('/storedqueries', storedQuery.toPersistableStoredQuery(), this.backend.getSearchBase())
.pipe(
map(res => {
const updatedQuery = new StoredQuery(res);
updatedQuery.setQuery(this.searchService.buildQuery(res.query));
this.storedQueries.select = updatedQuery.id;
this.storedQueries.queries = this.storedQueries.queries.filter(q => q.id !== updatedQuery.id);
this.storedQueries.queries.push(updatedQuery);
this.storedQueriesSource.next(this.storedQueries);
return updatedQuery;
}));
}
/**
* Save a stored query. Depending on whether or not the provided query is a new one or not
* this will either update or create a new query.
*
* @param storedQuery Stored query to be saved
*/
saveStoredQuery(storedQuery: StoredQuery): Observable<StoredQuery> {
if (storedQuery.id === StoredQueriesService.ID_NEW) {
return this.saveNewStoredQuery(storedQuery);
} else {
return this.updateStoredQuery(storedQuery);
}
}
/**
* Remove Stored Query be provided ID
* @param id ID of the stored query to be removed
*/
removeStoredQuery(id: string): Observable<any> {
return this.backend
.del(`/storedqueries/${id}`, this.backend.getSearchBase())
.pipe(
tap(() => {
this.storedQueries.select = null;
this.storedQueries.queries = this.storedQueries.queries.filter(q => q.id !== id);
this.storedQueriesSource.next(this.storedQueries);
})
);
}
/**
* Get StoredQuery with the provided Id
* @param id ID of the stored query to be fetched
*/
getStoredQuery(id: string): Observable<StoredQuery> {
return this.backend
.get(`/storedqueries/${id}`, this.backend.getSearchBase())
.pipe(
map(s => {
let sq = new StoredQuery(s);
/**
* set up search query properties for the stored query
*/
sq.setQuery(this.searchService.buildQuery(s.query));
return sq;
})
);
}
/**
* Creates a pseudo form element for a stored queries fulltext input field
* to be used inside of a stored query form.
*
* @param label The label to be set to the element
* @param value A value to be set to the element
*/
getFulltextFormElement(label: string, value: string) {
return {
name: StoredQueriesService.FULLTEXT,
qname: StoredQueriesService.FULLTEXT,
label: label,
value: value,
type: 'STRING'
};
}
}