search.component.ts 2.06 KB
Newer Older
Paulo's avatar
Paulo committed
1
2
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
3
import { ParseXmlService } from '../services/parse-xml.service';
4
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
Administrator's avatar
Administrator committed
5
6


7
8
9
10
export interface formData{
  [key: string]: string;
}

Administrator's avatar
Administrator committed
11
12
13
14
15
16
17
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class SearchComponent implements OnInit {

Administrator's avatar
Administrator committed
18

19
20
21
22
  public results: string[] = []; 
  firstTime = true;
  searchForm;
  searchedTerm;
23

Paulo's avatar
Paulo committed
24

25
26
27
28
29
  constructor(private parserService: ParseXmlService, private formBuilder: FormBuilder) {
    this.searchForm = this.formBuilder.group({
      inputSearchTerm: '',
    });
   }
Paulo's avatar
Paulo committed
30

Paulo's avatar
Paulo committed
31
32
  ngOnInit(): void {
  }
Administrator's avatar
Administrator committed
33

Paulo's avatar
Paulo committed
34
35
36
37
38
39
40
  loading = false;

  hasResults() {
    this.loading = this.results.length > 0;
    setTimeout(() => this.loading = false, 2000);
  }

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
  simpleSearch(formData){
    this.firstTime = false;
    //curl -X POST -H 'Accept: application/rdf+xml' -i 'http://10.6.10.9:8888/blazegraph/sparql' --data 'query=SELECT * where {?s a <http://www.w3.org/ns/dcat#Dataset> }'
    let term = formData["inputSearchTerm"]
    this.searchedTerm = term;
    let query='query=PREFIX dcat: <http://www.w3.org/ns/dcat#>\n\
              PREFIX dcterms: <http://purl.org/dc/terms/>\n\
              SELECT  ?title ?description ?uri \n\
              where {\n\?dataset a dcat:Dataset ;\n\
                        dcterms:title ?title ;\n\
                        dcterms:description ?description; \n\
                        dcat:keyword ?uri ; \n\
                        FILTER (contains( ?description, "' + 
                        term +'") || contains( ?title, "'+ term +'"))\n\.\n\
              }'
    
    this.parserService.getXmlResult(query).subscribe(
      data=>{
        if (data){
          this.results = [];
          data.results.bindings.forEach(element => {
            this.results.push(element);
          });
        }
      })
      this.searchForm.reset();
Paulo's avatar
Paulo committed
67
68
69
70
71
72
73
  }


  isXmlItemsEmpty(){
    return this.results.length === 0;
  } 

Administrator's avatar
Administrator committed
74
}