Country.java
/**
* Copyright 2014 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.impl;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
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 javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.hibernate.annotations.Type;
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;
@Cacheable
@Entity
@Table(name = "country")
@Document(indexName = "country")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Country.class)
@Getter
@Setter
@NoArgsConstructor
public class Country extends BasicModel implements Copyable<Country>, LazyLoading<Country> {
private static final ObjectMapper mapper = new ObjectMapper();
private static final long serialVersionUID = -1688723909298769804L;
@NotNull
@Size(max = 3)
@Column(nullable = false, unique = true, length = 3)
private String code3;
@Size(max = 2)
@Column(unique = false, length = 2)
@JsonView({ JsonViews.Public.class })
private String code2;
private boolean current;
@NotNull
@Size(max = 200)
@Column(unique = false, nullable = false, length = 200)
private String name;
@Size(max = 3)
@Column(length = 3)
@JsonView({ JsonViews.Public.class })
private String codeNum;
@JsonView({ JsonViews.Public.class })
private Long refnameId;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "regionId")
@JsonView({ JsonViews.Public.class })
@JsonIgnoreProperties({ "countries", "parentRegion" })
@Field(type = FieldType.Object)
private GeoRegion region;
@Column(name = "minLongitude")
@JsonView({ JsonViews.Public.class })
private Double minLongitude;
@Column(name = "minLatitude")
@JsonView({ JsonViews.Public.class })
private Double minLatitude;
@Column(name = "maxLongitude")
@JsonView({ JsonViews.Public.class })
private Double maxLongitude;
@Column(name = "maxLatitude")
@JsonView({ JsonViews.Public.class })
private Double maxLatitude;
/**
* Localized names
*/
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Internal.class })
@IgnoreField
private String nameL;
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Protected.class })
@IgnoreField
private String nameJCustom;
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class })
private String wikiLink;
@Transient
@JsonView({ JsonViews.Protected.class })
private JsonNode nameJ;
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "replacedBy")
@JsonView({ JsonViews.Internal.class })
@IgnoreField
private Country replacedBy;
@IgnoreField
public String getNameL() {
return nameL;
}
public String getName(Locale locale) {
return getNameLocal(locale);
}
public void setName(Locale locale, String name) {
try {
this.nameJ = mapper.readTree(nameL);
// ObjectNode newLang =
// mapper.createObjectNode().put(locale.toLanguageTag(), name);
// System.err.println(newLang);
} catch (final IOException e) {
}
}
private synchronized String getNameLocal(Locale locale) {
if (this.nameJ == null && this.nameL != null) {
try {
this.nameJ = mapper.readTree(nameL);
} catch (final IOException e) {
e.printStackTrace();
}
}
return this.nameJ != null && this.nameJ.has(locale.toLanguageTag()) ? this.nameJ.get(locale.toLanguageTag()).textValue() : this.name;
}
@Override
public String toString() {
return MessageFormat.format("Country id={0} name={1} current={2}", getId(), name, current);
}
@IgnoreField
public Country getReplacedBy() {
return replacedBy;
}
public static void sort(List<Country> all, final Locale locale) {
Collections.sort(all, new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
return o1.getName(locale).compareTo(o2.getName(locale));
}
});
}
@Override
public void lazyLoad() {
if (region != null)
region.getId();
}
@Override
public boolean canEqual(Object other) {
return other instanceof Country;
}
}