Commit f8b32747 authored by Matija Obreza's avatar Matija Obreza

Updated `countZzzz` by model filter methods

parent 66c2a304
......@@ -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.
......
......@@ -174,9 +174,10 @@ public interface DescriptorService {
void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException;
/**
* Cont published descriptors
* Count published descriptors.
*
* @param filter the filter
* @return the number of published descriptors
*/
long countPublished();
long countDescriptors(DescriptorFilter 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());
}
/**
......@@ -458,8 +458,8 @@ public class DescriptorServiceImpl implements DescriptorService {
}
@Override
public long countPublished() {
return descriptorRepository.count(descriptor.state.eq(PublishState.PUBLISHED));
public long countDescriptors(DescriptorFilter filter) {
return descriptorRepository.count(filter.buildQuery());
}
}
......@@ -26,7 +26,6 @@ import org.genesys.catalog.persistence.PartnerRepository;
import org.genesys.catalog.service.PartnerService;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -59,9 +58,6 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
@Autowired
private SecurityUtils securityUtils;
@Autowired
private ElasticsearchService elasticsearchService;
@Value("${partner.primary.uuid}")
private UUID primaryPartnerUuid;
......@@ -147,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());
}
}
......@@ -163,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());
}
/**
......@@ -226,14 +222,7 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
@Override
public long countPartners(PartnerFilter filter) {
long total = elasticsearchService.count(Partner.class, filter);
if (total < 10000) {
// If total is below 10K, use actual count
total = partnerRepository.count(filter.buildQuery());
}
return total;
return partnerRepository.count(filter.buildQuery());
}
/**
......
package org.genesys2.server.api.v1;
import io.swagger.annotations.Api;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
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;
......@@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.net.UnknownHostException;
import io.swagger.annotations.Api;
/**
* Accession API v1
......@@ -31,8 +31,6 @@ import java.net.UnknownHostException;
@Api(tags = { "api-info" })
public class ApiInfoController {
private static final Logger LOG = LoggerFactory.getLogger(ApiInfoController.class);
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/info";
@Value("${build.version}")
......@@ -44,6 +42,9 @@ public class ApiInfoController {
@Value("${build.revision}")
private String revision;
@Value("${base.url}")
private String baseUrl;
@Value("${cdn.servers}")
private String[] cdnServers;
......@@ -67,25 +68,17 @@ public class ApiInfoController {
*/
@RequestMapping(value = "/version", method = RequestMethod.GET)
public ApiInfo apiInfo() {
String hN;
try {
hN = java.net.InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) {
LOG.error("Error getting Host Name");
hN = "";
}
ApiInfo apiInfo = new ApiInfo();
apiInfo.version = this.version;
apiInfo.artifactId = this.artifactId;
apiInfo.revision = this.revision;
apiInfo.cdnServers = this.cdnServers;
apiInfo.hostName = hN;
apiInfo.accessionCount = accessionService.countAccessions(new AccessionFilter());
apiInfo.datasetCount = datasetService.countPublished();
apiInfo.subsetCount = subsetService.countPublished();
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.countPublished();
apiInfo.descriptorCount = descriptorService.countDescriptors(new DescriptorFilter().state(PublishState.PUBLISHED));
apiInfo.partnerCount = partnerService.countPartners(new PartnerFilter());
return apiInfo;
......@@ -98,7 +91,7 @@ public class ApiInfoController {
public String artifactId;
public String version;
public String revision;
public String hostName;
public String baseUrl;
public String[] cdnServers;
public long accessionCount;
......
......@@ -43,11 +43,12 @@ public interface SubsetService {
Page<Subset> list(SubsetFilter filter, Pageable page);
/**
* Cont published subsets
* Count published subsets.
*
* @param filter the filter
* @return the number of published subsets
*/
long countPublished();
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
......
......@@ -99,8 +99,8 @@ public class SubsetServiceImpl implements SubsetService {
}
@Override
public long countPublished() {
return subsetRepository.count(QSubset.subset.state.eq(PublishState.PUBLISHED));
public long countSubsets(SubsetFilter filter) {
return subsetRepository.count(filter.buildQuery());
}
@Override
......
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