SourceDescriptorServiceImpl.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 org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.SourceDescriptor;
import org.gringlobal.model.SourceDescriptorLang;
import org.gringlobal.persistence.SourceDescriptorLangRepository;
import org.gringlobal.persistence.SourceDescriptorRepository;
import org.gringlobal.service.SourceDescriptorService;
import org.gringlobal.service.SourceDescriptorTranslationService;
import org.gringlobal.service.SourceDescriptorTranslationService.TranslatedSourceDescriptor;
import org.gringlobal.service.filter.SourceDescriptorFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

/**
 * The Class SourceDescriptorServiceImpl.
 */
@Service
@Transactional(readOnly = true)
@Validated
public class SourceDescriptorServiceImpl extends FilteredTranslatedCRUDServiceImpl<SourceDescriptor, SourceDescriptorLang, TranslatedSourceDescriptor, SourceDescriptorFilter, SourceDescriptorRepository> implements SourceDescriptorService {

	private static final String[] BOOST_FIELDS = { "langs.title", "langs.description" };
	
	/**
	 * The Class SourceDescriptorTranslationSupport.
	 */
	@Component
	public static class SourceDescriptorTranslationSupport extends BaseTranslationSupport<SourceDescriptor, SourceDescriptorLang, TranslatedSourceDescriptor, SourceDescriptorFilter, SourceDescriptorLangRepository> implements SourceDescriptorTranslationService {

		/**
		 * Instantiates a new source descriptor translation support.
		 */
		public SourceDescriptorTranslationSupport() {
			super();
		}

		@Override
		protected TranslatedSourceDescriptor toTranslated(SourceDescriptor e, String title, String description) {
			return TranslatedSourceDescriptor.from(e, title, description, null);
		}
	}

	@Override
	public Page<TranslatedSourceDescriptor> listFiltered(SourceDescriptorFilter filter, Pageable page) throws SearchException {
		return super.listFiltered(SourceDescriptor.class, filter, page, BOOST_FIELDS);
	}
	
	@Override
	@Transactional
	// TODO Add SecurityAction.SourceDescriptor
	public SourceDescriptor create(SourceDescriptor source) {
		var target = new SourceDescriptor();
		target.apply(source);
		return repository.save(target);
	}

	@Override
	@Transactional
	// TODO Add SecurityAction.SourceDescriptor
	public SourceDescriptor update(SourceDescriptor updated, SourceDescriptor target) {
		target.apply(updated);
		return repository.save(target);
	}

	@Override
	@Transactional
	// TODO Add SecurityAction.SourceDescriptor
	public SourceDescriptor updateFast(SourceDescriptor updated, SourceDescriptor target) {
		target.apply(updated);
		return repository.save(target);
	}

}