Commit acc3be4b authored by Maxym Borodenko's avatar Maxym Borodenko

Merge branch 'ui-116-crops-module' into 'master'

Crop details and /species/{cropId}

See merge request !113
parents 2f211902 4bbd114b
......@@ -15,10 +15,9 @@
*/
package org.gringlobal.api.v1.impl;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.v1.ApiBaseController;
......@@ -29,13 +28,17 @@ import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
import org.gringlobal.model.QCrop;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.service.CropAttachmentService;
import org.gringlobal.service.CropService;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestBody;
......@@ -45,7 +48,11 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@RestController("cropApi1")
@RequestMapping(CropController.API_URL)
......@@ -69,6 +76,18 @@ public class CropController extends FilteredCRUDController<Crop, CropService, Cr
return new OrderSpecifier[] { QCrop.crop.id.asc() };
}
/**
* Retrieve crop details by id
*
* @param id the id
* @return the crop details
*/
@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "cropDetails", description = "Retrieve crop details by ID", summary = "Details")
public CropService.CropDetails details(@PathVariable("id") final long id) {
return crudService.getCropDetails(crudService.get(id));
}
@Override
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Operation(operationId = "createCrop", description = "Create a Crop", summary = "Add")
......@@ -120,4 +139,13 @@ public class CropController extends FilteredCRUDController<Crop, CropService, Cr
return super.filter(filterCode, page, filter);
}
@PostMapping(value = "/species/{cropId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "listCropSpecies", description = "List species of selected crop", summary = "List species")
public FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> listSpecies(@PathVariable("cropId") final long id, @Parameter(hidden = true) final Pagination page, @RequestBody(required = false) final TaxonomySpeciesFilter filter) throws IOException {
TaxonomySpeciesFilter normalizedFilter = shortFilterService.normalizeFilter(filter, TaxonomySpeciesFilter.class);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE);
FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> results = new FilteredPage<>(normalizedFilter, crudService.listSpecies(crudService.get(id), filter, pageable));
return results;
}
}
......@@ -15,21 +15,16 @@
*/
package org.gringlobal.model;
import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.genesys.blocks.model.Copyable;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
......@@ -61,6 +56,11 @@ public class Crop extends CooperatorOwnedModel implements Copyable<Crop> {
@Lob
private String note;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "crop")
@JsonIgnore
private List<CropAttach> attachments;
public Crop() {
}
......@@ -100,4 +100,12 @@ public class Crop extends CooperatorOwnedModel implements Copyable<Crop> {
public void setNote(final String note) {
this.note = note;
}
public List<CropAttach> getAttachments() {
return attachments;
}
public void setAttachments(List<CropAttach> attachments) {
this.attachments = attachments;
}
}
\ No newline at end of file
......@@ -15,10 +15,15 @@
*/
package org.gringlobal.service;
import org.gringlobal.model.Crop;
import org.gringlobal.model.TaxonomyCropMap;
import org.gringlobal.model.TaxonomySpecies;
import java.util.List;
import org.gringlobal.model.*;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
/**
* The Interface CropService.
......@@ -33,7 +38,6 @@ public interface CropService extends FilteredCRUDService<Crop, CropFilter> {
*/
Crop getCrop(String cropName);
/**
* Ensure crop taxonomy link.
*
......@@ -43,4 +47,31 @@ public interface CropService extends FilteredCRUDService<Crop, CropFilter> {
*/
TaxonomyCropMap ensureCropTaxonomyLink(Crop crop, TaxonomySpecies taxonomySpecies);
/**
* Gets the crop details.
*
* @param crop the crop
* @return the crop details
*/
CropDetails getCropDetails(Crop crop);
class CropDetails {
@JsonUnwrapped
public Crop crop;
public List<CropAttach> attachments;
}
/**
* List {@link TaxonomySpecies} associated with the crop.
*
* @param crop the crop
* @param filter the filter
* @param pageable the pageable
* @return the page
*/
Page<TaxonomySpecies> listSpecies(Crop crop, TaxonomySpeciesFilter filter, Pageable pageable);
}
......@@ -22,19 +22,24 @@ import java.nio.file.Paths;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.NotFoundElement;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
import org.gringlobal.model.QCropAttach;
import org.gringlobal.model.QTaxonomyCropMap;
import org.gringlobal.model.QTaxonomySpecies;
import org.gringlobal.model.TaxonomyCropMap;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.persistence.CropRepository;
import org.gringlobal.persistence.TaxonomyCropMapRepository;
import org.gringlobal.persistence.TaxonomySpeciesRepository;
import org.gringlobal.service.CropAttachmentService;
import org.gringlobal.service.CropAttachmentService.CropAttachmentRequest;
import org.gringlobal.service.CropService;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -45,6 +50,9 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.jpa.impl.JPAQuery;
/**
* The Class CropServiceImpl.
*/
......@@ -56,6 +64,9 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
@Autowired
private TaxonomyCropMapRepository taxonomyCropMapRepository;
@Autowired
private TaxonomySpeciesRepository taxonomySpeciesRepository;
@Component
protected static class AttachmentSupport extends BaseAttachmentSupport<Crop, CropAttach, CropAttachmentRequest> implements CropAttachmentService {
......@@ -95,6 +106,30 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
return repository.getByName(cropName);
}
@Override
public CropDetails getCropDetails(Crop crop) {
if (crop == null) {
throw new NotFoundElement();
}
crop = this.reload(crop);
// initialize lazy data
if (crop.getAttachments() != null) {
crop.getAttachments().size();
crop.getAttachments().forEach(a -> {
if (a.getRepositoryFile() != null)
a.getRepositoryFile().getId();
});
}
CropDetails cropDetails = new CropDetails();
cropDetails.crop = crop;
cropDetails.attachments = crop.getAttachments();
return cropDetails;
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
......@@ -151,4 +186,23 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
public Page<Crop> list(CropFilter filter, Pageable page) throws SearchException {
return super.list(Crop.class, filter, page);
}
/**
* List {@link TaxonomySpecies} associated with the crop.
*
* @param crop the crop
* @param filter the filter
* @param pageable the pageable
* @return the page
*/
@Override
public Page<TaxonomySpecies> listSpecies(Crop crop, TaxonomySpeciesFilter filter, Pageable pageable) {
QTaxonomySpecies species = new QTaxonomySpecies("taxonomySpecies"); // alias for filtering
JPAQuery<TaxonomySpecies> query = jpaQueryFactory.from(QTaxonomyCropMap.taxonomyCropMap).innerJoin(QTaxonomyCropMap.taxonomyCropMap.taxonomySpecies, species).select(species);
query.where(QTaxonomyCropMap.taxonomyCropMap.crop.eq(crop));
query.where(ExpressionUtils.allOf(filter.collectPredicates(species)));
return taxonomySpeciesRepository.findAll(query, pageable);
}
}
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