<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package qa.qcri.aidr.dbmanager.ejb.remote.facade.imp;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;

import qa.qcri.aidr.common.exception.PropertyNotSetException;
import qa.qcri.aidr.dbmanager.dto.CrisisAttributesDTO;
import qa.qcri.aidr.dbmanager.dto.NominalAttributeDTO;
import qa.qcri.aidr.dbmanager.ejb.local.facade.impl.CoreDBServiceFacadeImp;
import qa.qcri.aidr.dbmanager.ejb.remote.facade.NominalAttributeResourceFacade;
import qa.qcri.aidr.dbmanager.entities.model.NominalAttribute;
import qa.qcri.aidr.dbmanager.entities.task.Document;

/**
 *
 * @author Imran
 */
@Stateless(name = "NominalAttributeResourceFacadeImp")
public class NominalAttributeResourceFacadeImp extends CoreDBServiceFacadeImp<NominalAttribute, Long> implements NominalAttributeResourceFacade {

    private static Logger logger = Logger.getLogger("db-manager-log");

    public NominalAttributeResourceFacadeImp() {
		super(NominalAttribute.class);
	}
    
    public boolean addAttribute(NominalAttributeDTO attribute) throws PropertyNotSetException {
        try {
            save(attribute.toEntity());
        } catch (HibernateException he) {
            logger.error("Hibernate exception on save Nominal Attribute. \n" + he.getStackTrace());
            return false;
        }
        return true;
    }

    public NominalAttributeDTO editAttribute(NominalAttributeDTO attribute) throws PropertyNotSetException {
        if (attribute == null) {
            throw new RuntimeException("Missing data values");
        }
        NominalAttribute nominalAttributeDB = getEntityManager().find(NominalAttribute.class, attribute.getNominalAttributeId());
        if (nominalAttributeDB != null) {
            getEntityManager().merge(attribute);
            return attribute;
        } else {
            throw new RuntimeException("Can't edit");
        }
    }

    public boolean deleteAttribute(Long attributeID) throws PropertyNotSetException {
        NominalAttribute attribute = getEntityManager().find(NominalAttribute.class, attributeID);
        if (attribute != null) {
            try {
                getEntityManager().remove(attribute);
            } catch (HibernateException he) {
                logger.error("Error occured while removing Attribute with ID = " + attributeID + "\n" + he.getStackTrace());
                return false; // exception occured
            }
            return true; //successfully deleted
        } else {
            return false; // attribute does not exist with the provided id.
        }
    }

    public NominalAttributeDTO getAttributeByID(Long attributeID) throws PropertyNotSetException {
        NominalAttribute nominalAttribute = getById(attributeID);
        nominalAttribute.getNominalLabels(); //loading labels too
        return new NominalAttributeDTO(nominalAttribute);
    }

    public List<NominalAttributeDTO> getAllAttributes() throws PropertyNotSetException {
        List<NominalAttributeDTO> nominalAttributeDTOList = new ArrayList<NominalAttributeDTO>();
        List<NominalAttribute> nominalAttributeList = getAll();
        for (NominalAttribute nominalAttribute : nominalAttributeList) {
            nominalAttributeDTOList.add(new NominalAttributeDTO(nominalAttribute));
        }
        return nominalAttributeDTOList;
    }

    public Long isAttributeExists(String attributeCode) throws PropertyNotSetException {
        Criteria criteria = getCurrentSession().createCriteria(NominalAttribute.class);
        criteria.add(Restrictions.eq("code", attributeCode));
        NominalAttribute nominalAttribute = (NominalAttribute)criteria.uniqueResult();
        return nominalAttribute != null ? nominalAttribute.getNominalAttributeId() : null;
    }

    //TODO: Native query used in this method should be translated into a criteria query.
    public List<CrisisAttributesDTO> getAllAttributesExceptCrisis(Long crisisID) throws PropertyNotSetException {
        List<CrisisAttributesDTO> attributesList = new ArrayList();
        String sql = "SELECT na.nominalAttributeID, na.userID, na.name, na.description, na.code, "
                + " nl.nominalLabelID, nl.name AS lblName FROM nominal_attribute na \n"
                + " JOIN nominal_label nl ON na.nominalAttributeID = nl.nominalAttributeID \n"
                + " LEFT JOIN model_family mf ON na.nominalAttributeID = mf.nominalAttributeID \n"
                + " AND mf.crisisID = :crisisID WHERE mf.crisisID IS NULL";
        try {
            Query query = em.createNativeQuery(sql);
            query.setParameter("crisisID", crisisID);
            List<Object[]> rows = query.getResultList();
            CrisisAttributesDTO attribute;
            for (Object[] row : rows) {
                attribute = new CrisisAttributesDTO();
                attribute.setNominalAttributeID(((Integer) row[0]).intValue());
                attribute.setUserID(((Integer) row[1]).intValue());
                attribute.setName((String) row[2]);
                attribute.setDescription((String) row[3]);
                attribute.setCode(((String) row[4]));
                attribute.setLabelID(((Integer) row[5]).intValue());
                attribute.setLabelName(((String) row[6]));
                attributesList.add(attribute);
            }
            return attributesList;
        } catch (NoResultException e) {
            return attributesList;
        }
    }

}
<commit_msg>Refactoring: cleared minor warnings<commit_after>package qa.qcri.aidr.dbmanager.ejb.remote.facade.imp;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;

import qa.qcri.aidr.common.exception.PropertyNotSetException;
import qa.qcri.aidr.dbmanager.dto.CrisisAttributesDTO;
import qa.qcri.aidr.dbmanager.dto.NominalAttributeDTO;
import qa.qcri.aidr.dbmanager.ejb.local.facade.impl.CoreDBServiceFacadeImp;
import qa.qcri.aidr.dbmanager.ejb.remote.facade.NominalAttributeResourceFacade;
import qa.qcri.aidr.dbmanager.entities.model.NominalAttribute;

/**
 *
 * @author Imran
 */
@Stateless(name = "NominalAttributeResourceFacadeImp")
public class NominalAttributeResourceFacadeImp extends CoreDBServiceFacadeImp<NominalAttribute, Long> implements NominalAttributeResourceFacade {

    private static Logger logger = Logger.getLogger("db-manager-log");

    public NominalAttributeResourceFacadeImp() {
		super(NominalAttribute.class);
	}
    
    public boolean addAttribute(NominalAttributeDTO attribute) throws PropertyNotSetException {
        try {
            save(attribute.toEntity());
        } catch (HibernateException he) {
            logger.error("Hibernate exception on save Nominal Attribute. \n" + he.getStackTrace());
            return false;
        }
        return true;
    }

    public NominalAttributeDTO editAttribute(NominalAttributeDTO attribute) throws PropertyNotSetException {
        if (attribute == null) {
            throw new RuntimeException("Missing data values");
        }
        NominalAttribute nominalAttributeDB = getEntityManager().find(NominalAttribute.class, attribute.getNominalAttributeId());
        if (nominalAttributeDB != null) {
            getEntityManager().merge(attribute);
            return attribute;
        } else {
            throw new RuntimeException("Can't edit");
        }
    }

    public boolean deleteAttribute(Long attributeID) throws PropertyNotSetException {
        NominalAttribute attribute = getEntityManager().find(NominalAttribute.class, attributeID);
        if (attribute != null) {
            try {
                getEntityManager().remove(attribute);
            } catch (HibernateException he) {
                logger.error("Error occured while removing Attribute with ID = " + attributeID + "\n" + he.getStackTrace());
                return false; // exception occured
            }
            return true; //successfully deleted
        } else {
            return false; // attribute does not exist with the provided id.
        }
    }

    public NominalAttributeDTO getAttributeByID(Long attributeID) throws PropertyNotSetException {
        NominalAttribute nominalAttribute = getById(attributeID);
        nominalAttribute.getNominalLabels(); //loading labels too
        return new NominalAttributeDTO(nominalAttribute);
    }

    public List<NominalAttributeDTO> getAllAttributes() throws PropertyNotSetException {
        List<NominalAttributeDTO> nominalAttributeDTOList = new ArrayList<NominalAttributeDTO>();
        List<NominalAttribute> nominalAttributeList = getAll();
        for (NominalAttribute nominalAttribute : nominalAttributeList) {
            nominalAttributeDTOList.add(new NominalAttributeDTO(nominalAttribute));
        }
        return nominalAttributeDTOList;
    }

    public Long isAttributeExists(String attributeCode) throws PropertyNotSetException {
        Criteria criteria = getCurrentSession().createCriteria(NominalAttribute.class);
        criteria.add(Restrictions.eq("code", attributeCode));
        NominalAttribute nominalAttribute = (NominalAttribute)criteria.uniqueResult();
        return nominalAttribute != null ? nominalAttribute.getNominalAttributeId() : null;
    }

    //TODO: Native query used in this method should be translated into a criteria query.
    public List<CrisisAttributesDTO> getAllAttributesExceptCrisis(Long crisisID) throws PropertyNotSetException {
        List<CrisisAttributesDTO> attributesList = new ArrayList<>();
        String sql = "SELECT na.nominalAttributeID, na.userID, na.name, na.description, na.code, "
                + " nl.nominalLabelID, nl.name AS lblName FROM nominal_attribute na \n"
                + " JOIN nominal_label nl ON na.nominalAttributeID = nl.nominalAttributeID \n"
                + " LEFT JOIN model_family mf ON na.nominalAttributeID = mf.nominalAttributeID \n"
                + " AND mf.crisisID = :crisisID WHERE mf.crisisID IS NULL";
        try {
            Query query = em.createNativeQuery(sql);
            query.setParameter("crisisID", crisisID);
            List<Object[]> rows = query.getResultList();
            CrisisAttributesDTO attribute;
            for (Object[] row : rows) {
                attribute = new CrisisAttributesDTO();
                attribute.setNominalAttributeID(((Integer) row[0]).intValue());
                attribute.setUserID(((Integer) row[1]).intValue());
                attribute.setName((String) row[2]);
                attribute.setDescription((String) row[3]);
                attribute.setCode(((String) row[4]));
                attribute.setLabelID(((Integer) row[5]).intValue());
                attribute.setLabelName(((String) row[6]));
                attributesList.add(attribute);
            }
            return attributesList;
        } catch (NoResultException e) {
            return attributesList;
        }
    }

}

{'fraction_non_alphanumeric': 0.09203785708083702, 'fraction_numerical': 0.0013892506729182947, 'mean_word_length': 3.0931058990760483, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4561347', 'n_tokens_mistral': 3080, 'n_tokens_neox': 2990, 'n_words': 648}