From c1acf1baae598e7590dc3fa4b6da44c326c4f7bb Mon Sep 17 00:00:00 2001 From: CieyouRaoul Date: Thu, 10 Apr 2014 12:22:40 +0200 Subject: [PATCH] modified the ppi call to retrieve ppi entry. instead of returning the entry of the active ppi now return all the entries of a user --- .../survey/api/SurveyApiResource.java | 16 ++++- .../survey/data/ClientScoresOverview.java | 5 +- .../survey/service/ReadSurveyService.java | 1 + .../survey/service/ReadSurveyServiceImpl.java | 62 ++++++++++++++++++- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/api/SurveyApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/api/SurveyApiResource.java index f20b4d1e7..9c86440e1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/api/SurveyApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/api/SurveyApiResource.java @@ -121,11 +121,25 @@ public class SurveyApiResource { this.context.authenticatedUser().validateHasReadPermission(SurveyApiConstants.SURVEY_RESOURCE_NAME); - List scores = this.readSurveyService.retrieveClientSurveyScoreOverview(surveyName, clientId); + List scores = this.readSurveyService.retrieveClientSurveyScoreOverview(clientId); return this.toApiJsonClientScoreOverviewSerializer.serialize(scores); } +// @GET +// @Path("{clientId}") +// @Consumes({ MediaType.APPLICATION_JSON }) +// @Produces({ MediaType.APPLICATION_JSON }) +// public String getAllClientSurveyOverview( @PathParam("clientId") final Long clientId) { +// +// +// this.context.authenticatedUser().validateHasReadPermission(SurveyApiConstants.SURVEY_RESOURCE_NAME); +// +// List scores = this.readSurveyService.retrieveClientSurveyScoreOverview(clientId); +// +// return this.toApiJsonClientScoreOverviewSerializer.serialize(scores); +// } + @GET @Path("{surveyName}/{clientId}/{entryId}") @Consumes({ MediaType.APPLICATION_JSON }) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/data/ClientScoresOverview.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/data/ClientScoresOverview.java index 43a1a5138..7f8618fc3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/data/ClientScoresOverview.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/data/ClientScoresOverview.java @@ -7,6 +7,7 @@ import org.joda.time.LocalDate; */ public class ClientScoresOverview { + final private String surveyName; final private long id ; final private String likelihoodCode ; final private String likelihoodName; @@ -19,7 +20,8 @@ public class ClientScoresOverview { final long score, final Double povertyLine, final LocalDate date, - final long resourceId) { + final long resourceId, + final String surveyName) { this.likelihoodCode = likelihoodCode; this.likelihoodName = likelihoodName; @@ -27,6 +29,7 @@ public class ClientScoresOverview { this.povertyLine = povertyLine; this.date = date; this.id = resourceId; + this.surveyName = surveyName; } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyService.java index 94eb87cf7..54940b617 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyService.java @@ -17,6 +17,7 @@ public interface ReadSurveyService { SurveyDataTableData retrieveSurvey(String surveyName); ListretrieveClientSurveyScoreOverview(String surveyName, Long clientId); + ListretrieveClientSurveyScoreOverview(Long clientId); GenericResultsetData retrieveSurveyEntry(String surveyName, Long clientId, Long entryId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyServiceImpl.java index 595d4a2c2..e8121654a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/survey/service/ReadSurveyServiceImpl.java @@ -125,7 +125,7 @@ public class ReadSurveyServiceImpl implements ReadSurveyService { public List retrieveClientSurveyScoreOverview(String surveyName, Long clientId) { - final String sql = "SELECT tz.id, lkh.name, lkh.code, poverty_line, tz.date, tz.score FROM "+surveyName+" tz" + final String sql = "SELECT tz.id, lkh.name, lkh.code, poverty_line, tz.date, tz.score FROM "+surveyName+" tz" +" JOIN ppi_likelihoods_ppi lkp on lkp.ppi_name = '"+surveyName+"' AND enabled = '"+ LikelihoodStatus.ENABLED +"' JOIN ppi_scores sc on score_from <= tz.score AND score_to >=tz.score" +" JOIN ppi_poverty_line pvl on pvl.likelihood_ppi_id = lkp.id AND pvl.score_id = sc.id" @@ -144,7 +144,8 @@ public class ReadSurveyServiceImpl implements ReadSurveyService { rs.getLong("score"), rs.getDouble("poverty_line"), new LocalDate(rs.getTimestamp("date").getTime()), - rs.getLong("id") + rs.getLong("id"), + surveyName )); } @@ -152,6 +153,63 @@ public class ReadSurveyServiceImpl implements ReadSurveyService { return scoresOverviews ; } + public List retrieveClientSurveyScoreOverview(Long clientId) + { + final String surveyNameSql = retrieveAllSurveyNameSQL(); + final SqlRowSet surveyNames = this.jdbcTemplate.queryForRowSet(surveyNameSql); + + ArrayList sqls = new ArrayList(); + + while(surveyNames .next()) + { + sqls.add( "SELECT '"+surveyNames.getString("name") +"' as surveyName, tz.id, lkh.name, lkh.code, poverty_line, tz.date, tz.score FROM "+surveyNames.getString("name")+" tz" + +" JOIN ppi_likelihoods_ppi lkp on lkp.ppi_name = '"+surveyNames.getString("name")+"' AND enabled = '"+ LikelihoodStatus.ENABLED + +"' JOIN ppi_scores sc on score_from <= tz.score AND score_to >=tz.score" + +" JOIN ppi_poverty_line pvl on pvl.likelihood_ppi_id = lkp.id AND pvl.score_id = sc.id" + +" JOIN ppi_likelihoods lkh on lkh.id = lkp.likelihood_id " + +" WHERE client_id = "+clientId ); + } + + List scoresOverviews = new ArrayList(); + + for(String sql:sqls) + { + final SqlRowSet rs = this.jdbcTemplate.queryForRowSet(sql); + + + + while(rs.next()) + { + scoresOverviews.add(new ClientScoresOverview( + rs.getString("code"), + rs.getString("name"), + rs.getLong("score"), + rs.getDouble("poverty_line"), + new LocalDate(rs.getTimestamp("date").getTime()), + rs.getLong("id"), + rs.getString("surveyName") + )); + } + + } + + return scoresOverviews ; + } + + private String retrieveAllSurveyNameSQL() + { + // PERMITTED datatables + return "select cf.name from x_registered_table " + + " join c_configuration cf on x_registered_table.registered_table_name = cf.name " + + " where exists" + + " (select 'f'" + " from m_appuser_role ur " + " join m_role r on r.id = ur.role_id" + + " left join m_role_permission rp on rp.role_id = r.id" + " left join m_permission p on p.id = rp.permission_id" + + " where ur.appuser_id = " + this.context.authenticatedUser().getId() + + " and (p.code in ('ALL_FUNCTIONS', 'ALL_FUNCTIONS_READ') or p.code = concat('READ_', registered_table_name))) " + + " and x_registered_table.category = "+ DataTableApiConstant.CATEGORY_PPI + + " order by application_table_name, registered_table_name"; + } + public GenericResultsetData retrieveSurveyEntry(String surveyName, Long clientId, Long entryId) {