Skip to content
Snippets Groups Projects
Commit 3e435876 authored by Baptiste Toulemonde's avatar Baptiste Toulemonde
Browse files

Merge branch 'feature/publish-catalog-fdp' into 'master'

Add button to publish catalog on mongoDB

See merge request !4
parents 7b9fc8a7 5bc22c5e
No related branches found
No related tags found
1 merge request!4Add button to publish catalog on mongoDB
......@@ -2,7 +2,7 @@
<form [formGroup]="Form" >
<label>Upload and fill form width repository description file: </label><br>
<input type="file" id="repofile" name="repofile " class="btn btn-primary" (change)="repositorytoyaml('fill')" ><br>
<input type="file" id="repofile" name="repofile " class="btn btn-primary" (change)="yamlToRepository()" ><br>
<label>Or describe your repository and save a new file for publishing in the FDP database:</label><br>
......@@ -38,11 +38,11 @@
<div>
<br>
<button type="submit" (click)="repositorytoyaml('submit')" class="btn btn-primary">Save repository description file</button>
<button type="submit" (click)="repositorytoyaml()" class="btn btn-primary">Save repository description file</button>
</div>
<div>
<br>
<button type="submit" (click)="repositorytoyaml('publish')" class="btn btn-success">Publish catalog description in FDP</button>
<button type="submit" (click)="publishCatalog()" class="btn btn-success">Publish catalog description in FDP</button>
</div>
</form>
......@@ -21,6 +21,7 @@ export class RepositoryComponent implements OnInit {
public fileName: string;
public importFile: File;
public resultat: any;
public persistentUrl: string;
Form = new FormGroup({
repotype: new FormControl(),
......@@ -34,19 +35,60 @@ export class RepositoryComponent implements OnInit {
});
constructor(private appConfig: AppConfiguration, private http: HttpClient, private authService:AuthService,
private _FileSaverService: FileSaverService,private publishService: PublishRepositoryService) { }
constructor(
private appConfig: AppConfiguration,
private authService: AuthService,
private _FileSaverService: FileSaverService,
private publishService: PublishRepositoryService
) { }
ngOnInit() {
this.authService.getF2DSAuthToken()
this.authService.getF2DSAuthToken();
this.publishService.getPersistentUrl().subscribe({
next: (response) => this.persistentUrl = response.persistentUrl
});
}
repositorytoyaml(buttonType) {
if (buttonType == 'submit') {
let data: string;
yamlToRepository() {
const files = (event.target as HTMLInputElement).files;
this.importFile = files[0];
let text = "";
let lines = [];
let line = "";
let map = new Map();
let fileReader = new FileReader();
data = '\
fileReader.onload = (event) => {
text = fileReader.result as string;
lines = text.split("\n");
for (let i = 0; i < lines.length; i++) {
line = lines[i].split(": ");
map.set(line[0].trim(), line[1]);
}
this.Form.setValue({
repotype: map.get('type'),
reponame: map.get('title'),
repodescription: map.get('description'),
repourl: map.get('url'),
repoversion: map.get('version'),
repolicence: map.get('licence'),
repolanguage: map.get('language'),
filetofill: ''
});
}
fileReader.readAsText(this.importFile);
}
repositorytoyaml() {
let data: string;
data = '\
repository:\n\
type: '+ this.Form.value.repotype + '\n\
title: '+ this.Form.value.reponame + '\n\
......@@ -57,85 +99,34 @@ repository:\n\
language: '+ this.Form.value.repolanguage + '\n\
'
const fileName = `repository.yaml`;
const fileType = this._FileSaverService.genType(fileName);
const txtBlob = new Blob([data], { type: fileType });
this._FileSaverService.save(txtBlob, fileName);
}
if (buttonType == 'fill') {
const files = (event.target as HTMLInputElement).files;
this.importFile = files[0];
let text = "";
let lines = [];
let line = "";
let map = new Map();
let fileReader = new FileReader();
fileReader.onload = (event) => {
text = fileReader.result as string;
lines = text.split("\n");
for (let i = 0; i < lines.length; i++) {
line = lines[i].split(": ");
map.set(line[0].trim(), line[1]);
}
this.Form.setValue({
repotype: map.get('type'),
reponame: map.get('title'),
repodescription: map.get('description'),
repourl: map.get('url'),
repoversion: map.get('version'),
repolicence: map.get('licence'),
repolanguage: map.get('language'),
filetofill: ''
});
const fileName = `repository.yaml`;
const fileType = this._FileSaverService.genType(fileName);
const txtBlob = new Blob([data], { type: fileType });
}
fileReader.readAsText(this.importFile);
}
this._FileSaverService.save(txtBlob, fileName);
}
if (buttonType == 'publish') {
let data: string;
publishCatalog() {
let data: string;
data = '\
data = '\
@prefix dcat: <http://www.w3.org/ns/dcat#>.\n\
@prefix dct: <http://purl.org/dc/terms/>.\n\
@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
@prefix lang: <http://id.loc.gov/vocabulary/iso639-1/>. \n\
@prefix fdp: <'+ this.appConfig.fdpurl + '/>.\n\nfdp:new \n\
a dcat:Catalog, dcat:Resource;\n\
dct:description "'+ this.Form.value.repodescription + '";\n\
dct:hasVersion "'+ this.Form.value.repoversion + '";\n\
dct:isPartOf <'+ this.appConfig.fdpurl + '>;\n\
dct:isPartOf <' + this.persistentUrl + '>;\n\
dcat:keyword "'+ this.Form.value.repotype + '";\n\
dct:title "'+ this.Form.value.reponame + '".\n'
dct:title "'+ this.Form.value.reponame + '";\n\
foaf:homepage <' + this.Form.value.repourl + '>;\n\
dct:licence <' + this.Form.value.repolicence + '>;\n\
dct:language lang:' + this.Form.value.repolanguage + ' .\n';
console.log(data);
return this.publishService.publishRepository(data);
/* const httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/turtle',
'Content-Type': 'text/turtle',
'Authorization': 'Bearer ' + environment.token
})
};
this.resultat = this.http
.post(this.appConfig.fdpurl + "/catalog", data, httpOptions)
.subscribe((r) => { console.log('reponse: ', r) });
console.log("resultat: " + JSON.stringify(this.resultat));
return JSON.stringify(this.resultat); */
}
}
}
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { AppConfiguration } from 'src/app/AppConfiguration';
import { AuthService } from 'src/app/authentication/services/auth.service';
import { environment } from 'src/environments/environment.prod';
interface PersistentUrlResponse {
persistentUrl: string;
}
@Injectable({
......@@ -12,65 +14,56 @@ import { environment } from 'src/environments/environment.prod';
})
export class PublishRepositoryService {
fds2Token: string
constructor(private http: HttpClient,private appConfig: AppConfiguration, private authService: AuthService) { }
constructor(
private http: HttpClient,
private appConfig: AppConfiguration,
private authService: AuthService
) { }
get smartApiUrl(): string {
return 'http://localhost:8080/harvester/api'; // TO-DO
//return this.appConfig.smartapiurl + '/harvester/api'
}
publishRepository(data: string) {
publishRepository(data:string){
this.authService.getF2DSAuthToken().subscribe(tokenResponse => {
const fds2Token = tokenResponse.token;
if (fds2Token) {
this.authService.getF2DSAuthToken().subscribe(data=>{
this.fds2Token = data.token
})
if (this.fds2Token) {
const myHeaders = new Headers();
myHeaders.append('Accept', 'text/turtle');
myHeaders.append('Content-Type', 'text/turtle');
myHeaders.append('Authorization', 'Bearer ' + fds2Token);
const myInit = { method: 'POST', body: data, headers: myHeaders };
var myHeaders = new Headers();
myHeaders.append( 'Accept', 'text/turtle');
myHeaders.append('Content-Type', 'text/turtle');
myHeaders.append('Authorization', 'Bearer '+ this.fds2Token );
var myInit = { method: 'POST', body:data, headers: myHeaders };
const myRequest = new Request(this.appConfig.fdpurl + "/catalog", myInit);
var myRequest = new Request(this.appConfig.fdpurl+"/catalog", myInit);
fetch(myRequest, myInit)
.then(response => {
response.text()
.then(data => {
console.log('catalog creaetd with fdp / id = ' + data.substring(38,74));
/* update user (mongo ) with ( this.authService.user('data.substring(38,74)') -> smartharvester api userRpositories create/update)
catalog: {
'cat_id':'qsdqs-qsd-qs-dqs-d-qsd-q',
'cat_id': 'data.substring(38,74)'
} */
});
});
/*
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/turtle',
'Content-Type': 'text/turtle',
'Authorization': 'Bearer '+ this.fds2Token,
observe: 'response',
responseType: 'text'
})
};
fetch(myRequest, myInit)
.then(response => {
response.text()
.then(data => {
const catId = data.substring(38, 74);
console.log('catalog creaetd with fdp / id = ' + catId);
this.addUserCatalog(catId).then((response) => console.log(response));
});
});
let resultat = this.http.post(this.appConfig.fdpurl+"/catalog", data, httpOptions ).subscribe(
(r:HttpResponse<any>)=>{
console.log(r.headers.get('location'))
},
(error: any) => {
console.log(error)
});
if (resultat){
console.log("resultat: " + resultat);
return resultat;
return "The repository has not been published"
}
});
}
getPersistentUrl(): Observable<PersistentUrlResponse> {
return this.http.get<PersistentUrlResponse>(/* // TO-DO : this.appConfig.fdpurl*/ 'http://localhost:8084' + '/configs/bootstrap');
}
*/
async addUserCatalog(catId: string): Promise<any> {
const tokenResponse = await this.authService.getF2DSAuthToken().toPromise();
return "The repository has not been published"
}
return this.http.post(this.smartApiUrl + '/user/' + this.appConfig.fdpemail + '/catalogs', catId,
{ headers: { Authorization: 'Bearer ' + tokenResponse.token } })
.toPromise();
}
}
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from 'src/environments/environment.prod';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment