ElasticReindex.java

/*
 * Copyright 2018 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.component.elastic;

import org.genesys.blocks.model.EmptyModel;
import org.hibernate.proxy.HibernateProxy;

import java.io.Serializable;
import java.util.Objects;

/**
 * The Class ElasticReindex.
 */
public class ElasticReindex implements Serializable {

	private static final long serialVersionUID = -7779348224175021304L;
	private Class<?> clazz;
	private Long id;

	/**
	 * Instantiates a new elastic reindex.
	 *
	 * @param clazz the clazz
	 * @param id the id
	 */
	public ElasticReindex(Class<?> clazz, Long id) {
		this.clazz = clazz;
		if (HibernateProxy.class.isAssignableFrom(this.clazz)) {
			System.err.println("You're trying to work with a HibernateProxy! " + this.clazz);
		}
		this.id = id;
	}

	public ElasticReindex(EmptyModel toReindex) {
		this.clazz = toReindex.getClass();
		if (toReindex instanceof HibernateProxy) {
			HibernateProxy hib = (HibernateProxy) toReindex;
			this.clazz = hib.getHibernateLazyInitializer().getPersistentClass();
		}
		this.id = toReindex.getId();
	}

	/**
	 * Gets the clazz.
	 *
	 * @return the clazz
	 */
	public Class<?> getClazz() {
		return clazz;
	}
	
	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(clazz, id);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof ElasticReindex)) return false;
		ElasticReindex that = (ElasticReindex) o;
		return Objects.equals(id, that.id) & Objects.equals(clazz, that.clazz);
	}

	@Override
	public String toString() {
		return "ESRef " + clazz + " id=" + id;
	}
}