Partner.java
/*
* Copyright 2017 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.genesys.server.model;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.blocks.util.ClassAclOid;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.server.component.elastic.ElasticLoader;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.model.impl.LazyLoading;
import org.genesys.server.model.traits.Descriptor;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.model.vocab.ControlledVocabulary;
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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* Genesys Partners.
*
* @author Matija Obreza
*/
@Entity
@Table(name = "partner")
@Cacheable
@Document(indexName = "partner")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Partner.class)
@Getter
@Setter
@NoArgsConstructor
public class Partner extends UuidModel implements SelfCleaning, AclAwareModel, Copyable<Partner>, LazyLoading<Partner>, ElasticLoader {
private static final ClassAclOid<Partner> PARENT_OID = ClassAclOid.forClass(Partner.class);
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 7972197553356837382L;
/// partner short name (acronym or abbr.)
@Size(max = 20)
@NotNull
@Column(length = 20, nullable = false, unique = true)
private String shortName;
/// partner name
@Size(max = 200)
@NotNull
@Column(length = 200, nullable = false)
private String name;
/// partner description
@Column
@Lob
private String description;
/// partner email
@Size(max = 100)
@Column(length = 100)
private String email;
/// partner phone number
@Size(max = 100)
@Column(length = 100)
private String phone;
/// partner address
@Size(max = 500)
@Column(length = 500)
private String address;
/// partner-associated web links
@ElementCollection
@CollectionTable(name = "partner_url", joinColumns = @JoinColumn(name = "partnerId"))
private Set<URL> urls;
/// FAO WIEWS codes associated with this partner
@JsonIgnore
@IgnoreField(false)
@JsonView({ JsonViews.Indexed.class })
@Field(type = FieldType.Nested)
@JsonIgnoreProperties({ "owner", "settings", "pdciHistogram" })
@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
private List<FaoInstitute> institutes;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "partner_country", joinColumns = @JoinColumn(name = "partnerId"), uniqueConstraints = { @UniqueConstraint(columnNames = { "partnerId", "code" }) })
@Column(name = "code", length = 3, nullable = false)
private Set<String> countryCodes;
@JsonIgnore
@OneToMany(cascade = { CascadeType.REMOVE }, orphanRemoval = true, mappedBy = "owner", fetch = FetchType.LAZY)
private List<Descriptor> descriptors;
@JsonIgnore
@OneToMany(cascade = { CascadeType.REMOVE }, orphanRemoval = true, mappedBy = "owner", fetch = FetchType.LAZY)
private List<ControlledVocabulary> vocabularies;
@JsonIgnore
@OneToMany(cascade = { CascadeType.REMOVE }, orphanRemoval = true, mappedBy = "owner", fetch = FetchType.LAZY)
private List<DescriptorList> descriptorList;
/**
* Pre-persist, pre-update
*/
@PrePersist
@PreUpdate
private void preupdate() {
trimStringsToNull();
}
@Override
public AclAwareModel aclParentObject() {
return PARENT_OID;
}
/**
* Instantiates a new partner.
*
* @param partner the partner
*/
public Partner(final Partner partner) {
this(partner.getShortName(), partner.getName(), partner.getUrls(), partner.getInstitutes(), partner.getDescriptors(), partner.getVocabularies(), partner
.getDescriptorList());
}
/**
* Instantiates a new partner.
*
* @param shortName the short name
* @param name the name
* @param urls the urls
* @param institutes the institutes
* @param descriptors the descriptors
* @param vocabularies the vocabularies
* @param descriptorList the descriptor list
*/
public Partner(final String shortName, final String name, final Set<URL> urls, final List<FaoInstitute> institutes, final List<Descriptor> descriptors,
final List<ControlledVocabulary> vocabularies, final List<DescriptorList> descriptorList) {
this.shortName = shortName;
this.name = name;
if (urls != null) {
this.urls = new HashSet<>(urls);
}
if (institutes != null) {
this.institutes = new ArrayList<>(institutes);
}
this.descriptors = descriptors;
this.vocabularies = vocabularies;
this.descriptorList = descriptorList;
}
/**
* The ACL SID authority name for this Partner
*
* @return the name of ACL SID authority (`partner_${uuid}`)
*/
@Transient
public String getAuthorityName() {
return "partner_" + this.uuid;
}
@Override
public void lazyLoad() {
if (this.urls != null) {
this.urls.size();
}
if (this.institutes != null) {
this.institutes.size();
}
if (this.countryCodes != null) {
this.countryCodes.size();
}
}
@Override
public Partner apply(Partner source) {
this.setActive(source.isActive());
this.setName(source.getName());
this.setShortName(source.getShortName());
this.setDescription(source.getDescription());
this.setEmail(source.getEmail());
this.setAddress(source.getAddress());
this.setPhone(source.getPhone());
if (this.urls == null) {
this.urls = new HashSet<>();
} else {
this.urls.clear();
}
if (source.getUrls() != null) {
this.urls.addAll(source.getUrls());
}
if (this.institutes == null) {
this.institutes = new ArrayList<>();
} else {
this.institutes.clear();
}
if (source.getInstitutes() != null) {
this.institutes.addAll(source.getInstitutes());
}
if (this.countryCodes == null) {
this.countryCodes = new HashSet<>();
} else {
this.countryCodes.clear();
}
if (source.getCountryCodes() != null) {
this.countryCodes.addAll(source.getCountryCodes());
}
return this;
}
@Override
public boolean canEqual(Object other) {
return other instanceof Partner;
}
@Override
public void prepareForIndexing() {
this.institutes.size();
}
}