datasets.component.ts 2.99 KB
Newer Older
cazenave's avatar
cazenave committed
1
import { Component, OnInit } from '@angular/core';
Administrator's avatar
Administrator committed
2
3
4
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AppConfiguration } from '../AppConfiguration';
import { FileSaverService } from 'ngx-filesaver';
Administrator's avatar
Administrator committed
5
import { environment } from 'src/environments/environment.prod';
Administrator's avatar
Administrator committed
6

cazenave's avatar
cazenave committed
7
8
9
10
11
12

@Component({
  selector: 'app-datasets',
  templateUrl: './datasets.component.html',
  styleUrls: ['./datasets.component.scss']
})
Administrator's avatar
Administrator committed
13
14


cazenave's avatar
cazenave committed
15
export class DatasetsComponent implements OnInit {
Administrator's avatar
Administrator committed
16
  dataresult: any;
Administrator's avatar
Administrator committed
17
  itemsdatasets: any;
cazenave's avatar
cazenave committed
18

Administrator's avatar
Administrator committed
19
20
21
22
23
  constructor(
    private appConfig: AppConfiguration,
    private http: HttpClient,
    private _FileSaverService: FileSaverService,
  ) { }
cazenave's avatar
cazenave committed
24
25

  ngOnInit() {
Administrator's avatar
Administrator committed
26

cazenave's avatar
cazenave committed
27
28
  }

Administrator's avatar
Administrator committed
29
  listdatasets() {
Administrator's avatar
Administrator committed
30
31
32
    var myHeaders = new Headers();
        myHeaders.append("Content-Type", "Application/json");
    var myInit = { method: 'GET', headers: myHeaders};
Administrator's avatar
else    
Administrator committed
33
34
    
    
35
    //appeler smart havester pour récuperer l'api à lancer
Administrator's avatar
else    
Administrator committed
36

37
38
39

    // https://data.inrae.fr/api/search?q=*&per_page=10&type=dataset&start=16000&show_entity_ids=true&show_my_data=true

cazenave's avatar
cazenave committed
40
41
  //  var myRequest = new Request(this.appConfig.smartapiurl+'harvest/allurls',myInit);
      var myRequest = new Request('https://dataverse.ird.fr/api/search?q=*&per_page=100&show_facets=true&show_my_data=true',myInit);
Administrator's avatar
Administrator committed
42
43
44
45
46

          fetch(myRequest, myInit)
          .then(response => {          
              response.json()
                  .then(data => {
cazenave's avatar
cazenave committed
47
48
49
50
51
52
                    //this.dataresult =data;                    
                    this.itemsdatasets = data['data']['items'];                         
                    console.log(this.itemsdatasets);
                    for (var i = 0; i < this.itemsdatasets.length; i++) {
                      this.createdataset(data['data']['items'][i]);
                    }
Administrator's avatar
Administrator committed
53
54
                  });            
          });    
Administrator's avatar
Administrator committed
55
          return null; 
Administrator's avatar
Administrator committed
56
  }  
Administrator's avatar
Administrator committed
57
58


Administrator's avatar
Administrator committed
59
  createdataset(item: any) {
cazenave's avatar
cazenave committed
60
    console.log(item);
Administrator's avatar
Administrator committed
61
62
63
64
65
66
67
let data: string;
let description: string;
description = JSON.stringify(item['description']);
let name: string;
name = JSON.stringify(item['name']);
let url: string;
url = JSON.stringify(item['url']);
Administrator's avatar
Administrator committed
68
69
70
71
72

data ='\
@prefix dcat: <http://www.w3.org/ns/dcat#>.\n\
@prefix dct: <http://purl.org/dc/terms/>.\n\
@prefix language: <http://id.loc.gov/vocabulary/iso639-1/>.\n\
cazenave's avatar
cazenave committed
73
74
@prefix s: <'+this.appConfig.fdpurl+'/>.\n\
@prefix c: <'+this.appConfig.fdpurl+'/catalog/>.\n\
Administrator's avatar
Administrator committed
75
76
77
\n\
s:new\n\
    a dcat:Dataset, dcat:Resource;\n\
cazenave's avatar
cazenave committed
78
    dct:description '+ description+ ';\n\
Administrator's avatar
Administrator committed
79
    dct:hasVersion "1.0";\n\
cazenave's avatar
cazenave committed
80
    dct:isPartOf c:a21f9b06-b7e7-43c0-869d-d81f09053383;\n\
Administrator's avatar
Administrator committed
81
82
    dct:language language:en;\n\
    dct:license <http://rdflicense.appspot.com/rdflicense/cc-by-nc-nd3.0>;\n\
Administrator's avatar
Administrator committed
83
84
    dct:title '+ name +';\n\
    dcat:keyword '+ url +'.\n'
Administrator's avatar
Administrator committed
85
86
87
88
89
90
91
92
93
94
95
96

    const httpOptions = {
      headers: new HttpHeaders({
        'Accept':  'text/turtle',
        'Content-Type':  'text/turtle',
        'Authorization': 'Bearer '+ environment.token
      })
    };

    return this.http.post(this.appConfig.fdpurl+"/dataset", data, httpOptions ).subscribe( (r)=>{console.log('got r', r)}) ;

  }
Administrator's avatar
Administrator committed
97
98


cazenave's avatar
cazenave committed
99
}