Skip to content

Commit

Permalink
Np 45284 person nvi data (#329)
Browse files Browse the repository at this point in the history
* np-45284: new field PersonNvi in GET Person

Test code for it's nested field verifiedBy

* np-45284: mapped verifiedAt field

* np-45284: mapped verifiedDate field
  • Loading branch information
torarnet authored Oct 18, 2023
1 parent 87a299c commit f7b1409
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package no.unit.nva.cristin.person.model.cristin;

import static no.unit.nva.cristin.model.JsonPropertyNames.INSTITUTION;
import static no.unit.nva.cristin.model.JsonPropertyNames.UNIT;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
import java.util.Set;
import no.unit.nva.cristin.model.CristinUnit;
import no.unit.nva.model.Organization;

public record CristinNviInstitutionUnit(@JsonProperty(INSTITUTION) CristinPersonNviInstitution institution,
@JsonProperty(UNIT) CristinUnit unit) {

public Organization toOrganization() {
var organization = Optional.ofNullable(unit).map(CristinUnit::toOrganization);

if (organization.isPresent()) {
var present = organization.get();
var builder = new Organization.Builder();

builder.withId(present.getId());
builder.withLabels(present.getLabels());
builder.withAcronym(present.getAcronym());
topLevel().ifPresent(top -> builder.withPartOf(convertToSet(top)));
builder.withCountry(present.getCountry());

return builder.build();
} else {
return null;
}
}

private Set<Organization> convertToSet(Organization organization) {
return Set.of(organization);
}

private Optional<Organization> topLevel() {
return Optional.ofNullable(institution).map(CristinPersonNviInstitution::toOrganization);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import no.unit.nva.cristin.person.model.nva.ContactDetails;
import no.unit.nva.cristin.person.model.nva.Employment;
import no.unit.nva.cristin.person.model.nva.Person;
import no.unit.nva.cristin.person.model.nva.PersonNvi;
import no.unit.nva.cristin.person.model.nva.TypedValue;
import no.unit.nva.model.TypedLabel;
import nva.commons.core.JacocoGenerated;
Expand All @@ -33,7 +34,7 @@
import static no.unit.nva.cristin.model.Constants.PERSON_PATH_NVA;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.NATIONAL_IDENTITY_NUMBER;

@SuppressWarnings({"unused", "PMD.GodClass"})
@SuppressWarnings({"unused", "PMD.GodClass", "PMD.TooManyFields"})
@JacocoGenerated
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CristinPerson implements JsonSerializable {
Expand Down Expand Up @@ -66,6 +67,7 @@ public class CristinPerson implements JsonSerializable {
private Boolean identifiedCristinPerson;
private List<CristinTypedLabel> keywords;
private Map<String, String> background;
private CristinPersonNvi personNvi;

public String getCristinPersonId() {
return cristinPersonId;
Expand Down Expand Up @@ -188,6 +190,14 @@ public void setBackground(Map<String, String> background) {
this.background = background;
}

public CristinPersonNvi getPersonNvi() {
return personNvi;
}

public void setPersonNvi(CristinPersonNvi personNvi) {
this.personNvi = personNvi;
}

/**
* Creates a Nva person model from a Cristin person model. If the person is not publicly viewable, only returns
* identifier.
Expand All @@ -196,7 +206,8 @@ public void setBackground(Map<String, String> background) {
*/
public Person toPerson() {
if (Boolean.TRUE.equals(getReserved())) {
return new Person.Builder().withId(extractIdUri()).build(); // Also preserving size of hits from upstream
// Also preserving size of hits from upstream
return new Person.Builder().withId(extractIdUri()).build();
}
return new Person.Builder()
.withId(extractIdUri())
Expand All @@ -208,6 +219,7 @@ public Person toPerson() {
.withVerified(getIdentifiedCristinPerson())
.withKeywords(extractKeywords())
.withBackground(extractBackground())
.withNvi(extractNvi())
.build();
}

Expand All @@ -231,6 +243,7 @@ public Person toPersonWithAuthorizedFields() {
.withVerified(getIdentifiedCristinPerson())
.withKeywords(extractKeywords())
.withBackground(extractBackground())
.withNvi(extractNvi())
.build();
}

Expand Down Expand Up @@ -301,6 +314,10 @@ private Map<String, String> extractBackground() {
return nonNull(getBackground()) ? new ConcurrentHashMap<>(getBackground()) : Collections.emptyMap();
}

private PersonNvi extractNvi() {
return nonNull(getPersonNvi()) ? getPersonNvi().toPersonNvi() : null;
}

@Override
public String toString() {
return toJsonString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package no.unit.nva.cristin.person.model.cristin;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
import java.util.Optional;
import no.unit.nva.commons.json.JsonSerializable;
import no.unit.nva.cristin.person.model.nva.PersonNvi;

public record CristinPersonNvi(@JsonProperty(VERIFIED_BY) CristinPersonSummary verifiedBy,
@JsonProperty(VERIFIED_AT) CristinNviInstitutionUnit verifiedAt,
@JsonProperty(VERIFIED_DATE) Instant verifiedDate)
implements JsonSerializable {

public static final String VERIFIED_BY = "verified_by";
public static final String VERIFIED_AT = "verified_at";
public static final String VERIFIED_DATE = "verified_date";

public PersonNvi toPersonNvi() {
var personSummary = Optional.ofNullable(verifiedBy)
.map(CristinPersonSummary::toPersonSummary);

var organization = Optional.ofNullable(verifiedAt)
.map(CristinNviInstitutionUnit::toOrganization);

if (personSummary.isPresent() || organization.isPresent()) {
return new PersonNvi(personSummary.orElse(null), organization.orElse(null), verifiedDate);
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package no.unit.nva.cristin.person.model.cristin;

import static no.unit.nva.cristin.model.Constants.ORGANIZATION_PATH;
import static no.unit.nva.cristin.model.JsonPropertyNames.ACRONYM;
import static no.unit.nva.cristin.model.JsonPropertyNames.COUNTRY;
import static no.unit.nva.utils.UriUtils.getNvaApiId;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
import java.util.Optional;
import no.unit.nva.cristin.model.CristinUnit;
import no.unit.nva.model.Organization;

public record CristinPersonNviInstitution(@JsonProperty(ACRONYM) String acronym, @JsonProperty(COUNTRY) String country,
@JsonProperty(CORRESPONDING_UNIT) CristinUnit correspondingUnit,
@JsonProperty(INSTITUTION_NAME) Map<String, String> institutionName) {

public static final String CORRESPONDING_UNIT = "corresponding_unit";
public static final String INSTITUTION_NAME = "institution_name";

public Organization toOrganization() {
return new Organization.Builder()
.withId(getNvaApiId(getCorrespondingUnitIdentifier(), ORGANIZATION_PATH))
.withAcronym(acronym)
.withCountry(country)
.withLabels(institutionName)
.build();
}

private String getCorrespondingUnitIdentifier() {
return Optional.ofNullable(correspondingUnit).map(CristinUnit::getCristinUnitId).orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package no.unit.nva.cristin.person.model.cristin;

import static java.util.Objects.nonNull;
import static no.unit.nva.cristin.model.Constants.BASE_PATH;
import static no.unit.nva.cristin.model.Constants.DOMAIN_NAME;
import static no.unit.nva.cristin.model.Constants.HTTPS;
import static no.unit.nva.cristin.model.Constants.PERSON_PATH_NVA;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.net.URI;
import no.unit.nva.cristin.person.model.nva.PersonSummary;
import nva.commons.core.paths.UriWrapper;

public record CristinPersonSummary(@JsonProperty(FIRST_NAME) String firstName,
@JsonProperty(SURNAME) String surname,
@JsonProperty(URL_FIELD_NAME) String url,
@JsonProperty(CRISTIN_PERSON_ID) String cristinPersonId) {

public static final String FIRST_NAME = "first_name";
public static final String SURNAME = "surname";
public static final String URL_FIELD_NAME = "url";
public static final String CRISTIN_PERSON_ID = "cristin_person_id";

public static Builder builder() {
return new Builder();
}

public static final class Builder {

private String firstName;
private String surname;
private String url;
private String cristinPersonId;

private Builder() {
}

public Builder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public Builder withSurname(String surname) {
this.surname = surname;
return this;
}

public Builder withUrl(String url) {
this.url = url;
return this;
}

public Builder withCristinPersonId(String cristinPersonId) {
this.cristinPersonId = cristinPersonId;
return this;
}

public CristinPersonSummary build() {
return new CristinPersonSummary(firstName, surname, url, cristinPersonId);
}
}

public PersonSummary toPersonSummary() {
return nonNull(cristinPersonId) ? new PersonSummary(extractIdUri(), firstName, surname) : null;
}

private URI extractIdUri() {
return new UriWrapper(HTTPS, DOMAIN_NAME).addChild(BASE_PATH).addChild(PERSON_PATH_NVA)
.addChild(cristinPersonId).getUri();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public class JsonPropertyNames {
public static final String VERIFIED = "verified";
public static final String KEYWORDS = "keywords";
public static final String BACKGROUND = "background";
public static final String NVI = "nvi";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.KEYWORDS;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.NAMES;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.NATIONAL_IDENTITY_NUMBER;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.NVI;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.RESERVED;
import static no.unit.nva.cristin.person.model.nva.JsonPropertyNames.VERIFIED;

@JacocoGenerated
@SuppressWarnings("PMD.ExcessivePublicCount")
@JsonPropertyOrder({CONTEXT, ID, TYPE, IDENTIFIERS, NAMES, CONTACT_DETAILS, IMAGE, AFFILIATIONS, RESERVED, EMPLOYMENTS,
VERIFIED, KEYWORDS, BACKGROUND})
public class Person implements JsonSerializable {
Expand Down Expand Up @@ -71,6 +73,8 @@ public class Person implements JsonSerializable {
private Set<TypedLabel> keywords;
@JsonProperty(BACKGROUND)
private Map<String, String> background;
@JsonProperty(NVI)
private PersonNvi nvi;

private Person() {

Expand All @@ -90,6 +94,7 @@ private Person() {
* @param verified If this person is a verified person.
* @param keywords Keywords related to this person.
* @param background Background information about this person.
* @param nvi NVI information about this person.
*/
@JsonCreator
@SuppressWarnings("PMD.ExcessiveParameterList")
Expand All @@ -99,7 +104,7 @@ public Person(@JsonProperty(ID) URI id, @JsonProperty(IDENTIFIERS) Set<TypedValu
@JsonProperty(AFFILIATIONS) List<Affiliation> affiliations, @JsonProperty(RESERVED) Boolean reserved,
@JsonProperty(EMPLOYMENTS) Set<Employment> employments, @JsonProperty(VERIFIED) Boolean verified,
@JsonProperty(KEYWORDS) Set<TypedLabel> keywords,
@JsonProperty(BACKGROUND) Map<String, String> background) {
@JsonProperty(BACKGROUND) Map<String, String> background, @JsonProperty(NVI) PersonNvi nvi) {
this.id = id;
this.identifiers = identifiers;
this.names = names;
Expand All @@ -111,6 +116,7 @@ public Person(@JsonProperty(ID) URI id, @JsonProperty(IDENTIFIERS) Set<TypedValu
this.verified = verified;
this.keywords = keywords;
this.background = background;
this.nvi = nvi;
}

public String getContext() {
Expand Down Expand Up @@ -213,6 +219,14 @@ public void setBackground(Map<String, String> background) {
this.background = background;
}

public PersonNvi getNvi() {
return nvi;
}

public void setNvi(PersonNvi nvi) {
this.nvi = nvi;
}

/**
* Converts this object to an appropriate format for POST to Cristin.
*/
Expand Down Expand Up @@ -284,15 +298,16 @@ public boolean equals(Object o) {
&& Objects.equals(getEmployments(), person.getEmployments())
&& Objects.equals(getVerified(), person.getVerified())
&& Objects.equals(getKeywords(), person.getKeywords())
&& Objects.equals(getBackground(), person.getBackground());
&& Objects.equals(getBackground(), person.getBackground())
&& Objects.equals(getNvi(), person.getNvi());
}

@JacocoGenerated
@Override
public int hashCode() {
return Objects.hash(getId(), getContext(), getIdentifiers(), getNames(), getContactDetails(), getImage(),
getAffiliations(), getReserved(), getEmployments(), getVerified(), getKeywords(),
getBackground());
getBackground(), getNvi());
}

@Override
Expand Down Expand Up @@ -369,6 +384,11 @@ public Builder withBackground(Map<String, String> background) {
return this;
}

public Builder withNvi(PersonNvi nvi) {
person.setNvi(nvi);
return this;
}

public Person build() {
return person;
}
Expand Down
Loading

0 comments on commit f7b1409

Please sign in to comment.