FIXED MIFOSX-6: Implement basic api for Creating a Group
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> modifiedParameters;
|
||||
|
||||
public GroupCommand(Set<String> modifiedParameters, String externalId, String name) {
|
||||
this.externalId = externalId;
|
||||
this.name = name;
|
||||
this.modifiedParameters = modifiedParameters;
|
||||
}
|
||||
|
||||
public String getExternalId() {
|
||||
return externalId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
+3
@@ -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);
|
||||
|
||||
+24
@@ -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<Map<String, String>>(){}.getType();
|
||||
Map<String, String> requestMap = gsonConverter.fromJson(json, typeOfMap);
|
||||
|
||||
Set<String> supportedParams = new HashSet<String>(
|
||||
Arrays.asList("name", "externalId")
|
||||
);
|
||||
|
||||
checkForUnsupportedParameters(requestMap, supportedParams);
|
||||
|
||||
Set<String> modifiedParameters = new HashSet<String>();
|
||||
|
||||
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)) {
|
||||
|
||||
@@ -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<AppUser, Long> {
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Group, Long>, JpaSpecificationExecutor<Group> {
|
||||
// no behaviour added
|
||||
}
|
||||
+33
@@ -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<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -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);
|
||||
|
||||
}
|
||||
+41
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user