package com.palmnest.service.impl;

import com.palmnest.dao.GenericDao;
import com.palmnest.model.lingling.LingLingDevice;
import com.palmnest.service.GenericManager;

import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;

/**
 * This class serves as the Base class for all other Managers - namely to hold
 * common CRUD methods that they might all use. You should only need to extend
 * this class when your require custom CRUD logic.
 * <p/>
 * <p>To register this class in your Spring context file, use the following XML.
 * <pre>
 *     &lt;bean id="userManager" class="com.palmnest.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="com.palmnest.dao.hibernate.GenericDaoHibernate"&gt;
 *                 &lt;constructor-arg value="com.palmnest.model.User"/&gt;
 *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * <p/>
 * <p>If you're using iBATIS instead of Hibernate, use:
 * <pre>
 *     &lt;bean id="userManager" class="com.palmnest.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="org.com.dao.ibatis.GenericDaoiBatis"&gt;
 *                 &lt;constructor-arg value="com.palmnest.model.User"/&gt;
 *                 &lt;property name="dataSource" ref="dataSource"/&gt;
 *                 &lt;property name="sqlMapClient" ref="sqlMapClient"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 *
 * @param <T>  a type variable
 * @param <PK> the primary key for that type
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
@Slf4j
public class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {


	/**
	 * GenericDao instance, set by constructor of child classes
	 */
	protected GenericDao<T, PK> dao;


	public GenericManagerImpl() {
	}

	public GenericManagerImpl(GenericDao<T, PK> genericDao) {
		this.dao = genericDao;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<T> getAll() {
		return dao.getAll();
	}

	public List<T> getAllDistinct() {
		return dao.getAllDistinct();
	}

	/**
	 * {@inheritDoc}
	 */
	public T get(PK id) {
		return dao.get(id);
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean exists(PK id) {
		return dao.exists(id);
	}

	/**
	 * {@inheritDoc}
	 */
	public T save(T object) {
		return dao.save(object);
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(T object) {
		dao.remove(object);
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(PK id) {
		dao.remove(id);
	}

	/**
	 * {@inheritDoc}
	 * <p/>
	 * Search implementation using Hibernate Search.
	 */
	@SuppressWarnings("rawtypes")
	public List<T> search(String q, Class clazz) {
		if (q == null || "".equals(q.trim())) {
			return getAll();
		}

		return dao.search(q);
	}

	/**
	 * {@inheritDoc}
	 */
	public void reindex() {
		dao.reindex();
	}

	/**
	 * {@inheritDoc}
	 */
	public void reindexAll(boolean async) {
		dao.reindexAll(async);
	}

	@Override
	public T saveModel(T object) {
		return dao.saveObj(object);
	}

	@Override
	public void updateModel(T object) {

		dao.updateObj(object);
	}
}
