LanguageServiceImpl.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.List;
import java.util.Locale;
import lombok.extern.slf4j.Slf4j;
import org.gringlobal.model.QSysLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.SysLangRepository;
import org.gringlobal.service.LanguageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* The LanguageServiceImpl.
*
* @author Matija Obreza
*/
@Service
@Transactional(readOnly = true)
@Slf4j
public class LanguageServiceImpl extends CRUDServiceImpl<SysLang, SysLangRepository> implements LanguageService {
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
public SysLang create(SysLang source) {
log.debug("Create SysLang. Input data {}", source);
SysLang method = new SysLang();
method.apply(source);
return repository.save(method);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
public SysLang update(SysLang updated, SysLang target) {
log.debug("Update SysLang. Input data {}", updated);
target.apply(updated);
return repository.save(target);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
public SysLang ensureSysLang(String ietfTag, String iso6393Tag, String title, String description) {
SysLang existing = this.getLanguage(ietfTag);
if (existing == null) {
return this.create(new SysLang(ietfTag, iso6393Tag, title, description));
} else {
return existing;
}
}
@Override
public SysLang getLanguage(String lang) {
SysLang sysLang = null;
List<SysLang> languages = repository.findAll();
for (SysLang sl : languages) {
if (sysLang == null && sl.getIetfTag().startsWith(lang)) {
sysLang = sl;
}
if (sl.getIetfTag().equalsIgnoreCase(lang)) {
sysLang = sl;
break;
}
}
return sysLang;
}
@Override
public SysLang getLanguage(Locale locale) {
SysLang sysLang = null;
List<SysLang> languages = repository.findAll();
for (SysLang sl : languages) {
if (sysLang == null && sl.getIetfTag().startsWith(locale.getLanguage() + "-")) {
sysLang = sl;
}
if (sl.getIetfTag().equalsIgnoreCase(locale.toLanguageTag())) {
sysLang = sl;
break;
}
}
return sysLang;
}
@Override
public Iterable<SysLang> listEnabledLanguages() {
return repository.findAll(QSysLang.sysLang.isEnabled.eq("Y"), QSysLang.sysLang.ietfTag.asc());
}
}