MIFOSX-213: center, group and clients api for activating entity to move from pending to active status.

This commit is contained in:
Keith Woodlock
2013-04-21 23:57:10 +01:00
parent 500767a400
commit ea0ee84967
8 changed files with 48 additions and 100 deletions
+3 -1
View File
@@ -2050,7 +2050,9 @@ GET https://DomainName/api/v1/clients
<div class="method-section"> <div class="method-section">
<div class="method-description"> <div class="method-description">
<h4>Update a Client</h4> <h4>Update a Client</h4>
<p>You can update any attribute of a client except for its association with <i>groups</i>. The relationship between a client and a group must be removed through the <b>Groups</b> API.</p> <p><b>Note:</b>You can update any of the basic attributes of a client using this API but not its associations.</p>
<p>Chaning the relationship between a client and its office is not supported through this API. An API specific to handling <b>transfers</b> of clients between offices will be introduced for this.</p>
<p>The relationship between a client and a group must be removed through the <b>Groups</b> API.</p>
</div> </div>
<div class="method-example"> <div class="method-example">
<code class="method-declaration"> <code class="method-declaration">
@@ -24,7 +24,6 @@ public class ClientApiConstants {
public static final String lastnameParamName = "lastname"; public static final String lastnameParamName = "lastname";
public static final String fullnameParamName = "fullname"; public static final String fullnameParamName = "fullname";
public static final String officeIdParamName = "officeId"; public static final String officeIdParamName = "officeId";
public static final String staffIdParamName = "staffId";
public static final String activeParamName = "active"; public static final String activeParamName = "active";
public static final String activationDateParamName = "activationDate"; public static final String activationDateParamName = "activationDate";
@@ -33,20 +32,22 @@ public class ClientApiConstants {
public static final String hierarchyParamName = "hierarchy"; public static final String hierarchyParamName = "hierarchy";
public static final String displayNameParamName = "displayName"; public static final String displayNameParamName = "displayName";
public static final String officeNameParamName = "officeName"; public static final String officeNameParamName = "officeName";
public static final String staffNameParamName = "staffName";
public static final String imageKeyParamName = "imageKey"; public static final String imageKeyParamName = "imageKey";
public static final String imagePresentParamName = "imagePresent"; public static final String imagePresentParamName = "imagePresent";
// associations related part of response // associations related part of response
public static final String parentGroupsParamName = "groups"; public static final String groupsParamName = "groups";
// template related part of response // template related part of response
public static final String officeOptionsParamName = "officeOptions"; public static final String officeOptionsParamName = "officeOptions";
public static final String staffOptionsParamName = "staffOptions";
public static final Set<String> CLIENT_REQUEST_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(localeParamName, public static final Set<String> CLIENT_CREATE_REQUEST_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(localeParamName,
dateFormatParamName, groupIdParamName, accountNoParamName, externalIdParamName, firstnameParamName, middlenameParamName, dateFormatParamName, groupIdParamName, accountNoParamName, externalIdParamName, firstnameParamName, middlenameParamName,
lastnameParamName, fullnameParamName, officeIdParamName, staffIdParamName, activeParamName, activationDateParamName)); lastnameParamName, fullnameParamName, officeIdParamName, activeParamName, activationDateParamName));
public static final Set<String> CLIENT_UPDATE_REQUEST_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(localeParamName,
dateFormatParamName, accountNoParamName, externalIdParamName, firstnameParamName, middlenameParamName,
lastnameParamName, fullnameParamName, activeParamName, activationDateParamName));
/** /**
* These parameters will match the class level parameters of * These parameters will match the class level parameters of
@@ -54,9 +55,9 @@ public class ClientApiConstants {
* match those of request parameters. * match those of request parameters.
*/ */
public static final Set<String> CLIENT_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, accountNoParamName, public static final Set<String> CLIENT_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, accountNoParamName,
externalIdParamName, statusParamName, firstnameParamName, middlenameParamName, lastnameParamName, fullnameParamName, externalIdParamName, statusParamName, activeParamName, activationDateParamName, firstnameParamName, middlenameParamName,
displayNameParamName, officeIdParamName, officeNameParamName, staffIdParamName, staffNameParamName, hierarchyParamName, lastnameParamName, fullnameParamName, displayNameParamName, officeIdParamName, officeNameParamName, hierarchyParamName,
officeOptionsParamName, staffOptionsParamName)); imageKeyParamName, imagePresentParamName, groupsParamName, officeOptionsParamName));
public static final Set<String> ACTIVATION_REQUEST_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(localeParamName, public static final Set<String> ACTIVATION_REQUEST_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(localeParamName,
dateFormatParamName, activationDateParamName)); dateFormatParamName, activationDateParamName));
@@ -42,7 +42,7 @@ public final class ClientDataValidator {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, ClientApiConstants.CLIENT_REQUEST_DATA_PARAMETERS); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, ClientApiConstants.CLIENT_CREATE_REQUEST_DATA_PARAMETERS);
final JsonElement element = fromApiJsonHelper.parse(json); final JsonElement element = fromApiJsonHelper.parse(json);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
@@ -87,7 +87,7 @@ public final class ClientDataValidator {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, ClientApiConstants.CLIENT_REQUEST_DATA_PARAMETERS); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, ClientApiConstants.CLIENT_UPDATE_REQUEST_DATA_PARAMETERS);
final JsonElement element = fromApiJsonHelper.parse(json); final JsonElement element = fromApiJsonHelper.parse(json);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
@@ -124,12 +124,6 @@ public final class ClientDataValidator {
atLeastOneParameterPassedForUpdate = true; atLeastOneParameterPassedForUpdate = true;
} }
if (fromApiJsonHelper.parameterExists(ClientApiConstants.officeIdParamName, element)) {
atLeastOneParameterPassedForUpdate = true;
final Long officeId = fromApiJsonHelper.extractLongNamed(ClientApiConstants.officeIdParamName, element);
baseDataValidator.reset().parameter(ClientApiConstants.officeIdParamName).value(officeId).notNull().integerGreaterThanZero();
}
final Boolean active = fromApiJsonHelper.extractBooleanNamed(ClientApiConstants.activeParamName, element); final Boolean active = fromApiJsonHelper.extractBooleanNamed(ClientApiConstants.activeParamName, element);
if (active != null) { if (active != null) {
if (active.booleanValue()) { if (active.booleanValue()) {
@@ -187,29 +187,6 @@ public final class Client extends AbstractPersistable<Long> {
return getId().equals(clientId); return getId().equals(clientId);
} }
public void changeOffice(final Office newOffice) {
if (isNotAMemberOfAGroup()) {
this.office = newOffice;
} else {
cannotUpdateOfficeOfClientThatBelongsInGroupException(newOffice);
}
}
private boolean isNotAMemberOfAGroup() {
return this.groups == null || this.groups.isEmpty();
}
private void cannotUpdateOfficeOfClientThatBelongsInGroupException(final Office newOffice) {
final String defaultUserMessage = "Cannot update office of client that is member of a group.";
final ApiParameterError error = ApiParameterError.parameterError("error.msg.client.member.of.group.and.cannot.update.office",
defaultUserMessage, ClientApiConstants.officeIdParamName, newOffice.getId(), newOffice.getName());
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
dataValidationErrors.add(error);
throw new PlatformApiDataValidationException(dataValidationErrors);
}
public void updateAccountNo(final String accountIdentifier) { public void updateAccountNo(final String accountIdentifier) {
this.accountNumber = accountIdentifier; this.accountNumber = accountIdentifier;
this.accountNumberRequiresAutoGeneration = false; this.accountNumberRequiresAutoGeneration = false;
@@ -219,7 +196,7 @@ public final class Client extends AbstractPersistable<Long> {
if (isActive()) { if (isActive()) {
final String defaultUserMessage = "Cannot activate client. Client is already active."; final String defaultUserMessage = "Cannot activate client. Client is already active.";
final ApiParameterError error = ApiParameterError.parameterError("error.msg.clients.already.active", defaultUserMessage, final ApiParameterError error = ApiParameterError.parameterError("error.msg.clients.already.active", defaultUserMessage,
"activationDate", activationLocalDate.toString(formatter)); ClientApiConstants.activationDateParamName, activationLocalDate.toString(formatter));
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
dataValidationErrors.add(error); dataValidationErrors.add(error);
@@ -231,7 +208,7 @@ public final class Client extends AbstractPersistable<Long> {
final String defaultUserMessage = "Activation date cannot be in the future."; final String defaultUserMessage = "Activation date cannot be in the future.";
final ApiParameterError error = ApiParameterError.parameterError("error.msg.clients.activationDate.in.the.future", final ApiParameterError error = ApiParameterError.parameterError("error.msg.clients.activationDate.in.the.future",
defaultUserMessage, "activationDate", activationLocalDate); defaultUserMessage, ClientApiConstants.activationDateParamName, activationLocalDate);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
dataValidationErrors.add(error); dataValidationErrors.add(error);
@@ -285,11 +262,6 @@ public final class Client extends AbstractPersistable<Long> {
this.externalId = StringUtils.defaultIfEmpty(newValue, null); this.externalId = StringUtils.defaultIfEmpty(newValue, null);
} }
if (command.isChangeInLongParameterNamed(ClientApiConstants.officeIdParamName, this.office.getId())) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.officeIdParamName);
actualChanges.put(ClientApiConstants.officeIdParamName, newValue);
}
if (command.isChangeInStringParameterNamed(ClientApiConstants.firstnameParamName, this.firstname)) { if (command.isChangeInStringParameterNamed(ClientApiConstants.firstnameParamName, this.firstname)) {
final String newValue = command.stringValueOfParameterNamed(ClientApiConstants.firstnameParamName); final String newValue = command.stringValueOfParameterNamed(ClientApiConstants.firstnameParamName);
actualChanges.put(ClientApiConstants.firstnameParamName, newValue); actualChanges.put(ClientApiConstants.firstnameParamName, newValue);
@@ -341,32 +313,35 @@ public final class Client extends AbstractPersistable<Long> {
if (StringUtils.isNotBlank(this.fullname)) { if (StringUtils.isNotBlank(this.fullname)) {
baseDataValidator.reset().parameter("firstname").value(this.firstname) baseDataValidator.reset().parameter(ClientApiConstants.firstnameParamName).value(this.firstname)
.mustBeBlankWhenParameterProvided("fullname", this.fullname); .mustBeBlankWhenParameterProvided(ClientApiConstants.fullnameParamName, this.fullname);
baseDataValidator.reset().parameter("middlename").value(this.middlename) baseDataValidator.reset().parameter(ClientApiConstants.middlenameParamName).value(this.middlename)
.mustBeBlankWhenParameterProvided("fullname", this.fullname); .mustBeBlankWhenParameterProvided(ClientApiConstants.fullnameParamName, this.fullname);
baseDataValidator.reset().parameter("lastname").value(this.lastname) baseDataValidator.reset().parameter(ClientApiConstants.lastnameParamName).value(this.lastname)
.mustBeBlankWhenParameterProvided("fullname", this.fullname); .mustBeBlankWhenParameterProvided(ClientApiConstants.fullnameParamName, this.fullname);
} }
if (StringUtils.isBlank(this.fullname)) { if (StringUtils.isBlank(this.fullname)) {
baseDataValidator.reset().parameter("firstname").value(this.firstname).notBlank().notExceedingLengthOf(50); baseDataValidator.reset().parameter(ClientApiConstants.firstnameParamName).value(this.firstname).notBlank()
baseDataValidator.reset().parameter("middlename").value(this.middlename).ignoreIfNull().notExceedingLengthOf(50); .notExceedingLengthOf(50);
baseDataValidator.reset().parameter("lastname").value(this.lastname).notBlank().notExceedingLengthOf(50); baseDataValidator.reset().parameter(ClientApiConstants.middlenameParamName).value(this.middlename).ignoreIfNull()
.notExceedingLengthOf(50);
baseDataValidator.reset().parameter(ClientApiConstants.lastnameParamName).value(this.lastname).notBlank()
.notExceedingLengthOf(50);
} }
if (this.activationDate != null) { if (this.activationDate != null) {
if (office.isOpeningDateAfter(getActivationLocalDate())) { if (office.isOpeningDateAfter(getActivationLocalDate())) {
final String defaultUserMessage = "Client activation date cannot be a date before the office opening date."; final String defaultUserMessage = "Client activation date cannot be a date before the office opening date.";
final ApiParameterError error = ApiParameterError.parameterError( final ApiParameterError error = ApiParameterError.parameterError(
"error.msg.clients.activationDate.cannot.be.before.office.activation.date", defaultUserMessage, "activationDate", "error.msg.clients.activationDate.cannot.be.before.office.activation.date", defaultUserMessage,
getActivationLocalDate()); ClientApiConstants.activationDateParamName, getActivationLocalDate());
dataValidationErrors.add(error); dataValidationErrors.add(error);
} }
} }
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
} }
@@ -404,31 +379,11 @@ public final class Client extends AbstractPersistable<Long> {
return this.office.identifiedBy(officeId); return this.office.identifiedBy(officeId);
} }
public Office getOffice() { public String imageKey() {
return office;
}
public String getFirstName() {
return firstname;
}
public String getLastName() {
return lastname;
}
public Date getJoiningDate() {
return activationDate;
}
public String getExternalId() {
return externalId;
}
public String getImageKey() {
return imageKey; return imageKey;
} }
public void setImageKey(final String imageKey) { public void updateImageKey(final String imageKey) {
this.imageKey = imageKey; this.imageKey = imageKey;
} }
@@ -79,7 +79,7 @@ public class ClientIdentifierWritePlatformServiceJpaRepositoryImpl implements Cl
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) // .withCommandId(command.commandId()) //
.withOfficeId(client.getOffice().getId()) // .withOfficeId(client.officeId()) //
.withClientId(clientId) // .withClientId(clientId) //
.withEntityId(clientIdentifier.getId()) // .withEntityId(clientIdentifier.getId()) //
.build(); .build();
@@ -176,14 +176,6 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final Map<String, Object> changes = clientForUpdate.update(command); final Map<String, Object> changes = clientForUpdate.update(command);
if (changes.containsKey(ClientApiConstants.officeIdParamName)) {
final Long officeId = (Long) changes.get(ClientApiConstants.officeIdParamName);
final Office newOffice = this.officeRepository.findOne(officeId);
if (newOffice == null) { throw new OfficeNotFoundException(officeId); }
clientForUpdate.changeOffice(newOffice);
}
if (!changes.isEmpty()) { if (!changes.isEmpty()) {
this.clientRepository.saveAndFlush(clientForUpdate); this.clientRepository.saveAndFlush(clientForUpdate);
} }
@@ -252,8 +244,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId); final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId);
// delete image from the file system // delete image from the file system
if (StringUtils.isNotEmpty(client.getImageKey())) { if (StringUtils.isNotEmpty(client.imageKey())) {
FileUtils.deleteClientImage(clientId, client.getImageKey()); FileUtils.deleteClientImage(clientId, client.imageKey());
} }
return updateClientImage(clientId, client, null); return updateClientImage(clientId, client, null);
} }
@@ -278,8 +270,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final String imageUploadLocation = FileUtils.generateClientImageParentDirectory(clientId); final String imageUploadLocation = FileUtils.generateClientImageParentDirectory(clientId);
// delete previous image from the file system // delete previous image from the file system
if (StringUtils.isNotEmpty(client.getImageKey())) { if (StringUtils.isNotEmpty(client.imageKey())) {
FileUtils.deleteClientImage(clientId, client.getImageKey()); FileUtils.deleteClientImage(clientId, client.imageKey());
} }
/** Recursively create the directory if it does not exist **/ /** Recursively create the directory if it does not exist **/
@@ -290,7 +282,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
} }
private CommandProcessingResult updateClientImage(final Long clientId, final Client client, final String imageLocation) { private CommandProcessingResult updateClientImage(final Long clientId, final Client client, final String imageLocation) {
client.setImageKey(imageLocation); client.updateImageKey(imageLocation);
this.clientRepository.save(client); this.clientRepository.save(client);
return new CommandProcessingResult(clientId); return new CommandProcessingResult(clientId);
@@ -54,7 +54,7 @@ public class GroupingTypesApiConstants {
*/ */
public static final Set<String> CENTER_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, nameParamName, public static final Set<String> CENTER_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, nameParamName,
externalIdParamName, officeIdParamName, officeNameParamName, staffIdParamName, staffNameParamName, hierarchyParamName, externalIdParamName, officeIdParamName, officeNameParamName, staffIdParamName, staffNameParamName, hierarchyParamName,
"officeOptions", "staffOptions")); officeOptionsParamName, staffOptionsParamName));
public static final Set<String> CENTER_GROUP_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, nameParamName, public static final Set<String> CENTER_GROUP_RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList(idParamName, nameParamName,
externalIdParamName, officeIdParamName, officeNameParamName, staffIdParamName, staffNameParamName, hierarchyParamName, externalIdParamName, officeIdParamName, officeNameParamName, staffIdParamName, staffNameParamName, hierarchyParamName,
@@ -67,7 +67,9 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) // .withCommandId(command.commandId()) //
.withEntityId(newNote.getId()) // .withEntityId(newNote.getId()) //
.withClientId(client.getId()).withOfficeId(client.getOffice().getId()).build(); .withClientId(client.getId()) //
.withOfficeId(client.officeId()) //
.build();
} }
@@ -207,8 +209,10 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) // .withCommandId(command.commandId()) //
.withEntityId(noteForUpdate.getId()) // .withEntityId(noteForUpdate.getId()) //
.withClientId(client.getId()).withOfficeId(client.getOffice().getId()).with(changes).build(); .withClientId(client.getId()) //
.withOfficeId(client.officeId()) //
.with(changes) //
.build();
} }
private CommandProcessingResult updateGroupNote(final JsonCommand command) { private CommandProcessingResult updateGroupNote(final JsonCommand command) {