2019-07-31 16:57:34 +02:00
|
|
|
package eu.eudat.logic.managers;
|
|
|
|
|
2019-08-26 15:05:38 +02:00
|
|
|
import eu.eudat.data.dao.criteria.FunderCriteria;
|
|
|
|
import eu.eudat.data.entities.Funder;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.data.query.items.table.grant.GrantTableRequest;
|
|
|
|
import eu.eudat.exceptions.grant.GrantWithDMPsDeleteException;
|
|
|
|
import eu.eudat.logic.builders.model.models.GrantBuilder;
|
|
|
|
import eu.eudat.data.dao.entities.GrantDao;
|
|
|
|
import eu.eudat.data.entities.DMP;
|
2019-10-03 13:06:44 +02:00
|
|
|
import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
2019-10-03 13:06:44 +02:00
|
|
|
import eu.eudat.logic.utilities.helpers.ListHelper;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.models.data.external.ExternalSourcesItemModel;
|
|
|
|
import eu.eudat.models.data.external.GrantsExternalSourcesModel;
|
|
|
|
import eu.eudat.models.data.grant.Grant;
|
|
|
|
import eu.eudat.models.data.helpers.common.DataTableData;
|
|
|
|
import eu.eudat.data.query.items.item.grant.GrantCriteriaRequest;
|
|
|
|
import eu.eudat.models.data.grant.GrantListingModel;
|
|
|
|
import eu.eudat.models.data.security.Principal;
|
|
|
|
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
|
|
|
|
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
|
|
|
|
import eu.eudat.logic.proxy.fetching.RemoteFetcher;
|
|
|
|
import eu.eudat.queryable.QueryableList;
|
|
|
|
import eu.eudat.logic.services.ApiContext;
|
|
|
|
import eu.eudat.logic.services.helpers.FileStorageService;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-10-03 13:06:44 +02:00
|
|
|
import java.util.stream.Collectors;
|
2019-07-31 16:57:34 +02:00
|
|
|
|
|
|
|
@Component
|
|
|
|
public class GrantManager {
|
|
|
|
|
|
|
|
private ApiContext apiContext;
|
|
|
|
private DatabaseRepository databaseRepository;
|
|
|
|
private FileStorageService fileStorageService;
|
|
|
|
private RemoteFetcher remoteFetcher;
|
2019-10-25 12:57:51 +02:00
|
|
|
private ListHelper listHelper;
|
2019-07-31 16:57:34 +02:00
|
|
|
|
2019-10-25 12:57:51 +02:00
|
|
|
public GrantManager(ApiContext apiContext, ListHelper listHelper) {
|
2019-07-31 16:57:34 +02:00
|
|
|
this.apiContext = apiContext;
|
|
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
|
|
this.fileStorageService = apiContext.getOperationsContext().getFileStorageService();
|
|
|
|
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
|
2019-10-25 12:57:51 +02:00
|
|
|
this.listHelper = listHelper;
|
2019-07-31 16:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public DataTableData<eu.eudat.models.data.grant.GrantListingModel> getPaged(GrantTableRequest grantTableRequest, Principal principal, String fieldsGroup) throws Exception {
|
|
|
|
eu.eudat.data.entities.UserInfo userInfo = new eu.eudat.data.entities.UserInfo();
|
|
|
|
userInfo.setId(principal.getId());
|
|
|
|
GrantDao grantRepository = databaseRepository.getGrantDao();
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> items = grantRepository.getWithCriteria(grantTableRequest.getCriteria());
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> authItems = grantRepository.getAuthenticated(items, userInfo);
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> pagedItems = PaginationManager.applyPaging(authItems, grantTableRequest);
|
|
|
|
DataTableData<eu.eudat.models.data.grant.GrantListingModel> dataTable = new DataTableData<>();
|
|
|
|
CompletableFuture grantsFuture;
|
|
|
|
if (fieldsGroup.equals("listing")) {
|
|
|
|
grantsFuture = pagedItems.selectAsync(item -> new GrantListingModel().fromDataModelDetails(item))
|
|
|
|
.whenComplete((results, throwable) ->
|
|
|
|
dataTable.setData(results)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
grantsFuture = pagedItems.selectAsync(item -> new GrantListingModel().fromDataModel(item))
|
|
|
|
.whenComplete((results, throwable) ->
|
|
|
|
dataTable.setData(results)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
CompletableFuture countFuture = pagedItems.countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count));
|
|
|
|
|
|
|
|
CompletableFuture.allOf(grantsFuture, countFuture).join();
|
|
|
|
return dataTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DataTableData<eu.eudat.models.data.grant.GrantListingModel> getPublicPaged(GrantTableRequest grantTableRequest) throws Exception {
|
|
|
|
GrantDao grantRepository = databaseRepository.getGrantDao();
|
|
|
|
grantTableRequest.getCriteria().setPublic(true);
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> items = grantRepository.getWithCriteria(grantTableRequest.getCriteria());
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> pagedItems = PaginationManager.applyPaging(items, grantTableRequest);
|
|
|
|
DataTableData<eu.eudat.models.data.grant.GrantListingModel> dataTable = new DataTableData<>();
|
|
|
|
CompletableFuture grantsFuture;
|
|
|
|
grantsFuture = pagedItems.selectAsync(item -> new GrantListingModel().fromDataModel(item))
|
|
|
|
.whenComplete((results, throwable) -> {
|
|
|
|
dataTable.setData(results);
|
|
|
|
});
|
|
|
|
CompletableFuture countFuture = pagedItems.countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count));
|
|
|
|
|
|
|
|
CompletableFuture.allOf(grantsFuture, countFuture).join();
|
|
|
|
return dataTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public eu.eudat.models.data.grant.Grant getSingle(String id) throws InstantiationException, IllegalAccessException {
|
|
|
|
eu.eudat.models.data.grant.Grant grant = new eu.eudat.models.data.grant.Grant();
|
|
|
|
grant.fromDataModel(databaseRepository.getGrantDao().find(UUID.fromString(id)));
|
|
|
|
return grant;
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:57:43 +02:00
|
|
|
/*public eu.eudat.data.entities.Grant inactivate(String id) throws InstantiationException, IllegalAccessException {
|
2019-07-31 16:57:34 +02:00
|
|
|
GrantDao grantRepository = databaseRepository.getGrantDao();
|
|
|
|
eu.eudat.data.entities.Grant grant = grantRepository.find(UUID.fromString(id));
|
|
|
|
grant.setStatus(eu.eudat.data.entities.Grant.Status.DELETED.getValue());
|
|
|
|
grant = grantRepository.createOrUpdate(grant);
|
|
|
|
return grant;
|
2019-10-04 10:57:43 +02:00
|
|
|
}*/
|
2019-07-31 16:57:34 +02:00
|
|
|
|
2019-11-21 17:25:43 +01:00
|
|
|
public List<eu.eudat.models.data.grant.Grant> getCriteriaWithExternal(GrantCriteriaRequest grantCriteria, Principal principal) throws HugeResultSet, NoURLFound {
|
2019-07-31 16:57:34 +02:00
|
|
|
eu.eudat.data.entities.UserInfo userInfo = new eu.eudat.data.entities.UserInfo();
|
|
|
|
userInfo.setId(principal.getId());
|
2019-12-12 17:29:56 +01:00
|
|
|
/*if (grantCriteria.getCriteria().getFunderReference() != null && !grantCriteria.getCriteria().getFunderReference().trim().isEmpty()) {
|
2019-08-26 15:05:38 +02:00
|
|
|
FunderCriteria funderCriteria = new FunderCriteria();
|
2019-08-27 15:59:21 +02:00
|
|
|
funderCriteria.setReference(grantCriteria.getCriteria().getFunderReference());
|
2019-08-26 15:05:38 +02:00
|
|
|
Funder funder = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().getWithCritetia(funderCriteria).getSingleOrDefault();
|
2019-08-28 17:17:16 +02:00
|
|
|
if (funder != null) {
|
|
|
|
grantCriteria.getCriteria().setFunderId(funder.getId().toString());
|
|
|
|
}
|
2019-12-12 17:29:56 +01:00
|
|
|
}*/
|
|
|
|
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(grantCriteria.getCriteria().getLike());
|
|
|
|
if (grantCriteria.getCriteria().getFunderReference() != null) {
|
|
|
|
externalUrlCriteria.setFunderId(grantCriteria.getCriteria().getFunderReference());
|
|
|
|
grantCriteria.getCriteria().setFunderReference(null);
|
2019-08-26 15:05:38 +02:00
|
|
|
}
|
2019-08-26 15:16:50 +02:00
|
|
|
grantCriteria.getCriteria().setReference("dmp:");
|
2019-07-31 16:57:34 +02:00
|
|
|
QueryableList<eu.eudat.data.entities.Grant> items = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().getWithCriteria(grantCriteria.getCriteria());
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> authItems = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().getAuthenticated(items, userInfo);
|
|
|
|
List<eu.eudat.models.data.grant.Grant> grants = authItems.select(item -> new Grant().fromDataModel(item));
|
2019-10-03 13:06:44 +02:00
|
|
|
|
|
|
|
List<Map<String, String>> remoteRepos = remoteFetcher.getGrants(externalUrlCriteria);
|
|
|
|
|
2019-07-31 16:57:34 +02:00
|
|
|
GrantsExternalSourcesModel grantsExternalSourcesModel = new GrantsExternalSourcesModel().fromExternalItem(remoteRepos);
|
|
|
|
for (ExternalSourcesItemModel externalListingItem : grantsExternalSourcesModel) {
|
|
|
|
eu.eudat.models.data.grant.Grant grant = apiContext.getOperationsContext().getBuilderFactory().getBuilder(GrantBuilder.class)
|
|
|
|
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
|
|
|
|
.description(externalListingItem.getDescription()).uri(externalListingItem.getUri())
|
|
|
|
.abbreviation(externalListingItem.getAbbreviation()).status(eu.eudat.data.entities.Grant.Status.fromInteger(0))
|
2019-09-05 16:44:25 +02:00
|
|
|
.source(externalListingItem.getTag())
|
2019-07-31 16:57:34 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
grants.add(grant);
|
|
|
|
}
|
2019-09-05 16:44:25 +02:00
|
|
|
grants.sort(Comparator.comparing(Grant::getLabel));
|
2019-10-25 12:57:51 +02:00
|
|
|
grants = grants.stream().filter(listHelper.distinctByKey(Grant::getLabel)).collect(Collectors.toList());
|
2019-07-31 16:57:34 +02:00
|
|
|
return grants;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<eu.eudat.models.data.grant.Grant> getCriteria(GrantCriteriaRequest grantCriteria) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
|
|
|
|
GrantDao grantRepository = databaseRepository.getGrantDao();
|
|
|
|
QueryableList<eu.eudat.data.entities.Grant> items = grantRepository.getWithCriteria(grantCriteria.getCriteria());
|
|
|
|
if (grantCriteria.getLength() != null) items.take(grantCriteria.getLength());
|
|
|
|
List<eu.eudat.models.data.grant.Grant> grants = items.select(item -> new Grant().fromDataModel(item));
|
|
|
|
return grants;
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:57:43 +02:00
|
|
|
/*public void createOrUpdate(eu.eudat.models.data.grant.Grant grant, Principal principal) throws ParseException, IOException {
|
2019-07-31 16:57:34 +02:00
|
|
|
eu.eudat.data.entities.Grant grantEntity = grant.toDataModel();
|
|
|
|
if (grant.getFiles() != null) {
|
|
|
|
for (ContentFile file : grant.getFiles()) {
|
|
|
|
try {
|
|
|
|
ContentFile storedFile = fileStorageService.copyFromTempFileSystem(file);
|
|
|
|
Content content = new ContentBuilder().extension(file.getType())
|
|
|
|
.label(file.getFilename())
|
|
|
|
.locationType(Content.LocationType.INTERNAL.getValue())
|
|
|
|
.parentType(Content.ParentType.GRANT.getValue())
|
|
|
|
.uri("LOCAL:" + storedFile.getId())
|
|
|
|
.build();
|
|
|
|
grantEntity.setContent(databaseRepository.getContentDao().createOrUpdate(content));
|
|
|
|
} catch (TempFileNotFoundException e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grantEntity.setType(eu.eudat.data.entities.Grant.GrantType.INTERNAL.getValue());
|
|
|
|
grantEntity.setCreationUser(databaseRepository.getUserInfoDao().find(principal.getId()));
|
|
|
|
databaseRepository.getGrantDao().createOrUpdate(grantEntity);
|
2019-10-04 10:57:43 +02:00
|
|
|
}*/
|
2019-07-31 16:57:34 +02:00
|
|
|
|
|
|
|
public void delete(UUID uuid) {
|
|
|
|
eu.eudat.data.entities.Grant oldGrant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().find(uuid);
|
|
|
|
if (oldGrant.getDmps().size() > 0)
|
|
|
|
throw new GrantWithDMPsDeleteException("You cannot Remove Grants with DMPs");
|
|
|
|
oldGrant.setStatus(DMP.DMPStatus.DELETED.getValue());
|
|
|
|
apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().createOrUpdate(oldGrant);
|
|
|
|
}
|
|
|
|
}
|