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

This commit is contained in:
CieyouRaoul
2014-04-10 12:22:40 +02:00
parent 59429326d1
commit c1acf1baae
4 changed files with 80 additions and 4 deletions
@@ -121,11 +121,25 @@ public class SurveyApiResource {
this.context.authenticatedUser().validateHasReadPermission(SurveyApiConstants.SURVEY_RESOURCE_NAME);
List<ClientScoresOverview> scores = this.readSurveyService.retrieveClientSurveyScoreOverview(surveyName, clientId);
List<ClientScoresOverview> 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<ClientScoresOverview> scores = this.readSurveyService.retrieveClientSurveyScoreOverview(clientId);
//
// return this.toApiJsonClientScoreOverviewSerializer.serialize(scores);
// }
@GET
@Path("{surveyName}/{clientId}/{entryId}")
@Consumes({ MediaType.APPLICATION_JSON })
@@ -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;
}
}
@@ -17,6 +17,7 @@ public interface ReadSurveyService {
SurveyDataTableData retrieveSurvey(String surveyName);
List<ClientScoresOverview>retrieveClientSurveyScoreOverview(String surveyName, Long clientId);
List<ClientScoresOverview>retrieveClientSurveyScoreOverview(Long clientId);
GenericResultsetData retrieveSurveyEntry(String surveyName, Long clientId, Long entryId);
@@ -125,7 +125,7 @@ public class ReadSurveyServiceImpl implements ReadSurveyService {
public List<ClientScoresOverview> 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<ClientScoresOverview> retrieveClientSurveyScoreOverview(Long clientId)
{
final String surveyNameSql = retrieveAllSurveyNameSQL();
final SqlRowSet surveyNames = this.jdbcTemplate.queryForRowSet(surveyNameSql);
ArrayList<String> sqls = new ArrayList<String>();
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<ClientScoresOverview> scoresOverviews = new ArrayList<ClientScoresOverview>();
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)
{