Commit ca3dccef authored by Matija Obreza's avatar Matija Obreza

Merge branch '330-apiinfocontroller-v1' into 'master'

Resolve "ApiInfoController v1"

Closes #330

See merge request genesys-pgr/genesys-server!225
parents d62a988d f8b32747
......@@ -17,6 +17,7 @@ package org.genesys.catalog.model.filters;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
......@@ -124,4 +125,14 @@ public class DatasetFilter extends UuidModelFilter<DatasetFilter, Dataset> {
return and;
}
public DatasetFilter state(PublishState... state) {
if (this.state == null) {
this.state = new HashSet<>();
}
for (PublishState s : state) {
this.state.add(s);
}
return this;
}
}
......@@ -17,6 +17,7 @@ package org.genesys.catalog.model.filters;
import static org.genesys.catalog.model.traits.QDescriptor.descriptor;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
......@@ -171,4 +172,14 @@ public class DescriptorFilter extends UuidModelFilter<DescriptorFilter, Descript
return and;
}
public DescriptorFilter state(PublishState... state) {
if (this.state == null) {
this.state = new HashSet<>();
}
for (PublishState s : state) {
this.state.add(s);
}
return this;
}
}
......@@ -245,11 +245,12 @@ public interface DatasetService {
Dataset updateDatasetFile(Dataset dataset, RepositoryFile metadata) throws NoSuchRepositoryFileException;
/**
* Count the number of published datasets
* Count the number of published datasets.
*
* @param filter the filter
* @return the number of published datasets
*/
long countPublished();
long countDatasets(DatasetFilter filter);
/**
* Load data about last published datasets.
......
......@@ -172,4 +172,12 @@ public interface DescriptorService {
Descriptor nextVersion(@P("descriptor") Descriptor descriptor, boolean major);
void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException;
/**
* Count published descriptors.
*
* @param filter the filter
* @return the number of published descriptors
*/
long countDescriptors(DescriptorFilter filter);
}
......@@ -112,4 +112,6 @@ public interface PartnerService {
*/
Partner removePartner(Partner partner);
long countPartners(PartnerFilter filter);
}
......@@ -15,7 +15,7 @@
*/
package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.dataset.QDataset.*;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import java.io.IOException;
import java.nio.file.Paths;
......@@ -267,8 +267,16 @@ public class DatasetServiceImpl implements DatasetService {
@Override
public Page<Dataset> listDatasets(final DatasetFilter filter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(new BooleanBuilder().and(filter.buildQuery()).and(QDataset.dataset.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
Page<Dataset> res = datasetRepository.findAll(new BooleanBuilder().and(filter.buildQuery()).and(QDataset.dataset.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
/**
* {@inheritDoc}
*/
@Override
public long countDatasets(DatasetFilter filter) {
return datasetRepository.count(filter.buildQuery());
}
/**
......@@ -278,13 +286,13 @@ public class DatasetServiceImpl implements DatasetService {
public Page<Dataset> listDatasetsForCurrentUser(final DatasetFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
Page<Dataset> res = datasetRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(dataset.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
Page<Dataset> res = datasetRepository.findAll(dataset.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -528,14 +536,6 @@ public class DatasetServiceImpl implements DatasetService {
return lazyLoad(datasetRepository.save(loaded));
}
/**
* {@inheritDoc}
*/
@Override
public long countPublished() {
return datasetRepository.count(dataset.state.in(PublishState.PUBLISHED));
}
/**
* {@inheritDoc}
*/
......
......@@ -26,7 +26,6 @@ import java.util.UUID;
import javax.persistence.EntityManager;
import com.querydsl.core.types.Predicate;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.Partner;
......@@ -62,6 +61,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
/**
* The Class DescriptorServiceImpl.
......@@ -226,13 +226,13 @@ public class DescriptorServiceImpl implements DescriptorService {
public Page<Descriptor> listDescriptorsForCurrentUser(final DescriptorFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
Page<Descriptor> res = descriptorRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(descriptor.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
Page<Descriptor> res = descriptorRepository.findAll(descriptor.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -242,8 +242,8 @@ public class DescriptorServiceImpl implements DescriptorService {
@Override
public Page<Descriptor> listDescriptors(final DescriptorFilter descriptorFilter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(new BooleanBuilder().and(descriptorFilter.buildQuery()).and(QDescriptor.descriptor.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
Page<Descriptor> res = descriptorRepository.findAll(new BooleanBuilder().and(descriptorFilter.buildQuery()).and(QDescriptor.descriptor.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
/**
......@@ -457,4 +457,9 @@ public class DescriptorServiceImpl implements DescriptorService {
downloadService.writeXlsxDescriptor(descriptors, outputStream);
}
@Override
public long countDescriptors(DescriptorFilter filter) {
return descriptorRepository.count(filter.buildQuery());
}
}
......@@ -143,13 +143,13 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
public Page<Partner> listPartnersForCurrentUser(final PartnerFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
Page<Partner> res = partnerRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> ids = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(partner.id.in(ids).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
Page<Partner> res = partnerRepository.findAll(partner.id.in(ids).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -159,8 +159,8 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
@Override
public Page<Partner> listPartners(final PartnerFilter partnerFilter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(partnerFilter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
Page<Partner> res = partnerRepository.findAll(partnerFilter.buildQuery(), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
/**
......@@ -220,6 +220,11 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
return loadedPartner;
}
@Override
public long countPartners(PartnerFilter filter) {
return partnerRepository.count(filter.buildQuery());
}
/**
* Lazy load.
*
......
package org.genesys2.server.api.v1;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.filters.DescriptorFilter;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.PartnerService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.SubsetFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
/**
* Accession API v1
*/
@RestController("infoApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(ApiInfoController.CONTROLLER_URL)
@Api(tags = { "api-info" })
public class ApiInfoController {
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/info";
@Value("${build.version}")
private String version;
@Value("${build.artifactId}")
private String artifactId;
@Value("${build.revision}")
private String revision;
@Value("${base.url}")
private String baseUrl;
@Value("${cdn.servers}")
private String[] cdnServers;
@Autowired
private AccessionService accessionService;
@Autowired
private DatasetService datasetService;
@Autowired
private SubsetService subsetService;
@Autowired
private InstituteService instituteService;
@Autowired
private DescriptorService descriptorService;
@Autowired
private PartnerService partnerService;
/**
* Api info.
*
* @return the api info
*/
@RequestMapping(value = "/version", method = RequestMethod.GET)
public ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo();
apiInfo.version = this.version;
apiInfo.artifactId = this.artifactId;
apiInfo.revision = this.revision;
apiInfo.cdnServers = this.cdnServers;
apiInfo.baseUrl = this.baseUrl;
apiInfo.accessionCount = accessionService.countAccessions(new AccessionFilter().historic(false));
apiInfo.datasetCount = datasetService.countDatasets(new DatasetFilter().state(PublishState.PUBLISHED));
apiInfo.subsetCount = subsetService.countSubsets(new SubsetFilter().state(PublishState.PUBLISHED));
apiInfo.instituteCount = instituteService.countActive();
apiInfo.descriptorCount = descriptorService.countDescriptors(new DescriptorFilter().state(PublishState.PUBLISHED));
apiInfo.partnerCount = partnerService.countPartners(new PartnerFilter());
return apiInfo;
}
/**
* The Class ApiInfo.
*/
public static class ApiInfo {
public String artifactId;
public String version;
public String revision;
public String baseUrl;
public String[] cdnServers;
public long accessionCount;
public long datasetCount;
public long subsetCount;
public long instituteCount;
public long descriptorCount;
public long partnerCount;
}
}
......@@ -42,6 +42,14 @@ public interface SubsetService {
*/
Page<Subset> list(SubsetFilter filter, Pageable page);
/**
* Count published subsets.
*
* @param filter the filter
* @return the number of published subsets
*/
long countSubsets(SubsetFilter filter);
/**
* List current user's subsets matching the filter.
*
......
......@@ -15,7 +15,7 @@
*/
package org.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QAccession.*;
import static org.genesys2.server.model.genesys.QAccession.accession;
import java.util.Set;
import java.util.UUID;
......@@ -169,4 +169,9 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
}
}
public AccessionFilter historic(Boolean historic) {
this.historic = historic;
return this;
}
}
......@@ -17,6 +17,7 @@ package org.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QSubset.subset;
import java.util.HashSet;
import java.util.Set;
import org.genesys.blocks.model.filters.StringFilter;
......@@ -93,4 +94,14 @@ public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> {
}
return and;
}
public SubsetFilter state(PublishState ... state) {
if (this.state == null) {
this.state = new HashSet<>();
}
for (PublishState s : state) {
this.state.add(s);
}
return this;
}
}
......@@ -16,10 +16,12 @@
package org.genesys2.server.service.impl;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.genesys.PhenoStatistics;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.service.DatasetService;
import org.genesys2.server.persistence.PDCIRepository;
import org.genesys2.server.service.GenesysService;
......@@ -88,7 +90,7 @@ public class StatisticsServiceImpl implements StatisticsService {
@Override
@Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
public long numberOfPublishedDatasets() {
return datasetService.countPublished();
return datasetService.countDatasets(new DatasetFilter().state(PublishState.PUBLISHED));
}
@Override
......
......@@ -98,6 +98,11 @@ public class SubsetServiceImpl implements SubsetService {
return subsetRepository.findAll(published, page);
}
@Override
public long countSubsets(SubsetFilter filter) {
return subsetRepository.count(filter.buildQuery());
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or isAuthenticated()")
public Page<Subset> listSubsetsForCurrentUser(SubsetFilter filter, Pageable page) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment