MethodServiceImpl.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 org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Method;
import org.gringlobal.persistence.MethodRepository;
import org.gringlobal.service.MethodService;
import org.gringlobal.service.filter.MethodFilter;
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;
import org.springframework.validation.annotation.Validated;

/**
 * @author Maxym Borodenko
 */
@Service
@Transactional(readOnly = true)
@Validated
@Slf4j
public class MethodServiceImpl extends FilteredCRUDServiceImpl<Method, MethodFilter, MethodRepository> implements MethodService {

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('MethodData', 'CREATE')")
	public Method create(final Method source) {
		log.debug("Create Method. Input data {}", source);
		Method method = new Method();
		method.apply(source);

		Method saved = repository.save(method);
		saved.lazyLoad();

		return saved;
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('MethodData', 'WRITE')")
	public Method update(Method updated) {
		return super.update(updated);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('MethodData', 'WRITE')")
	public Method update(Method updated, Method target) {
		log.debug("Update Method. Input data {}", updated);
		target.apply(updated);

		Method saved = repository.save(target);
		return _lazyLoad(saved);
	}

	@Override
	public Page<Method> list(MethodFilter filter, Pageable page) throws SearchException {
		var methodPage = super.list(Method.class, filter, page);
		methodPage.getContent().forEach(Method::lazyLoad);
		return methodPage;
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('MethodData', 'DELETE')")
	public Method remove(Method entity) {
		return super.remove(entity);
	}
}