116 lines
6.7 KiB
Java
116 lines
6.7 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.enums.ReferenceType;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.dao.entities.OrganisationDao;
|
|
import eu.eudat.data.UserEntity;
|
|
import eu.eudat.data.query.items.table.organisations.OrganisationsTableRequest;
|
|
import eu.eudat.logic.builders.model.models.OrganisationBuilder;
|
|
import eu.eudat.service.reference.external.criteria.ExternalReferenceCriteria;
|
|
import eu.eudat.commons.exceptions.HugeResultSetException;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
|
import eu.eudat.models.data.dmp.Organisation;
|
|
import eu.eudat.models.data.external.ExternalSourcesItemModel;
|
|
import eu.eudat.models.data.external.OrganisationsExternalSourcesModel;
|
|
import eu.eudat.models.data.helpers.common.DataTableData;
|
|
import eu.eudat.queryable.QueryableList;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class OrganisationsManager {
|
|
|
|
private ApiContext apiContext;
|
|
private DatabaseRepository databaseRepository;
|
|
private final UserScope userScope;
|
|
|
|
@Autowired
|
|
public OrganisationsManager(ApiContext apiContext, UserScope userScope) {
|
|
this.apiContext = apiContext;
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
this.userScope = userScope;
|
|
}
|
|
|
|
public DataTableData<Organisation> getPagedOrganisations(OrganisationsTableRequest organisationsTableRequest) throws Exception {
|
|
UserEntity userInfo = new UserEntity();
|
|
userInfo.setId(this.userScope.getUserId());
|
|
OrganisationDao organisationDao = databaseRepository.getOrganisationDao();
|
|
|
|
QueryableList<eu.eudat.data.old.Organisation> items = organisationDao.getWithCriteria(organisationsTableRequest.getCriteria());
|
|
QueryableList<eu.eudat.data.old.Organisation> authItems = organisationDao.getAuthenticated(items, userInfo);
|
|
QueryableList<eu.eudat.data.old.Organisation> pagedItems = PaginationManager.applyPaging(authItems, organisationsTableRequest);
|
|
|
|
List<Organisation> org = pagedItems.toList().stream().distinct().map(item -> new Organisation().fromDataModel(item)).collect(Collectors.toList());
|
|
DataTableData<Organisation> organisationDataTableData = new DataTableData<>();
|
|
organisationDataTableData.setData(org);
|
|
organisationDataTableData.setTotalCount(pagedItems.count());
|
|
|
|
return organisationDataTableData;
|
|
}
|
|
|
|
public DataTableData<Organisation> getPublicPagedOrganisations(OrganisationsTableRequest organisationsTableRequest) throws Exception {
|
|
organisationsTableRequest.getCriteria().setPublic(true);
|
|
OrganisationDao organisationDao = databaseRepository.getOrganisationDao();
|
|
|
|
QueryableList<eu.eudat.data.old.Organisation> items = organisationDao.getWithCriteria(organisationsTableRequest.getCriteria());
|
|
QueryableList<eu.eudat.data.old.Organisation> pagedItems = PaginationManager.applyPaging(items, organisationsTableRequest);
|
|
|
|
List<Organisation> org = pagedItems.toList().stream().distinct().map(item -> new Organisation().fromDataModel(item)).collect(Collectors.toList());
|
|
DataTableData<Organisation> organisationDataTableData = new DataTableData<>();
|
|
organisationDataTableData.setData(org);
|
|
organisationDataTableData.setTotalCount(pagedItems.count());
|
|
|
|
return organisationDataTableData;
|
|
}
|
|
|
|
public List<Organisation> getWithExternal(OrganisationsTableRequest organisationsTableRequest) throws Exception {
|
|
UserEntity userInfo = new UserEntity();
|
|
userInfo.setId(this.userScope.getUserId());
|
|
OrganisationDao organisationDao = databaseRepository.getOrganisationDao();
|
|
|
|
QueryableList<eu.eudat.data.old.Organisation> items = organisationDao.getWithCriteria(organisationsTableRequest.getCriteria());
|
|
QueryableList<eu.eudat.data.old.Organisation> authItems = organisationDao.getAuthenticated(items, userInfo);
|
|
QueryableList<eu.eudat.data.old.Organisation> pagedItems = PaginationManager.applyPaging(authItems, organisationsTableRequest);
|
|
|
|
List<Organisation> org = pagedItems.toList().stream().distinct().map(item -> new Organisation().fromDataModel(item)).collect(Collectors.toList());
|
|
|
|
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria(organisationsTableRequest.getCriteria().getLabelLike());
|
|
List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(ReferenceType.Organizations, externalReferenceCriteria, null);
|
|
OrganisationsExternalSourcesModel organisationsExternalSourcesModel = new OrganisationsExternalSourcesModel().fromExternalItem(remoteRepos);
|
|
for (ExternalSourcesItemModel externalListingItem : organisationsExternalSourcesModel) {
|
|
Organisation organisation = apiContext.getOperationsContext().getBuilderFactory().getBuilder(OrganisationBuilder.class)
|
|
.name(externalListingItem.getName())
|
|
.reference(externalListingItem.getRemoteId())
|
|
.tag(externalListingItem.getTag())
|
|
.key(externalListingItem.getKey())
|
|
.build();
|
|
org.add(organisation);
|
|
}
|
|
return org;
|
|
}
|
|
|
|
public List<Organisation> getCriteriaWithExternal(String query, String type) throws HugeResultSetException, MyNotFoundException {
|
|
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria(query);
|
|
List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(ReferenceType.Organizations, externalReferenceCriteria, type);
|
|
OrganisationsExternalSourcesModel organisationsExternalSourcesModel = new OrganisationsExternalSourcesModel().fromExternalItem(remoteRepos);
|
|
List<Organisation> organisations = new LinkedList<>();
|
|
for (ExternalSourcesItemModel externalListingItem : organisationsExternalSourcesModel) {
|
|
Organisation organisation = apiContext.getOperationsContext().getBuilderFactory().getBuilder(OrganisationBuilder.class)
|
|
.name(externalListingItem.getName())
|
|
.reference(externalListingItem.getRemoteId())
|
|
.tag(externalListingItem.getTag())
|
|
.key(externalListingItem.getKey())
|
|
.build();
|
|
organisations.add(organisation);
|
|
}
|
|
return organisations.stream().distinct().collect(Collectors.toList());
|
|
}
|
|
}
|