Search in sources :

Example 1 with UserAccountData

use of org.mobicents.charging.server.data.UserAccountData in project charging-server by RestComm.

the class DirectDebitUnitsJdbcTask method executeSimple.

@Override
public Object executeSimple(JdbcTaskContext taskContext) {
    try {
        long balance = 0;
        // get Balance Before (can this be made more efficient?)
        PreparedStatement preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_SELECT);
        preparedStatement.setString(1, msisdn);
        preparedStatement.execute();
        ResultSet resultSet = preparedStatement.getResultSet();
        while (resultSet.next()) {
            balance = resultSet.getLong(DataSourceSchemaInfo._COL_BALANCE);
            ccInfo.setBalanceBefore(balance);
        }
        for (CreditControlUnit ccUnit : ccUnits) {
            long reservedAmount = ccUnit.getReservedAmount();
            long usedAmount = ccUnit.getUsedAmount();
            long requestedAmount = ccUnit.getRequestedAmount();
            long requestedUnits = ccUnit.getRequestedUnits();
            if (balance < requestedAmount) {
                accountData = new UserAccountData();
                accountData.setMsisdn(msisdn);
                accountData.setBalance(0);
                accountData.setFailure(true);
                ccUnit.setReservedUnits(0);
                ccUnit.setReservedAmount(0);
                if (tracer.isInfoEnabled()) {
                    tracer.info("[//] User does not have sufficient balance for reservation. Balance available: " + balance + ".");
                }
                break;
            } else if (balance > requestedAmount) {
                int n = 1;
                tracer.info(("[//] Executing DB Statement '" + DataSourceSchemaInfo._QUERY_DEBIT).replaceFirst("\\?", String.valueOf(requestedAmount)).replaceFirst("\\?", msisdn));
                preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_DEBIT);
                preparedStatement.setLong(n++, requestedAmount);
                preparedStatement.setString(n++, msisdn);
                accountData = new UserAccountData();
                accountData.setMsisdn(msisdn);
                if (preparedStatement.executeUpdate() == 1) {
                    // ok great, we have successfully reserved the units
                    preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_SELECT);
                    preparedStatement.setString(1, msisdn);
                    // tracer.info(("[//] Executing DB Statement '" + DataSourceSchemaInfo._QUERY_SELECT).replaceFirst("\\?", msisdn));
                    preparedStatement.execute();
                    resultSet = preparedStatement.getResultSet();
                    while (resultSet.next()) {
                        balance = resultSet.getLong(DataSourceSchemaInfo._COL_BALANCE);
                        accountData.setBalance(balance);
                        accountData.setFailure(false);
                        ccUnit.setReservedUnits(requestedUnits);
                        ccUnit.setReservedAmount(requestedAmount);
                        ccInfo.setBalanceAfter(balance);
                    }
                } else {
                    // check what kind of error happened
                    accountData.setBalance(0);
                    accountData.setFailure(true);
                    ccUnit.setReservedUnits(0);
                    ccUnit.setReservedAmount(0);
                }
            }
        }
    } catch (Exception e) {
        tracer.severe("[xx] Failed to execute task to Reserve Units for MSISDN '" + msisdn + "'", e);
    }
    return this;
}
Also used : UserAccountData(org.mobicents.charging.server.data.UserAccountData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CreditControlUnit(org.mobicents.charging.server.account.CreditControlUnit)

Example 2 with UserAccountData

use of org.mobicents.charging.server.data.UserAccountData in project charging-server by RestComm.

the class AccountBalanceManagementSbb method getAccountDataResult.

// ---------------------- Datasource Child SBB Callbacks ------------------
@Override
public void getAccountDataResult(List<UserAccountData> result) {
    if (tracer.isInfoEnabled()) {
        tracer.info(String.format("%20s | %10s |", "User ID", "Balance"));
        tracer.info("---------------------+------------+");
        for (UserAccountData uad : result) {
            tracer.info(String.format("%20s | %10s |", uad.getMsisdn(), uad.getBalance()));
        }
    }
}
Also used : UserAccountData(org.mobicents.charging.server.data.UserAccountData)

Example 3 with UserAccountData

use of org.mobicents.charging.server.data.UserAccountData in project charging-server by RestComm.

the class GetAccountDataJdbcTask method executeSimple.

@Override
public Object executeSimple(JdbcTaskContext taskContext) {
    try {
        PreparedStatement preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_SELECT);
        preparedStatement.setString(1, msisdn);
        tracer.info(("[//] Executing DB Statement '" + DataSourceSchemaInfo._QUERY_SELECT).replaceFirst("\\?", msisdn));
        preparedStatement.execute();
        ResultSet resultSet = preparedStatement.getResultSet();
        accountDataList = new ArrayList<UserAccountData>();
        while (resultSet.next()) {
            UserAccountData accountData = new UserAccountData();
            accountData.setMsisdn(resultSet.getString(DataSourceSchemaInfo._COL_MSISDN));
            accountData.setBalance(resultSet.getLong(DataSourceSchemaInfo._COL_BALANCE));
            accountDataList.add(accountData);
        }
    } catch (Exception e) {
        tracer.severe("[xx] Failed to execute task to get Account Data for MSISDN '" + msisdn + "'", e);
    }
    return this;
}
Also used : UserAccountData(org.mobicents.charging.server.data.UserAccountData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with UserAccountData

use of org.mobicents.charging.server.data.UserAccountData in project charging-server by RestComm.

the class ReserveUnitsJdbcTask method executeSimple.

@Override
public Object executeSimple(JdbcTaskContext taskContext) {
    try {
        long balance = 0;
        // get Balance Before (can this be made more efficient?)
        PreparedStatement preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_SELECT);
        preparedStatement.setString(1, msisdn);
        preparedStatement.execute();
        ResultSet resultSet = preparedStatement.getResultSet();
        while (resultSet.next()) {
            balance = resultSet.getLong(DataSourceSchemaInfo._COL_BALANCE);
            ccInfo.setBalanceBefore(balance);
        }
        for (int i = 0; i < ccUnits.size(); i++) {
            CreditControlUnit ccUnit = ccUnits.get(i);
            if (balance <= 0 && ccUnit.getRateForService() > 0) {
                accountData = new UserAccountData();
                accountData.setMsisdn(msisdn);
                accountData.setBalance(0);
                accountData.setFailure(true);
                ccUnit.setReservedUnits(0);
                ccUnit.setReservedAmount(0);
                if (tracer.isInfoEnabled()) {
                    tracer.info("[//] User does not have sufficient balance for reservation. Balance available: " + balance + ".");
                }
                break;
            } else {
                long reservedAmount = ccUnit.getReservedAmount();
                long usedAmount = ccUnit.getUsedAmount();
                long requestedAmount = ccUnit.getRequestedAmount();
                long requestedUnits = ccUnit.getRequestedUnits();
                if (ccUnit.getRateForService() > 0) {
                    // If RSU < balance, reserve and set GSU=balance
                    if ((reservedAmount - usedAmount + requestedAmount) > balance) {
                        long newRequestedAmount = balance;
                        long newRequestedUnits = (long) Math.floor(newRequestedAmount / ccUnit.getRateForService());
                        if (tracer.isInfoEnabled()) {
                            tracer.info("[//] User does not have sufficient balance for the entire reservation request (" + requestedUnits + " " + ccUnit.getUnitType() + " units @rate=" + ccUnit.getRateForService() + "). Balance available: " + balance + ". Reserving " + newRequestedUnits + " units instead ...");
                        }
                        requestedAmount = newRequestedAmount;
                        requestedUnits = newRequestedUnits;
                    // TODO: Need to set Final Unit Indication for this case.
                    // See http://www.ietf.org/rfc/rfc4006.txt, 8.34.  Final-Unit-Indication AVP
                    }
                }
                int n = 1;
                tracer.info(("[//] Executing DB Statement '" + DataSourceSchemaInfo._QUERY_RESERVE).replaceFirst("\\?", String.valueOf(reservedAmount - usedAmount)).replaceFirst("\\?", String.valueOf(requestedAmount)).replaceFirst("\\?", String.valueOf(requestedAmount)).replaceFirst("\\?", msisdn));
                preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_RESERVE);
                preparedStatement.setLong(n++, (reservedAmount - usedAmount));
                preparedStatement.setLong(n++, requestedAmount);
                preparedStatement.setLong(n++, requestedAmount);
                preparedStatement.setString(n++, msisdn);
                accountData = new UserAccountData();
                accountData.setMsisdn(msisdn);
                if (preparedStatement.executeUpdate() == 1) {
                    // ok great, we have successfully reserved the units
                    preparedStatement = taskContext.getConnection().prepareStatement(DataSourceSchemaInfo._QUERY_SELECT);
                    preparedStatement.setString(1, msisdn);
                    // tracer.info(("[//] Executing DB Statement '" + DataSourceSchemaInfo._QUERY_SELECT).replaceFirst("\\?", msisdn));
                    preparedStatement.execute();
                    resultSet = preparedStatement.getResultSet();
                    while (resultSet.next()) {
                        balance = resultSet.getLong(DataSourceSchemaInfo._COL_BALANCE);
                        accountData.setBalance(balance);
                        accountData.setFailure(false);
                        ccUnit.setReservedUnits(requestedUnits);
                        ccUnit.setReservedAmount(requestedAmount);
                        ccInfo.setBalanceAfter(balance);
                    }
                } else {
                    // check what kind of error happened
                    accountData.setBalance(0);
                    accountData.setFailure(true);
                    ccUnit.setReservedUnits(0);
                    ccUnit.setReservedAmount(0);
                }
            }
        }
    } catch (Exception e) {
        tracer.severe("[xx] Failed to execute task to Reserve Units for MSISDN '" + msisdn + "'", e);
    }
    return this;
}
Also used : UserAccountData(org.mobicents.charging.server.data.UserAccountData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CreditControlUnit(org.mobicents.charging.server.account.CreditControlUnit)

Aggregations

UserAccountData (org.mobicents.charging.server.data.UserAccountData)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 CreditControlUnit (org.mobicents.charging.server.account.CreditControlUnit)2