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.community.SecuredAction;
import org.gringlobal.persistence.community.SecuredActionRepository;
import org.gringlobal.service.SecuredActionService;
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
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Slf4j
public class SecuredActionServiceImpl extends CRUDServiceImpl<SecuredAction, SecuredActionRepository> implements SecuredActionService {
@Override
@Transactional
public SecuredAction create(final 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
public SecuredAction update(SecuredAction updated, SecuredAction target) {
throw new InvalidApiUsageException("SecuredActions can only be created, not updated.");
}
@Override
@Transactional
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);
}
}