StatisticsServiceImpl.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.server.service.impl;

import org.genesys.server.exception.SearchException;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.filters.DatasetFilter;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.PDCIStatistics;
import org.genesys.server.model.impl.PGRFANetwork;
import org.genesys.server.service.DatasetService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.GenesysService;
import org.genesys.server.service.GeoService;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.StatisticsService;
import org.genesys.server.service.filter.AccessionFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class StatisticsServiceImpl implements StatisticsService {
	public static final Logger LOG = LoggerFactory.getLogger(StatisticsServiceImpl.class);

	@Autowired
	private GeoService geoService;

	@Autowired
	private InstituteService instituteService;

	@Autowired
	private GenesysService genesysService;

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Autowired
	private DatasetService datasetService;

	@Override
	@Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfCountries() {
		return geoService.countActive();
	}

	@Override
	@Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfInstitutes() {
		return instituteService.countActive();
	}

	@Override
	// @Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfAccessions() throws SearchException {
		AccessionFilter af = new AccessionFilter();
		return elasticsearchService.count(Accession.class, af);
	}

	@Override
	// @Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfActiveAccessions() throws SearchException {
		AccessionFilter af = new AccessionFilter()
			.historic(false);
		return elasticsearchService.count(Accession.class, af);
	}

	@Override
	// @Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfHistoricAccessions() throws SearchException {
		AccessionFilter af = new AccessionFilter()
			.historic(true);
		return elasticsearchService.count(Accession.class, af);
	}

	@Override
	@Cacheable(value = "statistics", key = "'stats.' + #root.methodName")
	public long numberOfPublishedDatasets() throws SearchException {
		return datasetService.countDatasets(new DatasetFilter().state(PublishState.PUBLISHED));
	}

	@Override
	@Cacheable(unless = "#result == null", value = "statistics", key = "'stats.' + #root.methodName + '-org' + #network.id")
	public PDCIStatistics statisticsPDCI(PGRFANetwork network) {
		synchronized (this) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Regenerating PDCI statistics for {}", network);
			}
			return genesysService.statisticsPDCI(network);
		}
	}

}