SecuredActionServiceImpl.java
/*
* Copyright 2021 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 org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.model.Site;
import org.gringlobal.model.community.SecuredAction;
import org.gringlobal.persistence.community.SecuredActionRepository;
import org.gringlobal.service.SecuredActionService;
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.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
/**
* The Class SecuredActionServiceImpl.
*/
@Service
@Transactional(readOnly = true)
@Validated
@Slf4j
@CacheConfig(cacheNames = SecuredActionServiceImpl._CACHE)
public class SecuredActionServiceImpl extends CRUDService2Impl<SecuredAction, SecuredActionRepository> implements SecuredActionService {
static final String _CACHE = "securedActionCache";
@Override
@Cacheable(key = "#action + '-' + (#site == null ? -1 : #site.id)")
public SecuredAction getByActionAndSite(String action, Site site) {
log.trace("Loading action {} {}!!", action, site);
return repository.getByActionAndSite(action, site);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "#source.action + '-' + (#source.site == null ? -1 : #source.site.id)"),
})
public SecuredAction create(final SecuredAction source) {
return createFast(source);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "#source.action + '-' + (#source.site == null ? -1 : #source.site.id)"),
})
public SecuredAction createFast(SecuredAction source) {
if (source.getSite() == null) {
throw new InvalidApiUsageException("Refusing to create SecuredAction on null site.");
}
// MySQL does not consider nulls as part of a constraint
if (repository.getByActionAndSite(source.getAction(), source.getSite()) != null) {
throw new InvalidApiUsageException("Refusing to create duplicate SecuredAction");
}
log.debug("Create SecuredAction. Input data {}", source);
SecuredAction securedAction = new SecuredAction();
securedAction.apply(source);
securedAction.setParentAction(repository.getByActionAndSite(source.getAction(), null));
return repository.save(securedAction);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "#updated.action + '-' + (#updated.site == null ? -1 : #updated.site.id)"),
})
public SecuredAction update(SecuredAction updated, SecuredAction target) {
throw new InvalidApiUsageException("SecuredActions can only be created, not updated.");
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "#updated.action + '-' + (#updated.site == null ? -1 : #updated.site.id)"),
})
public SecuredAction updateFast(SecuredAction updated, SecuredAction target) {
throw new InvalidApiUsageException("SecuredActions can only be created, not updated.");
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Caching(evict = {
@CacheEvict(key = "#entity.action + '-' + (#entity.site == null ? -1 : #entity.site.id)"),
})
public SecuredAction remove(SecuredAction entity) {
entity = reload(entity);
if (entity.getSite() == null) {
throw new InvalidApiUsageException("Refusing to delete SecuredAction on null site.");
}
return super.remove(entity);
}
}