Commit 19b3171e authored by Matija Obreza's avatar Matija Obreza

CropTrait: Order by title, description

parent 8965a5e6
......@@ -21,34 +21,14 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.NotFoundElement;
import org.gringlobal.model.CooperatorOwnedLang;
import org.gringlobal.model.CooperatorOwnedModel;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitAttach;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeAttach;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.model.QCropTrait;
import org.gringlobal.model.QCropTraitAttach;
import org.gringlobal.model.QCropTraitCode;
import org.gringlobal.model.QCropTraitCodeAttach;
import org.gringlobal.model.QCropTraitCodeLang;
import org.gringlobal.model.QCropTraitLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.model.*;
import org.gringlobal.persistence.CropTraitCodeLangRepository;
import org.gringlobal.persistence.CropTraitCodeRepository;
import org.gringlobal.persistence.CropTraitLangRepository;
......@@ -75,9 +55,16 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
/**
* The Class CropTraitServiceImpl.
......@@ -340,69 +327,77 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
private abstract static class Translation<E extends CooperatorOwnedModel, EL extends CooperatorOwnedLang<E>> {
private abstract static class Translation<E extends CooperatorOwnedModel> {
public E entity;
public String title;
public String description;
public Translation(E entity, EL defaultTranslation, EL selectedTranslation) {
this.entity = entity;
// Apply default
translate(defaultTranslation);
// Apply selected, can be in the same language as default
translate(selectedTranslation);
}
// public Translation(E entity, EL defaultTranslation, EL selectedTranslation) {
// this.entity = entity;
// // Apply default
// translate(defaultTranslation);
// // Apply selected, can be in the same language as default
// translate(selectedTranslation);
// }
public void translate(EL translation) {
if (translation != null) {
this.title = StringUtils.defaultIfBlank(translation.getTitle(), this.title);
this.description = StringUtils.defaultIfBlank(translation.getDescription(), this.description);
}
public Translation(E entity, String title, String description) {
this.entity = entity;
this.title = title;
this.description = description;
}
/**
* @return true if title and description are blank strings
*/
public boolean isBlank() {
return StringUtils.isBlank(this.title) && StringUtils.isBlank(this.description);
}
// public void translate(EL translation) {
// if (translation != null) {
// this.title = StringUtils.defaultIfBlank(translation.getTitle(), this.title);
// this.description = StringUtils.defaultIfBlank(translation.getDescription(), this.description);
// }
// }
}
private static class CropTraitTranslation extends Translation<CropTrait, CropTraitLang> {
private static class CropTraitTranslation extends Translation<CropTrait> {
public CropTraitTranslation(Tuple tuple) {
super(tuple.get(0, CropTrait.class), tuple.get(1, CropTraitLang.class), tuple.get(2, CropTraitLang.class));
super(tuple.get(0, CropTrait.class), tuple.get(1, String.class), tuple.get(2, String.class));
}
}
private static class CropTraitCodeTranslation extends Translation<CropTraitCode, CropTraitCodeLang> {
private static class CropTraitCodeTranslation extends Translation<CropTraitCode> {
public CropTraitCodeTranslation(Tuple tuple) {
super(tuple.get(0, CropTraitCode.class), tuple.get(1, CropTraitCodeLang.class), tuple.get(2, CropTraitCodeLang.class));
super(tuple.get(0, CropTraitCode.class), tuple.get(1, String.class), tuple.get(2, String.class));
}
}
private List<CropTraitCodeTranslation> fetchTranslationsOfCodes(final CropTrait cropTrait, final SysLang targetLanguage) {
var def = new QCropTraitCodeLang("def"); // alias
var lan = new QCropTraitCodeLang("lan"); // alias
var def = new QCropTraitCodeLang("def"); // join alias
var lan = new QCropTraitCodeLang("lan"); // join alias
var title = Expressions.cases().when(lan.title.isNotNull()).then(lan.title).otherwise(def.title);
var description = Expressions.cases().when(lan.description.isNotNull()).then(lan.description).otherwise(def.description);
List<Tuple> result = jpaQueryFactory.from(QCropTraitCode.cropTraitCode)
.select(QCropTraitCode.cropTraitCode, def, lan)
.select(QCropTraitCode.cropTraitCode, title, description)
// Default language
.leftJoin(QCropTraitCode.cropTraitCode.codeLangs, def).on(def.sysLang.id.eq(DEFAULT_LANGUAGE.getId()))
// Target language
.leftJoin(QCropTraitCode.cropTraitCode.codeLangs, lan).on(lan.sysLang.id.eq(targetLanguage.getId()))
// Filters
.where(QCropTraitCode.cropTraitCode.cropTrait.eq(cropTrait))
.orderBy(QCropTraitCode.cropTraitCode.code.asc())
.fetch();
return result.stream().map(CropTraitCodeTranslation::new).collect(Collectors.toList());
}
private Page<CropTraitTranslation> fetchTranslationsOfTraits(final Predicate filter, final Pageable page, final SysLang targetLanguage) {
var def = new QCropTraitLang("def"); // alias
var lan = new QCropTraitLang("lan"); // alias
var def = new QCropTraitLang("def"); // join alias
var lan = new QCropTraitLang("lan"); // join alias
var title = Expressions.cases().when(lan.title.isNotNull()).then(lan.title).otherwise(def.title);
var description = Expressions.cases().when(lan.description.isNotNull()).then(lan.description).otherwise(def.description);
// query aliases for use in orderby clause
var titlePath = ExpressionUtils.path(String.class, "title");
var descriptionPath = ExpressionUtils.path(String.class, "description");
JPAQuery<Tuple> query = jpaQueryFactory.from(QCropTrait.cropTrait)
.select(QCropTrait.cropTrait, def, lan)
.select(QCropTrait.cropTrait, Expressions.as(title, titlePath), Expressions.as(description, descriptionPath))
// Default language
.leftJoin(QCropTrait.cropTrait.cropTraitLangs, def).on(def.sysLang.id.eq(DEFAULT_LANGUAGE.getId()))
// Target language
......@@ -415,8 +410,14 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
var totalElements = query.fetchCount();
for (Sort.Order o : page.getSort()) {
var orderByExpression = new PathBuilder<>(CropTrait.class, QCropTrait.cropTrait.getMetadata());
query.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, orderByExpression.get(o.getProperty())));
if (o.getProperty().equalsIgnoreCase("title")) {
query.orderBy(new OrderSpecifier<String>(o.isAscending() ? Order.ASC : Order.DESC, titlePath));
} else if (o.getProperty().equalsIgnoreCase("description")) {
query.orderBy(new OrderSpecifier<String>(o.isAscending() ? Order.ASC : Order.DESC, descriptionPath));
} else {
var orderByExpression = new PathBuilder<>(CropTrait.class, QCropTrait.cropTrait.getMetadata());
query.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, orderByExpression.get(o.getProperty())));
}
}
var content = query.fetch().stream().map(CropTraitTranslation::new).collect(Collectors.toList());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment