ElasticReindex.java

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

import java.io.Serializable;

import org.genesys.blocks.model.EmptyModel;
import org.hibernate.proxy.HibernateProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
 * The Class ElasticReindex.
 */
@EqualsAndHashCode
public class ElasticReindex implements Serializable {
	private static final Logger LOGtest = LoggerFactory.getLogger("org.gringlobal.test.Elasticsearch");

	private static final long serialVersionUID = 3243889775463883768L;
	@Getter private Class<?> clazz;
	@Getter 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();
		this.id = toReindex.getId();
		if (toReindex instanceof HibernateProxy) { // This must not be the case!
			HibernateProxy hib = (HibernateProxy) toReindex;
			this.clazz = hib.getHibernateLazyInitializer().getPersistentClass();
			LOGtest.trace("ElasticReindex of proxy {} id={}", this.clazz, this.id);
		}
	}


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

}