This commit is contained in:
Michele Artini 2023-02-14 16:55:42 +01:00
parent c84ae70a12
commit 3faf80e84d
10 changed files with 149 additions and 8 deletions

View File

@ -0,0 +1,43 @@
package eu.dnetlib.data.mapping;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.data.mapping.cleaner.CleanerFactory;
@RestController
@RequestMapping("/ajax/mapping")
public class MappingAjaxController {
@Autowired
private CleanerFactory cleanerFactory;
@PostMapping(value = "/clean", consumes = {
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE,
}, produces = MediaType.APPLICATION_XML_VALUE)
public void importResource(@RequestParam final String rule, final HttpServletRequest req, final HttpServletResponse res) throws Exception {
final String xmlIn = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8);
final String xmlOut = cleanerFactory.obtainCleaningRule(rule).transform(xmlIn);
res.setCharacterEncoding(StandardCharsets.UTF_8.name());
res.setContentType(MediaType.APPLICATION_XML_VALUE);
if (StringUtils.isNotBlank(xmlOut)) {
IOUtils.write(xmlOut, res.getOutputStream(), StandardCharsets.UTF_8.name());
} else {
res.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid record");
}
}
}

View File

@ -8,6 +8,7 @@ import { VocabulariesComponent, VocabularyEditorComponent } from './vocabularies
import { ContextViewerComponent, ContextsComponent } from './contexts/contexts.component';
import { DsmSearchComponent, DsmResultsComponent, DsmApiComponent } from './dsm/dsm.component';
import { MdstoreInspectorComponent, MdstoresComponent } from './mdstores/mdstores.component';
import { CleanerTesterComponent } from './cleaner-tester/cleaner-tester.component';
const routes: Routes = [
{ path: "", redirectTo: 'info', pathMatch: 'full' },
@ -22,7 +23,8 @@ const routes: Routes = [
{ path: "dsm/search", component: DsmSearchComponent },
{ path: "dsm/results/:page/:size", component: DsmResultsComponent },
{ path: "mdstores", component: MdstoresComponent },
{ path: "mdrecords/:versionId/:limit", component: MdstoreInspectorComponent }
{ path: "mdrecords/:versionId/:limit", component: MdstoreInspectorComponent },
{ path: "cleaner", component: CleanerTesterComponent }
];
@NgModule({

View File

@ -35,6 +35,7 @@ import { MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { SpinnerHttpInterceptor } from './common/spinner.service';
import { MdstoresComponent, MdstoreInspectorComponent, MDStoreVersionsDialog, AddMDStoreDialog } from './mdstores/mdstores.component';
import { CleanerTesterComponent } from './cleaner-tester/cleaner-tester.component';
@NgModule({
declarations: [
@ -64,7 +65,8 @@ import { MdstoresComponent, MdstoreInspectorComponent, MDStoreVersionsDialog, Ad
MdstoresComponent,
MdstoreInspectorComponent,
MDStoreVersionsDialog,
AddMDStoreDialog
AddMDStoreDialog,
CleanerTesterComponent
],
imports: [
BrowserModule,

View File

@ -0,0 +1,25 @@
<h2>Cleaner Tester</h2>
<form [formGroup]="cleanForm" (ngSubmit)="clean()">
<mat-form-field appearance="fill" floatLabel="always" style="width: 100%;">
<mat-label>Cleaning rule</mat-label>
<mat-select matInput formControlName="rule">
<mat-option *ngFor="let i of rules" [value]="i.name">{{i.name}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="fill" floatLabel="always" style="width: 50%;">
<mat-label>Input Record</mat-label>
<textarea matInput rows="10" formControlName="xmlIn"></textarea>
</mat-form-field>
<mat-form-field appearance="fill" floatLabel="always" style="width: 50%;">
<mat-label>Output Record</mat-label>
<textarea matInput rows="10" formControlName="xmlOut" readonly></textarea>
</mat-form-field>
<button mat-stroked-button color="primary" type="submit" [disabled]="!cleanForm.valid">Submit</button>
<mat-error *ngIf="cleanForm.errors?.['serverError']">
{{ cleanForm.errors?.['serverError'] }}
</mat-error>
</form>

View File

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CleanerTesterComponent } from './cleaner-tester.component';
describe('CleanerTesterComponent', () => {
let component: CleanerTesterComponent;
let fixture: ComponentFixture<CleanerTesterComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ CleanerTesterComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(CleanerTesterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,32 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute } from '@angular/router';
import { ResourceType, SimpleResource } from '../common/is.model';
import { ISService } from '../common/is.service';
@Component({
selector: 'app-cleaner-tester',
templateUrl: './cleaner-tester.component.html',
styleUrls: ['./cleaner-tester.component.css']
})
export class CleanerTesterComponent {
rules: SimpleResource[] = [];
xmlOutput: string = ''
cleanForm = new FormGroup({
xmlIn: new FormControl('', [Validators.required]),
rule: new FormControl('', [Validators.required]),
xmlOut: new FormControl('')
});
constructor(public service: ISService, public route: ActivatedRoute, public dialog: MatDialog) {
this.service.loadSimpleResources("cleaning_rule", (data: SimpleResource[]) => this.rules = data);
}
clean() {
this.service.testCleaning(this.cleanForm.get("rule")?.value!, this.cleanForm.get("xmlIn")?.value!, (data: string) => this.cleanForm.get("xmlOut")?.setValue(data), this.cleanForm);
}
}

View File

@ -319,7 +319,20 @@ export class ISService {
});
}
testCleaning(rule: string, xml: string, onSuccess: Function, relatedForm?: FormGroup): void {
var headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
this.client.post<any>('./ajax/mapping/clean?rule=' + encodeURIComponent(rule), xml, { headers, responseType: 'text' as 'json' }).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
private showError(error: any, form?: FormGroup) {
console.log(error);
const msg = this.errorMessage(error);
if (form) {
form.setErrors({ serverError: msg })

View File

@ -8,14 +8,14 @@
<mat-expansion-panel-header>
<mat-panel-title>Home</mat-panel-title>
</mat-expansion-panel-header>
<a class="menu-item" [routerLink]="['']">Home</a>
<a class="menu-item" routerLink="">Home</a>
</mat-expansion-panel>
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>Datasources</mat-panel-title>
</mat-expansion-panel-header>
<div>
<a class="menu-item" [routerLink]="['dsm/search']">Search</a>
<a class="menu-item" routerLink="dsm/search">Search</a>
</div>
</mat-expansion-panel>
@ -56,7 +56,8 @@
<mat-panel-title>Tools</mat-panel-title>
</mat-expansion-panel-header>
<div>
<a class="menu-item" [routerLink]="['mdstores']">MDStore Inspector</a>
<a class="menu-item" routerLink="mdstores">MDStore Inspector</a>
<a class="menu-item" routerLink="cleaner">Cleaner Tester</a>
</div>
</mat-expansion-panel>
@ -65,7 +66,7 @@
<mat-panel-title>Logs</mat-panel-title>
</mat-expansion-panel-header>
<div>
<a class="menu-item" [routerLink]="['wf_history']">Workflow History</a>
<a class="menu-item" routerLink="wf_history">Workflow History</a>
</div>
</mat-expansion-panel>
@ -74,7 +75,7 @@
<mat-panel-title>Info</mat-panel-title>
</mat-expansion-panel-header>
<div>
<a class="menu-item" [routerLink]="['info']">Container Info</a>
<a class="menu-item" routerLink="info">Container Info</a>
</div>
</mat-expansion-panel>
</mat-accordion>

View File

@ -39,7 +39,7 @@ public class Cleaner extends RecordTransformer<String, String> {
} catch (final Exception e) {
log.error("Error evaluating rule", e);
}
return "";
return null;
}
private void markAsInvalid(final Document doc, final List<Map<String, String>> errors) {