diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/EnumOptionData.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/EnumOptionData.java index aa34ddaad..35c3c011d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/EnumOptionData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/EnumOptionData.java @@ -26,4 +26,12 @@ public class EnumOptionData { public Long getId() { return id; } + + public String getCode() { + return code; + } + + public String getValue() { + return value; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java index c3dc9ae90..f115fc694 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java @@ -5,25 +5,6 @@ */ package org.mifosplatform.portfolio.calendar.api; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.UriInfo; - import org.mifosplatform.commands.domain.CommandWrapper; import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; @@ -37,12 +18,20 @@ import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext import org.mifosplatform.portfolio.calendar.data.CalendarData; import org.mifosplatform.portfolio.calendar.domain.Calendar; import org.mifosplatform.portfolio.calendar.domain.CalendarEntityType; +import org.mifosplatform.portfolio.calendar.domain.CalendarType; import org.mifosplatform.portfolio.calendar.service.CalendarDropdownReadPlatformService; +import org.mifosplatform.portfolio.calendar.service.CalendarEnumerations; import org.mifosplatform.portfolio.calendar.service.CalendarReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; +import java.util.*; + @Path("/{entityType}/{entityId}/calendars") @Component @Scope("singleton") @@ -102,23 +91,24 @@ public class CalendarsApiResource { @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public String retrieveCalendarsByEntity(@PathParam("entityType") final String entityType, @PathParam("entityId") final Long entityId, - @Context final UriInfo uriInfo) { + @Context final UriInfo uriInfo, @QueryParam("calendarType") String calendarType) { this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions); final Set associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters()); Collection calendarsData = new ArrayList(); + List calendarTypeOptions = createEnumOptionDataListFromQueryParameter(calendarType); if (!associationParameters.isEmpty()) { if (associationParameters.contains("parentCalendars")) { calendarsData.addAll(this.readPlatformService.retrieveParentCalendarsByEntity(entityId, - CalendarEntityType.valueOf(entityType.toUpperCase()).getValue())); + CalendarEntityType.valueOf(entityType.toUpperCase()).getValue(),calendarTypeOptions)); } } calendarsData.addAll(this.readPlatformService.retrieveCalendarsByEntity(entityId, - CalendarEntityType.valueOf(entityType.toUpperCase()).getValue())); + CalendarEntityType.valueOf(entityType.toUpperCase()).getValue(),calendarTypeOptions)); // Add recurring dates calendarsData = this.readPlatformService.generateRecurringDates(calendarsData); @@ -192,4 +182,29 @@ public class CalendarsApiResource { final List remindByOptions = this.dropdownReadPlatformService.retrieveCalendarRemindByOptions(); return new CalendarData(calendarData, entityTypeOptions, calendarTypeOptions, remindByOptions); } + + + public List createEnumOptionDataListFromQueryParameter(String calendarTypeQuery) { + List calendarTypeOptions = null; + // creating a list of calendar type options from the comma separated query parameter. + List calendarTypeOptionsInQuery = new ArrayList(); + StringTokenizer st = new StringTokenizer(calendarTypeQuery, ","); + while (st.hasMoreElements()) { + calendarTypeOptionsInQuery.add(st.nextElement().toString()); + } + + for(String calType : calendarTypeOptionsInQuery){ + if(calType.equalsIgnoreCase("collection")) { + calendarTypeOptions.add(CalendarEnumerations.calendarType(CalendarType.COLLECTION)); + } else if(calType.equalsIgnoreCase("training")) { + calendarTypeOptions.add(CalendarEnumerations.calendarType(CalendarType.TRAINING)); + } else if(calType.equalsIgnoreCase("audit")) { + calendarTypeOptions.add(CalendarEnumerations.calendarType(CalendarType.AUDIT)); + } else if(calType.equalsIgnoreCase("general")) { + calendarTypeOptions.add(CalendarEnumerations.calendarType(CalendarType.GENERAL)); + } + } + return calendarTypeOptions; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformService.java index dc75f2f9f..3a70381a3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformService.java @@ -5,17 +5,19 @@ */ package org.mifosplatform.portfolio.calendar.service; -import java.util.Collection; - +import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.portfolio.calendar.data.CalendarData; +import java.util.Collection; +import java.util.List; + public interface CalendarReadPlatformService { CalendarData retrieveCalendar(final Long calendarId, Long entityId, Integer entityTypeId); - Collection retrieveCalendarsByEntity(final Long entityId, final Integer entityTypeId); + Collection retrieveCalendarsByEntity(final Long entityId, final Integer entityTypeId, List calendarTypeOptions); - Collection retrieveParentCalendarsByEntity(final Long entityId, final Integer entityTypeId); + Collection retrieveParentCalendarsByEntity(final Long entityId, final Integer entityTypeId, List calendarTypeOptions); Collection retrieveAllCalendars(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformServiceImpl.java index fe82f7991..c8d28d219 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarReadPlatformServiceImpl.java @@ -5,11 +5,6 @@ */ package org.mifosplatform.portfolio.calendar.service; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; - import org.joda.time.LocalDate; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.infrastructure.core.domain.JdbcSupport; @@ -25,6 +20,12 @@ import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + @Service public class CalendarReadPlatformServiceImpl implements CalendarReadPlatformService { @@ -98,24 +99,43 @@ public class CalendarReadPlatformServiceImpl implements CalendarReadPlatformServ } @Override - public Collection retrieveCalendarsByEntity(final Long entityId, final Integer entityTypeId) { + public Collection retrieveCalendarsByEntity(final Long entityId, final Integer entityTypeId, List calendarTypeOptions) { final CalendarDataMapper rm = new CalendarDataMapper(); - - final String sql = rm.schema() + " and ci.entity_id = ? and ci.entity_type_enum = ? order by c.start_date "; - - return this.jdbcTemplate.query(sql, rm, new Object[] { entityId, entityTypeId }); + // the collection result will store the final result + Collection result=null; + // string sql is the sql statement that will be executed by jdbc. + String sql=""; + // if calendarType options are not given by user then return all calendars. + if(calendarTypeOptions.isEmpty()) { + sql = rm.schema() + " and ci.entity_id = ? and ci.entity_type_enum = ? order by c.start_date "; + result = this.jdbcTemplate.query(sql, rm, new Object[] { entityId, entityTypeId }); + } else if(!calendarTypeOptions.isEmpty()){ + String sqlCalendarTypeOptions = createSqlValuesInString(calendarTypeOptions); + sql = rm.schema() + " and ci.entity_id = ? and ci.entity_type_enum = ? and c.calendar_type_enum in ? order by c.start_date "; + result = this.jdbcTemplate.query(sql,rm, new Object[] {entityId, entityTypeId, sqlCalendarTypeOptions}); + } + return result; } - + @Override - public Collection retrieveParentCalendarsByEntity(Long entityId, Integer entityTypeId) { - + public Collection retrieveParentCalendarsByEntity( Long entityId, Integer entityTypeId, List calendarTypeOptions ) { + final CalendarDataMapper rm = new CalendarDataMapper(); + Collection result=null; + String sql=""; CalendarEntityType ceType = CalendarEntityType.fromInt(entityTypeId); String parentHeirarchyCondition = getParentHierarchyCondition(ceType); - final String sql = rm.schema() + " " + parentHeirarchyCondition - + " and ci.entity_type_enum = ? order by c.start_date "; + //FIXME :AA center is the parent entity of group, change this code to support more parent entity types. - return this.jdbcTemplate.query(sql, rm, new Object[] { entityId, CalendarEntityType.CENTERS.getValue() }); + if(calendarTypeOptions.isEmpty()){ + sql = rm.schema() + " " + parentHeirarchyCondition + " and ci.entity_type_enum = ? order by c.start_date "; + result = this.jdbcTemplate.query(sql, rm, new Object[] { entityId, CalendarEntityType.CENTERS.getValue() }); + } else { + String sqlCalendarTypeOptions = createSqlValuesInString(calendarTypeOptions); + sql = rm.schema() + " " + parentHeirarchyCondition + " and ci.entity_type_enum = ? and c.calendar_type_enum in ? order by c.start_date "; + result = this.jdbcTemplate.query(sql,rm, new Object[] {entityId, entityTypeId, sqlCalendarTypeOptions}); + } + return result; } @Override @@ -210,4 +230,23 @@ public class CalendarReadPlatformServiceImpl implements CalendarReadPlatformServ return conditionSql; } + /** + * This method takes the List of calendarType options given in the request and creates a string to be used in the sql statement. + * for ex. (1,2,3,4) + * @param calendarTypeOptions + * @return + */ + public String createSqlValuesInString(List calendarTypeOptions){ + int size = calendarTypeOptions.size(); + String sqlCalendarTypeOptions = "(" + calendarTypeOptions.get(0).getId().toString(); + for(int i=1;i