MaterielServiceImpl.java

/*
 * Copyright 2023 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 org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Materiel;
import org.gringlobal.persistence.MaterielRepository;
import org.gringlobal.service.MaterielService;
import org.gringlobal.service.filter.MaterielFilter;
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;

@Service
@Transactional(readOnly = true)
@Validated
public class MaterielServiceImpl extends FilteredCRUDServiceImpl<Materiel, MaterielFilter, MaterielRepository> implements MaterielService {
	
	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('Materiel', 'CREATE', #source.site) || hasRole('ADMINISTRATOR')")
	public Materiel create(Materiel source) {
		Materiel saved = new Materiel();
		saved.apply(source);
		return repository.save(saved);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('Materiel', 'WRITE', #target.site) || hasRole('ADMINISTRATOR')")
	public Materiel update(Materiel input, Materiel target) {
		target.apply(input);
		Materiel saved = repository.save(target);
		return _lazyLoad(saved);
	}

	@Override
	@PreAuthorize("@ggceSec.actionAllowed('Materiel', 'READ') || hasRole('ADMINISTRATOR')")
	public Page<Materiel> list(MaterielFilter filter, Pageable page) throws SearchException {
		var list = super.list(filter, page);
		list.getContent().forEach(Materiel::lazyLoad);
		return list;
	}

	@Override
	@PreAuthorize("@ggceSec.actionAllowed('Materiel', 'READ') || hasRole('ADMINISTRATOR')")
	public Page<Materiel> list(Pageable page) {
		return super.list(page);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('Materiel', 'DELETE', #entity.site) || hasRole('ADMINISTRATOR')")
	public Materiel remove(Materiel entity) {
		return super.remove(entity);
	}
}