Commit eae5a6ea authored by Matija Obreza's avatar Matija Obreza

Merge branch '282-accession-apiv1-filter' into 'master'

Resolve "Accession APIv1 /filter"

Closes #282

See merge request genesys-pgr/genesys-server!159
parents 8038a9bf 691c5437
/*
* Copyright 2018 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.genesys2.server.api.v1;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import io.swagger.annotations.Api;
/**
* Accession API v1
*/
@RestController("accessionApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(AccessionController.API_BASE)
@Api(tags = { "accession" })
public class AccessionController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/acn";
@Autowired
private AccessionService accessionService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/{UUID}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByUuid(@PathVariable("UUID") final UUID uuid) {
return accessionService.getByUuid(uuid);
}
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/10.{dummy:[0-9]+}/**}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByDoi(HttpServletRequest request) {
final String fullpath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
final String doi = fullpath.substring(1);
return accessionService.getByDoi(doi);
}
/**
* List accessions by filterCode or filter
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<Accession> list(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort,
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) AccessionFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, accessionService.list(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
}
...@@ -28,6 +28,7 @@ import org.genesys.blocks.security.model.AclObjectIdentity; ...@@ -28,6 +28,7 @@ import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.blocks.security.model.AclSid; import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.serialization.SidPermissions; import org.genesys.blocks.security.serialization.SidPermissions;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.UserRole; import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User; import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.UserService; import org.genesys2.server.service.UserService;
...@@ -55,10 +56,12 @@ import io.swagger.annotations.Api; ...@@ -55,10 +56,12 @@ import io.swagger.annotations.Api;
* @author Matija Obreza * @author Matija Obreza
*/ */
@RestController("permissionApi1") @RestController("permissionApi1")
@RequestMapping(value = { "/api/v1/permission" }) @RequestMapping(value = PermissionController.API_BASE )
@Api(tags = { "permission" }) @Api(tags = { "permission" })
public class PermissionController { public class PermissionController {
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/permission";
private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class); private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class);
/** The acl service. */ /** The acl service. */
......
...@@ -22,6 +22,7 @@ import java.util.UUID; ...@@ -22,6 +22,7 @@ import java.util.UUID;
import org.genesys.catalog.api.FilteredPage; import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService; import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.impl.Subset; import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.SubsetService; import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter; import org.genesys2.server.service.filter.SubsetFilter;
...@@ -53,7 +54,7 @@ import io.swagger.annotations.Api; ...@@ -53,7 +54,7 @@ import io.swagger.annotations.Api;
public class SubsetController { public class SubsetController {
/** The Constant API_BASE. */ /** The Constant API_BASE. */
public static final String API_BASE = "/api/v1/subset"; public static final String API_BASE = ApiBaseController.APIv1_BASE + "/subset";
@Autowired @Autowired
private SubsetService subsetService; private SubsetService subsetService;
......
...@@ -36,6 +36,7 @@ import javax.persistence.Transient; ...@@ -36,6 +36,7 @@ import javax.persistence.Transient;
import org.genesys.blocks.model.AuditedVersionedModel; import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID; import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning; import org.genesys.blocks.model.SelfCleaning;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
...@@ -45,8 +46,8 @@ import org.genesys2.util.NumberUtils; ...@@ -45,8 +46,8 @@ import org.genesys2.util.NumberUtils;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
@MappedSuperclass @MappedSuperclass
public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, Serializable, SelfCleaning { public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, Serializable, SelfCleaning {
...@@ -70,6 +71,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -70,6 +71,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "instituteId") @JoinColumn(name = "instituteId")
@JsonView({ JsonViews.Minimal.class })
private FaoInstitute institute; private FaoInstitute institute;
@Column(name = "acceNumb", nullable = false, length = 128) @Column(name = "acceNumb", nullable = false, length = 128)
...@@ -87,7 +89,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -87,7 +89,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "taxonomyId2") @JoinColumn(name = "taxonomyId2")
@JsonIgnoreProperties(value = { "id", "version" }) @JsonView({ JsonViews.Minimal.class })
private Taxonomy2 taxonomy; private Taxonomy2 taxonomy;
// FIXME: Remove? // FIXME: Remove?
......
...@@ -27,17 +27,16 @@ import javax.persistence.Lob; ...@@ -27,17 +27,16 @@ import javax.persistence.Lob;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.GlobalVersionedAuditedModel; import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.model.impl.CropTaxonomy; import org.genesys2.server.model.impl.CropTaxonomy;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Cacheable @Cacheable
@Entity @Entity
// Add index on all fields // Add index on all fields
...@@ -124,8 +123,6 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel { ...@@ -124,8 +123,6 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
return sb.toString(); return sb.toString();
} }
@Transient
public String getTaxonNameHtml() { public String getTaxonNameHtml() {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append("<i>").append(genus).append(" ").append(species).append("</i>"); sb.append("<i>").append(genus).append(" ").append(species).append("</i>");
......
...@@ -23,20 +23,27 @@ import java.util.Comparator; ...@@ -23,20 +23,27 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.persistence.*; import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.genesys.blocks.model.BasicModel; import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.api.serialization.CountrySerializer; import org.genesys.blocks.model.JsonViews;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable @Cacheable
@Entity @Entity
@Table(name = "country") @Table(name = "country")
@JsonSerialize(using = CountrySerializer.class)
public class Country extends BasicModel { public class Country extends BasicModel {
private static final long serialVersionUID = -1688723909298769804L; private static final long serialVersionUID = -1688723909298769804L;
...@@ -45,6 +52,7 @@ public class Country extends BasicModel { ...@@ -45,6 +52,7 @@ public class Country extends BasicModel {
private String code3; private String code3;
@Column(unique = false, length = 2) @Column(unique = false, length = 2)
@JsonView({ JsonViews.Public.class } )
private String code2; private String code2;
private boolean current; private boolean current;
...@@ -52,23 +60,31 @@ public class Country extends BasicModel { ...@@ -52,23 +60,31 @@ public class Country extends BasicModel {
private String name; private String name;
@Column(length = 3) @Column(length = 3)
@JsonView({ JsonViews.Public.class } )
private String codeNum; private String codeNum;
@JsonView({ JsonViews.Public.class } )
private Long refnameId; private Long refnameId;
@ManyToOne(fetch = FetchType.LAZY, optional = true) @ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "regionId") @JoinColumn(name = "regionId")
@JsonView({ JsonViews.Public.class } )
private GeoRegion region; private GeoRegion region;
@Column(name = "minLongitude") @Column(name = "minLongitude")
@JsonView({ JsonViews.Public.class } )
private Double minLongitude; private Double minLongitude;
@Column(name = "minLatitude") @Column(name = "minLatitude")
@JsonView({ JsonViews.Public.class } )
private Double minLatitude; private Double minLatitude;
@Column(name = "maxLongitude") @Column(name = "maxLongitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLongitude; private Double maxLongitude;
@Column(name = "maxLatitude") @Column(name = "maxLatitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLatitude; private Double maxLatitude;
/** /**
...@@ -76,17 +92,21 @@ public class Country extends BasicModel { ...@@ -76,17 +92,21 @@ public class Country extends BasicModel {
*/ */
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class })
private String nameL; private String nameL;
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class } )
private String wikiLink; private String wikiLink;
@Transient @Transient
@JsonView({ JsonViews.Protected.class })
private JsonNode nameJ; private JsonNode nameJ;
@ManyToOne(cascade = {}, optional = true) @ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "replacedBy") @JoinColumn(name = "replacedBy")
@JsonView({ JsonViews.Public.class })
private Country replacedBy; private Country replacedBy;
public Country() { public Country() {
......
...@@ -35,19 +35,17 @@ import javax.persistence.OneToMany; ...@@ -35,19 +35,17 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.BasicModel; import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.EntityId; import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.model.AclAwareModel; import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys2.server.api.serialization.FaoInstituteSerializer;
import com.fasterxml.jackson.annotation.JsonView;
@Cacheable @Cacheable
@Entity @Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") }) @Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") })
@JsonSerialize(using = FaoInstituteSerializer.class)
public class FaoInstitute extends BasicModel implements GeoReferencedEntity, AclAwareModel, EntityId { public class FaoInstitute extends BasicModel implements GeoReferencedEntity, AclAwareModel, EntityId {
private static final long serialVersionUID = -8773002513838748431L; private static final long serialVersionUID = -8773002513838748431L;
...@@ -61,12 +59,15 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl ...@@ -61,12 +59,15 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String fullName; private String fullName;
@Column(length = 100) @Column(length = 100)
@JsonView({ JsonViews.Public.class })
private String type; private String type;
@Column(length = 300) @Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String url; private String url;
@Column(length = 300) @Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String email; private String email;
@Column(length = LEN_ACRONYM) @Column(length = LEN_ACRONYM)
...@@ -76,16 +77,17 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl ...@@ -76,16 +77,17 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String vCode; private String vCode;
@Column(name = "codeSGSV") @Column(name = "codeSGSV")
@JsonView({ JsonViews.Public.class })
private String codeSGSV; private String codeSGSV;
private boolean current; private boolean current;
@JsonIgnore
@ManyToOne(cascade = {}, optional = true) @ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "countryId") @JoinColumn(name = "countryId")
@JsonView({ JsonViews.Minimal.class })
private Country country; private Country country;
@JsonIgnore @JsonView({ JsonViews.Protected.class })
@OneToMany(cascade = {}) @OneToMany(cascade = {})
@JoinColumn(referencedColumnName = "code", name = "instCode") @JoinColumn(referencedColumnName = "code", name = "instCode")
@MapKey(name = "setting") @MapKey(name = "setting")
......
...@@ -17,9 +17,12 @@ import javax.persistence.OneToMany; ...@@ -17,9 +17,12 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.VersionedModel; import org.genesys.blocks.model.VersionedModel;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -38,16 +41,20 @@ public class GeoRegion extends VersionedModel { ...@@ -38,16 +41,20 @@ public class GeoRegion extends VersionedModel {
@ManyToOne() @ManyToOne()
@JoinColumn(name = "parentId") @JoinColumn(name = "parentId")
@JsonView({ JsonViews.Public.class })
private GeoRegion parentRegion; private GeoRegion parentRegion;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "region") @OneToMany(fetch = FetchType.LAZY, mappedBy = "region")
@JsonView({ JsonViews.Public.class })
private List<Country> countries; private List<Country> countries;
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonIgnore
private String nameL; private String nameL;
@Transient @Transient
@JsonIgnore
private JsonNode nameJ; private JsonNode nameJ;
public String getIsoCode() { public String getIsoCode() {
......
...@@ -33,9 +33,10 @@ import org.springframework.data.domain.Sort; ...@@ -33,9 +33,10 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom { public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom, QueryDslPredicateExecutor<Accession> {
@Query("select a.id from Accession a") @Query("select a.id from Accession a")
public List<Long> listAccessionsIds(Pageable pageable); public List<Long> listAccessionsIds(Pageable pageable);
......