<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.mifosplatform.portfolio.floatingrates.service;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.joda.time.LocalDate;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.portfolio.floatingrates.data.FloatingRateData;
import org.mifosplatform.portfolio.floatingrates.data.FloatingRatePeriodData;
import org.mifosplatform.portfolio.floatingrates.data.InterestRatePeriodData;
import org.mifosplatform.portfolio.floatingrates.exception.FloatingRateNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service
public class FloatingRatesReadPlatformServiceImpl implements
		FloatingRatesReadPlatformService {

	private final JdbcTemplate jdbcTemplate;

	@Autowired
	public FloatingRatesReadPlatformServiceImpl(
			final RoutingDataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public List<FloatingRateData> retrieveAll() {
		FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(false);
		final String sql = "select " + rateMapper.schema();
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public List<FloatingRateData> retrieveAllActive() {
		FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(false);
		final String sql = "select " + rateMapper.schema()
				+ " where rate.is_active = 1 ";
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public List<FloatingRateData> retrieveLookupActive() {
		FloatingRateLookupMapper rateMapper = new FloatingRateLookupMapper();
		final String sql = "select " + rateMapper.schema()
				+ " where rate.is_active = 1 ";
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public FloatingRateData retrieveOne(final Long floatingRateId) {
		try {
			FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(true);
			final String sql = "select " + rateMapper.schema()
					+ " where rate.id = ?";
			return this.jdbcTemplate.queryForObject(sql, rateMapper,
					new Object[] { floatingRateId });
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(floatingRateId);
		}
	}

	@Override
	public List<InterestRatePeriodData> retrieveInterestRatePeriods(
			final Long floatingRateId) {
		try {
			FloatingInterestRatePeriodRowMapper mapper = new FloatingInterestRatePeriodRowMapper();
			return this.jdbcTemplate.query(mapper.schema(), mapper,
					new Object[] { floatingRateId });
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(floatingRateId);
		}
	}

	@Override
	public FloatingRateData retrieveBaseLendingRate() {
		try {
			FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(true);
			final String sql = "select "
					+ rateMapper.schema()
					+ " where rate.is_base_lending_rate = 1 and rate.is_active = 1";
			return this.jdbcTemplate.queryForObject(sql, rateMapper);
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(
					"error.msg.floatingrate.base.lending.rate.not.found");
		}
	}

	private final class FloatingRateRowMapper implements
			RowMapper<FloatingRateData> {

		private final boolean addRatePeriods;

		private final StringBuilder sqlQuery = new StringBuilder()
				.append("rate.id as id, ")
				.append("rate.name as name, ")
				.append("rate.is_base_lending_rate as isBaseLendingRate, ")
				.append("rate.is_active as isActive, ")
				.append("crappu.username as createdBy, ")
				.append("rate.created_date as createdOn, ")
				.append("moappu.username as modifiedBy, ")
				.append("rate.lastmodified_date as modifiedOn ")
				.append("FROM m_floating_rates as rate ")
				.append("LEFT JOIN m_appuser as crappu on rate.createdby_id = crappu.id ")
				.append("LEFT JOIN m_appuser as moappu on rate.lastmodifiedby_id = moappu.id ");

		public FloatingRateRowMapper(final boolean addRatePeriods) {
			this.addRatePeriods = addRatePeriods;
		}

		@Override
		public FloatingRateData mapRow(final ResultSet rs,
				@SuppressWarnings("unused") final int rowNum)
				throws SQLException {
			final Long id = rs.getLong("id");
			final String name = rs.getString("name");
			final boolean isBaseLendingRate = rs
					.getBoolean("isBaseLendingRate");
			final boolean isActive = rs.getBoolean("isActive");
			final String createdBy = rs.getString("createdBy");
			final LocalDate createdOn = JdbcSupport.getLocalDate(rs, "createdOn");
			final String modifiedBy = rs.getString("modifiedBy");
			final LocalDate modifiedOn = JdbcSupport.getLocalDate(rs, "modifiedOn");
			List<FloatingRatePeriodData> ratePeriods = null;
			if (addRatePeriods) {
				FloatingRatePeriodRowMapper ratePeriodMapper = new FloatingRatePeriodRowMapper();
				final String sql = "select "
						+ ratePeriodMapper.schema()
						+ " where period.is_active = 1 and period.floating_rates_id = ? "
						+ " order by period.from_date desc ";
				ratePeriods = jdbcTemplate.query(sql, ratePeriodMapper,
						new Object[] { id });
			}
			return new FloatingRateData(id, name, isBaseLendingRate, isActive,
					createdBy, createdOn, modifiedBy, modifiedOn, ratePeriods,<commit_msg>Fixing ordering of Rate periods while retrieving floating rate data<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.mifosplatform.portfolio.floatingrates.service;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.joda.time.LocalDate;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.portfolio.floatingrates.data.FloatingRateData;
import org.mifosplatform.portfolio.floatingrates.data.FloatingRatePeriodData;
import org.mifosplatform.portfolio.floatingrates.data.InterestRatePeriodData;
import org.mifosplatform.portfolio.floatingrates.exception.FloatingRateNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service
public class FloatingRatesReadPlatformServiceImpl implements
		FloatingRatesReadPlatformService {

	private final JdbcTemplate jdbcTemplate;

	@Autowired
	public FloatingRatesReadPlatformServiceImpl(
			final RoutingDataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public List<FloatingRateData> retrieveAll() {
		FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(false);
		final String sql = "select " + rateMapper.schema();
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public List<FloatingRateData> retrieveAllActive() {
		FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(false);
		final String sql = "select " + rateMapper.schema()
				+ " where rate.is_active = 1 ";
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public List<FloatingRateData> retrieveLookupActive() {
		FloatingRateLookupMapper rateMapper = new FloatingRateLookupMapper();
		final String sql = "select " + rateMapper.schema()
				+ " where rate.is_active = 1 ";
		return this.jdbcTemplate.query(sql, rateMapper);
	}

	@Override
	public FloatingRateData retrieveOne(final Long floatingRateId) {
		try {
			FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(true);
			final String sql = "select " + rateMapper.schema()
					+ " where rate.id = ?";
			return this.jdbcTemplate.queryForObject(sql, rateMapper,
					new Object[] { floatingRateId });
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(floatingRateId);
		}
	}

	@Override
	public List<InterestRatePeriodData> retrieveInterestRatePeriods(
			final Long floatingRateId) {
		try {
			FloatingInterestRatePeriodRowMapper mapper = new FloatingInterestRatePeriodRowMapper();
			return this.jdbcTemplate.query(mapper.schema(), mapper,
					new Object[] { floatingRateId });
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(floatingRateId);
		}
	}

	@Override
	public FloatingRateData retrieveBaseLendingRate() {
		try {
			FloatingRateRowMapper rateMapper = new FloatingRateRowMapper(true);
			final String sql = "select "
					+ rateMapper.schema()
					+ " where rate.is_base_lending_rate = 1 and rate.is_active = 1";
			return this.jdbcTemplate.queryForObject(sql, rateMapper);
		} catch (final EmptyResultDataAccessException e) {
			throw new FloatingRateNotFoundException(
					"error.msg.floatingrate.base.lending.rate.not.found");
		}
	}

	private final class FloatingRateRowMapper implements
			RowMapper<FloatingRateData> {

		private final boolean addRatePeriods;

		private final StringBuilder sqlQuery = new StringBuilder()
				.append("rate.id as id, ")
				.append("rate.name as name, ")
				.append("rate.is_base_lending_rate as isBaseLendingRate, ")
				.append("rate.is_active as isActive, ")
				.append("crappu.username as createdBy, ")
				.append("rate.created_date as createdOn, ")
				.append("moappu.username as modifiedBy, ")
				.append("rate.lastmodified_date as modifiedOn ")
				.append("FROM m_floating_rates as rate ")
				.append("LEFT JOIN m_appuser as crappu on rate.createdby_id = crappu.id ")
				.append("LEFT JOIN m_appuser as moappu on rate.lastmodifiedby_id = moappu.id ");

		public FloatingRateRowMapper(final boolean addRatePeriods) {
			this.addRatePeriods = addRatePeriods;
		}

		@Override
		public FloatingRateData mapRow(final ResultSet rs,
				@SuppressWarnings("unused") final int rowNum)
				throws SQLException {
			final Long id = rs.getLong("id");
			final String name = rs.getString("name");
			final boolean isBaseLendingRate = rs
					.getBoolean("isBaseLendingRate");
			final boolean isActive = rs.getBoolean("isActive");
			final String createdBy = rs.getString("createdBy");
			final LocalDate createdOn = JdbcSupport.getLocalDate(rs, "createdOn");
			final String modifiedBy = rs.getString("modifiedBy");
			final LocalDate modifiedOn = JdbcSupport.getLocalDate(rs, "modifiedOn");
			List<FloatingRatePeriodData> ratePeriods = null;
			if (addRatePeriods) {
				FloatingRatePeriodRowMapper ratePeriodMapper = new FloatingRatePeriodRowMapper();
				final String sql = "select "
						+ ratePeriodMapper.schema()
						+ " where period.is_active = 1 and period.floating_rates_id = ? "
						+ " order by period.from_date ";
				ratePeriods = jdbcTemplate.query(sql, ratePeriodMapper,
						new Object[] { id });
			}
			return new FloatingRateData(id, name, isBaseLendingRate, isActive,
					createdBy, createdOn, modifiedBy, modifiedOn, ratePeriods,
{'fraction_non_alphanumeric': 0.09422360786351433, 'fraction_numerical': 0.001558846453624318, 'mean_word_length': 4.980321077162092, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3529755', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3417, 'n_words': 775}