final String name = command.stringValueOfParameterNamed(GroupingTypesApiConstants.nameParamName);
final String externalId = command.stringValueOfParameterNamed(GroupingTypesApiConstants.externalIdParamName);
final AppUser currentUser = this.context.authenticatedUser();
Long officeId = null;
Group parentGroup = null;
if (centerId == null) {
officeId = command.longValueOfParameterNamed(GroupingTypesApiConstants.officeIdParamName);
} else {
parentGroup = this.groupRepository.findOneWithNotFoundDetection(centerId);
officeId = parentGroup.officeId();
}
final Office groupOffice = this.officeRepository.findOne(officeId);
if (groupOffice == null) { throw new OfficeNotFoundException(officeId); }
final LocalDate activationDate = command.localDateValueOfParameterNamed(GroupingTypesApiConstants.activationDateParamName);
final GroupLevel groupLevel = this.groupLevelRepository.findOne(groupingType.getId());
validateOfficeOpeningDateisAfterGroupOrCenterOpeningDate(groupOffice, groupLevel, activationDate);
Staff staff = null;
final Long staffId = command.longValueOfParameterNamed(GroupingTypesApiConstants.staffIdParamName);
if (staffId != null) {
staff = this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(staffId, groupOffice.getHierarchy());
}
final Set<Client> clientMembers = assembleSetOfClients(officeId, command);
final Set<Group> groupMembers = assembleSetOfChildGroups(officeId, command);
final boolean active = command.booleanPrimitiveValueOfParameterNamed(GroupingTypesApiConstants.activeParamName);
LocalDate submittedOnDate = new LocalDate();
if (active && submittedOnDate.isAfter(activationDate)) {
submittedOnDate = activationDate;
}
if (command.hasParameter(GroupingTypesApiConstants.submittedOnDateParamName)) {
submittedOnDate = command.localDateValueOfParameterNamed(GroupingTypesApiConstants.submittedOnDateParamName);
}
final Group newGroup = Group.newGroup(groupOffice, staff, parentGroup, groupLevel, name, externalId, active, activationDate,
clientMembers, groupMembers, submittedOnDate, currentUser);
boolean rollbackTransaction = false;
if (newGroup.isActive()) {
// validate Group creation rules for Group
if (newGroup.isGroup()) {
validateGroupRulesBeforeActivation(newGroup);
}
if (newGroup.isCenter()) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().activateCenter(null).build();
rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser);
} else {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().activateGroup(null).build();
rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser);
}
}
if (!newGroup.isCenter() && newGroup.hasActiveClients()) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().associateClientsToGroup(newGroup.getId()).build();
rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser);
}
// pre-save to generate id for use in group hierarchy
this.groupRepository.save(newGroup);
newGroup.generateHierarchy();
this.groupRepository.saveAndFlush(newGroup);
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withOfficeId(groupOffice.getId()) //
.withGroupId(newGroup.getId()) //
.withEntityId(newGroup.getId()) //
.setRollbackTransaction(rollbackTransaction)//
.build();
} catch (final DataIntegrityViolationException dve) {
handleGroupDataIntegrityIssues(command, dve, groupingType);