From 969df0357ade0d7b533ef4a82d73875bd0e7511a Mon Sep 17 00:00:00 2001 From: Aldo Mihasi Date: Mon, 27 Mar 2023 14:58:45 +0300 Subject: [PATCH] migrate rda properties in dataset profiles definitions in db to schematics --- .../controllers/ManagementController.java | 43 ++++++++++++++++ .../logic/managers/DatasetProfileManager.java | 50 ++++++++++++++++++ dmp-frontend/src/app/app-routing.module.ts | 7 +++ .../maintenance-tasks.service.ts | 20 ++++++++ .../maintenance-tasks.component.html | 8 +++ .../maintenance-tasks.component.scss | 7 +++ .../maintenance-tasks.component.ts | 51 +++++++++++++++++++ .../maintenance-tasks.module.ts | 23 +++++++++ .../maintenance-tasks.routing.ts | 15 ++++++ 9 files changed, 224 insertions(+) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/controllers/ManagementController.java create mode 100644 dmp-frontend/src/app/core/services/maintenance-tasks/maintenance-tasks.service.ts create mode 100644 dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.html create mode 100644 dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.scss create mode 100644 dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.ts create mode 100644 dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.module.ts create mode 100644 dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.routing.ts diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/ManagementController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/ManagementController.java new file mode 100644 index 000000000..45e810b23 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/ManagementController.java @@ -0,0 +1,43 @@ +package eu.eudat.controllers; + +import eu.eudat.exceptions.datasetprofile.DatasetProfileNewVersionException; +import eu.eudat.logic.managers.DatasetProfileManager; +import eu.eudat.logic.security.claims.ClaimedAuthorities; +import eu.eudat.logic.services.ApiContext; +import eu.eudat.models.data.admin.composite.DatasetProfile; +import eu.eudat.models.data.helpers.responses.ResponseItem; +import eu.eudat.models.data.security.Principal; +import eu.eudat.types.ApiMessageCode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.transaction.Transactional; + +import static eu.eudat.types.Authorities.ADMIN; + +@RestController +@CrossOrigin +@RequestMapping(value = {"/api/management/"}) +public class ManagementController extends BaseController { + + private DatasetProfileManager datasetProfileManager; + + @Autowired + public ManagementController(ApiContext apiContext, DatasetProfileManager datasetProfileManager){ + super(apiContext); + this.datasetProfileManager = datasetProfileManager; + } + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = {"/addSchematics"}) + public ResponseEntity addSchematicsInDatasetProfiles(@ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception { + try { + this.datasetProfileManager.addSchematicsInDatasetProfiles(); + return ResponseEntity.status(HttpStatus.OK).body(null); + } catch (Exception exception) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message(exception.getMessage())); + } + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetProfileManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetProfileManager.java index 12fd713ed..2f9f87d23 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetProfileManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetProfileManager.java @@ -45,9 +45,17 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import javax.activation.MimetypesFileTypeMap; import javax.transaction.Transactional; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.*; import java.io.*; import java.nio.file.Files; @@ -376,4 +384,46 @@ public class DatasetProfileManager { } return filteredSchematics; } + + public void addSchematicsInDatasetProfiles() throws XPathExpressionException { + List datasetProfiles = this.databaseRepository.getDatasetProfileDao().getAll().toList(); + for(DatasetProfile datasetProfile: datasetProfiles){ + Document document = XmlBuilder.fromXml(datasetProfile.getDefinition()); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + XPathExpression expr = xpath.compile("//rdaCommonStandard"); + NodeList rdaProperties = (NodeList) expr.evaluate(document, XPathConstants.NODESET); + for(int i = 0; i < rdaProperties.getLength(); i++){ + Node rdaPropertyNode = rdaProperties.item(i); + String rdaProperty = rdaPropertyNode.getTextContent(); + Element schematics = document.createElement("schematics"); + Node fieldParent = rdaPropertyNode.getParentNode(); + if(rdaProperty != null && !rdaProperty.isEmpty()){ + Element schematic = document.createElement("schematic"); + schematic.setTextContent(rdaProperty); + schematics.appendChild(schematic); + } + fieldParent.insertBefore(schematics, rdaPropertyNode); + fieldParent.removeChild(rdaPropertyNode); + } + try { + DOMSource domSource = new DOMSource(document); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.transform(domSource, result); + String newDefinition = writer.toString(); + if(newDefinition != null){ + + } + datasetProfile.setDefinition(newDefinition); + this.databaseRepository.getDatasetProfileDao().createOrUpdate(datasetProfile); + } + catch(TransformerException ex) { + logger.error(ex.getMessage(), ex); + } + } + } } diff --git a/dmp-frontend/src/app/app-routing.module.ts b/dmp-frontend/src/app/app-routing.module.ts index 120a88ec1..3cba707ac 100644 --- a/dmp-frontend/src/app/app-routing.module.ts +++ b/dmp-frontend/src/app/app-routing.module.ts @@ -224,6 +224,13 @@ const appRoutes: Routes = [ title: 'GENERAL.TITLES.INDEX-MANAGMENT' }, }, + { + path: 'maintenance-tasks', + loadChildren: () => import('./ui/admin/maintenance-tasks/maintenance-tasks.module').then(m => m.MaintenanceTasksModule), + data: { + breadcrumb: true + }, + }, { path: 'login/admin', loadChildren: () => import('./ui/auth/admin-login/admin-login.module').then(m => m.AdminLoginModule), diff --git a/dmp-frontend/src/app/core/services/maintenance-tasks/maintenance-tasks.service.ts b/dmp-frontend/src/app/core/services/maintenance-tasks/maintenance-tasks.service.ts new file mode 100644 index 000000000..babdd7137 --- /dev/null +++ b/dmp-frontend/src/app/core/services/maintenance-tasks/maintenance-tasks.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from "@angular/core"; +import { BaseService } from "@common/base/base.service"; +import { Observable } from "rxjs"; +import { ConfigurationService } from "../configuration/configuration.service"; +import { BaseHttpService } from "../http/base-http.service"; + +@Injectable() +export class MaintenanceTasksService extends BaseService { + + private actionUrl: string; + + constructor(private http: BaseHttpService, configurationService: ConfigurationService) { + super(); + this.actionUrl = configurationService.server + 'management/'; + } + + migrateSchematics(): Observable { + return this.http.post(this.actionUrl + 'addSchematics/', null); + } +} \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.html b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.html new file mode 100644 index 000000000..f3193d329 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.html @@ -0,0 +1,8 @@ +
+ +
+
Warning: Danger zone. Irreversible action!
+ +
+
+
diff --git a/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.scss b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.scss new file mode 100644 index 000000000..0faaf9e53 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.scss @@ -0,0 +1,7 @@ +.root { + padding-bottom: 2em; + + .button { + margin: 5px; + } +} \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.ts b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.ts new file mode 100644 index 000000000..54c811e25 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.component.ts @@ -0,0 +1,51 @@ +import { Component, OnInit } from '@angular/core'; +import { BaseComponent } from '@common/base/base.component'; +import { takeUntil } from 'rxjs/operators'; +import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/services/notification/ui-notification-service'; +import { TranslateService } from '@ngx-translate/core'; +import { Router } from '@angular/router'; +import { MaintenanceTasksService } from '@app/core/services/maintenance-tasks/maintenance-tasks.service'; + +@Component({ + selector: 'app-maintenance-tasks', + templateUrl: './maintenance-tasks.component.html', + styleUrls: ['./maintenance-tasks.component.scss'] +}) +export class MaintenanceTasksComponent extends BaseComponent implements OnInit { + + constructor( + private maintenanceTasksService: MaintenanceTasksService, + private uiNotificationService: UiNotificationService, + private translate: TranslateService, + private router: Router, + ) { + super(); + } + + ngOnInit(): void { + } + + migrateSchematics(ev: Event) { + (ev.srcElement as HTMLButtonElement).disabled = true; + this.maintenanceTasksService.migrateSchematics().pipe(takeUntil(this._destroyed)).subscribe( + response => { + (ev.srcElement as HTMLButtonElement).disabled = false; + this.onCallbackSuccess(); + }, + error => { + (ev.srcElement as HTMLButtonElement).disabled = false; + this.onCallbackError(error); + } + ); + } + + onCallbackSuccess(): void { + this.uiNotificationService.snackBarNotification( this.translate.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); + this.router.navigate(['/reload']).then(() => this.router.navigate(['/maintenance-tasks'])); + } + + onCallbackError(error: any) { + this.uiNotificationService.snackBarNotification( error, SnackBarNotificationLevel.Error); + } + +} diff --git a/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.module.ts b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.module.ts new file mode 100644 index 000000000..fe1988755 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.module.ts @@ -0,0 +1,23 @@ +import { NgModule } from '@angular/core'; + +import { MaintenanceTasksRoutingModule } from './maintenance-tasks.routing'; +import { MaintenanceTasksComponent } from './maintenance-tasks.component'; +import { CommonUiModule } from '@common/ui/common-ui.module'; +import { CommonFormsModule } from '@common/forms/common-forms.module'; +import { ConfirmationDialogModule } from '@common/modules/confirmation-dialog/confirmation-dialog.module'; +import { MaintenanceTasksService } from '@app/core/services/maintenance-tasks/maintenance-tasks.service'; + + +@NgModule({ + declarations: [MaintenanceTasksComponent], + imports: [ + CommonUiModule, + CommonFormsModule, + ConfirmationDialogModule, + MaintenanceTasksRoutingModule + ], + providers: [ + MaintenanceTasksService + ] +}) +export class MaintenanceTasksModule { } \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.routing.ts b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.routing.ts new file mode 100644 index 000000000..daec08ac0 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/maintenance-tasks/maintenance-tasks.routing.ts @@ -0,0 +1,15 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { MaintenanceTasksComponent } from './maintenance-tasks.component'; +import { AdminAuthGuard } from '@app/core/admin-auth-guard.service'; + + +const routes: Routes = [ + { path: '', component: MaintenanceTasksComponent, canActivate: [AdminAuthGuard] }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule] +}) +export class MaintenanceTasksRoutingModule { } \ No newline at end of file