Commit c3fe2afa authored by Matija Obreza's avatar Matija Obreza

Merge branch 'smarter-JSON-serialization-for-ui' into 'master'

Smarter json serialization for ui

See merge request genesys-pgr/genesys-server!419
parents 6fad525c 8a1e9aad
......@@ -45,8 +45,8 @@ import java.util.Set;
@Entity
@Table(name = "partner")
@Cacheable
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
@Document(indexName = "partner")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class Partner extends UuidModel implements SelfCleaning, AclAwareModel {
private static final ClassAclOid<Partner> PARENT_OID = ClassAclOid.forClass(Partner.class);
......
......@@ -46,6 +46,8 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
......@@ -64,6 +66,7 @@ import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* {@link Dataset} captures the metadata information as a snapshot. Multiple
......@@ -78,6 +81,7 @@ import com.fasterxml.jackson.annotation.JsonView;
@Cacheable
@Audited
@Document(indexName = "dataset")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class Dataset extends UuidModel implements Publishable, SelfCleaning, AclAwareModel {
/** The Constant serialVersionUID. */
......
......@@ -17,6 +17,8 @@ package org.genesys.catalog.model.dataset;
import org.genesys.blocks.model.UuidModel;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
......@@ -44,6 +46,7 @@ public class DatasetVersions extends UuidModel {
/** The all versions. */
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "versions", orphanRemoval = true)
@JsonIdentityReference(alwaysAsId = true)
private List<Dataset> allVersions;
/**
......
......@@ -46,8 +46,8 @@ import java.util.List;
@Cacheable
@Table(name = "descriptor")
@Audited
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
@Document(indexName = "descriptor")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class Descriptor extends UuidModel implements SelfCleaning, Publishable, Copyable<Descriptor>, AclAwareModel {
/** The Constant serialVersionUID. */
......
......@@ -47,8 +47,8 @@ import java.util.Map;
@Table(name="descriptorlist")
@Audited
@Cacheable
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
@Document(indexName = "descriptorlist")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class DescriptorList extends UuidModel implements Publishable, SelfCleaning, AclAwareModel {
/** The Constant serialVersionUID. */
......
......@@ -188,7 +188,7 @@ public class AccessionController {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<AccessionFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, accessionService.list(filterInfo.filter, page.toPageRequest(25, Sort.Direction.ASC, "seqNo")));
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, accessionService.list(filterInfo.filter, page.toPageRequest(50, Sort.Direction.ASC, "seqNo")));
}
/**
* List accessions by filterCode or filter
......@@ -200,7 +200,7 @@ public class AccessionController {
* @throws IOException
* @throws SearchException
*/
@JsonView({ JsonViews.Root.class }) // same as getAccessionDetails
@JsonView({ JsonViews.Root.class }) // same as getAccessionDetails so we get imageGallery!
@PostMapping(value = "/images", produces = { MediaType.APPLICATION_JSON_VALUE })
public AccessionSuggestionPage<AccessionService.AccessionDetails> images(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) AccessionFilter filter) throws IOException, SearchException {
......@@ -230,8 +230,8 @@ public class AccessionController {
* @throws IOException
*/
@PostMapping(value = "/filter", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Internal.class })
public AccessionSuggestionPage<Accession> listSuggestions(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@JsonView({ JsonViews.Public.class })
public AccessionSuggestionPage<Accession> filter(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) AccessionFilter filter) throws IOException, SearchException {
if (filterCode != null) {
......@@ -241,7 +241,7 @@ public class AccessionController {
}
FilterInfo<AccessionFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
FilteredPage<Accession> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, accessionService.list(filterInfo.filter, page.toPageRequest(25, Sort.Direction.ASC, "seqNo")));
FilteredPage<Accession> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, accessionService.list(filterInfo.filter, page.toPageRequest(50, Sort.Direction.ASC, "seqNo")));
Map<String, TermResult> suggestionRes = accessionService.getSuggestions(filter);
return new AccessionSuggestionPage<Accession>(pageRes, suggestionRes);
......
......@@ -15,11 +15,19 @@
*/
package org.genesys2.server.component.elastic;
import java.lang.annotation.Annotation;
import java.util.List;
import org.genesys.blocks.security.serialization.CurrentPermissionsWriter;
import org.genesys.custom.elasticsearch.IgnoreField;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
/**
* Extra controls to what gets serialized to JSON before sending data over to
......@@ -33,6 +41,22 @@ import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
public class ElasticJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
private static final long serialVersionUID = 6225410633030301962L;
@Override
protected boolean _hasAnnotation(Annotated annotated, Class<? extends Annotation> annoClass) {
if (JsonIdentityInfo.class.isAssignableFrom(annoClass)) {
// Completely ignore JsonIdentityInfo annotations
return false;
}
return super._hasAnnotation(annotated, annoClass);
}
@Override
public void findAndAddVirtualProperties(MapperConfig<?> config, AnnotatedClass ac, List<BeanPropertyWriter> properties) {
super.findAndAddVirtualProperties(config, ac, properties);
// Remove current permissions writer
properties.removeIf(bpw -> bpw instanceof CurrentPermissionsWriter);
}
/**
* Ignore properties where @Fileld(index = no)
*/
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.model.genesys;
import java.beans.Transient;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -178,6 +179,7 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
this.usedBy = usedBy;
}
@Transient
public boolean isEmpty() {
return StringUtils.isBlank(name) || ! HAS_WORD_CHARACTER.matcher(name).find();
}
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.model.genesys;
import java.beans.Transient;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -196,6 +197,7 @@ public class AccessionCollect extends BasicModel implements AccessionRelated, Se
this.collSrc = collSrc;
}
@Transient
public boolean isEmpty() {
if (StringUtils.isNotBlank(collDate)) {
return false;
......
......@@ -94,7 +94,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@JoinColumn(name = "instituteId")
@JsonView({ JsonViews.Minimal.class })
@Field(type = FieldType.Object)
@JsonIgnoreProperties({"settings"})
@JsonIgnoreProperties({"settings", "pdciHistogram", "statisticsPDCI"})
@QueryInit({ "country.*", "owner.*"})
private FaoInstitute institute;
......
......@@ -38,16 +38,19 @@ import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Cacheable
@Entity
// Add index on all fields
@Table(name = "taxonomy2", uniqueConstraints = { @UniqueConstraint(name="UK_taxonomy2", columnNames = { "genus", "species", "spAuthor", "subtaxa", "subtAuthor" }) })
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Taxonomy2 extends GlobalVersionedAuditedModel {
private static final long serialVersionUID = 8881324404490162933L;
......
......@@ -43,8 +43,10 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -52,6 +54,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "country")
@Document(indexName = "country")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Country extends BasicModel {
private static final long serialVersionUID = -1688723909298769804L;
......
......@@ -47,14 +47,17 @@ import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.hibernate.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.annotations.Document;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -62,6 +65,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "crop")
@Document(indexName = "crop")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Crop extends GlobalVersionedAuditedModel implements SelfCleaning, AclAwareModel {
private static final long serialVersionUID = -2686341831839109257L;
......@@ -101,6 +105,7 @@ public class Crop extends GlobalVersionedAuditedModel implements SelfCleaning, A
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Internal.class })
@IgnoreField
private String i18n;
/**
......
......@@ -66,6 +66,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") })
@Document(indexName = "faoinstitute")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class FaoInstitute extends BasicModel implements GeoReferencedEntity, AclAwareModel, EntityId {
private static final ClassAclOid<FaoInstitute> DEFAULT_PARENT_OID = ClassAclOid.forClass(FaoInstitute.class);
......@@ -154,8 +155,10 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private Double pdciMin;
private Double pdciMax;
private Double pdciAvg;
@Column(name = "pdciHistogram")
@JsonProperty(access = Access.READ_ONLY)
@IgnoreField
private String pdciHistogram;
......@@ -380,6 +383,7 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
this.owner = owner;
}
@IgnoreField
public PDCIStatistics getStatisticsPDCI() {
if (this.pdciMin == null || this.pdciMax == null || this.pdciAvg == null) {
return null;
......
......@@ -43,6 +43,7 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
......@@ -59,6 +60,7 @@ import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* The Class Subset.
......@@ -67,6 +69,7 @@ import com.fasterxml.jackson.annotation.JsonView;
@Table(name = "subset", uniqueConstraints = @UniqueConstraint(name = "UQ_current_subs_version", columnNames={"versionsId", "current"}))
@Audited
@Document(indexName = "subset")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
/** The Constant serialVersionUID. */
......
......@@ -139,7 +139,7 @@ public class GeoControllerTest extends AbstractApiTest {
.andExpect(jsonPath("$.name", is(geoRegion.getName())))
.andExpect(jsonPath("$.isoCode", is(geoRegion.getIsoCode())))
.andExpect(jsonPath("$.parentRegion.isoCode", is(geoRegion.getParentRegion().getIsoCode())))
.andExpect(jsonPath("$.countries[0].code3", is(country.getCode3())))
.andExpect(jsonPath("$.countries[0]", notNullValue())) // can be country.code3 or only the id!
.andExpect(jsonPath("$.subRegions", hasSize(0)));
}
......
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