chore: Split the import code to smaller parts (#25548)

## Description
This PR refactors the import application code so that the global
variables are no more there. It also breaks down the code into small
functions.

#### PR fixes following issue(s)
Fixes #25662
This commit is contained in:
Nayan 2023-08-09 11:29:15 +06:00 committed by GitHub
parent 87b8aa58ce
commit 69afe8dee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1357 additions and 934 deletions

View File

@ -663,159 +663,177 @@ public class ActionCollectionServiceCEImpl extends BaseService<ActionCollectionR
/* Mono.just(application) is created to avoid the eagerly fetching of existing actionCollections
* during the pipeline construction. It should be fetched only when the pipeline is subscribed/executed.
*/
return Mono.just(application).flatMap(importedApplication -> {
ImportActionCollectionResultDTO resultDTO = new ImportActionCollectionResultDTO();
final String workspaceId = importedApplication.getWorkspaceId();
return Mono.just(application)
.flatMap(importedApplication -> {
ImportActionCollectionResultDTO resultDTO = new ImportActionCollectionResultDTO();
final String workspaceId = importedApplication.getWorkspaceId();
// Map of gitSyncId to actionCollection of the existing records in DB
Mono<Map<String, ActionCollection>> actionCollectionsInCurrentAppMono = repository
.findByApplicationId(importedApplication.getId())
.filter(collection -> collection.getGitSyncId() != null)
.collectMap(ActionCollection::getGitSyncId);
// Map of gitSyncId to actionCollection of the existing records in DB
Mono<Map<String, ActionCollection>> actionCollectionsInCurrentAppMono = repository
.findByApplicationId(importedApplication.getId())
.filter(collection -> collection.getGitSyncId() != null)
.collectMap(ActionCollection::getGitSyncId);
Mono<Map<String, ActionCollection>> actionCollectionsInBranchesMono;
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId =
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
actionCollectionsInBranchesMono = repository
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
.filter(actionCollection -> actionCollection.getGitSyncId() != null)
.collectMap(ActionCollection::getGitSyncId);
} else {
actionCollectionsInBranchesMono = Mono.just(Collections.emptyMap());
}
Mono<Map<String, ActionCollection>> actionCollectionsInBranchesMono;
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId =
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
actionCollectionsInBranchesMono = repository
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
.filter(actionCollection -> actionCollection.getGitSyncId() != null)
.collectMap(ActionCollection::getGitSyncId);
} else {
actionCollectionsInBranchesMono = Mono.just(Collections.emptyMap());
}
return Mono.zip(actionCollectionsInCurrentAppMono, actionCollectionsInBranchesMono)
.flatMap(objects -> {
Map<String, ActionCollection> actionsCollectionsInCurrentApp = objects.getT1();
Map<String, ActionCollection> actionsCollectionsInBranches = objects.getT2();
return Mono.zip(actionCollectionsInCurrentAppMono, actionCollectionsInBranchesMono)
.flatMap(objects -> {
Map<String, ActionCollection> actionsCollectionsInCurrentApp = objects.getT1();
Map<String, ActionCollection> actionsCollectionsInBranches = objects.getT2();
// set the existing action collections in the result DTO, this will be required in next phases
resultDTO.setExistingActionCollections(actionsCollectionsInCurrentApp.values());
// set the existing action collections in the result DTO, this will be required in next
// phases
resultDTO.setExistingActionCollections(actionsCollectionsInCurrentApp.values());
List<ActionCollection> newActionCollections = new ArrayList<>();
List<ActionCollection> existingActionCollections = new ArrayList<>();
List<ActionCollection> newActionCollections = new ArrayList<>();
List<ActionCollection> existingActionCollections = new ArrayList<>();
for (ActionCollection actionCollection : importedActionCollectionList) {
if (actionCollection.getUnpublishedCollection() == null
|| StringUtils.isEmpty(actionCollection
.getUnpublishedCollection()
.getPageId())) {
continue; // invalid action collection, skip it
}
final String idFromJsonFile = actionCollection.getId();
NewPage parentPage = new NewPage();
final ActionCollectionDTO unpublishedCollection =
actionCollection.getUnpublishedCollection();
final ActionCollectionDTO publishedCollection = actionCollection.getPublishedCollection();
for (ActionCollection actionCollection : importedActionCollectionList) {
if (actionCollection.getUnpublishedCollection() == null
|| StringUtils.isEmpty(actionCollection
.getUnpublishedCollection()
.getPageId())) {
continue; // invalid action collection, skip it
}
final String idFromJsonFile = actionCollection.getId();
NewPage parentPage = new NewPage();
final ActionCollectionDTO unpublishedCollection =
actionCollection.getUnpublishedCollection();
final ActionCollectionDTO publishedCollection =
actionCollection.getPublishedCollection();
// If pageId is missing in the actionCollectionDTO create a fallback pageId
final String fallbackParentPageId = unpublishedCollection.getPageId();
// If pageId is missing in the actionCollectionDTO create a fallback pageId
final String fallbackParentPageId = unpublishedCollection.getPageId();
if (unpublishedCollection.getName() != null) {
unpublishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
.getUnpublishedCollectionIdToActionIdsMap()
.get(idFromJsonFile));
unpublishedCollection.setPluginId(pluginMap.get(unpublishedCollection.getPluginId()));
parentPage = updatePageInActionCollection(unpublishedCollection, pageNameMap);
}
if (unpublishedCollection.getName() != null) {
unpublishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
.getUnpublishedCollectionIdToActionIdsMap()
.get(idFromJsonFile));
unpublishedCollection.setPluginId(
pluginMap.get(unpublishedCollection.getPluginId()));
parentPage = updatePageInActionCollection(unpublishedCollection, pageNameMap);
}
if (publishedCollection != null && publishedCollection.getName() != null) {
publishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
.getPublishedCollectionIdToActionIdsMap()
.get(idFromJsonFile));
publishedCollection.setPluginId(pluginMap.get(publishedCollection.getPluginId()));
if (StringUtils.isEmpty(publishedCollection.getPageId())) {
publishedCollection.setPageId(fallbackParentPageId);
}
NewPage publishedCollectionPage =
updatePageInActionCollection(publishedCollection, pageNameMap);
parentPage = parentPage == null ? publishedCollectionPage : parentPage;
}
if (publishedCollection != null && publishedCollection.getName() != null) {
publishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
.getPublishedCollectionIdToActionIdsMap()
.get(idFromJsonFile));
publishedCollection.setPluginId(
pluginMap.get(publishedCollection.getPluginId()));
if (StringUtils.isEmpty(publishedCollection.getPageId())) {
publishedCollection.setPageId(fallbackParentPageId);
}
NewPage publishedCollectionPage =
updatePageInActionCollection(publishedCollection, pageNameMap);
parentPage = parentPage == null ? publishedCollectionPage : parentPage;
}
actionCollection.makePristine();
actionCollection.setWorkspaceId(workspaceId);
actionCollection.setApplicationId(importedApplication.getId());
actionCollection.makePristine();
actionCollection.setWorkspaceId(workspaceId);
actionCollection.setApplicationId(importedApplication.getId());
// Check if the action has gitSyncId and if it's already in DB
if (actionCollection.getGitSyncId() != null
&& actionsCollectionsInCurrentApp.containsKey(actionCollection.getGitSyncId())) {
// Check if the action has gitSyncId and if it's already in DB
if (actionCollection.getGitSyncId() != null
&& actionsCollectionsInCurrentApp.containsKey(
actionCollection.getGitSyncId())) {
// Since the resource is already present in DB, just update resource
ActionCollection existingActionCollection =
actionsCollectionsInCurrentApp.get(actionCollection.getGitSyncId());
// Since the resource is already present in DB, just update resource
ActionCollection existingActionCollection =
actionsCollectionsInCurrentApp.get(actionCollection.getGitSyncId());
Set<Policy> existingPolicy = existingActionCollection.getPolicies();
copyNestedNonNullProperties(actionCollection, existingActionCollection);
// Update branchName
existingActionCollection.getDefaultResources().setBranchName(branchName);
// Recover the deleted state present in DB from imported actionCollection
existingActionCollection
.getUnpublishedCollection()
.setDeletedAt(actionCollection
Set<Policy> existingPolicy = existingActionCollection.getPolicies();
copyNestedNonNullProperties(actionCollection, existingActionCollection);
// Update branchName
existingActionCollection
.getDefaultResources()
.setBranchName(branchName);
// Recover the deleted state present in DB from imported actionCollection
existingActionCollection
.getUnpublishedCollection()
.getDeletedAt());
existingActionCollection.setDeletedAt(actionCollection.getDeletedAt());
existingActionCollection.setDeleted(actionCollection.getDeleted());
existingActionCollection.setPolicies(existingPolicy);
.setDeletedAt(actionCollection
.getUnpublishedCollection()
.getDeletedAt());
existingActionCollection.setDeletedAt(actionCollection.getDeletedAt());
existingActionCollection.setDeleted(actionCollection.getDeleted());
existingActionCollection.setPolicies(existingPolicy);
existingActionCollection.updateForBulkWriteOperation();
existingActionCollections.add(existingActionCollection);
resultDTO.getSavedActionCollectionIds().add(existingActionCollection.getId());
resultDTO.getSavedActionCollectionMap().put(idFromJsonFile, existingActionCollection);
} else {
if (!permissionProvider.canCreateAction(parentPage)) {
throw new AppsmithException(
AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.PAGE, parentPage.getId());
}
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId = importedApplication
.getGitApplicationMetadata()
.getDefaultApplicationId();
if (actionsCollectionsInBranches.containsKey(actionCollection.getGitSyncId())) {
ActionCollection branchedActionCollection =
actionsCollectionsInBranches.get(actionCollection.getGitSyncId());
populateDefaultResources(
actionCollection, branchedActionCollection, branchName);
existingActionCollection.updateForBulkWriteOperation();
existingActionCollections.add(existingActionCollection);
resultDTO.getSavedActionCollectionIds().add(existingActionCollection.getId());
resultDTO
.getSavedActionCollectionMap()
.put(idFromJsonFile, existingActionCollection);
} else {
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(defaultApplicationId);
defaultResources.setBranchName(branchName);
actionCollection.setDefaultResources(defaultResources);
if (!permissionProvider.canCreateAction(parentPage)) {
throw new AppsmithException(
AppsmithError.ACL_NO_RESOURCE_FOUND,
FieldName.PAGE,
parentPage.getId());
}
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId = importedApplication
.getGitApplicationMetadata()
.getDefaultApplicationId();
if (actionsCollectionsInBranches.containsKey(
actionCollection.getGitSyncId())) {
ActionCollection branchedActionCollection =
actionsCollectionsInBranches.get(
actionCollection.getGitSyncId());
populateDefaultResources(
actionCollection, branchedActionCollection, branchName);
} else {
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(defaultApplicationId);
defaultResources.setBranchName(branchName);
actionCollection.setDefaultResources(defaultResources);
}
}
// this will generate the id and other auto generated fields e.g. createdAt
actionCollection.updateForBulkWriteOperation();
generateAndSetPolicies(parentPage, actionCollection);
// create or update default resources for the action
// values already set to defaultResources are kept unchanged
DefaultResourcesUtils.createDefaultIdsOrUpdateWithGivenResourceIds(
actionCollection, branchName);
// generate gitSyncId if it's not present
if (actionCollection.getGitSyncId() == null) {
actionCollection.setGitSyncId(
actionCollection.getApplicationId() + "_" + new ObjectId());
}
// it's new actionCollection
newActionCollections.add(actionCollection);
resultDTO.getSavedActionCollectionIds().add(actionCollection.getId());
resultDTO.getSavedActionCollectionMap().put(idFromJsonFile, actionCollection);
}
}
// this will generate the id and other auto generated fields e.g. createdAt
actionCollection.updateForBulkWriteOperation();
generateAndSetPolicies(parentPage, actionCollection);
// create or update default resources for the action
// values already set to defaultResources are kept unchanged
DefaultResourcesUtils.createDefaultIdsOrUpdateWithGivenResourceIds(
actionCollection, branchName);
// generate gitSyncId if it's not present
if (actionCollection.getGitSyncId() == null) {
actionCollection.setGitSyncId(
actionCollection.getApplicationId() + "_" + new ObjectId());
}
// it's new actionCollection
newActionCollections.add(actionCollection);
resultDTO.getSavedActionCollectionIds().add(actionCollection.getId());
resultDTO.getSavedActionCollectionMap().put(idFromJsonFile, actionCollection);
}
}
log.info(
"Saving action collections in bulk. New: {}, Updated: {}",
newActionCollections.size(),
existingActionCollections.size());
return repository
.bulkInsert(newActionCollections)
.then(repository.bulkUpdate(existingActionCollections))
.thenReturn(resultDTO);
});
});
log.info(
"Saving action collections in bulk. New: {}, Updated: {}",
newActionCollections.size(),
existingActionCollections.size());
return repository
.bulkInsert(newActionCollections)
.then(repository.bulkUpdate(existingActionCollections))
.thenReturn(resultDTO);
});
})
.onErrorResume(e -> {
log.error("Error saving action collections", e);
return Mono.error(e);
});
}
}

View File

@ -289,6 +289,7 @@ public class ApplicationServiceCEImpl extends BaseService<ApplicationRepository,
return applicationIdMono.flatMap(appId -> repository
.updateById(appId, application, applicationPermission.getEditPermission())
.onErrorResume(error -> {
log.error("failed to update application {}", appId, error);
if (error instanceof DuplicateKeyException) {
// Error message : E11000 duplicate key error collection: appsmith.application index:
// workspace_app_deleted_gitApplicationMetadata dup key:

View File

@ -1698,149 +1698,177 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
/* Mono.just(application) is created to avoid the eagerly fetching of existing actions
* during the pipeline construction. It should be fetched only when the pipeline is subscribed/executed.
*/
return Mono.just(application).flatMap(importedApplication -> {
Mono<Map<String, NewAction>> actionsInCurrentAppMono = repository
.findByApplicationId(importedApplication.getId())
.filter(newAction -> newAction.getGitSyncId() != null)
.collectMap(NewAction::getGitSyncId);
return Mono.just(application)
.flatMap(importedApplication -> {
Mono<Map<String, NewAction>> actionsInCurrentAppMono = repository
.findByApplicationId(importedApplication.getId())
.filter(newAction -> newAction.getGitSyncId() != null)
.collectMap(NewAction::getGitSyncId);
// find existing actions in all the branches of this application and put them in a map
Mono<Map<String, NewAction>> actionsInOtherBranchesMono;
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId =
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
actionsInOtherBranchesMono = repository
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
.filter(newAction -> newAction.getGitSyncId() != null)
.collectMap(NewAction::getGitSyncId);
} else {
actionsInOtherBranchesMono = Mono.just(Collections.emptyMap());
}
return Mono.zip(actionsInCurrentAppMono, actionsInOtherBranchesMono).flatMap(objects -> {
Map<String, NewAction> actionsInCurrentApp = objects.getT1();
Map<String, NewAction> actionsInOtherBranches = objects.getT2();
List<NewAction> newNewActionList = new ArrayList<>();
List<NewAction> existingNewActionList = new ArrayList<>();
final String workspaceId = importedApplication.getWorkspaceId();
ImportActionResultDTO importActionResultDTO = new ImportActionResultDTO();
// existing actions will be required when we'll delete the outdated actions later
importActionResultDTO.setExistingActions(actionsInCurrentApp.values());
for (NewAction newAction : importedNewActionList) {
if (newAction.getUnpublishedAction() == null
|| !StringUtils.hasLength(
newAction.getUnpublishedAction().getPageId())) {
continue;
}
NewPage parentPage = new NewPage();
ActionDTO unpublishedAction = newAction.getUnpublishedAction();
ActionDTO publishedAction = newAction.getPublishedAction();
// If pageId is missing in the actionDTO create a fallback pageId
final String fallbackParentPageId = unpublishedAction.getPageId();
if (unpublishedAction.getValidName() != null) {
unpublishedAction.setId(newAction.getId());
parentPage = updatePageInAction(
unpublishedAction, pageNameMap, importActionResultDTO.getActionIdMap());
sanitizeDatasourceInActionDTO(unpublishedAction, datasourceMap, pluginMap, workspaceId, false);
}
if (publishedAction != null && publishedAction.getValidName() != null) {
publishedAction.setId(newAction.getId());
if (!StringUtils.hasLength(publishedAction.getPageId())) {
publishedAction.setPageId(fallbackParentPageId);
}
NewPage publishedActionPage = updatePageInAction(
publishedAction, pageNameMap, importActionResultDTO.getActionIdMap());
parentPage = parentPage == null ? publishedActionPage : parentPage;
sanitizeDatasourceInActionDTO(publishedAction, datasourceMap, pluginMap, workspaceId, false);
}
newAction.makePristine();
newAction.setWorkspaceId(workspaceId);
newAction.setApplicationId(importedApplication.getId());
newAction.setPluginId(pluginMap.get(newAction.getPluginId()));
this.generateAndSetActionPolicies(parentPage, newAction);
// Check if the action has gitSyncId and if it's already in DB
if (newAction.getGitSyncId() != null && actionsInCurrentApp.containsKey(newAction.getGitSyncId())) {
// Since the resource is already present in DB, just update resource
NewAction existingAction = actionsInCurrentApp.get(newAction.getGitSyncId());
updateExistingAction(existingAction, newAction, branchName, permissionProvider);
// Add it to actions list that'll be updated in bulk
existingNewActionList.add(existingAction);
importActionResultDTO.getImportedActionIds().add(existingAction.getId());
putActionIdInMap(existingAction, importActionResultDTO);
// find existing actions in all the branches of this application and put them in a map
Mono<Map<String, NewAction>> actionsInOtherBranchesMono;
if (importedApplication.getGitApplicationMetadata() != null) {
final String defaultApplicationId =
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
actionsInOtherBranchesMono = repository
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
.filter(newAction -> newAction.getGitSyncId() != null)
.collectMap(NewAction::getGitSyncId);
} else {
// check whether user has permission to add new action
if (!permissionProvider.canCreateAction(parentPage)) {
log.error(
"User does not have permission to create action in page with id: {}",
parentPage.getId());
throw new AppsmithException(
AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.PAGE, parentPage.getId());
}
// this will generate the id and other auto generated fields e.g. createdAt
newAction.updateForBulkWriteOperation();
// set gitSyncId if doesn't exist
if (newAction.getGitSyncId() == null) {
newAction.setGitSyncId(newAction.getApplicationId() + "_"
+ Instant.now().toString());
}
if (importedApplication.getGitApplicationMetadata() != null) {
// application is git connected, check if the action is already present in any other branch
if (actionsInOtherBranches.containsKey(newAction.getGitSyncId())) {
// action found in other branch, copy the default resources from that action
NewAction branchedAction = actionsInOtherBranches.get(newAction.getGitSyncId());
populateDefaultResources(newAction, branchedAction, branchName);
} else {
// This is the first action we are saving with given gitSyncId in this instance
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(importedApplication
.getGitApplicationMetadata()
.getDefaultApplicationId());
defaultResources.setActionId(newAction.getId());
defaultResources.setBranchName(branchName);
newAction.setDefaultResources(defaultResources);
}
} else {
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(importedApplication.getId());
defaultResources.setActionId(newAction.getId());
newAction.setDefaultResources(defaultResources);
}
// Add it to actions list that'll be inserted or updated in bulk
newNewActionList.add(newAction);
importActionResultDTO.getImportedActionIds().add(newAction.getId());
putActionIdInMap(newAction, importActionResultDTO);
actionsInOtherBranchesMono = Mono.just(Collections.emptyMap());
}
}
log.info(
"Saving actions in bulk. New: {}, Updated: {}",
newNewActionList.size(),
existingNewActionList.size());
return Mono.zip(actionsInCurrentAppMono, actionsInOtherBranchesMono)
.flatMap(objects -> {
Map<String, NewAction> actionsInCurrentApp = objects.getT1();
Map<String, NewAction> actionsInOtherBranches = objects.getT2();
// Save all the new actions in bulk
return repository
.bulkInsert(newNewActionList)
.then(repository.bulkUpdate(existingNewActionList))
.thenReturn(importActionResultDTO);
});
});
List<NewAction> newNewActionList = new ArrayList<>();
List<NewAction> existingNewActionList = new ArrayList<>();
final String workspaceId = importedApplication.getWorkspaceId();
ImportActionResultDTO importActionResultDTO = new ImportActionResultDTO();
// existing actions will be required when we'll delete the outdated actions later
importActionResultDTO.setExistingActions(actionsInCurrentApp.values());
for (NewAction newAction : importedNewActionList) {
if (newAction.getUnpublishedAction() == null
|| !StringUtils.hasLength(newAction
.getUnpublishedAction()
.getPageId())) {
continue;
}
NewPage parentPage = new NewPage();
ActionDTO unpublishedAction = newAction.getUnpublishedAction();
ActionDTO publishedAction = newAction.getPublishedAction();
// If pageId is missing in the actionDTO create a fallback pageId
final String fallbackParentPageId = unpublishedAction.getPageId();
if (unpublishedAction.getValidName() != null) {
unpublishedAction.setId(newAction.getId());
parentPage = updatePageInAction(
unpublishedAction, pageNameMap, importActionResultDTO.getActionIdMap());
sanitizeDatasourceInActionDTO(
unpublishedAction, datasourceMap, pluginMap, workspaceId, false);
}
if (publishedAction != null && publishedAction.getValidName() != null) {
publishedAction.setId(newAction.getId());
if (!StringUtils.hasLength(publishedAction.getPageId())) {
publishedAction.setPageId(fallbackParentPageId);
}
NewPage publishedActionPage = updatePageInAction(
publishedAction, pageNameMap, importActionResultDTO.getActionIdMap());
parentPage = parentPage == null ? publishedActionPage : parentPage;
sanitizeDatasourceInActionDTO(
publishedAction, datasourceMap, pluginMap, workspaceId, false);
}
newAction.makePristine();
newAction.setWorkspaceId(workspaceId);
newAction.setApplicationId(importedApplication.getId());
newAction.setPluginId(pluginMap.get(newAction.getPluginId()));
this.generateAndSetActionPolicies(parentPage, newAction);
// Check if the action has gitSyncId and if it's already in DB
if (newAction.getGitSyncId() != null
&& actionsInCurrentApp.containsKey(newAction.getGitSyncId())) {
// Since the resource is already present in DB, just update resource
NewAction existingAction = actionsInCurrentApp.get(newAction.getGitSyncId());
updateExistingAction(existingAction, newAction, branchName, permissionProvider);
// Add it to actions list that'll be updated in bulk
existingNewActionList.add(existingAction);
importActionResultDTO
.getImportedActionIds()
.add(existingAction.getId());
putActionIdInMap(existingAction, importActionResultDTO);
} else {
// check whether user has permission to add new action
if (!permissionProvider.canCreateAction(parentPage)) {
log.error(
"User does not have permission to create action in page with id: {}",
parentPage.getId());
throw new AppsmithException(
AppsmithError.ACL_NO_RESOURCE_FOUND,
FieldName.PAGE,
parentPage.getId());
}
// this will generate the id and other auto generated fields e.g. createdAt
newAction.updateForBulkWriteOperation();
// set gitSyncId if doesn't exist
if (newAction.getGitSyncId() == null) {
newAction.setGitSyncId(newAction.getApplicationId() + "_"
+ Instant.now().toString());
}
if (importedApplication.getGitApplicationMetadata() != null) {
// application is git connected, check if the action is already present in
// any other branch
if (actionsInOtherBranches.containsKey(newAction.getGitSyncId())) {
// action found in other branch, copy the default resources from that
// action
NewAction branchedAction =
actionsInOtherBranches.get(newAction.getGitSyncId());
populateDefaultResources(newAction, branchedAction, branchName);
} else {
// This is the first action we are saving with given gitSyncId in this
// instance
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(importedApplication
.getGitApplicationMetadata()
.getDefaultApplicationId());
defaultResources.setActionId(newAction.getId());
defaultResources.setBranchName(branchName);
newAction.setDefaultResources(defaultResources);
}
} else {
DefaultResources defaultResources = new DefaultResources();
defaultResources.setApplicationId(importedApplication.getId());
defaultResources.setActionId(newAction.getId());
newAction.setDefaultResources(defaultResources);
}
// Add it to actions list that'll be inserted or updated in bulk
newNewActionList.add(newAction);
importActionResultDTO
.getImportedActionIds()
.add(newAction.getId());
putActionIdInMap(newAction, importActionResultDTO);
}
}
log.info(
"Saving actions in bulk. New: {}, Updated: {}",
newNewActionList.size(),
existingNewActionList.size());
// Save all the new actions in bulk
return repository
.bulkInsert(newNewActionList)
.then(repository.bulkUpdate(existingNewActionList))
.thenReturn(importActionResultDTO);
});
})
.onErrorResume(e -> {
log.error("Error importing actions", e);
return Mono.error(e);
})
.elapsed()
.map(tuple -> {
log.debug(
"time to import {} actions: {} ms",
tuple.getT2().getImportedActionIds().size(),
tuple.getT1());
return tuple.getT2();
});
}
@Override
@ -1892,7 +1920,7 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
.get(newAction.getId())
.get(0));
if (unpublishedAction.getDefaultResources() != null
&& !StringUtils.hasLength(
&& org.apache.commons.lang3.StringUtils.isEmpty(
unpublishedAction.getDefaultResources().getCollectionId())) {
unpublishedAction
@ -1910,7 +1938,7 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
.get(0));
if (publishedAction.getDefaultResources() != null
&& !StringUtils.hasLength(
&& org.apache.commons.lang3.StringUtils.isEmpty(
publishedAction.getDefaultResources().getCollectionId())) {
publishedAction

View File

@ -289,4 +289,33 @@ public class ApplicationSnapshotServiceUnitTest {
})
.verifyComplete();
}
@Test
public void test() {
Mono<Integer> mono = Mono.just(1)
.map(s -> {
System.out.println("s at line 296: " + s);
if (s == 1) {
throw new RuntimeException("equal to 1");
}
return s;
})
.onErrorResume(e -> {
System.out.println("error at integer mono on Error resume");
return Mono.error(e);
});
Mono<String> mono1 = Mono.just("1");
Mono<String> finalMono = mono.then(mono1)
.map(s -> {
System.out.println("s at line 300: " + s);
return s;
})
.onErrorResume(e -> {
System.out.println("error at final mono on error resume ");
return Mono.error(e);
});
finalMono.subscribe();
}
}