/**
 *
 */
package kr.co.kihyun.beans.entity;

import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import kr.co.kihyun.moumi.MoumiConfig;
import org.datanucleus.OMFContext;
import org.datanucleus.jdo.JDOPersistenceManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author bhs
 *
 */
public abstract class MoumiEntity<T> implements IMoumiEntity<T> {

	/**
	 *
	 */
	private static final long serialVersionUID = 9001792590328116313L;
	private static final Logger LOG = LoggerFactory.getLogger(MoumiEntity.class);
	private static final OMFContext omfContext =
			new JDOPersistenceManagerFactory(MoumiConfig.getPmfProperties()).getOMFContext();

	public MoumiEntity() {}

	public abstract void setId(T id);

	public abstract T getId();

//	public abstract void setId(T id);
	/**
	 * @param pm
	 * @param cls
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <C extends MoumiEntity<?>> Query getNamedQuery(PersistenceManager pm, Class<C> cls, String name) {

		Query q = pm.newNamedQuery(cls, name);
                //v2. 6.SQL 삽입 : DO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
		return pm.newQuery(cls, (List<C>) q.execute());
                //================
	}

	@Override
	public String toString() {

		StringBuilder str = new StringBuilder(100)
				.append(this.getClass().getSimpleName()).append("@").append(this.getId());

		return str.toString();

	}

	public static String getTableName(Class clazz) {

		return MoumiEntity.omfContext.getMetaDataManager().getMetaDataForClass(
				clazz, MoumiEntity.omfContext.getClassLoaderResolver(null)).getTable();

	}
}
