Commit 8a1e9aad authored by Matija Obreza's avatar Matija Obreza

Improved JSON serialization for ES and API responses

- Removed unnecessary fields from JSON
- Removed _permissions from ES
parent a4a79186
......@@ -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) {
......
......@@ -16,13 +16,18 @@
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
......@@ -45,6 +50,13 @@ public class ElasticJacksonAnnotationIntrospector extends JacksonAnnotationIntro
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;
......
......@@ -47,6 +47,7 @@ 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;
......@@ -104,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;
/**
......
......@@ -155,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;
......@@ -381,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;
......
......@@ -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