Crop.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.util.List;

import javax.persistence.Cacheable;
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.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
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.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.custom.elasticsearch.SearchField;
import org.genesys.server.model.GlobalModel;
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.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Cacheable
@Entity
@Table(name = "crop")
@Document(indexName = "crop")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Crop.class)
@Getter
@Setter
@NoArgsConstructor
public class Crop extends TranslatedAuditedVersionedModel<CropLang, Crop> implements GlobalModel, SelfCleaning {

	private static final long serialVersionUID = -2686341831839109257L;

	public static final Logger LOG = LoggerFactory.getLogger(Crop.class);

	public static final int CROP_SHORTNAME_LENGTH = 50;

	@Column(length = 500)
	private String rdfUri;

	/**
	 * Crop short name used as short name in URLs
	 */
	@Size(max = CROP_SHORTNAME_LENGTH)
	@NotNull
	@Column(nullable = false, length = CROP_SHORTNAME_LENGTH, unique = true)
	@SearchField
	private String shortName;

	@Column(name = "otherName", nullable = false, unique = true, length = 255)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "cropname", joinColumns = @JoinColumn(name = "cropId", referencedColumnName = "id"), uniqueConstraints = { @UniqueConstraint(columnNames = "otherName") })
	@OrderBy("otherName")
	@JsonView({ JsonViews.Public.class })
	@SearchField
	private List<String> otherNames;

	@Size(max = 200)
	@NotNull
	@Column(nullable = false, length = 200)
	@SearchField
	private String name;

	@Column(name = "annex1")
	private boolean annex1 = false;

	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String description;

	public Crop(Long id) {
		this.setId(id);
	}

	@PrePersist
	@PreUpdate
	private void prePersist() {
		trimStringsToNull();
		this.shortName = this.shortName.toLowerCase();
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof Crop;
	}
}