diff --git a/mifosng-db/migrations/core_db/V92__group_center_assign_staff_permission.sql b/mifosng-db/migrations/core_db/V92__group_center_assign_staff_permission.sql new file mode 100644 index 000000000..2f8eaa718 --- /dev/null +++ b/mifosng-db/migrations/core_db/V92__group_center_assign_staff_permission.sql @@ -0,0 +1,2 @@ +INSERT INTO `m_permission` (grouping,code,`entity_name`,`action_name`,`can_maker_checker') +values('portfolio_group','ASSIGNSTAFF_GROUP','GROUP','ASSIGNSTAFF',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 483bc88ff..3385bdee5 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 @@ -413,7 +413,11 @@ public class CommandWrapper { public boolean isUnassignStaff() { return this.actionName.equalsIgnoreCase("UNASSIGNSTAFF") && this.entityName.equalsIgnoreCase("GROUP"); } - + + public boolean isAssignStaff() { + return this.actionName.equalsIgnoreCase("ASSIGNSTAFF"); + } + public String commandName() { return this.actionName + "_" + this.entityName; } 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 282259cd5..6da48c263 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 @@ -990,7 +990,16 @@ public class CommandWrapperBuilder { this.href = "/groups/" + groupId; return this; } - + + public CommandWrapperBuilder assignGroupStaff(final Long groupId) { + this.actionName = "ASSIGNSTAFF"; + this.entityName = "GROUP"; + this.entityId = groupId; + this.groupId = groupId; + this.href = "/groups/" + groupId +"?command=assignStaff"; + return this; + } + public CommandWrapperBuilder createCollateral(final Long loanId) { this.actionName = "CREATE"; this.entityName = "COLLATERAL"; 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 80df9e704..f8f3614b2 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 @@ -441,7 +441,9 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer handler = this.applicationContext.getBean("unassignRoleCommandHandler", NewCommandSourceHandler.class); } else if (wrapper.isUpdateGroupRole()) { handler = this.applicationContext.getBean("updateGroupRoleCommandHandler", NewCommandSourceHandler.class); - } else { + } else if (wrapper.isAssignStaff()) { + handler = this.applicationContext.getBean("assignGroupStaffCommandHandler", NewCommandSourceHandler.class); + }else { throw new UnsupportedCommandException(wrapper.commandName()); } } else if (wrapper.isCenterResource()) { 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 465507b64..6d47d5812 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 @@ -222,7 +222,7 @@ public class GroupsApiResource { return this.toApiJsonSerializer.serialize(result); } - + @PUT @Path("{groupId}") @Consumes({ MediaType.APPLICATION_JSON }) @@ -287,6 +287,10 @@ public class GroupsApiResource { final CommandWrapper commandRequest = builder.unassignGroupStaff(groupId).build(); result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.toApiJsonSerializer.serialize(result); + } else if (is(commandParam, "assignStaff")) { + final CommandWrapper commandRequest = builder.assignGroupStaff(groupId).build(); + result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + return this.toApiJsonSerializer.serialize(result); } else if (is(commandParam, "assignRole")) { final CommandWrapper commandRequest = builder.assignRole(groupId).build(); result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java index 5270fedcf..79f6d0bd0 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java @@ -380,4 +380,5 @@ public final class Group extends AbstractPersistable { public Office getOffice() { return this.office; } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/AssignGroupStaffCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/AssignGroupStaffCommandHandler.java new file mode 100644 index 000000000..1896e641b --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/handler/AssignGroupStaffCommandHandler.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.GroupingTypesWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class AssignGroupStaffCommandHandler implements NewCommandSourceHandler { + + private final GroupingTypesWritePlatformService groupWritePlatformService; + + @Autowired + public AssignGroupStaffCommandHandler(final GroupingTypesWritePlatformService groupWritePlatformService) { + this.groupWritePlatformService = groupWritePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(final JsonCommand command) { + return this.groupWritePlatformService.assignGroupOrCenterStaff(command.entityId(), command); + } +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java index bd0b242c5..60813c17a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java @@ -345,6 +345,28 @@ public final class GroupingTypesDataValidator { if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } } + public void validateForAssignStaff(final String json) { + if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } + + final Type typeOfMap = new TypeToken>() {}.getType(); + + final Set supportedParametersAssignStaff = new HashSet(Arrays.asList(GroupingTypesApiConstants.staffIdParamName)); + + this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersAssignStaff); + final JsonElement element = this.fromApiJsonHelper.parse(json); + + final List dataValidationErrors = new ArrayList(); + + final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) + .resource(GroupingTypesApiConstants.GROUP_RESOURCE_NAME); + + final String staffIdParameterName = GroupingTypesApiConstants.staffIdParamName; + final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); + baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().longGreaterThanZero(); + + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } + } + public void validateForAssociateClients(final String json){ if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java index 351f4a3d7..928379deb 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java @@ -24,6 +24,8 @@ public interface GroupingTypesWritePlatformService { CommandProcessingResult unassignGroupOrCenterStaff(Long groupId, JsonCommand command); + CommandProcessingResult assignGroupOrCenterStaff(Long groupId, JsonCommand command); + CommandProcessingResult associateClientsToGroup(Long groupId, JsonCommand command); CommandProcessingResult disassociateClientsFromGroup(Long groupId, JsonCommand command); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java index 2dd5288d2..e5b2ce415 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -350,7 +350,35 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group .build(); } + + @Override + public CommandProcessingResult assignGroupOrCenterStaff(final Long grouptId, final JsonCommand command) { + this.context.authenticatedUser(); + + final Map actualChanges = new LinkedHashMap(5); + + this.fromApiJsonDeserializer.validateForAssignStaff(command.json()); + + final Group groupForUpdate = this.groupRepository.findOneWithNotFoundDetection(grouptId); + + Staff staff = null; + final Long staffId = command.longValueOfParameterNamed(GroupingTypesApiConstants.staffIdParamName); + staff = this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(staffId, groupForUpdate.getOffice().getHierarchy()); + groupForUpdate.updateStaff(staff); + + this.groupRepository.saveAndFlush(groupForUpdate); + + actualChanges.put(GroupingTypesApiConstants.staffIdParamName, staffId); + + return new CommandProcessingResultBuilder() // + .withOfficeId(groupForUpdate.officeId()) // + .withEntityId(groupForUpdate.getId()) // + .withGroupId(grouptId) // + .with(actualChanges) // + .build(); + } + @Transactional @Override public CommandProcessingResult deleteGroup(final Long groupId) {