AppResourceRepositoryCustomImpl.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.persistence;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.AppResource;
import org.gringlobal.model.QAppResource;
import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.AppResourceRepository.AppResourceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.querydsl.jpa.impl.JPAQueryFactory;
/**
* The Class AppResourceRepositoryCustomImpl.
*/
public class AppResourceRepositoryCustomImpl implements AppResourceRepositoryCustom {
@Autowired
private JPAQueryFactory jpaQueryFactory;
/**
* List app resources.
*
* @param appName the app name
* @param defaultLanguage the default language
* @param targetLanguage the target language
* @return the list
*/
@Override
public List<AppResourceInfo> listAppResources(String appName, SysLang defaultLanguage, SysLang targetLanguage) {
List<AppResourceInfo> defaults = jpaQueryFactory.selectFrom(QAppResource.appResource).where(QAppResource.appResource.appName.eq(appName).and(
QAppResource.appResource.sysLang().eq(defaultLanguage))).orderBy(QAppResource.appResource.sortOrder.asc()).fetch()
// convert
.stream().map(ar -> new AppResourceInfo(ar)).collect(Collectors.toList());
if (Objects.equals(targetLanguage.getId(), defaultLanguage.getId())) {
return defaults;
}
List<AppResourceInfo> language = jpaQueryFactory.selectFrom(QAppResource.appResource).where(QAppResource.appResource.appName.eq(appName).and(
QAppResource.appResource.sysLang().eq(targetLanguage))).orderBy(QAppResource.appResource.sortOrder.asc()).fetch()
// convert
.stream().map(ar -> new AppResourceInfo(ar)).collect(Collectors.toList());
if (language.size() == 0) {
return defaults;
}
List<AppResourceInfo> mix = new ArrayList<>(defaults.size());
for (AppResourceInfo def : defaults) {
Optional<AppResourceInfo> translated = language.stream().filter(r -> StringUtils.equals(r.formName, def.formName) && StringUtils.equals(r.resourceName,
def.resourceName)).findFirst();
mix.add(translated.isPresent() ? translated.get() : def);
}
return mix;
}
@Override
public AppResource findByAppNameAndAppResourceNameAndLang(String appName, String appResourceName, SysLang defaultLanguage, SysLang targetLanguage) {
QAppResource qAppResource = QAppResource.appResource;
AppResource defaultAppRes = jpaQueryFactory.selectFrom(qAppResource).where(qAppResource.appName.eq(appName).and(qAppResource.appResourceName.eq(appResourceName)).and(
qAppResource.sysLang().eq(defaultLanguage))).orderBy(qAppResource.sortOrder.asc()).fetchFirst();
if (Objects.equals(targetLanguage.getId(), defaultLanguage.getId())) {
return defaultAppRes;
}
AppResource targetAppRes = jpaQueryFactory.selectFrom(qAppResource).where(qAppResource.appName.eq(appName).and(qAppResource.appResourceName.eq(appResourceName)).and(
qAppResource.sysLang().eq(targetLanguage))).orderBy(qAppResource.sortOrder.asc()).fetchFirst();
return targetAppRes == null ? defaultAppRes : targetAppRes;
}
}