package beaform.dao;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import beaform.entities.FormulaTag;

/**
 * This class handles all DB access for tags.
 *
 * @author Steven Post
 *
 */
public final class FormulaTagDAO {

	/** logger */
	private static final Logger LOG = LoggerFactory.getLogger(FormulaTagDAO.class);

	/** The query to search a tag by name */
	private static final String QRY_TAG_BY_NAME = "match (n:FormulaTag { name:{name} }) return n";

	private FormulaTagDAO() {
		// Utility classes should not have a public constructor.
	}

	/**
	 * Find a tag by its name.
	 * This method assumes we are already in a transaction.
	 *
	 * @param name the name of the tag
	 * @return the tag that was found, null if no tag was found
	 */
	public static FormulaTag findByName(final String name) {

		final EntityManager entityManager = GraphDbHandler.getInstance().getEntityManager();

		final FormulaTag result = findByName(name, entityManager);
		if (LOG.isDebugEnabled()) {
			LOG.debug("Found: " + result);
		}

		return result;
	}

	/**
	 * Find a tag in the DB by its name.
	 * @param name the name of the tag to look for
	 * @param entityManager the entity manager
	 * @return the tag found
	 */
	private static FormulaTag findByName(final String name, final EntityManager entityManager) {
		final Query query = entityManager.createNativeQuery(QRY_TAG_BY_NAME, FormulaTag.class);
		query.setParameter("name", name);
		return (FormulaTag) query.getSingleResult();
	}

	/**
	 * Find a tag by using a tag object.
	 * This method assumes we are already in a transaction.
	 *
	 * @param tag The tag to find
	 * @return the tag that was found, null if no tag was found
	 */
	public static FormulaTag findByObject(final FormulaTag tag) {

		final EntityManager entityManager = GraphDbHandler.getInstance().getEntityManager();

		final FormulaTag result = findByName(tag.getName(), entityManager);
		if (LOG.isDebugEnabled()) {
			LOG.debug("Found: " + result);
		}

		return result;
	}

}

@@ -632,502 +632,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Find a tag by its name.%0A%09 * This method assumes we are already in a transaction.%0A%09 *%0A%09 * @param name the name of the tag%0A%09 * @return the tag that was found, null if no tag was found%0A%09 */%0A%09public static FormulaTag findByName(final String name) %7B%0A%0A%09%09final EntityManager entityManager = GraphDbHandler.getInstance().getEntityManager();%0A%0A%09%09final FormulaTag result = findByName(name, entityManager);%0A%09%09if (LOG.isDebugEnabled()) %7B%0A%09%09%09LOG.debug(%22Found: %22 + result);%0A%09%09%7D%0A%0A%09%09return result;%0A%09%7D%0A%0A
 %09/**

Remove unused method