assign staff for group/center(improvement)

This commit is contained in:
goutham-M
2013-08-01 17:53:58 +05:30
parent ce93ec677e
commit 425f5f3822
10 changed files with 109 additions and 4 deletions
@@ -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)
@@ -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;
}
@@ -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";
@@ -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()) {
@@ -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);
@@ -380,4 +380,5 @@ public final class Group extends AbstractPersistable<Long> {
public Office getOffice() {
return this.office;
}
}
@@ -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);
}
}
@@ -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<Map<String, Object>>() {}.getType();
final Set<String> supportedParametersAssignStaff = new HashSet<String>(Arrays.asList(GroupingTypesApiConstants.staffIdParamName));
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersAssignStaff);
final JsonElement element = this.fromApiJsonHelper.parse(json);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
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(); }
@@ -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);
@@ -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<String, Object> actualChanges = new LinkedHashMap<String, Object>(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) {