/**
 * Revenue Settlement and Sharing System GE
 * Copyright (C) 2011-2014, Javier Lucio - lucio@tid.es
 * Telefonica Investigacion y Desarrollo, S.A.
 *
 * Copyright (C) 2015, CoNWeT Lab., Universidad Politécnica de Madrid
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package es.tid.fiware.rss.dao.impl;

import java.util.Collections;
import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import es.tid.fiware.rss.dao.DbeTransactionDao;
import es.tid.fiware.rss.exception.RSSException;
import es.tid.fiware.rss.model.DbeTransaction;

/**
 * 
 * Class that extends GenericDaoImpl and implements DbeTransactionDao.
 * 
 */
@Repository
public class DbeTransactionDaoImpl extends GenericDaoImpl<DbeTransaction, String> implements DbeTransactionDao {

    /**
     * MINIMUN_SIZE_TRANSACTION_ID establish the minimun size for the transaction_id. it is necessary to be longer than
     * we user to extracts id for the partition
     */

    /**
     * Variable to print the trace.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DbeTransactionDaoImpl.class);

    @Override
    protected Class<DbeTransaction> getDomainClass() {
        return DbeTransaction.class;
    }


    @Override
    public List<DbeTransaction> getTransactionByTxPbCorrelationId(Integer pbCorrelationId) {
        String hql = "from DbeTransaction as trans where trans.txPbCorrelationId=:correlation";
        List<DbeTransaction> resultList;
        try {
            List list = this.getSession().createQuery(hql).setParameter("correlation", pbCorrelationId).list();
            resultList = Collections.checkedList(list, DbeTransaction.class);
        } catch (Exception e) {
            DbeTransactionDaoImpl.LOGGER.error("Error db", e);
            return null;
        }
        return resultList;
    }

    @Override
    public DbeTransaction getTransactionByRfCdeSvc(final String txReferenceCode,
        final String applicationId) {
        DbeTransactionDaoImpl.LOGGER.debug("Entering getTransactionByRfCdeSvc...");
        // RVD SQLi
        String hql = " from DbeTransaction l where l.txReferenceCode = '" 
                + txReferenceCode + "'" + " and l.txApplicationId = '" + applicationId + "'";

        List<DbeTransaction> lpp = this.listDbeTransactionQuery(hql);

        if (!lpp.isEmpty()) {
            // must be only one
            return lpp.get(0);
        } else { // <=0
            return null;
        }
    }

    @Override
    public DbeTransaction getTransactionByTxId(final String transactionId) throws RSSException {

        DbeTransactionDaoImpl.LOGGER.debug("Entering getTransactionByTxId...");
        String hql = "from DbeTransaction l where l.txTransactionId=:txID";

        try {
            List<Object> txs = (List<Object>) this.getSession().
            		createQuery(hql).
            		setParameter("txID", transactionId).
            		list();

            if (txs.isEmpty()) {
                DbeTransactionDaoImpl.LOGGER.debug("There is no data");
                return null;
            } else {
                return (DbeTransaction) txs.get(0);
            }
        } catch (Exception e) {
            DbeTransactionDaoImpl.LOGGER.error("Error db", e);
            return null;
        }
    }

    @Override
    public void deleteTransactionsByProviderId(String providerId) {
        StringBuilder sbSql = new StringBuilder("delete from dbe_transaction where ");
        sbSql.append("app_provider.txAppProviderId = ?");
        Query query = super.getSession().createSQLQuery(sbSql.toString()).addEntity(DbeTransaction.class);
        query.setString(0, providerId);
        query.executeUpdate();
    }

    @Override
    public List<DbeTransaction> getTransactionsByProviderId(String providerId) {
        DbeTransactionDaoImpl.LOGGER.debug("getTransactionsByProviderId..");
        String hql = "from DbeTransaction l where l.app_provider.txAppProviderId='" + providerId + "'"
                + " l.tx_state='pending' order by l.correlation_number";

        return listDbeTransactionQuery(hql);
    }

    @Override
    public List<DbeTransaction> getTransactionByAggregatorId(String aggregatorId) {
        DbeTransactionDaoImpl.LOGGER.debug("getTransactionsByAggregatorId..");
        String hql = "from DbeTransaction l where l.source_aggregator.txEmail='" + aggregatorId + "'"
                + " l.tx_state='pending' order by l.correlation_number";

        return listDbeTransactionQuery(hql);
    }

    @Override
    public List<DbeTransaction> getTransactions() {
        DbeTransactionDaoImpl.LOGGER.debug("getTransactionsByAggregatorId..");
        String hql = "from DbeTransaction l where l.tx_state='pending'";
        return listDbeTransactionQuery(hql);
    }

    /* Private Methods */
    /**
     * Method executes HQL query.
     * 
     * @param hql
     *            String with HQL query
     * @return resultList
     */
    private List<DbeTransaction> listDbeTransactionQuery(final String hql) {
        DbeTransactionDaoImpl.LOGGER.debug("listDbeTransactionQuery hql-->" + hql);
        List<DbeTransaction> resultList;
        try {
            List list = this.getSession().createQuery(hql).list();
            resultList = Collections.checkedList(list, DbeTransaction.class);
        } catch (Exception e) {
            DbeTransactionDaoImpl.LOGGER.error("Error db", e);
            return null;
        }
        return resultList;
    }

}

@@ -4625,18 +4625,17 @@
 re l.app
-_p
+P
 rovider.
@@ -4684,37 +4684,38 @@
             + %22 
-l.tx_
+and l.
 state='pending' 
@@ -4717,33 +4717,37 @@
 ing' order by l.
-c
+txPbC
 orrelation_numbe
@@ -4732,39 +4732,34 @@
 .txPbCorrelation
-_number
+Id
 %22;%0A%0A        retu
@@ -5031,25 +5031,17 @@
 e l.
-s
+cdrS
 ource
-_aggregator
 .txE
@@ -5089,21 +5089,22 @@
     + %22 
-l.tx_
+and l.
 state='p
@@ -5122,17 +5122,21 @@
 er by l.
-c
+txPbC
 orrelati
@@ -5141,15 +5141,10 @@
 tion
-_number
+Id
 %22;%0A%0A
@@ -5332,32 +5332,32 @@
 gregatorId..%22);%0A
+
         String h
@@ -5396,11 +5396,8 @@
 e l.
-tx_
 stat

Fix bug with queries for retrieving transactions