Commit 62ec5c84 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

WIP: Crop trait API

parent c89b2e84
......@@ -30,7 +30,7 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
*
* @param <T> the generic type
*/
public class FilteredPage<T, F extends SuperModelFilter<?, T>> implements Iterable<T>, Serializable {
public class FilteredPage<T, F extends SuperModelFilter<?, ?>> implements Iterable<T>, Serializable {
private static final long serialVersionUID = 6965069448240229428L;
......
/*
* 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.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 org.apache.commons.lang3.ArrayUtils;
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;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitAttach;
import org.gringlobal.model.CropTraitCodeAttach;
import org.gringlobal.model.QCropTrait;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService;
import org.gringlobal.service.CropTraitService;
import org.gringlobal.service.filter.CropTraitFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;
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> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv1_BASE + "/crop-trait";
protected Class<CropTraitFilter> filterType() {
return CropTraitFilter.class;
}
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QCropTrait.cropTrait.id.asc() };
}
@Autowired
@Qualifier(value = "cropTraitAttachmentSupport")
private CropTraitAttachmentService cropTraitAttachmentService;
@Autowired
@Qualifier(value = "cropTraitCodeAttachmentSupport")
private CropTraitCodeAttachmentService cropTraitCodeAttachmentService;
@GetMapping(value = "/translated/{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);
}
@PostMapping(value = "/translated/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);
}
@Override
public CropTrait remove(@PathVariable("id") final long id) {
return super.remove(id);
}
@Override
public CropTrait create(@RequestBody final CropTrait entity) {
return super.create(entity);
}
@Override
public CropTrait update(@RequestBody final CropTrait entity) {
return super.update(entity);
}
@PostMapping(value = "/attach/{cropTraitId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Attach crop trait file", summary = "Attach file")
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);
}
@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);
}
@PostMapping(value = "/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);
}
@DeleteMapping(value = "/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);
}
}
/*
* 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.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
/**
* The Class CooperatorLang.
*
* @author Maxym Borodenko
*/
@MappedSuperclass
public abstract class CooperatorLang extends CooperatorOwnedModel {
private static final long serialVersionUID = -8074420732594755502L;
@Basic
@Column
@Lob
protected String description;
@Basic
@Column(length = 500)
protected String title;
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
......@@ -17,9 +17,14 @@ package org.gringlobal.model;
import javax.persistence.*;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.custom.validation.javax.CodeValueField;
/**
* Auto-generated by:
......@@ -28,10 +33,11 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "crop_trait")
@JsonIdentityInfo(scope = CropTrait.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CropTrait extends CooperatorOwnedModel {
public class CropTrait extends CooperatorOwnedModel implements Copyable<CropTrait> {
private static final long serialVersionUID = -368513465504238634L;
@Basic
@CodeValueField("DESCRIPTOR_CATEGORY")
@Column(name = "category_code", nullable = false, length = 20)
private String categoryCode;
......@@ -44,6 +50,7 @@ public class CropTrait extends CooperatorOwnedModel {
private Crop crop;
@Basic
@CodeValueField("CROP_TRAIT_DATA_TYPE")
@Column(name = "data_type_code", nullable = false, length = 20)
private String dataTypeCode;
......@@ -95,9 +102,14 @@ public class CropTrait extends CooperatorOwnedModel {
private String originalValueFormat;
@Basic
@CodeValueField("CROP_TRAIT_DATA_TYPE")
@Column(name = "original_value_type_code", length = 20)
private String originalValueTypeCode;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "cropTrait")
@JsonIgnore
private List<CropTraitLang> cropTraitLangs;
public CropTrait() {
}
......@@ -232,4 +244,12 @@ public class CropTrait extends CooperatorOwnedModel {
public void setOriginalValueTypeCode(final String originalValueTypeCode) {
this.originalValueTypeCode = originalValueTypeCode;
}
public List<CropTraitLang> getCropTraitLangs() {
return cropTraitLangs;
}
public void setCropTraitLangs(final List<CropTraitLang> cropTraitLangs) {
this.cropTraitLangs = cropTraitLangs;
}
}
\ No newline at end of file
......@@ -22,6 +22,7 @@ import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
......@@ -32,7 +33,7 @@ import org.gringlobal.model.community.CommunityCodeValues;
@Entity
@Table(name = "crop_trait_attach")
@JsonIdentityInfo(scope = CropTraitAttach.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CropTraitAttach extends CooperatorAttachment {
public class CropTraitAttach extends CooperatorAttachment implements Copyable<CropTraitAttach> {
private static final long serialVersionUID = 8275278678243646534L;
@Id
......
......@@ -15,9 +15,12 @@
*/
package org.gringlobal.model;
import java.util.List;
import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
......@@ -45,6 +48,10 @@ public class CropTraitCode extends CooperatorOwnedModel {
@Column(name = "crop_trait_code_id")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "cropTraitCode")
@JsonIgnore
private List<CropTraitCodeLang> codeLangs;
public CropTraitCode() {
}
......@@ -75,4 +82,12 @@ public class CropTraitCode extends CooperatorOwnedModel {
public void setId(final Long id) {
this.id = id;
}
public List<CropTraitCodeLang> getCodeLangs() {
return codeLangs;
}
public void setCodeLangs(final List<CropTraitCodeLang> codeLangs) {
this.codeLangs = codeLangs;
}
}
\ No newline at end of file
......@@ -22,6 +22,7 @@ import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
......@@ -32,7 +33,7 @@ import org.gringlobal.model.community.CommunityCodeValues;
@Entity
@Table(name = "crop_trait_code_attach")
@JsonIdentityInfo(scope = CropTraitCodeAttach.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CropTraitCodeAttach extends CooperatorAttachment {
public class CropTraitCodeAttach extends CooperatorAttachment implements Copyable<CropTraitCodeAttach> {
private static final long serialVersionUID = 8125365963724877174L;
@Id
......
......@@ -28,18 +28,13 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "crop_trait_code_lang")
@JsonIdentityInfo(scope = CropTraitCodeLang.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CropTraitCodeLang extends CooperatorOwnedModel {
public class CropTraitCodeLang extends CooperatorLang {
private static final long serialVersionUID = 8734983132443264074L;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "crop_trait_code_id", nullable = false)
private CropTraitCode cropTraitCode;
@Basic
@Column
@Lob
private String description;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -50,10 +45,6 @@ public class CropTraitCodeLang extends CooperatorOwnedModel {
@JoinColumn(name = "sys_lang_id", nullable = false)
private SysLang sysLang;
@Basic
@Column(length = 500)
private String title;
public CropTraitCodeLang() {
}
......@@ -69,14 +60,6 @@ public class CropTraitCodeLang extends CooperatorOwnedModel {
this.cropTraitCode = cropTraitCode;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public Long getId() {
return id;
}
......@@ -92,12 +75,4 @@ public class CropTraitCodeLang extends CooperatorOwnedModel {
public void setSysLang(final SysLang sysLang) {
this.sysLang = sysLang;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
\ No newline at end of file
......@@ -28,18 +28,13 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "crop_trait_lang")
@JsonIdentityInfo(scope = CropTraitLang.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CropTraitLang extends CooperatorOwnedModel {
public class CropTraitLang extends CooperatorLang {
private static final long serialVersionUID = 1415634897733917322L;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "crop_trait_id", nullable = false)
private CropTrait cropTrait;
@Basic
@Column
@Lob
private String description;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -50,10 +45,6 @@ public class CropTraitLang extends CooperatorOwnedModel {
@JoinColumn(name = "sys_lang_id", nullable = false)
private SysLang sysLang;
@Basic
@Column(length = 500)
private String title;
public CropTraitLang() {
}
......@@ -69,14 +60,6 @@ public class CropTraitLang extends CooperatorOwnedModel {
this.cropTrait = cropTrait;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public Long getId() {
return id;
}
......@@ -93,11 +76,4 @@ public class CropTraitLang extends CooperatorOwnedModel {
this.sysLang = sysLang;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
\ No newline at end of file
......@@ -28,14 +28,9 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "source_descriptor_code_lang")
@JsonIdentityInfo(scope = SourceDescriptorCodeLang.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class SourceDescriptorCodeLang extends CooperatorOwnedModel {
public class SourceDescriptorCodeLang extends CooperatorLang {
private static final long serialVersionUID = -6651240212098239872L;
@Basic
@Column
@Lob
private String description;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -50,10 +45,6 @@ public class SourceDescriptorCodeLang extends CooperatorOwnedModel {
@JoinColumn(name = "sys_lang_id", nullable = false)
private SysLang sysLang;
@Basic
@Column(length = 500)
private String title;
public SourceDescriptorCodeLang() {
}
......@@ -61,14 +52,6 @@ public class SourceDescriptorCodeLang extends CooperatorOwnedModel {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public Long getId() {
return id;
}
......@@ -93,11 +76,4 @@ public class SourceDescriptorCodeLang extends CooperatorOwnedModel {
this.sysLang = sysLang;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
\ No newline at end of file
......@@ -28,14 +28,9 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "source_descriptor_lang")
@JsonIdentityInfo(scope = SourceDescriptorLang.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class SourceDescriptorLang extends CooperatorOwnedModel {
public class SourceDescriptorLang extends CooperatorLang {
private static final long serialVersionUID = 7116474808205086996L;
@Basic
@Column
@Lob
private String description;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -50,10 +45,6 @@ public class SourceDescriptorLang extends CooperatorOwnedModel {
@JoinColumn(name = "sys_lang_id", nullable = false)
private SysLang sysLang;
@Basic
@Column(length = 500)
private String title;
public SourceDescriptorLang() {
}
......@@ -61,14 +52,6 @@ public class SourceDescriptorLang extends CooperatorOwnedModel {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public Long getId() {
return id;
}
......@@ -93,11 +76,4 @@ public class SourceDescriptorLang extends CooperatorOwnedModel {
this.sysLang = sysLang;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
\ No newline at end of file
/*
* 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;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitAttach;
/**
* The Interface CropTraitAttachmentService.
*/
public interface CropTraitAttachmentService extends AttachmentService<CropTrait, CropTraitAttach, CropTraitAttachmentService.CropTraitAttachmentRequest> {
class CropTraitAttachmentRequest extends AttachmentService.AttachmentRequest<CropTraitAttach> {
}
}
/*
* 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;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeAttach;
/**
* The Interface CropTraitCodeAttachmentService.
*/