Commit 7a34fb0e authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Unit tests

- Updated endpoints URLs
- Updated CropTraitService
- Added constraints in models
- Usage of Cascade.REMOVE in CropTrait and CropTraitCode
parent a54a24f7
......@@ -15,6 +15,8 @@
*/
package org.gringlobal.api.v1.impl;
import java.io.IOException;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
......@@ -24,7 +26,6 @@ import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.PageableAsQueryParam;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
......@@ -35,6 +36,7 @@ import org.gringlobal.model.QCropTrait;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService;
import org.gringlobal.service.CropTraitService;
import org.gringlobal.service.ShortFilterService;
import org.gringlobal.service.filter.CropTraitFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -45,19 +47,18 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController("cropTraitApi1")
@RequestMapping(CropTraitController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "CropTrait")
public class CropTraitController extends CRUDController<CropTrait, CropTraitService> {
public class CropTraitController extends ApiBaseController {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv1_BASE + "/crop-trait";
......@@ -78,40 +79,62 @@ public class CropTraitController extends CRUDController<CropTrait, CropTraitServ
@Qualifier(value = "cropTraitCodeAttachmentSupport")
private CropTraitCodeAttachmentService cropTraitCodeAttachmentService;
@GetMapping(value = "/translated/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Autowired
private CropTraitService cropTraitService;
@Autowired
private ShortFilterService shortFilterService;
@GetMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Get translated trait by ID", summary = "Get trait")
public CropTraitService.TranslatedCropTrait getTranslated(@PathVariable("id") final long id) {
return crudService.getTranslatedTrait(id);
return cropTraitService.getTranslatedTrait(id);
}
@PostMapping(value = "/translated/list", produces = { MediaType.APPLICATION_JSON_VALUE })
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Retrieve list of translated traits matching the filter", summary = "List by filter")
@PageableAsQueryParam
public FilteredPage<CropTraitService.TranslatedCropTrait, CropTraitFilter> list(@Parameter(hidden = true) final Pagination page, @RequestBody(required = false) final CropTraitFilter filter) throws SearchException, IOException {
CropTraitFilter normalizedFilter = shortFilterService.normalizeFilter(filter, filterType());
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, crudService.list(filter, pageable));
}
@GetMapping(value = "/translated/code/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Get translated trait code by ID", summary = "Get trait code")
public CropTraitService.TranslatedCropTraitCode getTranslatedTraitCode(@PathVariable("id") final long id) {
return crudService.getTranslatedTraitCode(id);
return new FilteredPage<>(normalizedFilter, cropTraitService.list(filter, pageable));
}
@Override
/**
* Remove the entity.
*
* @param id the id
* @return the removed record
*/
@DeleteMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Delete existing record by ID", summary = "Delete")
public CropTrait remove(@PathVariable("id") final long id) {
return super.remove(id);
return cropTraitService.remove(cropTraitService.get(id));
}
@Override
/**
* Register a new entity.
*
* @param entity the site
* @return the recorded record
*/
@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Create a record", summary = "Add")
public CropTrait create(@RequestBody final CropTrait entity) {
return super.create(entity);
return cropTraitService.create(entity);
}
@Override
/**
* Update the entity.
*
* @param entity entity with updates
* @return the updated record
*/
@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "update", description = "Update an existing record", summary = "Update")
public CropTrait update(@RequestBody final CropTrait entity) {
return super.update(entity);
return cropTraitService.update(entity);
}
@PostMapping(value = "/attach/{cropTraitId}", produces = { MediaType.APPLICATION_JSON_VALUE })
......@@ -119,27 +142,27 @@ public class CropTraitController extends CRUDController<CropTrait, CropTraitServ
public CropTraitAttach uploadFile(@PathVariable(name = "cropTraitId") final Long cropTraitId, @RequestPart(name = "file") final MultipartFile file,
@RequestPart(name = "metadata") final CropTraitAttachmentService.CropTraitAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {
return cropTraitAttachmentService.uploadFile(crudService.get(cropTraitId), file, metadata);
return cropTraitAttachmentService.uploadFile(cropTraitService.get(cropTraitId), file, metadata);
}
@DeleteMapping(value = "/attach/{cropTraitId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Remove attached file", summary = "Remove file")
public CropTraitAttach removeFile(@PathVariable(name = "cropTraitId") final Long cropTraitId, @PathVariable(name = "attachmentId") final Long attachmentId) {
return cropTraitAttachmentService.removeFile(crudService.get(cropTraitId), attachmentId);
return cropTraitAttachmentService.removeFile(cropTraitService.get(cropTraitId), attachmentId);
}
@PostMapping(value = "/attach/{cropTraitCodeId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@PostMapping(value = "/code/attach/{cropTraitCodeId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Attach crop trait file", summary = "Attach file")
public CropTraitCodeAttach uploadTraitCodeFile(@PathVariable(name = "cropTraitCodeId") final Long cropTraitCodeId, @RequestPart(name = "file") final MultipartFile file,
@RequestPart(name = "metadata") final CropTraitCodeAttachmentService.CropTraitCodeAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {
return cropTraitCodeAttachmentService.uploadFile(crudService.getTraitCode(cropTraitCodeId), file, metadata);
return cropTraitCodeAttachmentService.uploadFile(cropTraitService.getTraitCode(cropTraitCodeId), file, metadata);
}
@DeleteMapping(value = "/attach/{cropTraitCodeId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@DeleteMapping(value = "/code/attach/{cropTraitCodeId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Remove attached file", summary = "Remove file")
public CropTraitCodeAttach removeTraitCodeFile(@PathVariable(name = "cropTraitCodeId") final Long cropTraitCodeId, @PathVariable(name = "attachmentId") final Long attachmentId) {
return cropTraitCodeAttachmentService.removeFile(crudService.getTraitCode(cropTraitCodeId), attachmentId);
return cropTraitCodeAttachmentService.removeFile(cropTraitService.getTraitCode(cropTraitCodeId), attachmentId);
}
}
......@@ -22,6 +22,8 @@ import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
* The Class CooperatorLang.
......@@ -41,10 +43,13 @@ public abstract class CooperatorOwnedLang<T extends CooperatorOwnedModel> extend
@Lob
protected String description;
@NotNull
@Size(min = 1, max = 500)
@Basic
@Column(nullable = false, length = 500)
protected String title;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "sys_lang_id", nullable = false)
protected SysLang sysLang;
......
......@@ -16,6 +16,8 @@
package org.gringlobal.model;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
......@@ -41,14 +43,18 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
@Column(name = "category_code", nullable = false, length = 20)
private String categoryCode;
@NotNull
@Size(min = 1, max = 30)
@Basic
@Column(name = "coded_name", nullable = false, length = 30)
private String codedName;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "crop_id", nullable = false)
private Crop crop;
@NotNull
@Basic
@CodeValueField("CROP_TRAIT_DATA_TYPE")
@Column(name = "data_type_code", nullable = false, length = 20)
......@@ -60,14 +66,20 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
@Column(name = "crop_trait_id")
private Long id;
@NotNull
@Size(min = 1, max = 1)
@Basic
@Column(name = "is_archived", nullable = false, length = 1)
private String isArchived;
@NotNull
@Size(min = 1, max = 1)
@Basic
@Column(name = "is_coded", nullable = false, length = 1)
private String isCoded;
@NotNull
@Size(min = 1, max = 1)
@Basic
@Column(name = "is_peer_reviewed", nullable = false, length = 1)
private String isPeerReviewed;
......@@ -81,6 +93,7 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
@Lob
private String note;
@Size(max = 15)
@Basic
@Column(name = "numeric_format", length = 15)
private String numericFormat;
......@@ -93,10 +106,12 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
@Column(name = "numeric_minimum")
private Double numericMinimum;
@Size(max = 300)
@Basic
@Column(name = "ontology_url", length = 300)
private String ontologyUrl;
@Size(max = 50)
@Basic
@Column(name = "original_value_format", length = 50)
private String originalValueFormat;
......@@ -106,10 +121,14 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
@Column(name = "original_value_type_code", length = 20)
private String originalValueTypeCode;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "entity")
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "entity")
@JsonIgnore
private List<CropTraitLang> cropTraitLangs;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "cropTrait")
@JsonIgnore
private List<CropTraitCode> codes;
public CropTrait() {
}
......@@ -252,4 +271,21 @@ public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrai
public void setCropTraitLangs(final List<CropTraitLang> cropTraitLangs) {
this.cropTraitLangs = cropTraitLangs;
}
public List<CropTraitCode> getCodes() {
return codes;
}
public void setCodes(final List<CropTraitCode> codes) {
this.codes = codes;
}
@Override
public CropTrait lazyLoad() {
super.lazyLoad();
if (this.crop != null) {
this.crop.getId();
}
return this;
}
}
\ No newline at end of file
......@@ -18,6 +18,8 @@ package org.gringlobal.model;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
......@@ -34,6 +36,8 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
public class CropTraitCode extends CooperatorOwnedModel {
private static final long serialVersionUID = 3419091039669933935L;
@NotNull
@Size(min = 1, max = 30)
@Basic
@Column(nullable = false, length = 30)
private String code;
......@@ -48,7 +52,7 @@ public class CropTraitCode extends CooperatorOwnedModel {
@Column(name = "crop_trait_code_id")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "entity")
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "entity")
@JsonIgnore
private List<CropTraitCodeLang> codeLangs;
......
......@@ -16,9 +16,9 @@
package org.gringlobal.service;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitCode;
......@@ -31,14 +31,29 @@ import org.springframework.data.domain.Pageable;
*/
public interface CropTraitService extends CRUDService<CropTrait> {
/**
* Fetch TraitCode by ID
*
* @param id the id
* @return the entity
*/
CropTraitCode getTraitCode(long id);
TranslatedCropTraitCode getTranslatedTraitCode(long id);
List<TranslatedCropTraitCode> listTranslatedCodesByTrait(long traitId);
/** List translated CropTrait records matching the filter.
*
* @param filter the CropTraitFilter
* @param page the page
* @return the list of matches
*/
Page<TranslatedCropTrait> list(CropTraitFilter filter, Pageable page);
/**
* Fetch translated CropTrait by ID
* including its translated CropTraitCodes.
*
* @param id the id
* @return found CropTrait with its CropTraitCodes
*/
TranslatedCropTrait getTranslatedTrait(long id);
......@@ -47,9 +62,9 @@ public interface CropTraitService extends CRUDService<CropTrait> {
@JsonUnwrapped
public CropTrait cropTrait;
public String title;
public String description;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List<TranslatedCropTraitCode> codes;
public static TranslatedCropTrait from(CropTrait cropTrait, String title, String description, List<TranslatedCropTraitCode> codes) {
......@@ -57,7 +72,7 @@ public interface CropTraitService extends CRUDService<CropTrait> {
translatedCropTrait.cropTrait = cropTrait;
translatedCropTrait.title = title;
translatedCropTrait.description = description;
translatedCropTrait.codes = new ArrayList<>(codes);
translatedCropTrait.codes = codes;
return translatedCropTrait;
}
}
......@@ -67,7 +82,6 @@ public interface CropTraitService extends CRUDService<CropTrait> {
@JsonUnwrapped
public CropTraitCode cropTraitCode;
public String title;
public String description;
......
......@@ -18,7 +18,6 @@ package org.gringlobal.service.impl;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.querydsl.core.BooleanBuilder;
......@@ -29,7 +28,6 @@ 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 org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.NotFoundElement;
......@@ -52,7 +50,9 @@ import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.CropTraitCodeRepository;
import org.gringlobal.persistence.CropTraitRepository;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitAttachmentService.CropTraitAttachmentRequest;
import org.gringlobal.service.CropTraitCodeAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService.CropTraitCodeAttachmentRequest;
import org.gringlobal.service.CropTraitService;
import org.gringlobal.service.LanguageService;
import org.gringlobal.service.filter.CropTraitFilter;
......@@ -80,7 +80,7 @@ import org.springframework.validation.annotation.Validated;
public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRepository> implements CropTraitService {
private final static Logger LOG = LoggerFactory.getLogger(CropTraitServiceImpl.class);
private final static SysLang DEFAULT_LANGUAGE = new SysLang(1L);
private final static SysLang DEFAULT_LANGUAGE = new SysLang(1L); // English
@Autowired
private CropTraitCodeRepository traitCodeRepository;
......@@ -92,7 +92,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
private JPAQueryFactory jpaQueryFactory;
@Component(value = "cropTraitAttachmentSupport")
protected static class CropTraitAttachmentSupport extends BaseAttachmentSupport<CropTrait, CropTraitAttach, CropTraitAttachmentService.CropTraitAttachmentRequest> implements CropTraitAttachmentService {
protected static class CropTraitAttachmentSupport extends BaseAttachmentSupport<CropTrait, CropTraitAttach, CropTraitAttachmentRequest> implements CropTraitAttachmentService {
public CropTraitAttachmentSupport() {
super(QCropTraitAttach.cropTraitAttach.cropTrait.id, QCropTraitAttach.cropTraitAttach.id);
......@@ -118,7 +118,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
}
@Component(value = "cropTraitCodeAttachmentSupport")
protected static class CropTraitCodeAttachmentSupport extends BaseAttachmentSupport<CropTraitCode, CropTraitCodeAttach, CropTraitCodeAttachmentService.CropTraitCodeAttachmentRequest> implements CropTraitCodeAttachmentService {
protected static class CropTraitCodeAttachmentSupport extends BaseAttachmentSupport<CropTraitCode, CropTraitCodeAttach, CropTraitCodeAttachmentRequest> implements CropTraitCodeAttachmentService {
public CropTraitCodeAttachmentSupport() {
super(QCropTraitCodeAttach.cropTraitCodeAttach.cropTraitCode.id, QCropTraitCodeAttach.cropTraitCodeAttach.id);
......@@ -146,31 +146,10 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
}
@Override
public CropTraitCode getTraitCode(long id) {
public CropTraitCode getTraitCode(final long id) {
return traitCodeRepository.findById(id).orElseThrow(() -> new NotFoundElement("No record with id=" + id));
}
@Override
public TranslatedCropTraitCode getTranslatedTraitCode(long id) {
CropTraitCode traitCode = getTraitCode(id);
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
var codes = translateCropTraitCodes(QCropTraitCode.cropTraitCode.eq(traitCode), targetLanguage);
if (CollectionUtils.isEmpty(codes)) {
return TranslatedCropTraitCode.from(traitCode, null, null);
}
return TranslatedCropTraitCode.from(traitCode, codes.get(0).title, codes.get(0).description);
}
@Override
public List<TranslatedCropTraitCode> listTranslatedCodesByTrait(long traitId) {
CropTrait cropTrait = get(traitId);
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
List<CropTraitCodeTranslation> codes = translateCropTraitCodes(QCropTraitCode.cropTraitCode.cropTrait.eq(cropTrait), targetLanguage);
return codes.stream().map(t -> TranslatedCropTraitCode.from(t.entity, t.title, t.description)).collect(Collectors.toList());
}
@Override
public Page<TranslatedCropTrait> list(CropTraitFilter filter, Pageable page) {
BooleanBuilder predicate = new BooleanBuilder();
......@@ -179,26 +158,19 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
}
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
List<CropTraitTranslation> cropTraitsTranslation = translateCropTraits(predicate, page, targetLanguage);
Set<CropTrait> cropTraits = cropTraitsTranslation.stream().map(e -> e.entity).collect(Collectors.toSet());
List<TranslatedCropTraitCode> translatedCodes = translateCropTraitCodes(QCropTraitCode.cropTraitCode.cropTrait.in(cropTraits), targetLanguage)
.stream().map(e -> TranslatedCropTraitCode.from(e.entity, e.title, e.description)).collect(Collectors.toList());
Page<CropTraitTranslation> cropTraitsTranslation = fetchTranslationsOfTraits(predicate, page, targetLanguage);
List<TranslatedCropTrait> content = cropTraitsTranslation.stream().map(t -> TranslatedCropTrait.from(t.entity, t.title, t.description, null)).collect(Collectors.toList());
List<TranslatedCropTrait> content = cropTraitsTranslation.stream()
.map(t -> TranslatedCropTrait.from(t.entity, t.title, t.description, translatedCodes.stream().filter(c -> c.cropTraitCode.getCropTrait().equals(t.entity)).collect(Collectors.toList())))
.collect(Collectors.toList());
return new PageImpl<TranslatedCropTrait>(content, page, content.size());
return new PageImpl<TranslatedCropTrait>(content, page, cropTraitsTranslation.getTotalElements());
}
@Override
public TranslatedCropTrait getTranslatedTrait(long id) {
CropTrait cropTrait = get(id);
public TranslatedCropTrait getTranslatedTrait(final long id) {
CropTrait cropTrait = load(id);
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
CropTraitTranslation cropTraitTranslation = translateCropTraits(QCropTrait.cropTrait.eq(cropTrait), PageRequest.of(0, 1), targetLanguage).get(0);
List<TranslatedCropTraitCode> codes = translateCropTraitCodes(QCropTraitCode.cropTraitCode.cropTrait.eq(cropTrait), targetLanguage)
CropTraitTranslation cropTraitTranslation = fetchTranslationsOfTraits(QCropTrait.cropTrait.eq(cropTrait), PageRequest.of(0, 1), targetLanguage).getContent().get(0);
List<TranslatedCropTraitCode> codes = fetchTranslationsOfCodes(cropTrait, targetLanguage)
.stream().map(e -> TranslatedCropTraitCode.from(e.entity, e.title, e.description)).collect(Collectors.toList());
return TranslatedCropTrait.from(cropTrait, cropTraitTranslation.title, cropTraitTranslation.description, codes);
......@@ -275,9 +247,9 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
}
}
private List<CropTraitCodeTranslation> translateCropTraitCodes(Predicate filter, SysLang targetLanguage) {
var def = new QCropTraitCodeLang("def");
var lan = new QCropTraitCodeLang("lan");
private List<CropTraitCodeTranslation> fetchTranslationsOfCodes(final CropTrait cropTrait, final SysLang targetLanguage) {
var def = new QCropTraitCodeLang("def"); // alias
var lan = new QCropTraitCodeLang("lan"); // alias
List<Tuple> result = jpaQueryFactory.from(QCropTraitCode.cropTraitCode)
.select(QCropTraitCode.cropTraitCode, def, lan)
......@@ -286,15 +258,15 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
// Target language
.leftJoin(QCropTraitCode.cropTraitCode.codeLangs, lan).on(lan.sysLang.id.eq(targetLanguage.getId()))
// Filters
.where(filter)
.where(QCropTraitCode.cropTraitCode.cropTrait.eq(cropTrait))
.fetch();
return result.stream().map(CropTraitCodeTranslation::new).collect(Collectors.toList());
}
private List<CropTraitTranslation> translateCropTraits(Predicate filter, Pageable page, SysLang targetLanguage) {
var def = new QCropTraitLang("def");
var lan = new QCropTraitLang("lan");
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
JPAQuery<Tuple> query = jpaQueryFactory.from(QCropTrait.cropTrait)
.select(QCropTrait.cropTrait, def, lan)
......@@ -307,14 +279,15 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
.offset(page.getOffset())
.limit(page.getPageSize());
var totalElements = query.fetchCount();
for (Sort.Order o : page.getSort()) {
var orderByExpression = new PathBuilder(Object.class, QCropTrait.cropTrait.getMetadata());
var orderByExpression = new PathBuilder<>(CropTrait.class, QCropTrait.cropTrait.getMetadata());
query.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, orderByExpression.get(o.getProperty())));
}
List<Tuple> result = query.fetch();
return result.stream().map(CropTraitTranslation::new).collect(Collectors.toList());
var content = query.fetch().stream().map(CropTraitTranslation::new).collect(Collectors.toList());
return new PageImpl<CropTraitTranslation>(content, page, totalElements);
}
}
......@@ -256,7 +256,7 @@ public class CropControllerTest extends AbstractApiV1Test {
.param("term", term)
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
......
/*
* 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.test.service;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.CropRepository;
import org.gringlobal.persistence.CropTraitCodeLangRepository;
import org.gringlobal.persistence.CropTraitCodeRepository;
import org.gringlobal.persistence.CropTraitLangRepository;
import org.gringlobal.persistence.CropTraitRepository;
import org.gringlobal.service.CropService;
import org.gringlobal.service.CropTraitService;
import org.gringlobal.service.filter.CropTraitFilter;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;