MIFOSX-24: Fixed remaining issues.

This commit is contained in:
Michal Dudzinski
2012-08-10 14:41:57 +02:00
parent 9d916c1630
commit f277cce894
3 changed files with 56 additions and 24 deletions
@@ -0,0 +1,2 @@
DROP INDEX external_id ON portfolio_group;
ALTER TABLE portfolio_group ADD UNIQUE (external_id);
@@ -75,7 +75,7 @@ public class GroupsApiResource {
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public String retrieveGroupData(@PathParam("groupId") final Long groupId, @Context final UriInfo uriInfo) { public String retrieveGroupData(@PathParam("groupId") final Long groupId, @Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId")); Set<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId", "clientMembers"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo
.getQueryParameters()); .getQueryParameters());
@@ -9,10 +9,14 @@ import org.mifosng.platform.client.domain.Client;
import org.mifosng.platform.client.domain.ClientRepository; import org.mifosng.platform.client.domain.ClientRepository;
import org.mifosng.platform.exceptions.ClientNotFoundException; import org.mifosng.platform.exceptions.ClientNotFoundException;
import org.mifosng.platform.exceptions.GroupNotFoundException; import org.mifosng.platform.exceptions.GroupNotFoundException;
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
import org.mifosng.platform.group.domain.Group; import org.mifosng.platform.group.domain.Group;
import org.mifosng.platform.group.domain.GroupRepository; import org.mifosng.platform.group.domain.GroupRepository;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@@ -20,6 +24,8 @@ import org.springframework.util.ObjectUtils;
@Service @Service
public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePlatformService { public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePlatformService {
private final static Logger logger = LoggerFactory.getLogger(GroupWritePlatformServiceJpaRepositoryImpl.class);
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final GroupRepository groupRepository; private final GroupRepository groupRepository;
@@ -37,42 +43,51 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
@Transactional @Transactional
@Override @Override
public EntityIdentifier createGroup(GroupCommand command) { public EntityIdentifier createGroup(GroupCommand command) {
try {
this.context.authenticatedUser();
this.context.authenticatedUser(); GroupCommandValidator validator = new GroupCommandValidator(command);
validator.validateForCreate();
GroupCommandValidator validator = new GroupCommandValidator(command); final Set<Client> clientMembers = assembleSetOfClients(command);
validator.validateForCreate();
final Set<Client> clientMembers = assembleSetOfClients(command); Group newGroup = Group.newGroup(command.getName(), command.getExternalId(), clientMembers);
Group newGroup = Group.newGroup(command.getName(), command.getExternalId(), clientMembers); this.groupRepository.saveAndFlush(newGroup);
this.groupRepository.save(newGroup); return new EntityIdentifier(newGroup.getId());
} catch (DataIntegrityViolationException dve) {
return new EntityIdentifier(newGroup.getId()); handleGroupDataIntegrityIssues(command, dve);
return new EntityIdentifier(Long.valueOf(-1));
}
} }
@Transactional @Transactional
@Override @Override
public EntityIdentifier updateGroup(GroupCommand command) { public EntityIdentifier updateGroup(GroupCommand command) {
context.authenticatedUser(); try {
context.authenticatedUser();
GroupCommandValidator validator = new GroupCommandValidator(command); GroupCommandValidator validator = new GroupCommandValidator(command);
validator.validateForUpdate(); validator.validateForUpdate();
Group groupForUpdate = this.groupRepository.findOne(command.getId()); Group groupForUpdate = this.groupRepository.findOne(command.getId());
if (groupForUpdate == null || groupForUpdate.isDeleted()) { if (groupForUpdate == null || groupForUpdate.isDeleted()) {
throw new GroupNotFoundException(command.getId()); throw new GroupNotFoundException(command.getId());
}
final Set<Client> clientMembers = assembleSetOfClients(command);
groupForUpdate.update(command, clientMembers);
groupRepository.saveAndFlush(groupForUpdate);
return new EntityIdentifier(groupForUpdate.getId());
} catch (DataIntegrityViolationException dve) {
handleGroupDataIntegrityIssues(command, dve);
return new EntityIdentifier(Long.valueOf(-1));
} }
final Set<Client> clientMembers = assembleSetOfClients(command);
groupForUpdate.update(command, clientMembers);
groupRepository.save(groupForUpdate);
return new EntityIdentifier(groupForUpdate.getId());
} }
@Transactional @Transactional
@@ -100,7 +115,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
for (String clientId : clientMembersArray) { for (String clientId : clientMembersArray) {
Long id = Long.valueOf(clientId); Long id = Long.valueOf(clientId);
Client client = this.clientRepository.findOne(id); Client client = this.clientRepository.findOne(id);
if (client == null) { if (client == null || client.isDeleted()) {
throw new ClientNotFoundException(id); throw new ClientNotFoundException(id);
} }
clientMembers.add(client); clientMembers.add(client);
@@ -110,4 +125,19 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
return clientMembers; return clientMembers;
} }
/*
* Guaranteed to throw an exception no matter what the data integrity issue is.
*/
private void handleGroupDataIntegrityIssues(final GroupCommand command, DataIntegrityViolationException dve) {
Throwable realCause = dve.getMostSpecificCause();
if (realCause.getMessage().contains("external_id")) {
throw new PlatformDataIntegrityException("error.msg.group.duplicate.externalId", "Group with externalId {0} already exists", "externalId", command.getExternalId());
} else if (realCause.getMessage().contains("name")) {
throw new PlatformDataIntegrityException("error.msg.group.duplicate.name", "Group with name {0} already exists", "name", command.getName());
}
logger.error(dve.getMessage(), dve);
throw new PlatformDataIntegrityException("error.msg.group.unknown.data.integrity.issue", "Unknown data integrity issue with resource.");
}
} }