SiteServiceImpl.java
/*
* Copyright 2020 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.gringlobal.service.impl;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Site;
import org.gringlobal.persistence.SiteRepository;
import org.gringlobal.service.SiteService;
import org.gringlobal.service.filter.SiteFilter;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Maxym Borodenko
*/
@Service
@Transactional(readOnly = true)
@Slf4j
@CacheConfig(cacheNames = { SiteServiceImpl._CACHE })
public class SiteServiceImpl extends FilteredCRUDService2Impl<Site, SiteFilter, SiteRepository> implements SiteService {
static final String _CACHE = "siteCache";
@Override
@Cacheable(key = "'*'")
public List<Site> listAll() {
log.warn("Loading all sites!!");
var all = repository.findAll();
all.forEach(entityManager::detach);
return all;
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "'*'"),
@CacheEvict(key = "#target.id"),
})
public Site update(final Site input, Site target) {
return _lazyLoad(updateFast(input, target));
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "'*'"),
@CacheEvict(key = "#target.id"),
})
public Site updateFast(Site updated, Site target) {
log.debug("Update Site. Input data {}", updated);
target.apply(updated);
Site saved = repository.save(target);
return saved;
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "'*'"),
})
public Site create(final Site source) {
return _lazyLoad(createFast(source));
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "'*'"),
})
public Site createFast(Site source) {
log.debug("Create Site. Input data {}", source);
Site site = new Site();
site.apply(source);
Site saved = repository.save(site);
return saved;
}
@Override
public Page<Site> list(SiteFilter filter, Pageable page) throws SearchException {
return super.list(Site.class, filter, page);
}
@Override
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "'*'"),
@CacheEvict(key = "#entity.id"),
})
// This method is overridden because we need permission check
public Site remove(Site entity) {
return super.remove(entity);
}
}