From 47be8388561bf9df0bf4fcbbf33f230fa23a39b4 Mon Sep 17 00:00:00 2001 From: Michal Dudzinski Date: Thu, 2 Aug 2012 12:12:56 +0200 Subject: [PATCH] FIXED MIFOSX-6: Implement basic api for Creating a Group --- .../platform/api/GroupsApiResource.java | 41 ++++++++++++++++++ .../platform/api/commands/GroupCommand.java | 26 +++++++++++ .../ApiDataConversionService.java | 3 ++ .../ApiDataConversionServiceImpl.java | 24 +++++++++++ .../mifosng/platform/group/domain/Group.java | 43 +++++++++++++++++++ .../group/domain/GroupRepository.java | 8 ++++ .../group/service/GroupCommandValidator.java | 33 ++++++++++++++ .../service/GroupWritePlatformService.java | 12 ++++++ ...WritePlatformServiceJpaRepositoryImpl.java | 41 ++++++++++++++++++ 9 files changed, 231 insertions(+) create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/GroupsApiResource.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/commands/GroupCommand.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/group/domain/Group.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/group/domain/GroupRepository.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupCommandValidator.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformService.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/GroupsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/GroupsApiResource.java new file mode 100644 index 000000000..636ab698b --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/GroupsApiResource.java @@ -0,0 +1,41 @@ +package org.mifosng.platform.api; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.mifosng.platform.api.commands.GroupCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.mifosng.platform.api.infrastructure.ApiDataConversionService; +import org.mifosng.platform.group.service.GroupWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Path("/groups") +@Component +@Scope("singleton") +public class GroupsApiResource { + + @Autowired + private GroupWritePlatformService groupWritePlatformService; + + @Autowired + private ApiDataConversionService apiDataConversionService; + + @POST + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response createGroup(final String jsonRequestBody) { + + final GroupCommand command = this.apiDataConversionService.convertJsonToGroupCommand(null, jsonRequestBody); + + EntityIdentifier entityIdentifier = this.groupWritePlatformService.createGroup(command); + + return Response.ok().entity(entityIdentifier).build(); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/GroupCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/GroupCommand.java new file mode 100644 index 000000000..9837812c9 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/GroupCommand.java @@ -0,0 +1,26 @@ +package org.mifosng.platform.api.commands; + +import java.util.Set; + +public class GroupCommand { + + private final String externalId; + private final String name; + + private final Set modifiedParameters; + + public GroupCommand(Set modifiedParameters, String externalId, String name) { + this.externalId = externalId; + this.name = name; + this.modifiedParameters = modifiedParameters; + } + + public String getExternalId() { + return externalId; + } + + public String getName() { + return name; + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index 8a924c04b..9ff5d75f7 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -6,6 +6,7 @@ import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.FundCommand; +import org.mifosng.platform.api.commands.GroupCommand; import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanTransactionCommand; @@ -92,6 +93,8 @@ public interface ApiDataConversionService { ClientCommand convertJsonToClientCommand(Long resourceIdentifier, String jsonRequestBody); + GroupCommand convertJsonToGroupCommand(Long resourceIdentifier, String jsonRequestBody); + SubmitLoanApplicationCommand convertJsonToSubmitLoanApplicationCommand(String jsonRequestBody); LoanStateTransitionCommand convertJsonToLoanStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index db004cc63..7e0842c98 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -22,6 +22,7 @@ import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.FundCommand; +import org.mifosng.platform.api.commands.GroupCommand; import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanTransactionCommand; @@ -818,6 +819,29 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } @Override + public GroupCommand convertJsonToGroupCommand(final Long resourceIdentifier, final String json) { + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + Type typeOfMap = new TypeToken>(){}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); + + Set supportedParams = new HashSet( + Arrays.asList("name", "externalId") + ); + + checkForUnsupportedParameters(requestMap, supportedParams); + + Set modifiedParameters = new HashSet(); + + String externalId = extractStringParameter("externalId", requestMap, modifiedParameters); + String name = extractStringParameter("name", requestMap, modifiedParameters); + + return new GroupCommand(modifiedParameters, externalId, name); + } + + @Override public LoanProductCommand convertJsonToLoanProductCommand(final Long resourceIdentifier, final String json) { if (StringUtils.isBlank(json)) { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/Group.java b/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/Group.java new file mode 100644 index 000000000..27aa0a591 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/Group.java @@ -0,0 +1,43 @@ +package org.mifosng.platform.group.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.apache.commons.lang.StringUtils; +import org.mifosng.platform.infrastructure.AbstractAuditableCustom; +import org.mifosng.platform.user.domain.AppUser; + +@Entity +@Table(name = "portfolio_group") +public class Group extends AbstractAuditableCustom { + + @SuppressWarnings("unused") + @Column(name = "name", length = 100) + private String name; + + @Column(name = "external_id", length = 100, unique = true) + private String externalId; + + public Group() { + this.name = null; + this.externalId = null; + } + + public static Group newGroup(String name, String externalId){ + return new Group(name, externalId); + } + + public Group(String name, String externalId) { + if (StringUtils.isNotBlank(name)) { + this.name = name.trim(); + } else { + this.name = null; + } + if (StringUtils.isNotBlank(externalId)) { + this.externalId = externalId.trim(); + } else { + this.externalId = null; + } + } +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/GroupRepository.java b/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/GroupRepository.java new file mode 100644 index 000000000..086947a66 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/group/domain/GroupRepository.java @@ -0,0 +1,8 @@ +package org.mifosng.platform.group.domain; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface GroupRepository extends JpaRepository, JpaSpecificationExecutor { + // no behaviour added +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupCommandValidator.java new file mode 100644 index 000000000..d3d14ad50 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupCommandValidator.java @@ -0,0 +1,33 @@ +package org.mifosng.platform.group.service; + +import java.util.ArrayList; +import java.util.List; + +import org.mifosng.platform.DataValidatorBuilder; +import org.mifosng.platform.api.commands.GroupCommand; +import org.mifosng.platform.api.data.ApiParameterError; +import org.mifosng.platform.exceptions.PlatformApiDataValidationException; + +public class GroupCommandValidator { + + private final GroupCommand command; + + public GroupCommandValidator(GroupCommand command) { + this.command = command; + } + + public void validateForCreate() { + + List dataValidationErrors = new ArrayList(); + + DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("group"); + + baseDataValidator.reset().parameter("name").value(command.getName()).notBlank(); + baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); + + if (!dataValidationErrors.isEmpty()) { + throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); + } + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformService.java new file mode 100644 index 000000000..f3054a6ef --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformService.java @@ -0,0 +1,12 @@ +package org.mifosng.platform.group.service; + +import org.mifosng.platform.api.commands.GroupCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.springframework.security.access.prepost.PreAuthorize; + +public interface GroupWritePlatformService { + + @PreAuthorize(value = "hasAnyRole('PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE')") + EntityIdentifier createGroup(GroupCommand command); + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java new file mode 100644 index 000000000..3f145f28c --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java @@ -0,0 +1,41 @@ +package org.mifosng.platform.group.service; + +import org.mifosng.platform.api.commands.GroupCommand; +import org.mifosng.platform.api.data.EntityIdentifier; +import org.mifosng.platform.group.domain.Group; +import org.mifosng.platform.group.domain.GroupRepository; +import org.mifosng.platform.security.PlatformSecurityContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePlatformService { + + private final PlatformSecurityContext context; + + private final GroupRepository groupRepository; + + @Autowired + public GroupWritePlatformServiceJpaRepositoryImpl(PlatformSecurityContext context, GroupRepository groupRepository) { + this.context = context; + this.groupRepository = groupRepository; + } + + @Transactional + @Override + public EntityIdentifier createGroup(GroupCommand command) { + + this.context.authenticatedUser(); + + GroupCommandValidator validator = new GroupCommandValidator(command); + validator.validateForCreate(); + + Group newGroup = Group.newGroup(command.getName(), command.getExternalId()); + + this.groupRepository.save(newGroup); + + return new EntityIdentifier(newGroup.getId()); + } + +}