package eu.eudat.migration.services; import eu.eudat.controllers.Datasets; import eu.eudat.data.entities.UserInfo; import eu.eudat.data.entities.UserRole; import eu.eudat.data.entities.UserToken; import eu.eudat.logic.builders.entity.UserTokenBuilder; import eu.eudat.logic.managers.AdminManager; import eu.eudat.logic.managers.DataManagementPlanManager; import eu.eudat.logic.managers.DatasetProfileManager; import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.operations.authentication.NonVerifiedUserEmailAuthenticationService; import eu.eudat.models.data.admin.composite.DatasetProfile; import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.dmp.DataManagementPlan; import eu.eudat.models.data.dmp.DataManagementPlanEditorModel; import eu.eudat.models.data.listingmodels.UserInfoListingModel; import eu.eudat.models.data.security.Principal; import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.queryable.QueryableList; import eu.eudat.types.Authorities; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.io.IOException; import java.sql.Timestamp; import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.UUID; @Service public class ArgosService { private final ApiContext context; private final NonVerifiedUserEmailAuthenticationService authenticationService; private final DataManagementPlanManager dataManagementPlanManager; private final DatasetProfileManager datasetProfileManager; private final Datasets datasets; @Autowired public ArgosService(ApiContext context, NonVerifiedUserEmailAuthenticationService authenticationService, DatasetProfileManager datasetProfileManager, DataManagementPlanManager dataManagementPlanManager, Datasets datasets) { this.context = context; this.authenticationService = authenticationService; this.datasetProfileManager = datasetProfileManager; this.dataManagementPlanManager = dataManagementPlanManager; this.datasets = datasets; } /** * Auth methods */ @Transactional public UserInfo getUserInfo(String email) { QueryableList query = context.getOperationsContext().getDatabaseRepository().getUserInfoDao().asQueryable(); query.where((builder, root) -> builder.equal(root.get("email"), email)); return query.getSingleOrDefault(); } @Transactional public void createUserInfo(UserInfo userInfo) { if (getUserInfo(userInfo.getEmail()) == null) { context.getOperationsContext().getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo); } } @Transactional public Principal login(UserInfo userInfo) { QueryableList query = context.getOperationsContext().getDatabaseRepository().getUserTokenDao().asQueryable(); query.where((builder, root) -> builder.equal(root.get("user").get("id"), userInfo.getId())); query.where((builder, root) -> builder.greaterThan(root.get("expiresAt"), new Date())); UserToken userToken = query.getSingleOrDefault(); if(userToken == null) { userToken = this.context.getOperationsContext().getBuilderFactory().getBuilder(UserTokenBuilder.class) .issuedAt(new Date()).user(userInfo) .token(UUID.randomUUID()).expiresAt(Timestamp.valueOf(LocalDateTime.now().plusDays(10))) .build(); userToken = context.getOperationsContext().getDatabaseRepository().getUserTokenDao().createOrUpdate(userToken); } return authenticationService.Touch(userToken); } @Transactional public void assignRole(UserInfo userInfo, Authorities authority) { QueryableList query = context.getOperationsContext().getDatabaseRepository().getUserRoleDao().asQueryable(); query.where((builder, root) -> builder.equal(root.get("userInfo").get("id"), userInfo.getId())); if(query.getSingleOrDefault() == null) { UserRole role = new UserRole(); role.setUserInfo(userInfo); role.setRole(authority.getValue()); context.getOperationsContext().getDatabaseRepository().getUserRoleDao().createOrUpdate(role); } } /** * Dataset Profiles */ @Transactional public UUID createDatasetProfile(DatasetProfile datasetProfile, Principal principal) { return this.datasetProfileManager.addDmp(datasetProfile, principal, false).getId(); } @Transactional public UUID createNewVersionDatasetProfile(UUID id, DatasetProfile datasetProfile) throws Exception { return this.datasetProfileManager.createNewVersionDatasetProfile(id.toString(), datasetProfile, false).getId(); } @Transactional public void deleteDatasetProfile(UUID id) { AdminManager.inactivate(context.getOperationsContext().getDatabaseRepository().getDatasetProfileDao(), context.getOperationsContext().getDatabaseRepository().getDatasetDao(), id.toString()); } /** * DMP methods * @return */ @Transactional public DataManagementPlan createDMP(DataManagementPlanEditorModel model, Principal principal) throws Exception { return new eu.eudat.models.data.dmp.DataManagementPlan(). fromDataModel(this.dataManagementPlanManager.createOrUpdate(model, principal)); } @Transactional public void updateUsers(UUID id, List users, Principal principal) throws Exception { this.dataManagementPlanManager.updateUsers(id, users, principal); } @Transactional public void deleteDMP(UUID id) { try { this.dataManagementPlanManager.delete(id); } catch (IOException e) { e.printStackTrace(); } } /** * Dataset Methods */ @Transactional public PagedDatasetProfile getDataset(UUID id) { return Objects.requireNonNull(this.datasets.getSingle(id.toString()).getBody()).getPayload(); } @Transactional public DatasetWizardModel createDataset(DatasetWizardModel model, Principal principal) throws Exception { return Objects.requireNonNull(this.datasets.createOrUpdate(model, principal).getBody()).getPayload(); } @Transactional public void deleteDataset(UUID id, Principal principal) { try { this.datasets.delete(id, principal); } catch (Exception e) { e.printStackTrace(); } } }