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 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.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
public class SiteServiceImpl extends FilteredCRUDServiceImpl<Site, SiteFilter, SiteRepository> implements SiteService {

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public Site update(final Site input, Site target) {
		log.debug("Update Site. Input data {}", input);
		target.apply(input);

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

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public Site create(final Site source) {
		log.debug("Create Site. Input data {}", source);
		Site site = new Site();
		site.apply(source);

		Site saved = repository.save(site);
		return _lazyLoad(saved);
	}
	
	@Override
	public Page<Site> list(SiteFilter filter, Pageable page) throws SearchException {
		return super.list(Site.class, filter, page);
	}

	@Override
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	// This method is overridden because we need permission check
	public Site remove(Site entity) {
		return super.remove(entity);
	}
}