
package org.folio.cataloging.dao;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.type.Type;
import org.folio.cataloging.Global;
import org.folio.cataloging.dao.common.HibernateUtil;
import org.folio.cataloging.dao.persistence.RecordTypeMaterial;

import java.util.List;
import java.util.Objects;

/**
 * @author paulm
 * @since 1.0
 */
public class DAORecordTypeMaterial extends HibernateUtil {

	/**
	 * The method gets the material type for 006/008 tags field using leader values.
	 *
	 * @param session the valid hibernate session.
	 * @param recordType the record type value used here as filter criterion.
	 * @param bibliographicLevel the bibliographic level value used here as filter criterion.
	 * @return RecordTypeMaterial to represent form of item
	 * @throws HibernateException in case of hibernate exception.
	 */
	@SuppressWarnings("unchecked")
	public RecordTypeMaterial getMaterialHeaderCode(final Session session, final char recordType, final char bibliographicLevel) throws HibernateException {
        final List<RecordTypeMaterial> recordTypeMaterials = session.find(
						"from RecordTypeMaterial as t "
							+ " where t.RECORD_TYPE_CODE = ? and "
							+ "       (t.bibliographicLevel = ? "
							+ "        OR t.bibliographicLevel is NULL)",
						new Object[] {
							recordType,
							bibliographicLevel},
						new Type[] { Hibernate.CHARACTER, Hibernate.CHARACTER });

		return recordTypeMaterials.stream().filter(Objects::nonNull).findFirst().orElse(null);
	}

	//TODO : dont'call me! Use getMaterialHeaderCode()
	@Deprecated
	public RecordTypeMaterial get008HeaderCode(char recordType, char bibliographicLevel){
		RecordTypeMaterial result = null;
		/*try {
			Session s = currentSession();
			List l =
					s.find(
							"from RecordTypeMaterial as t "
									+ " where t.RECORD_TYPE_CODE = ? and "
									+ "       (t.bibliographicLevel = ? "
									+ "        OR t.bibliographicLevel is NULL)",
							new Object[] {
									new Character(recordType),
									new Character(bibliographicLevel)},
							new Type[] { Hibernate.CHARACTER, Hibernate.CHARACTER });
			if (l.size() > 0) {
				result = (RecordTypeMaterial) l.get(0);
			}
		} catch (DataAccessException e) {
			return result;
		} catch (HibernateException e) {
			return result;
		}*/
		return result;
	}
	//TODO : dont'call me! Use getMaterialHeaderCode()
	@Deprecated
	public RecordTypeMaterial get006HeaderCode(Character materialType) {
		RecordTypeMaterial result = null;
		/*try {
			Session s = currentSession();
				List l =
					s.find(
						"from RecordTypeMaterial as t "
							+ " where t.RECORD_TYPE_CODE = ?",
						new Object[] { materialType },
						new Type[] { Hibernate.CHARACTER });
				if (l.size() > 0) {
					result = (RecordTypeMaterial) l.get(0);
				}
		} catch (DataAccessException e) {
			return result;
		} catch (HibernateException e) {
			return result;
		}*/
		return result;
	}
    @SuppressWarnings("unchecked")
	public RecordTypeMaterial getDefaultTypeByHeaderCode(final Session session, final int headerCode, final String code) throws HibernateException {
		final List<RecordTypeMaterial> recordTypeMaterials = session.find(
					"from RecordTypeMaterial as t "
						+ (code.equals(Global.OTHER_MATERIAL_TAG_CODE) ? " where t.bibHeader006 = ?" : " where t.bibHeader008 = ?"),
					new Object[] { headerCode },
					new Type[] { Hibernate.INTEGER });

		return recordTypeMaterials.stream().filter(Objects::nonNull).findFirst().orElse(null);
	}
}

@@ -1228,32 +1228,30 @@
 where t.
-RECORD_TYPE_CODE
+recordTypeCode
  = ? and

Fix to recordType