-
Notifications
You must be signed in to change notification settings - Fork 6
/
advanced-search.component.ts
188 lines (149 loc) · 6.13 KB
/
advanced-search.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
AfterViewChecked,
Component,
EventEmitter,
Inject,
Input,
OnDestroy,
OnInit,
Output,
ViewChild
} from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ApiResponseError, Constants, KnoraApiConnection, OntologiesMetadata, OntologyMetadata } from '@dasch-swiss/dsp-js';
import { Subscription } from 'rxjs';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { NotificationService } from 'src/app/main/services/notification.service';
import { SearchParams } from '../../results/list-view/list-view.component';
import { GravsearchGenerationService } from '../services/gravsearch-generation.service';
import { PropertyWithValue } from './resource-and-property-selection/search-select-property/specify-property-value/operator';
import { ResourceAndPropertySelectionComponent } from './resource-and-property-selection/resource-and-property-selection.component';
@Component({
selector: 'app-advanced-search',
templateUrl: './advanced-search.component.html',
styleUrls: ['./advanced-search.component.scss']
})
export class AdvancedSearchComponent implements OnInit, OnDestroy, AfterViewChecked {
// reference to the component that controls the resource class selection
@ViewChild('resAndPropSel') resourceAndPropertySelection: ResourceAndPropertySelectionComponent;
/**
* filter ontologies by specified project IRI
*
* @param limitToProject
*/
@Input() limitToProject?: string;
/**
* the data event emitter of type SearchParams
*
* @param search
*/
@Output() search = new EventEmitter<SearchParams>();
ontologiesMetadata: OntologiesMetadata;
// formGroup (used as parent for child components)
form: FormGroup;
// form validation status
formValid = false;
activeOntology: string;
formChangesSubscription: Subscription;
errorMessage: ApiResponseError;
constructor(
@Inject(FormBuilder) private _fb: FormBuilder,
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _notification: NotificationService,
private _gravsearchGenerationService: GravsearchGenerationService) {
}
ngOnInit() {
// parent form is empty, it gets passed to the child components
this.form = this._fb.group({});
// initialize ontologies to be used for the ontologies selection in the search form
this.initializeOntologies();
}
ngAfterViewChecked() {
// if form status changes, re-run validation
this.formChangesSubscription = this.form.statusChanges.subscribe((data) => {
this.formValid = this._validateForm();
});
}
ngOnDestroy() {
if (this.formChangesSubscription !== undefined) {
this.formChangesSubscription.unsubscribe();
}
}
/**
* @ignore
* Gets all available ontologies for the search form.
* @returns void
*/
initializeOntologies(): void {
if (this.limitToProject) {
this._dspApiConnection.v2.onto.getOntologiesByProjectIri(this.limitToProject).subscribe(
(response: OntologiesMetadata) => {
// filter out system ontologies
response.ontologies = response.ontologies.filter(onto => onto.attachedToProject !== Constants.SystemProjectIRI);
this.ontologiesMetadata = response;
this.preSelectFirstOntology(this.ontologiesMetadata.ontologies);
},
(error: ApiResponseError) => {
this._notification.openSnackBar(error);
this.errorMessage = error;
});
} else {
this._dspApiConnection.v2.onto.getOntologiesMetadata().subscribe(
(response: OntologiesMetadata) => {
// filter out system ontologies
response.ontologies = response.ontologies.filter(onto => onto.attachedToProject !== Constants.SystemProjectIRI);
this.ontologiesMetadata = response;
this.preSelectFirstOntology(this.ontologiesMetadata.ontologies);
},
(error: ApiResponseError) => {
this._notification.openSnackBar(error);
this.errorMessage = error;
});
}
}
setActiveOntology(ontologyIri: string) {
this.activeOntology = ontologyIri;
}
preSelectFirstOntology(ontologies: OntologyMetadata[]) {
if (ontologies.length === 1) {
this.activeOntology = ontologies[0].id;
}
}
submit() {
if (!this.formValid) {
return; // check that form is valid
}
const resClassOption = this.resourceAndPropertySelection.resourceClassComponent.selectedResourceClassIri;
let resClass;
if (resClassOption !== false) {
resClass = resClassOption;
}
const properties: PropertyWithValue[] = this.resourceAndPropertySelection.propertyComponents.map(
(propComp) => propComp.getPropertySelectedWithValue()
);
const gravsearch = this._gravsearchGenerationService.createGravsearchQuery(properties, resClass);
if (gravsearch) {
// emit query
this.search.emit({
query: gravsearch,
mode: 'gravsearch'
});
}
}
/**
* @ignore
* Validates form and returns its status (boolean).
*/
private _validateForm(): boolean {
if (this.resourceAndPropertySelection === undefined
|| this.resourceAndPropertySelection.resourceClassComponent === undefined
|| this.resourceAndPropertySelection.propertyComponents === undefined) {
return false;
}
// check that either a resource class is selected or at least one property is specified
return this.form.valid &&
(this.resourceAndPropertySelection.propertyComponents.length > 0
|| this.resourceAndPropertySelection.resourceClassComponent.selectedResourceClassIri !== false
);
}
}