From efe0379258de48c7582297fc53ff2254833ad3d3 Mon Sep 17 00:00:00 2001 From: Nayan Ambali Date: Wed, 10 Apr 2013 16:22:46 +0530 Subject: [PATCH] Groups api for centers/groups --- .../migrations/V15__center_permissions.sql | 7 ++ .../commands/domain/CommandWrapper.java | 4 + .../service/CommandWrapperBuilder.java | 23 +++++ .../SynchronousCommandProcessingService.java | 11 ++- ...ollectionSheetReadPlatformServiceImpl.java | 3 +- ...iResource.java => CentersApiResource.java} | 95 ++++++++----------- .../group/api/GroupsApiResource.java | 18 ++-- .../portfolio/group/data/GroupTypes.java | 72 ++++++++++++++ .../handler/CreateCenterCommandHandler.java | 32 +++++++ .../handler/CreateGroupCommandHandler.java | 5 +- .../handler/DeleteCenterCommandHandler.java | 31 ++++++ .../handler/UpdateCenterCommandHandler.java | 32 +++++++ .../GroupCommandFromApiJsonDeserializer.java | 10 +- .../service/GroupReadPlatformService.java | 5 +- .../service/GroupReadPlatformServiceImpl.java | 14 +-- .../group/service/GroupTypeEnumerations.java | 34 +++++++ .../service/GroupWritePlatformService.java | 3 +- ...WritePlatformServiceJpaRepositoryImpl.java | 21 +--- .../service/LoanReadPlatformServiceImpl.java | 5 +- ...SavingsAccountReadPlatformServiceImpl.java | 3 +- 20 files changed, 327 insertions(+), 101 deletions(-) create mode 100644 mifosng-db/migrations/V15__center_permissions.sql rename mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/{CenterApiResource.java => CentersApiResource.java} (71%) create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupTypes.java create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateCenterCommandHandler.java create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/DeleteCenterCommandHandler.java create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/UpdateCenterCommandHandler.java create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupTypeEnumerations.java diff --git a/mifosng-db/migrations/V15__center_permissions.sql b/mifosng-db/migrations/V15__center_permissions.sql new file mode 100644 index 000000000..95482dc2b --- /dev/null +++ b/mifosng-db/migrations/V15__center_permissions.sql @@ -0,0 +1,7 @@ +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'READ_CENTER', 'CENTER', 'READ', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'CREATE_CENTER', 'CENTER', 'CREATE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'CREATE_CENTER_CHECKER', 'CENTER', 'CREATE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'UPDATE_CENTER', 'CENTER', 'UPDATE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'UPDATE_CENTER_CHECKER', 'CENTER', 'UPDATE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'DELETE_CENTER', 'CENTER', 'DELETE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('portfolio', 'DELETE_CENTER_CHECKER', 'CENTER', 'DELETE', 0); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java index 0b2e4bdd6..778c20a5d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java @@ -476,4 +476,8 @@ public class CommandWrapper { return this.entityName.equals("COLLECTIONSHEET"); } + public boolean isCenterResource() { + return this.entityName.equalsIgnoreCase("CENTER"); + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java index 3aaeeee4e..a25beb74c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java @@ -853,4 +853,27 @@ public class CommandWrapperBuilder { this.href = "/groups/" + groupId + "/collectionsheet"; return this; } + + public CommandWrapperBuilder createCenter() { + this.actionName = "CREATE"; + this.entityName = "CENTER"; + this.href = "/centers/template"; + return this; + } + + public CommandWrapperBuilder updateCenter(Long centerId) { + this.actionName = "UPDATE"; + this.entityName = "CENTER"; + this.entityId = centerId; + this.href = "/centers/" + centerId; + return this; + } + + public CommandWrapperBuilder deleteCenter(Long centerId) { + this.actionName = "DELETE"; + this.entityName = "CENTER"; + this.entityId = centerId; + this.href = "/centers/" + centerId; + return this; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java index 0898782aa..3f8723ec1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java @@ -397,7 +397,16 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer } else { throw new UnsupportedCommandException(wrapper.commandName()); } - + } else if (wrapper.isCenterResource()) { + if (wrapper.isCreate()) { + handler = applicationContext.getBean("createCenterCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isUpdate()) { + handler = applicationContext.getBean("updateCenterCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isDelete()) { + handler = applicationContext.getBean("deleteCenterCommandHandler", NewCommandSourceHandler.class); + } else { + throw new UnsupportedCommandException(wrapper.commandName()); + } } else if (wrapper.isCollectionSheetResource()) { if (wrapper.isUpdate()) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/collectionsheet/service/CollectionSheetReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/collectionsheet/service/CollectionSheetReadPlatformServiceImpl.java index 4341e301d..d75b130c5 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/collectionsheet/service/CollectionSheetReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/collectionsheet/service/CollectionSheetReadPlatformServiceImpl.java @@ -20,6 +20,7 @@ import org.mifosplatform.portfolio.collectionsheet.data.JLGCollectionSheetData; import org.mifosplatform.portfolio.collectionsheet.data.JLGCollectionSheetFlatData; import org.mifosplatform.portfolio.collectionsheet.data.LoanDueData; import org.mifosplatform.portfolio.group.data.GroupData; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.exception.GroupNotFoundException; import org.mifosplatform.portfolio.group.service.GroupReadPlatformServiceImpl; import org.mifosplatform.portfolio.loanproduct.data.LoanProductData; @@ -53,7 +54,7 @@ public class CollectionSheetReadPlatformServiceImpl implements CollectionSheetRe final String hierarchy = currentUser.getOffice().getHierarchy(); final String officeHierarchy = hierarchy + "%"; - final GroupData groupData = this.groupReadPlatformServiceImpl.retrieveGroup(groupId); + final GroupData groupData = this.groupReadPlatformServiceImpl.retrieveGroup(groupId , GroupTypes.CENTER.getId()); if (groupData == null) { throw new GroupNotFoundException(groupId); } final String groupHierarchy = groupData.getHierarchy() + "%"; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CenterApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CentersApiResource.java similarity index 71% rename from mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CenterApiResource.java rename to mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CentersApiResource.java index 0306ddcc1..a3e774c04 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CenterApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/CentersApiResource.java @@ -35,20 +35,21 @@ import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupData; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.service.GroupReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; -@Path("/groups") +@Path("/centers") @Component @Scope("singleton") -public class CenterApiResource { +public class CentersApiResource { /* * GROUP_DATA_PARAMETERS is used by ToApiJsonSerializer, make sure E's properties and E_PARAMETERS are in same */ - private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "name", "externalId", "officeId", + private static final Set CENTER_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "name", "externalId", "officeId", "officeName", "staffId", "staffName", "parentId", "parentName", "hierarchy", "groupSummaryData", "groupLevelData", "clientMembers", "allowedClients", "allowedOffices", "allowedParentGroups", "allowedStaffs", "childGroups")); @@ -58,9 +59,11 @@ public class CenterApiResource { private final ToApiJsonSerializer groupSummaryToApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; + + private static final Long LEVEL_ID = GroupTypes.CENTER.getId(); @Autowired - public CenterApiResource(final PlatformSecurityContext context, final GroupReadPlatformService groupReadPlatformService, + public CentersApiResource(final PlatformSecurityContext context, final GroupReadPlatformService groupReadPlatformService, final ToApiJsonSerializer toApiJsonSerializer, final ToApiJsonSerializer groupSummaryToApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, @@ -76,80 +79,62 @@ public class CenterApiResource { @GET @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String retrieveAllGroups(@Context final UriInfo uriInfo, @QueryParam("sqlSearch") final String sqlSearch, + public String retrieveAllCenters(@Context final UriInfo uriInfo, @QueryParam("sqlSearch") final String sqlSearch, @QueryParam("officeId") final Long officeId, @QueryParam("externalId") final String externalId, - @QueryParam("name") final String name, @QueryParam("underHierarchy") final String hierarchy, - @QueryParam("levelId") final Long levelId) { + @QueryParam("name") final String name , @QueryParam("underHierarchy") final String hierarchy) { - this.context.authenticatedUser().validateHasReadPermission("GROUP"); + this.context.authenticatedUser().validateHasReadPermission("CENTER"); - final String extraCriteria = getGroupExtraCriteria(sqlSearch, officeId, externalId, name, hierarchy, levelId); - final Collection groups = this.groupReadPlatformService.retrieveAllGroups(extraCriteria); + + final String extraCriteria = getCenterExtraCriteria(sqlSearch, officeId, externalId, name , LEVEL_ID , hierarchy); + final Collection centers = this.groupReadPlatformService.retrieveAllGroups(extraCriteria); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.toApiJsonSerializer.serialize(settings, groups, GROUP_DATA_PARAMETERS); + return this.toApiJsonSerializer.serialize(settings, centers, CENTER_DATA_PARAMETERS); } @GET - @Path("{groupId}") + @Path("{centerId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String retrieveGroupData(@Context final UriInfo uriInfo, @PathParam("groupId") final Long groupId) { + public String retrieveCenterData(@Context final UriInfo uriInfo, @PathParam("centerId") final Long centerId) { - this.context.authenticatedUser().validateHasReadPermission("GROUP"); + this.context.authenticatedUser().validateHasReadPermission("CENTER"); final boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); - final GroupData group = this.groupReadPlatformService.retrieveGroupDetails(groupId, template); + final GroupData group = this.groupReadPlatformService.retrieveGroupDetails(centerId, LEVEL_ID, template); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.toApiJsonSerializer.serialize(settings, group, GROUP_DATA_PARAMETERS); + return this.toApiJsonSerializer.serialize(settings, group, CENTER_DATA_PARAMETERS); } @GET @Path("template") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String newGroupDetails(@Context final UriInfo uriInfo, @QueryParam("officeId") final Long officeId, - @QueryParam("levelId") final Long levelId, @QueryParam("parentGroupId") final Long parentGroupId) { + public String newGroupDetails(@Context final UriInfo uriInfo, @QueryParam("officeId") final Long officeId) { - this.context.authenticatedUser().validateHasReadPermission("GROUP"); + this.context.authenticatedUser().validateHasReadPermission("CENTER"); - GroupData groupTemplateData = null; - if (levelId != null && parentGroupId != null) { - groupTemplateData = this.groupReadPlatformService.retrieveNewChildGroupDetails(officeId, levelId, parentGroupId); - } else if (officeId != null && levelId != null) { - groupTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(officeId, levelId); - } else if (levelId != null) { - groupTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(this.context.authenticatedUser().getOffice().getId(), - levelId); + GroupData centerTemplateData = null; + if (officeId != null) { + centerTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(officeId, LEVEL_ID); + } else { + centerTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(this.context.authenticatedUser().getOffice().getId(), + LEVEL_ID); } final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.toApiJsonSerializer.serialize(settings, groupTemplateData, GROUP_DATA_PARAMETERS); + return this.toApiJsonSerializer.serialize(settings, centerTemplateData, CENTER_DATA_PARAMETERS); } @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String createGroup(final String apiRequestBodyAsJson) { + public String createCenter(final String apiRequestBodyAsJson) { final CommandWrapper commandRequest = new CommandWrapperBuilder() // - .createGroup() // - .withJson(apiRequestBodyAsJson) // - .build(); // - final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); - return this.toApiJsonSerializer.serialize(result); - - } - - @POST - @Path("{groupId}/command/unassign_staff") - @Consumes({ MediaType.APPLICATION_JSON }) - @Produces({ MediaType.APPLICATION_JSON }) - public String unassignLoanOfficer(@PathParam("groupId") final Long groupId, final String apiRequestBodyAsJson) { - - final CommandWrapper commandRequest = new CommandWrapperBuilder() // - .unassignStaff(groupId) // + .createCenter() // .withJson(apiRequestBodyAsJson) // .build(); // final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -158,13 +143,13 @@ public class CenterApiResource { } @PUT - @Path("{groupId}") + @Path("{centerId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String updateGroup(@PathParam("groupId") final Long groupId, final String apiRequestBodyAsJson) { + public String updateGroup(@PathParam("centerId") final Long centerId, final String apiRequestBodyAsJson) { final CommandWrapper commandRequest = new CommandWrapperBuilder() // - .updateGroup(groupId) // + .updateCenter(centerId) // .withJson(apiRequestBodyAsJson) // .build(); // final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -172,13 +157,13 @@ public class CenterApiResource { } @DELETE - @Path("{groupId}") + @Path("{centerId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String deleteGroup(@PathParam("groupId") final Long groupId) { + public String deleteGroup(@PathParam("centerId") final Long centerId) { final CommandWrapper commandRequest = new CommandWrapperBuilder() // - .deleteGroup(groupId) // + .deleteCenter(centerId) // .build(); // final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.toApiJsonSerializer.serialize(result); @@ -206,10 +191,9 @@ public class CenterApiResource { // 'g.' preffix because of ERROR 1052 (23000): Column 'column_name' in where // clause is ambiguous // caused by the same name of columns in m_office and m_group tables - private String getGroupExtraCriteria(String sqlSearch, final Long officeId, final String externalId, final String name, - final String hierarchy, final Long levelId) { + private String getCenterExtraCriteria(String sqlSearch, final Long officeId, final String externalId, final String name , final Long levelId ,final String hierarchy) { - String extraCriteria = ""; + String extraCriteria = " and g.level_id = " + LEVEL_ID + " "; if (sqlSearch != null) { sqlSearch = sqlSearch.replaceAll(" name ", " g.name "); @@ -233,10 +217,11 @@ public class CenterApiResource { extraCriteria += " and g.name like " + ApiParameterHelper.sqlEncodeString(name + "%"); } + if (hierarchy != null) { extraCriteria += " and o.hierarchy like " + ApiParameterHelper.sqlEncodeString(hierarchy + "%"); } - + if (StringUtils.isNotBlank(extraCriteria)) { extraCriteria = extraCriteria.substring(4); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java index e76688120..dc2b45ba4 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java @@ -35,6 +35,7 @@ import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupData; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.service.GroupReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -59,7 +60,7 @@ public class GroupsApiResource { private final ApiRequestParameterHelper apiRequestParameterHelper; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; - private static final Long LEVEL_ID = (long) 2; + private static final Long LEVEL_ID = GroupTypes.GROUP.getId(); @Autowired public GroupsApiResource(final PlatformSecurityContext context, final GroupReadPlatformService groupReadPlatformService, @@ -80,11 +81,11 @@ public class GroupsApiResource { @Produces({ MediaType.APPLICATION_JSON }) public String retrieveAllGroups(@Context final UriInfo uriInfo, @QueryParam("sqlSearch") final String sqlSearch, @QueryParam("officeId") final Long officeId, @QueryParam("externalId") final String externalId, - @QueryParam("name") final String name) { + @QueryParam("name") final String name , @QueryParam("underHierarchy") final String hierarchy) { this.context.authenticatedUser().validateHasReadPermission("GROUP"); - final String extraCriteria = getGroupExtraCriteria(sqlSearch, officeId, externalId, name , LEVEL_ID); + final String extraCriteria = getGroupExtraCriteria(sqlSearch, officeId, externalId, name , LEVEL_ID , hierarchy); final Collection groups = this.groupReadPlatformService.retrieveAllGroups(extraCriteria); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); @@ -100,7 +101,7 @@ public class GroupsApiResource { this.context.authenticatedUser().validateHasReadPermission("GROUP"); final boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); - final GroupData group = this.groupReadPlatformService.retrieveGroupDetails(groupId, template); + final GroupData group = this.groupReadPlatformService.retrieveGroupDetails(groupId, LEVEL_ID, template); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); return this.toApiJsonSerializer.serialize(settings, group, GROUP_DATA_PARAMETERS); @@ -208,10 +209,10 @@ public class GroupsApiResource { // clause is ambiguous // caused by the same name of columns in m_office and m_group tables private String getGroupExtraCriteria(String sqlSearch, final Long officeId, final String externalId, final String name, - final Long levelId) { + final Long levelId , final String hierarchy) { String extraCriteria = ""; - + if (sqlSearch != null) { sqlSearch = sqlSearch.replaceAll(" name ", " g.name "); sqlSearch = sqlSearch.replaceAll("name ", "g.name "); @@ -234,6 +235,11 @@ public class GroupsApiResource { extraCriteria += " and g.name like " + ApiParameterHelper.sqlEncodeString(name + "%"); } + + if (hierarchy != null) { + extraCriteria += " and o.hierarchy like " + ApiParameterHelper.sqlEncodeString(hierarchy + "%"); + } + if (StringUtils.isNotBlank(extraCriteria)) { extraCriteria = extraCriteria.substring(4); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupTypes.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupTypes.java new file mode 100644 index 000000000..92ae44760 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupTypes.java @@ -0,0 +1,72 @@ +package org.mifosplatform.portfolio.group.data; + +import java.util.HashMap; +import java.util.Map; + +public enum GroupTypes { + + CENTER(new Long(1), "groupTypes.center", "center"), // + GROUP(new Long(2), "groupTypes.group", "group"), // + INVALID(new Long(99), "lendingStrategy.invalid", "invalid"); + + private Long id; + private String code; + private String value; + + GroupTypes(final Long id, final String code, final String value) { + this.id = id; + this.code = code; + this.value = value; + } + + private static final Map intToEnumMap = new HashMap(); + private static long minValue; + private static long maxValue; + static { + int i = 0; + for (final GroupTypes type : GroupTypes.values()) { + if (i == 0) { + minValue = type.id; + } + intToEnumMap.put(type.id, type); + if (minValue >= type.id) { + minValue = type.id; + } + if (maxValue < type.id) { + maxValue = type.id; + } + i = i + 1; + } + } + + public static GroupTypes fromInt(final int i) { + final GroupTypes type = intToEnumMap.get(Integer.valueOf(i)); + return type; + } + + public static long getMinValue() { + return minValue; + } + + public static long getMaxValue() { + return maxValue; + } + + @Override + public String toString() { + return name().toString(); + } + + public Long getId() { + return this.id; + } + + public String getCode() { + return this.code; + } + + public String getValue() { + return this.value; + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateCenterCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateCenterCommandHandler.java new file mode 100644 index 000000000..2ea3d46a1 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateCenterCommandHandler.java @@ -0,0 +1,32 @@ +/** + * 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.group.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.portfolio.group.service.GroupWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class CreateCenterCommandHandler implements NewCommandSourceHandler { + + private final GroupWritePlatformService groupWritePlatformService; + private static final Long LEVEL_ID = (long) 1; + + @Autowired + public CreateCenterCommandHandler(final GroupWritePlatformService groupWritePlatformService) { + this.groupWritePlatformService = groupWritePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(final JsonCommand command) { + return this.groupWritePlatformService.createGroup(null , LEVEL_ID , command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateGroupCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateGroupCommandHandler.java index dd8454ba0..6bca12166 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateGroupCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/CreateGroupCommandHandler.java @@ -8,6 +8,7 @@ package org.mifosplatform.portfolio.group.handler; import org.mifosplatform.commands.handler.NewCommandSourceHandler; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.service.GroupWritePlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional; public class CreateGroupCommandHandler implements NewCommandSourceHandler { private final GroupWritePlatformService groupWritePlatformService; + private static final Long LEVEL_ID = GroupTypes.GROUP.getId();; @Autowired public CreateGroupCommandHandler(final GroupWritePlatformService groupWritePlatformService) { @@ -27,6 +29,7 @@ public class CreateGroupCommandHandler implements NewCommandSourceHandler { @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.groupWritePlatformService.createGroup(command); + final Long centerId = command.longValueOfParameterNamed("centerId"); + return this.groupWritePlatformService.createGroup(centerId , LEVEL_ID , command); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/DeleteCenterCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/DeleteCenterCommandHandler.java new file mode 100644 index 000000000..464a38780 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/DeleteCenterCommandHandler.java @@ -0,0 +1,31 @@ +/** + * 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.group.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.portfolio.group.service.GroupWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class DeleteCenterCommandHandler implements NewCommandSourceHandler { + + private final GroupWritePlatformService groupWritePlatformService; + + @Autowired + public DeleteCenterCommandHandler(final GroupWritePlatformService groupWritePlatformService) { + this.groupWritePlatformService = groupWritePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(final JsonCommand command) { + return this.groupWritePlatformService.deleteGroup(command.entityId()); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/UpdateCenterCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/UpdateCenterCommandHandler.java new file mode 100644 index 000000000..2be7f6574 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/UpdateCenterCommandHandler.java @@ -0,0 +1,32 @@ +/** + * 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.group.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.portfolio.group.service.GroupWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class UpdateCenterCommandHandler implements NewCommandSourceHandler { + + private final GroupWritePlatformService groupWritePlatformService; + + @Autowired + public UpdateCenterCommandHandler(final GroupWritePlatformService groupWritePlatformService) { + this.groupWritePlatformService = groupWritePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(final JsonCommand command) { + + return this.groupWritePlatformService.updateGroup(command.entityId(), command); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java index b2ec935c8..ad66c1812 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java @@ -40,7 +40,7 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs * The parameters supported for this command. */ private final Set supportedParameters = new HashSet(Arrays.asList("id", "externalId", "name", "officeId", "staffId", - "parentId", "clientMembers", "childGroups")); + "centerId", "clientMembers", "childGroups")); private final FromJsonHelper fromApiJsonHelper; @@ -98,10 +98,10 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs * Either OfficeId or ParentId is mandatory to have value */ - final String parentIdParameterName = "parentId"; - if (this.fromApiJsonHelper.parameterExists(parentIdParameterName, element)) { - final Long parentId = this.fromApiJsonHelper.extractLongNamed(parentIdParameterName, element); - baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).notNull().integerGreaterThanZero(); + final String centerIdParameterName = "centerId"; + if (this.fromApiJsonHelper.parameterExists(centerIdParameterName, element)) { + final Long centerId = this.fromApiJsonHelper.extractLongNamed(centerIdParameterName, element); + baseDataValidator.reset().parameter(centerIdParameterName).value(centerId).notNull().integerGreaterThanZero(); }else{ final String officeIdParameterName = "officeId"; final Long officeId = this.fromApiJsonHelper.extractLongNamed(officeIdParameterName, element); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java index 4c9b057b8..9f0774784 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java @@ -20,7 +20,7 @@ public interface GroupReadPlatformService { Collection retrieveAllGroups(String extraCriteria); - GroupData retrieveGroup(Long groupId); + GroupData retrieveGroup(Long groupId, Long levelId); GroupData retrieveNewGroupDetails(Long officeId, Long levelId); @@ -36,7 +36,7 @@ public interface GroupReadPlatformService { Collection retrieveStaffsbyOfficeId(Long officeId); - GroupData retrieveGroupDetails(Long groupId, boolean template); + GroupData retrieveGroupDetails(Long groupId, Long levelId, boolean template); Collection retrieveChildGroupsbyGroupId(Long groupId); @@ -51,4 +51,5 @@ public interface GroupReadPlatformService { StaffData retrieveStaffsbyId(Long staffId); Long getLevelIdByGroupId(final Long groupId); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java index 95437d620..6aeff92f2 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java @@ -72,7 +72,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { final GroupDataMapper rm = new GroupDataMapper(); - String sql = "select " + rm.groupSchema() + " where o.hierarchy like ? and g.is_deleted=0"; + String sql = "select " + rm.groupSchema() + " where o.hierarchy like ? and g.is_deleted=0 "; if (StringUtils.isNotBlank(extraCriteria)) { sql += " and (" + extraCriteria + ")"; @@ -84,14 +84,14 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } @Override - public GroupData retrieveGroup(final Long groupId) { + public GroupData retrieveGroup(final Long groupId , final Long levelId) { try { this.context.authenticatedUser(); final GroupDataMapper rm = new GroupDataMapper(); - final String sql = "select " + rm.groupSchema() + " where g.id = ? and g.is_deleted=0"; + final String sql = "select " + rm.groupSchema() + " where g.id = ? and g.is_deleted=0 and g.level_id = " + levelId; return this.jdbcTemplate.queryForObject(sql, rm, new Object[] { groupId }); } catch (final EmptyResultDataAccessException e) { @@ -207,9 +207,9 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } @Override - public GroupData retrieveGroupDetails(final Long groupId, final boolean template) { + public GroupData retrieveGroupDetails(final Long groupId, final Long levelId , final boolean template) { - GroupData group = retrieveGroup(groupId); + GroupData group = retrieveGroup(groupId, levelId); final Collection clientMembers = retrieveClientMembers(groupId); Collection availableClients = null; Collection allowedOffices = null; @@ -467,7 +467,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { this.context.authenticatedUser(); // Check if group exists - retrieveGroup(groupId); + //retrieveGroup(groupId); final List pendingApprovalLoans = new ArrayList(); final List awaitingDisbursalLoans = new ArrayList(); @@ -534,7 +534,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { this.context.authenticatedUser(); // Check if group exists - retrieveGroup(groupId); + //retrieveGroup(groupId); final GroupLoanAccountSummaryDataMapper rm = new GroupLoanAccountSummaryDataMapper(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupTypeEnumerations.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupTypeEnumerations.java new file mode 100644 index 000000000..4a05233e8 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupTypeEnumerations.java @@ -0,0 +1,34 @@ +/** + * 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.group.service; + +import org.mifosplatform.infrastructure.core.data.EnumOptionData; +import org.mifosplatform.portfolio.group.data.GroupTypes; + +public class GroupTypeEnumerations { + + public static EnumOptionData groupType(final Integer id) { + return groupType(GroupTypes.fromInt(id)); + } + + public static EnumOptionData groupType(final GroupTypes type) { + EnumOptionData optionData = null; + switch (type) { + case CENTER: + optionData = new EnumOptionData(type.getId().longValue(), type.getCode(), "Individual loan"); + break; + case GROUP: + optionData = new EnumOptionData(type.getId().longValue(), type.getCode(), "Group loan"); + break; + default: + optionData = new EnumOptionData(GroupTypes.INVALID.getId().longValue(), GroupTypes.INVALID.getCode() , "Invalid"); + break; + + } + return optionData; + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformService.java index 536b79422..6f7d49a85 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformService.java @@ -10,7 +10,8 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; public interface GroupWritePlatformService { - CommandProcessingResult createGroup(JsonCommand command); + CommandProcessingResult createGroup(Long parentId, Long levelId, + JsonCommand command); CommandProcessingResult updateGroup(Long grouptId, JsonCommand command); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java index 4f206cba8..67eddf9ed 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java @@ -72,9 +72,9 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla @Transactional @Override - public CommandProcessingResult createGroup(final JsonCommand command) { + public CommandProcessingResult createGroup(final Long parentId , final Long levelId , final JsonCommand command) { - GroupLevel groupLevel = null; + GroupLevel groupLevel = this.groupLevelRepository.findOne(levelId); try { this.context.authenticatedUser(); @@ -83,33 +83,16 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla final String name = command.stringValueOfParameterNamed("name"); final String externalId = command.stringValueOfParameterNamed("externalId"); - final Long parentId = command.longValueOfParameterNamed("parentId"); - Long officeId = null; Group parentGroup = null; if (parentId == null) { - /* - * Scenario: Creating parent group [group with highest level] - * In-case of Centre/Groups, parent group with highest level is - * Centre. In-case of Federation/Cluster/SHG, parent group with - * highest level is Federation. In-case of - * Village/Centre/Groups, parent group with highest level is - * Village. - */ - officeId = command.longValueOfParameterNamed("officeId"); - groupLevel = this.groupLevelRepository.findBySuperParent(true); - } else { - parentGroup = this.groupRepository.findOne(parentId); if (parentGroup == null || parentGroup.isDeleted()) { throw new GroupNotFoundException(parentId); } - officeId = parentGroup.getOfficeId(); - groupLevel = this.groupLevelRepository.findByParentId(parentGroup.getGroupLevel().getId()); - } final Office groupOffice = this.officeRepository.findOne(officeId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java index 34f7087f6..1a456510f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java @@ -27,6 +27,7 @@ import org.mifosplatform.organisation.monetary.exception.CurrencyNotFoundExcepti import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.mifosplatform.portfolio.group.data.GroupData; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.service.GroupReadPlatformService; import org.mifosplatform.portfolio.loanaccount.data.DisbursementData; import org.mifosplatform.portfolio.loanaccount.data.LoanAccountData; @@ -161,7 +162,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { context.authenticatedUser(); - final GroupData groupAccount = this.groupReadPlatformService.retrieveGroupDetails(groupId, false); + final GroupData groupAccount = this.groupReadPlatformService.retrieveGroupDetails(groupId , GroupTypes.GROUP.getId() , false); final LocalDate expectedDisbursementDate = DateUtils.getLocalDateOfTenant(); LoanAccountData loanDetails = LoanAccountData.groupDefaults(groupAccount, expectedDisbursementDate); @@ -178,7 +179,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { context.authenticatedUser(); - final GroupData groupAccount = this.groupReadPlatformService.retrieveGroup(groupId); + final GroupData groupAccount = this.groupReadPlatformService.retrieveGroup(groupId , GroupTypes.GROUP.getId() ); final LocalDate expectedDisbursementDate = DateUtils.getLocalDateOfTenant(); LoanAccountData loanDetails = LoanAccountData.groupDefaults(groupAccount, expectedDisbursementDate); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsAccountReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsAccountReadPlatformServiceImpl.java index 329960b25..6b2368d73 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsAccountReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsAccountReadPlatformServiceImpl.java @@ -20,6 +20,7 @@ import org.mifosplatform.organisation.monetary.data.CurrencyData; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.mifosplatform.portfolio.group.data.GroupData; +import org.mifosplatform.portfolio.group.data.GroupTypes; import org.mifosplatform.portfolio.group.service.GroupReadPlatformService; import org.mifosplatform.portfolio.savings.data.SavingsAccountData; import org.mifosplatform.portfolio.savings.data.SavingsAccountStatusEnumData; @@ -212,7 +213,7 @@ public class SavingsAccountReadPlatformServiceImpl implements SavingsAccountRead GroupData group = null; if (groupId != null) { - group = this.groupReadPlatformService.retrieveGroup(groupId); + group = this.groupReadPlatformService.retrieveGroup(groupId , GroupTypes.GROUP.getId()); } final Collection productOptions = this.savingsProductReadPlatformService.retrieveAllForLookup();