this.fromApiJsonDeserializer.validateForCreate(command.json());
final Long officeId = command.longValueOfParameterNamed(ClientApiConstants.officeIdParamName);
final Office clientOffice = this.officeRepository.findOne(officeId);
if (clientOffice == null) { throw new OfficeNotFoundException(officeId); }
final Long groupId = command.longValueOfParameterNamed(ClientApiConstants.groupIdParamName);
Group clientParentGroup = null;
if (groupId != null) {
clientParentGroup = this.groupRepository.findOne(groupId);
if (clientParentGroup == null) { throw new GroupNotFoundException(groupId); }
}
Staff staff = null;
final Long staffId = command.longValueOfParameterNamed(ClientApiConstants.staffIdParamName);
if (staffId != null) {
staff = this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(staffId, clientOffice.getHierarchy());
}
CodeValue gender = null;
final Long genderId = command.longValueOfParameterNamed(ClientApiConstants.genderIdParamName);
if (genderId != null) {
gender = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(ClientApiConstants.GENDER, genderId);
}
CodeValue clientType = null;
final Long clientTypeId = command.longValueOfParameterNamed(ClientApiConstants.clientTypeIdParamName);
if (clientTypeId != null) {
clientType = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(ClientApiConstants.CLIENT_TYPE,
clientTypeId);
}
CodeValue clientClassification = null;
final Long clientClassificationId = command.longValueOfParameterNamed(ClientApiConstants.clientClassificationIdParamName);
if (clientClassificationId != null) {
clientClassification = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(
ClientApiConstants.CLIENT_CLASSIFICATION, clientClassificationId);
}
SavingsProduct savingsProduct = null;
final Long savingsProductId = command.longValueOfParameterNamed(ClientApiConstants.savingsProductIdParamName);
if (savingsProductId != null) {
savingsProduct = this.savingsProductRepository.findOne(savingsProductId);
if (savingsProduct == null) { throw new SavingsProductNotFoundException(savingsProductId); }
}
final Client newClient = Client.createNew(currentUser, clientOffice, clientParentGroup, staff, savingsProduct, gender,
clientType, clientClassification, command);
boolean rollbackTransaction = false;
if (newClient.isActive()) {
validateParentGroupRulesBeforeClientActivation(newClient);
final CommandWrapper commandWrapper = new CommandWrapperBuilder().activateClient(null).build();
rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser);
}
this.clientRepository.save(newClient);
if (newClient.isAccountNumberRequiresAutoGeneration()) {
final AccountNumberGenerator accountNoGenerator = this.accountIdentifierGeneratorFactory
.determineClientAccountNoGenerator(newClient.getId());
newClient.updateAccountNo(accountNoGenerator.generate());
this.clientRepository.save(newClient);
}
final Locale locale = command.extractLocale();
final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
CommandProcessingResult result = openSavingsAccount(newClient, fmt);
if (result.getSavingsId() != null) {
this.clientRepository.save(newClient);
}
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withOfficeId(clientOffice.getId()) //
.withClientId(newClient.getId()) //
.withGroupId(groupId) //
.withEntityId(newClient.getId()) //
.withSavingsId(result.getSavingsId())//
.setRollbackTransaction(rollbackTransaction)//