From 2dc27fa4eb7340e1b135a365d0f530aaa2b48e9a Mon Sep 17 00:00:00 2001 From: Anuruddha Premalal Date: Sun, 5 May 2013 13:36:47 +0530 Subject: [PATCH] Implemented severside sorting and pagination for journal entries --- .../api/JournalEntriesApiResource.java | 29 +++---- .../JournalEntryReadPlatformService.java | 10 +-- .../JournalEntryReadPlatformServiceImpl.java | 86 +++++++++++-------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java index 5ecb419ec..150cf6757 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java @@ -8,7 +8,6 @@ package org.mifosplatform.accounting.journalentry.api; import java.util.Arrays; import java.util.Date; import java.util.HashSet; -import java.util.List; import java.util.Set; import javax.ws.rs.Consumes; @@ -33,6 +32,7 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.exception.UnrecognizedQueryParamException; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; +import org.mifosplatform.infrastructure.core.service.Page; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -72,15 +72,16 @@ public class JournalEntriesApiResource { @GET @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String retrieveAllJournalEntries(@Context final UriInfo uriInfo, @QueryParam("officeId") final Long officeId, - @QueryParam("glAccountId") final Long glAccountId, @QueryParam("manualEntriesOnly") final Boolean onlyManualEntries, - @QueryParam("fromDate") final DateParam fromDateParam, @QueryParam("toDate") final DateParam toDateParam, - @QueryParam("transactionId") final String transactionId ) { + public String retrieveAll(@QueryParam("officeId") final Long officeId, @QueryParam("glAccountId") final Long glAccountId, + @QueryParam("manualEntriesOnly") final Boolean onlyManualEntries, @QueryParam("fromDate") final DateParam fromDateParam, + @QueryParam("toDate") final DateParam toDateParam, @QueryParam("transactionId") final String transactionId, + @QueryParam("offset") final Integer offset, @QueryParam("limit") final Integer limit, + @QueryParam("orderBy") final String orderBy, @QueryParam("sortOrder") final String sortOrder) { this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermission); - List glJournalEntryDatas = null; - // get dates from date params + Page glJournalEntries = null; + Date fromDate = null; if (fromDateParam != null) { fromDate = fromDateParam.getDate(); @@ -89,17 +90,11 @@ public class JournalEntriesApiResource { if (toDateParam != null) { toDate = toDateParam.getDate(); } - if (StringUtils.isBlank(transactionId)) { - glJournalEntryDatas = this.journalEntryReadPlatformService.retrieveAllGLJournalEntries(officeId, glAccountId, - onlyManualEntries, fromDate, toDate); - } else { - glJournalEntryDatas = this.journalEntryReadPlatformService.retrieveRelatedJournalEntries(transactionId); - } - - final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.apiJsonSerializerService.serialize(settings, glJournalEntryDatas, RESPONSE_DATA_PARAMETERS); + glJournalEntries = this.journalEntryReadPlatformService.retrieveAll(officeId, glAccountId, onlyManualEntries, fromDate, toDate, + offset, limit, orderBy, sortOrder, transactionId); + return this.apiJsonSerializerService.serialize(glJournalEntries); } - + @GET @Path("{journalEntryId}") @Consumes({ MediaType.APPLICATION_JSON }) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformService.java index df25841e5..54bb0d71b 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformService.java @@ -6,17 +6,15 @@ package org.mifosplatform.accounting.journalentry.service; import java.util.Date; -import java.util.List; import org.mifosplatform.accounting.journalentry.data.JournalEntryData; +import org.mifosplatform.infrastructure.core.service.Page; public interface JournalEntryReadPlatformService { - List retrieveAllGLJournalEntries(Long officeId, Long glAccountId, Boolean onlyManualEntries, Date fromDate, - Date toDate); - - List retrieveRelatedJournalEntries(String transactionId); - JournalEntryData retrieveGLJournalEntryById(long glJournalEntryId); + Page retrieveAll(Long officeId, Long glAccountId, Boolean onlyManualEntries, Date fromDate, Date toDate, + Integer offset, Integer limit, String orderBy, String sortOrder, String transactionId); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java index 632e9d433..0e4c61add 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java @@ -12,14 +12,16 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; -import java.util.List; +import org.apache.commons.lang.StringUtils; import org.joda.time.LocalDate; import org.mifosplatform.accounting.common.AccountingEnumerations; import org.mifosplatform.accounting.journalentry.data.JournalEntryData; import org.mifosplatform.accounting.journalentry.exception.JournalEntriesNotFoundException; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.infrastructure.core.domain.JdbcSupport; +import org.mifosplatform.infrastructure.core.service.Page; +import org.mifosplatform.infrastructure.core.service.PaginationHelper; import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; @@ -31,6 +33,9 @@ import org.springframework.stereotype.Service; public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlatformService { private final JdbcTemplate jdbcTemplate; + + private final GLJournalEntryMapper journalEntryMapper = new GLJournalEntryMapper(); + private final PaginationHelper paginationHelper = new PaginationHelper(); @Autowired public JournalEntryReadPlatformServiceImpl(final TenantAwareRoutingDataSource dataSource) { @@ -40,8 +45,8 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat private static final class GLJournalEntryMapper implements RowMapper { public String schema() { - return " journalEntry.id as journalEntryId, glAccount.classification_enum as classification ," - + " glAccount.name as glAccountName, glAccount.gl_code as glCode, journalEntry.account_id as glAccountId," + return " journalEntry.id as id, glAccount.classification_enum as classification ," + + " glAccount.name as glAccountName, glAccount.gl_code as glAccountCode, journalEntry.account_id as glAccountId," + " journalEntry.office_id as officeId, office.name as officeName, journalEntry.ref_num as referenceNumber, " + " journalEntry.manual_entry as manualEntry,journalEntry.entry_date as transactionDate, " + " journalEntry.type_enum as entryType,journalEntry.amount as amount, journalEntry.transaction_id as transactionId," @@ -56,10 +61,10 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat @Override public JournalEntryData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { - final Long journalEntryId = rs.getLong("journalEntryId"); + final Long id = rs.getLong("id"); final Long officeId = rs.getLong("officeId"); final String officeName = rs.getString("officeName"); - final String glCode = rs.getString("glCode"); + final String glCode = rs.getString("glAccountCode"); final String glAccountName = rs.getString("glAccountName"); final Long glAccountId = rs.getLong("glAccountId"); final int accountTypeId = JdbcSupport.getInteger(rs, "classification"); @@ -84,29 +89,38 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat final Boolean reversed = rs.getBoolean("reversed"); final String referenceNumber = rs.getString("referenceNumber"); - return new JournalEntryData(journalEntryId, officeId, officeName, glAccountName, glAccountId, glCode, accountType, + return new JournalEntryData(id, officeId, officeName, glAccountName, glAccountId, glCode, accountType, transactionDate, entryType, amount, transactionId, manualEntry, entityType, entityId, createdByUserId, createdDate, createdByUserName, comments, reversed, referenceNumber); } } @Override - public List retrieveAllGLJournalEntries(final Long officeId, final Long glAccountId, final Boolean onlyManualEntries, - final Date fromDate, final Date toDate) { - final GLJournalEntryMapper rm = new GLJournalEntryMapper(); + public Page retrieveAll(final Long officeId, final Long glAccountId, final Boolean onlyManualEntries, + final Date fromDate, final Date toDate, final Integer offset, final Integer limit, final String orderBy, + final String sortOrder, final String transactionId) { - String sql = "select " + rm.schema(); - final Object[] objectArray = new Object[4]; + StringBuilder sqlBuilder = new StringBuilder(200); + sqlBuilder.append("select SQL_CALC_FOUND_ROWS "); + sqlBuilder.append(journalEntryMapper.schema()); + + final Object[] objectArray = new Object[5]; int arrayPos = 0; + if (StringUtils.isNotBlank(transactionId)) { + sqlBuilder.append(" and journalEntry.transaction_id = ?"); + objectArray[arrayPos] = transactionId; + arrayPos = arrayPos + 1; + } + if (officeId != null && officeId != 0) { - sql += " and journalEntry.office_id = ?"; + sqlBuilder.append(" and journalEntry.office_id = ?"); objectArray[arrayPos] = officeId; arrayPos = arrayPos + 1; } if (glAccountId != null && glAccountId != 0) { - sql += " and journalEntry.account_id = ?"; + sqlBuilder.append(" and journalEntry.account_id = ?"); objectArray[arrayPos] = glAccountId; arrayPos = arrayPos + 1; } @@ -116,7 +130,7 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat String fromDateString = null; String toDateString = null; if (fromDate != null && toDate != null) { - sql += " and journalEntry.entry_date between ? and ? "; + sqlBuilder.append(" and journalEntry.entry_date between ? and ? "); fromDateString = df.format(fromDate); toDateString = df.format(toDate); objectArray[arrayPos] = fromDateString; @@ -124,12 +138,12 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat objectArray[arrayPos] = toDateString; arrayPos = arrayPos + 1; } else if (fromDate != null) { - sql += " and journalEntry.entry_date >= ? "; + sqlBuilder.append(" and journalEntry.entry_date >= ? "); fromDateString = df.format(fromDate); objectArray[arrayPos] = fromDateString; arrayPos = arrayPos + 1; } else if (toDate != null) { - sql += " and journalEntry.entry_date <= ? "; + sqlBuilder.append(" and journalEntry.entry_date <= ? "); toDateString = df.format(toDate); objectArray[arrayPos] = toDateString; arrayPos = arrayPos + 1; @@ -138,14 +152,33 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat if (onlyManualEntries != null) { if (onlyManualEntries) { - sql += " and journalEntry.manual_entry = 1"; + sqlBuilder.append(" and journalEntry.manual_entry = 1"); } } - sql += " order by journalEntry.entry_date desc,journalEntry.id desc"; + if (StringUtils.isNotBlank(orderBy)) { + sqlBuilder.append(" order by ").append(orderBy); + if (StringUtils.isNotBlank(sortOrder)) { + sqlBuilder.append(' ').append(sortOrder); + } + } + + if (limit != null && limit > 0) { + Integer maxLimitAllowed = 200; + if (limit < maxLimitAllowed) { + maxLimitAllowed = limit; + } + sqlBuilder.append(" limit ").append(maxLimitAllowed); + } + + if (offset != null) { + sqlBuilder.append(" offset ").append(offset); + } final Object[] finalObjectArray = Arrays.copyOf(objectArray, arrayPos); - return this.jdbcTemplate.query(sql, rm, finalObjectArray); + final String sqlCountRows = "SELECT FOUND_ROWS()"; + return this.paginationHelper.fetchPage(this.jdbcTemplate, sqlCountRows, sqlBuilder.toString(), finalObjectArray, + this.journalEntryMapper); } @Override @@ -163,19 +196,4 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat } } - @Override - public List retrieveRelatedJournalEntries(final String transactionId) { - try { - - final GLJournalEntryMapper rm = new GLJournalEntryMapper(); - final String sql = "select " + rm.schema() + " and journalEntry.transaction_id = ?"; - - final List journalEntryDatas = this.jdbcTemplate.query(sql, rm, new Object[] { transactionId }); - - return journalEntryDatas; - } catch (final EmptyResultDataAccessException e) { - throw new JournalEntriesNotFoundException(transactionId); - } - } - }