Skip to content

Commit

Permalink
Simple search page implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
EricSommerhalder committed Aug 27, 2020
1 parent 3970e7b commit 4629e03
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/app/components/search-page/search-page.component.ts
Expand Up @@ -421,7 +421,6 @@ export class SearchPageComponent implements OnInit {
fire(querystring) {
this.knoraService.gravsearchQueryByStringCount(querystring).subscribe(
(no: number) => {
console.log('Found COUNT: ', no);
this.countRes = no;
}
);
Expand Down
40 changes: 30 additions & 10 deletions src/app/components/simple-search/simple-search.component.html
Expand Up @@ -2,6 +2,10 @@
<textarea #nameField matInput [(ngModel)] = "nameInput" placeholder="Name"></textarea>
</mat-form-field>
<br>
<mat-form-field>
<textarea #lastNameField matInput [(ngModel)] = "lastNameInput" placeholder="Last Name"></textarea>
</mat-form-field>
<br>
<mat-form-field>
<textarea #fathersField matInput [(ngModel)] = "fathersNameInput" placeholder="Father's Name"></textarea>
</mat-form-field>
Expand All @@ -24,22 +28,38 @@
</mat-select>
</mat-form-field>
<br>
<mat-form-field>
<textarea #spouseField matInput [(ngModel)] = "spouseInput" placeholder="Spouse name"></textarea>
</mat-form-field>
<div *ngFor ="let child of childrenInput; let i = index; trackBy: trackByFn">
<ng-container *ngTemplateOutlet="childName; context: {id: i}">
<div *ngFor ="let relative of relativeInput; let i = index; trackBy: trackByFn">
<ng-container *ngTemplateOutlet="relativeName; context: {id: i}">

</ng-container>

</div>
<a mat-button (click)="addChild()">Add Child</a>
<a mat-button (click)="addRelative()">Add Relative</a>
<br>
<a mat-button (click)="fireSearch()">Search</a>

<ng-template #childName let-id="id">
<br>
<div *ngIf = "this.countRes >= 0">
<p>Count found: {{this.countRes}}</p>
</div>
<table>
<tr *ngIf ="results.length > 0">
<td>Name</td>
<td>Last Name</td>
<td>Town</td>
<td>Father's Name</td>
</tr>
<tr *ngFor="let result of results" (click)="resultClicked(result.iri)">
<td>{{getString(result.names)}}</td>
<td>{{getString(result.lastNames)}}</td>
<td>{{getString(result.towns)}}</td>
<td>{{getString(result.fathersNames)}}</td>
</tr>
</table>
<a mat-button (click) = "changePage(-1)">Previous</a>
<a mat-button (click) = "changePage(1)">Next</a>
<ng-template #relativeName let-id="id">
<mat-form-field>
<textarea matInput [(ngModel)] = "childrenInput[id]" placeholder="Child name"></textarea>
<textarea matInput [(ngModel)] = "relativeInput[id]" placeholder="Relative name"></textarea>
</mat-form-field>
<a mat-button #removeChildButton (click) = "removeChild(id)">Remove child</a>
<a mat-button #removeRelativeButton (click) = "removeRelative(id)">Remove Relative</a>
</ng-template>
249 changes: 230 additions & 19 deletions src/app/components/simple-search/simple-search.component.ts
@@ -1,19 +1,46 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {StorageService} from '../../services/storage.service';
import {ReadResource} from '@knora/api';
import {KnoraService} from '../../services/knora.service';
import {last} from 'rxjs/operators';
import {Helpers} from '../../classes/helpers';
import {Router} from '@angular/router';

class Result {
iri: string;
names: string[];
lastNames: string[];
towns: string[];
fathersNames: string[];
constructor(iri: string, names: string[], lastNames: string[], towns: string[], fathersNames: string[]) {
this.iri = iri;
this.names = names;
this.lastNames = lastNames;
this.towns = towns;
this.fathersNames = fathersNames;
}
}


@Component({
selector: 'app-simple-search',
templateUrl: './simple-search.component.html',
styleUrls: ['./simple-search.component.scss']
})

export class SimpleSearchComponent implements OnInit, OnDestroy {
results: Result[] = [];
nameInput: string;
lastNameInput: string;
fathersNameInput: string;
townInput: string;
arrivalInput: number;
precisionInput: string;
spouseInput: string;
childrenInput: Array<string>;
countRes = -1 ;
resultPage;
lastQuery = '';
// spouseInput: string;
relativeInput: Array<string>;
minDate = new Date(1850, 0, 1);
maxDate = new Date(1907, 11, 31);
townList = ['Adana',
Expand Down Expand Up @@ -45,12 +72,12 @@ export class SimpleSearchComponent implements OnInit, OnDestroy {
'Yozgad',
'Çorum',
'İzmiR'];
constructor(public storage: StorageService) { }
addChild() {
this.childrenInput.push('');
constructor(public storage: StorageService, public knoraService: KnoraService, public helpers: Helpers, public router: Router) { }
addRelative() {
this.relativeInput.push('');
}
removeChild(id: number) {
this.childrenInput.splice(id, 1);
removeRelative(id: number) {
this.relativeInput.splice(id, 1);
}
trackByFn(index: any, item: any) {
return index;
Expand All @@ -75,34 +102,218 @@ export class SimpleSearchComponent implements OnInit, OnDestroy {
if (townStrings[townFilter]) {
townFilter = townStrings[townFilter];
}
console.log('NAME: ', this.nameInput);
console.log('FATHERS NAME', this.fathersNameInput);
console.log('TOWN: ', townFilter);
console.log('ARRIVAL: ', this.arrivalInput);
console.log('PRECISION: ', this.precisionInput);
console.log('SPOUSE: ', this.spouseInput);
console.log('CHILDREN: ', this.childrenInput);
let query = 'PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>\n' +
'PREFIX pou: <http://api.pou.test.dasch.swiss/ontology/0827/pou/v2#>\n' +
'PREFIX knora-api-simple: <http://api.knora.org/ontology/knora-api/simple/v2#>\n' +
'CONSTRUCT {\n' +
'?person knora-api:isMainResource true .\n' +
'?person pou:nameOfPerson ?nameObj .\n' +
'?nameObj pou:text ?name .\n' +
'?person pou:lastName ?lastName .\n' +
'?person pou:turkishName ?turkishName .\n' +
'?person pou:originTown ?originTown .\n' +
'?person pou:fathersName ?fathersName .\n' +
'} WHERE {\n' +
'?person a knora-api:Resource .\n' +
'?person a pou:Person .\n';
if (this.nameInput === '') {
query += 'OPTIONAL {\n' +
'?person pou:nameOfPerson ?nameObj .\n' +
'?nameObj pou:text ?name .\n' +
'}\n';
} else {
query += '?person pou:nameOfPerson ?nameObj .\n' +
'?nameObj pou:text ?name .\n' +
'?name knora-api:valueAsString ?nameStr .\n' +
'FILTER(regex(?nameStr, "' + this.nameInput + '", "i")).\n';
}
if (this.lastNameInput === '') {
query += 'OPTIONAL {?person pou:lastName ?lastName .}\n' +
'OPTIONAL {?person pou:turkishName ?turkishName .}\n';
} else {
query += '{\n' +
'?person pou:lastName ?lastName .\n' +
'?lastName knora-api:valueAsString ?lastNameStr .\n' +
'FILTER(regex(?lastNameStr, "' + this.lastNameInput + '", "i")) .\n' +
'}\n' +
'UNION {\n' +
'?person pou:turkishName ?turkishName .\n' +
'?turkishName knora-api:valueAsString ?turkishNameStr .\n' +
'FILTER(regex(?turkishNameStr, "' + this.lastNameInput + '", "i")) .\n' +
'}\n';
}
if (this.fathersNameInput === '') {
// TODO: Expand with Filters via relationships
query += 'OPTIONAL {?person pou:fathersName ?fathersName .}\n';
} else {
query += '?person pou:fathersName ?fathersName .\n' +
'?fathersName knora-api:valueAsString ?fathersNameStr .\n' +
'FILTER(regex(?fathersNameStr, "' + this.fathersNameInput + '", "i")) .\n';
}
if (townFilter === '') {
query += 'OPTIONAL {?person pou:originTown ?originTown .}\n';
} else {
query += '?person pou:originTown ?originTown .\n' +
'?originTown knora-api:valueAsString ?originTownStr .\n' +
'FILTER(regex(?originTownStr, "' + this.townInput + '", "i")) .\n';
}
if (this.arrivalInput !== -1) {
const filterString = 'FILTER (knora-api:toSimpleDate(PROP) <= "GREGORIAN:' + (this.arrivalInput + Number(this.precisionInput)).toString() + '-12-31"^^knora-api-simple:Date && knora-api:toSimpleDate(PROP) > "GREGORIAN:' + (this.arrivalInput - Number(this.precisionInput) - 1).toString() + '-12-31"^^knora-api-simple:Date) .';
const datePropsPerson = ['dateHRFile'];
for (const prop of datePropsPerson) {
query += '{\n' +
'?person pou:' + prop + ' ?' + prop + ' .\n' +
filterString.replace(/PROP/g, '?' + prop) +
'\n}\n' +
'UNION '; // no \n as it is continued by the next loop.
}
const sourcedDateProps = ['dateOfAdmission', 'dateOfArrival'];
for (const prop of sourcedDateProps) {
query += '{\n' +
'?person pou:' + prop + ' ?' + prop + ' .\n' +
'?' + prop + ' pou:dateValue ?' + prop + 'Val .\n' +
filterString.replace(/PROP/g, '?' + prop + 'Val') + '\n}\n';
query += 'UNION \n';
}
const datePropsPhysCop = ['dateOfDocument', 'dateOfPhotograph', 'dateOnPhotograph'];
for (const prop of datePropsPhysCop) {
query += '{\n' +
'?photo pou:peopleOnPic ?person .\n' +
'?photo pou:physicalCopy ?physicalCopy .' +
'?physicalCopy pou:' + prop + ' ?' + prop + ' .\n' +
filterString.replace(/PROP/g, '?' + prop) +
'\n}\n' +
'UNION '; // no \n as it is continued by the next loop.
}
query += '{\n' +
'?photo pou:peopleOnPic ?person .\n' +
'?photo pou:dateOfPassport ?dateOfPassport .\n' +
filterString.replace(/PROP/g, '?dateOfPassport') + '\n}\n';
query += 'UNION {\n' +
'?photo pou:peopleOnPic ?person .\n' +
'?coverLetter pou:photograph ?photo .\n' +
'?coverLetter pou:dateOfCoverLetter ?dateOfCoverLetter .\n' +
filterString.replace(/PROP/g, '?dateOfCoverLetter') + '\n}\n';
}
/* if (this.spouseInput !== '') {
const spouseRelationships = [
['Self', 'Intended'],
['Self, Husband'],
['Self, Wife'],
['Self', 'Spouse'],
['Self', 'Fiancee'],
['Grandmother', 'Grandfather'],
['Father', 'Stepmother'],
['Motherinlaw', 'Fatherinlaw'],
['Father', 'Mother'],
['Brotherswife', 'Brother']
];
}*/
if (this.relativeInput.length > 1 || this.relativeInput[0] !== '') {
for (let i = 0; i < this.relativeInput.length ; i++) {
const name = this.relativeInput[i];
if (name !== '') {
query += '?photo pou:peopleOnPic ?person .\n' +
'?photo pou:peopleOnPic ?relative' + i.toString() + ' .\n' +
'?relative' + i.toString() + ' pou:nameOfPerson ?relNameObj' + i.toString() + ' .\n' +
'?relNameObj' + i.toString() + ' pou:text ?relName' + i.toString() + ' .\n' +
'?relName' + i.toString() + ' knora-api:valueAsString ?relNameStr' + i.toString() + ' .\n' +
'FILTER( regex(?relNameStr' + i.toString() + ', "' + name + '", "i")) .\n';
}
}
}
query += '}';
this.fireQuery(query);
}
fireQuery(query: string) {
this.results = [];
this.lastQuery = query;
this.knoraService.gravsearchQueryByStringCount(query).subscribe(
(no: number) => {
this.countRes = no;
}
);
query += '\nOFFSET ' + this.resultPage.toString();
this.knoraService.gravsearchQueryByString(query).subscribe(
(readResources: ReadResource[]) => {
const turkishNameProp = this.knoraService.pouOntology + 'turkishName';
const lastNameProp = this.knoraService.pouOntology + 'lastName';
const fathersNameProp = this.knoraService.pouOntology + 'fathersName';
const originTownProp = this.knoraService.pouOntology + 'originTown';
const nameProp = this.knoraService.pouOntology + 'nameOfPersonValue';
const textProp = this.knoraService.pouOntology + 'text';
for (const readResource of readResources) {
const iri = readResource.id;
console.log(iri);
let names = [];
let lastNames = [];
let fathersNames = [];
let towns = [];
const nameObjects = this.helpers.getLinkedReadResources(readResource, nameProp);
for (const name of nameObjects) {
names = names.concat(name.getValuesAsStringArray(textProp));
}
lastNames = lastNames.concat(readResource.getValuesAsStringArray(lastNameProp))
.concat(readResource.getValuesAsStringArray(turkishNameProp));
fathersNames = fathersNames.concat(readResource.getValuesAsStringArray(fathersNameProp));
towns = towns.concat(readResource.getValuesAsStringArray(originTownProp));
this.results.push(new Result(iri, names, lastNames, towns, fathersNames));
}
console.log(this.results);
}
);
}

getString(arr: string[]) {
let str = '';
for (const s of arr) {
str += s + ', ';
}
str = str.slice(0, -2);
return str;
}
changePage(increment: number) {
if (this.resultPage + increment >= 0 && this.resultPage + increment < this.countRes / 25) {
this.resultPage += increment;
this.fireQuery(this.lastQuery);
}

}
resultClicked(targetIri: string): void {
const url: string = 'details/' + encodeURIComponent(targetIri);
this.router.navigateByUrl(url).then(e => {
if (e) {
console.log("Navigation is successful!");
} else {
console.log("Navigation has failed!");
}
});
}

ngOnInit() {
this.nameInput = this.storage.simpleNameInput;
this.lastNameInput = this.storage.simpleLastNameInput;
this.fathersNameInput = this.storage.simpleFathersNameInput;
this.townInput = this.storage.simpleTownInput;
this.arrivalInput = this.storage.simpleArrivalInput;
this.precisionInput = this.storage.simplePrecisionInput;
this.spouseInput = this.storage.simpleSpouseInput;
this.childrenInput = this.storage.simpleChildrenInput;
if (this.nameInput !== '' || this.fathersNameInput !== '' || this.townInput !== '' || this.arrivalInput !== -1 || this.precisionInput !== '' || this.spouseInput !== '' || this.childrenInput.length !== 1 || this.childrenInput[0] !== '') {
// this.spouseInput = this.storage.simpleSpouseInput;
this.relativeInput = this.storage.simpleRelativeInput;
this.resultPage = this.storage.simplePage;
if (this.nameInput !== '' || this.lastNameInput !== '' || this.fathersNameInput !== '' || this.townInput !== '' || this.arrivalInput !== -1 || this.precisionInput !== '0' || this.relativeInput.length > 1 || this.relativeInput[0] !== '') {
this.fireSearch();
}
}
ngOnDestroy() {
this.storage.simpleNameInput = this.nameInput;
this.storage.simpleLastNameInput = this.lastNameInput;
this.storage.simpleFathersNameInput = this.fathersNameInput;
this.storage.simpleTownInput = this.townInput;
this.storage.simpleArrivalInput = this.arrivalInput;
this.storage.simplePrecisionInput = this.precisionInput;
this.storage.simpleSpouseInput = this.spouseInput;
this.storage.simpleChildrenInput = this.childrenInput;
// this.storage.simpleSpouseInput = this.spouseInput;
this.storage.simpleRelativeInput = this.relativeInput;
this.storage.simplePage = this.resultPage;
}

}
8 changes: 5 additions & 3 deletions src/app/services/storage.service.ts
Expand Up @@ -14,11 +14,13 @@ export class StorageService {
searchPageOnlyCount = false;
searchPageGravField = '';
simpleNameInput = '';
simpleLastNameInput = '';
simpleFathersNameInput = '';
simpleTownInput = '';
simpleArrivalInput = -1;
simplePrecisionInput = '';
simpleSpouseInput = '';
simpleChildrenInput = [''];
simplePrecisionInput = '0';
// simpleSpouseInput = '';
simpleRelativeInput = [''];
simplePage = 0;
constructor() { }
}

0 comments on commit 4629e03

Please sign in to comment.