AppResourceServiceImpl.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 java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.AppResource;
import org.gringlobal.model.QSysLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.AppResourceRepository;
import org.gringlobal.persistence.SysLangRepository;
import org.gringlobal.service.AppResourceService;
import org.gringlobal.service.LanguageService;
import org.gringlobal.service.filter.AppResourceFilter;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * The AppResourceServiceImpl class.
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class AppResourceServiceImpl extends FilteredCRUDServiceImpl<AppResource, AppResourceFilter, AppResourceRepository> implements AppResourceService {

	@Autowired
	private AppResourceRepository appResourceRepository;

	@Autowired
	private LanguageService languageService;

	@Autowired
	private SysLangRepository sysLangRepository;

	@Override
	public AppResource getResource(String appName, String appResourceName, Locale locale) {
		SysLang targetLanguage = languageService.getLanguage(locale);
		return appResourceRepository.findByAppNameAndAppResourceNameAndLang(appName, appResourceName, LanguageService.DEFAULT_LANGUAGE, targetLanguage);
	}

	@Override
	public List<AppResource> listTranslations(String applicationName, String formName, String resourceName, Integer sortOrder) {
		AppResourceFilter filter = new AppResourceFilter();
		filter.appName().eq(Set.of(applicationName));
		filter.formName().eq(Set.of(formName));
		filter.appResourceName().eq(Set.of(resourceName));
		filter.sortOrder(sortOrder);

		var appResources = Lists.newArrayList(appResourceRepository.findAll(filter.buildPredicate(), QSysLang.sysLang.ietfTag.asc()));
		Optional<AppResource> defaultTranslation = appResources.stream().filter(ar -> ar.getSysLang().getId().equals(LanguageService.DEFAULT_LANGUAGE.getId())).findFirst();

		List<AppResource> res = new ArrayList<>();
		sysLangRepository.findAll(QSysLang.sysLang.ietfTag.asc()).forEach(sysLang -> {
			res.add(appResources.stream().filter(ar -> ar.getSysLang().getId().equals(sysLang.getId())).findFirst().orElse(new AppResource(defaultTranslation, sysLang)));
		});

		return res;
	}

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

		AppResource saved = repository.save(appResource);
		saved.lazyLoad();

		return saved;
	}

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

		AppResource saved = repository.save(target);
		saved.lazyLoad();
		return saved;
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public AppResource remove(AppResource entity) {
		return super.remove(entity);
	}

	@Override
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public Page<AppResource> list(AppResourceFilter filter, Pageable page) throws SearchException {
		return super.list(AppResource.class, filter, page);
	}

}