if (applicationVersionForm != null && applicationVersionForm.getParentId() != null) {
Application parentApplication = applicationService.get(applicationVersionForm.getParentId());
if (parentApplication != null) {
ApplicationVersion currentApplicationVersion = applicationVersionService.get(applicationVersionForm.getId());
// Check whether we are editing or not.
boolean editing = false;
if (currentApplicationVersion != null) {
editing = true;
}
// Check what the current AppState of the applicationVersion is if the version already exists and is being edited.
AppState currentAppState = determineCurrentAppState(currentApplicationVersion);
// Make a copy of the original requested AppState in case the application version is to be resigned.
AppState requestedAppState = applicationVersionForm.getAppState();
Domain parentDomain = parentApplication.getOwnedGroup();
// Only continue if the parent domain exists.
if (parentDomain != null) {
// Set the application version AppState to Resigning if requested.
if (currentAppState != AppState.RESIGNING && applicationVersionForm.getKeyVaultEntryId() != null && applicationVersionForm.getKeyVaultEntryId() > 0 && domainService.isApplicationResignerEnabled(parentDomain)) {
applicationVersionForm.setAppState(AppState.RESIGNING);
}
// Save the applicationVersion
ApplicationVersion savedApplicationVersion = applicationVersionService.saveApplicationVersion(applicationVersionForm);
if (savedApplicationVersion != null && savedApplicationVersion.getId() != null && savedApplicationVersion.getId() > 0) {
// Update the application version form so that the user can be notified if an error occurs during the resigning staging process.
applicationVersionForm.setId(savedApplicationVersion.getId());
applicationVersionForm.setEditing(true);
// Resign the application if it was requested to be resigned and is not already being resigned
if (AppState.RESIGNING.equals(savedApplicationVersion.getAppState()) && !AppState.RESIGNING.equals(requestedAppState)) {
boolean resignSuccess = false;
KeyVaultEntry keyVaultEntry = keyVaultEntryService.get(applicationVersionForm.getKeyVaultEntryId());
resignSuccess = applicationVersionService.resign(savedApplicationVersion, requestedAppState, keyVaultEntry);
// Check if application version was successfully staged to be resigned and if not the the application version in an error state.
if (!resignSuccess) {
// TODO: refactor to send notifications whenever proper audit trail is in place so that admins being notified know why the application version is in the error state.
updateApplicationVersionState(savedApplicationVersion, AppState.ERROR, false);
throw new ApplicationVersionResignException(ResignErrorType.GENERIC);
}
}
// Send notifications only if the application version is newly persisted or it was being edited and the AppState changed
if (sendNotifications && ((currentAppState != null && !currentAppState.equals(savedApplicationVersion.getAppState()))
|| (EntityState.NEWLY_PERSISTED.equals(savedApplicationVersion.getState()) || !editing))) {
sendNotifications(savedApplicationVersion, EventType.APPLICATION_VERSION_STATE_CHANGED);
}
return savedApplicationVersion;
}