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:
parent
87b8aa58ce
commit
69afe8dee2
|
|
@ -663,159 +663,177 @@ public class ActionCollectionServiceCEImpl extends BaseService<ActionCollectionR
|
||||||
/* Mono.just(application) is created to avoid the eagerly fetching of existing actionCollections
|
/* 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.
|
* during the pipeline construction. It should be fetched only when the pipeline is subscribed/executed.
|
||||||
*/
|
*/
|
||||||
return Mono.just(application).flatMap(importedApplication -> {
|
return Mono.just(application)
|
||||||
ImportActionCollectionResultDTO resultDTO = new ImportActionCollectionResultDTO();
|
.flatMap(importedApplication -> {
|
||||||
final String workspaceId = importedApplication.getWorkspaceId();
|
ImportActionCollectionResultDTO resultDTO = new ImportActionCollectionResultDTO();
|
||||||
|
final String workspaceId = importedApplication.getWorkspaceId();
|
||||||
|
|
||||||
// Map of gitSyncId to actionCollection of the existing records in DB
|
// Map of gitSyncId to actionCollection of the existing records in DB
|
||||||
Mono<Map<String, ActionCollection>> actionCollectionsInCurrentAppMono = repository
|
Mono<Map<String, ActionCollection>> actionCollectionsInCurrentAppMono = repository
|
||||||
.findByApplicationId(importedApplication.getId())
|
.findByApplicationId(importedApplication.getId())
|
||||||
.filter(collection -> collection.getGitSyncId() != null)
|
.filter(collection -> collection.getGitSyncId() != null)
|
||||||
.collectMap(ActionCollection::getGitSyncId);
|
.collectMap(ActionCollection::getGitSyncId);
|
||||||
|
|
||||||
Mono<Map<String, ActionCollection>> actionCollectionsInBranchesMono;
|
Mono<Map<String, ActionCollection>> actionCollectionsInBranchesMono;
|
||||||
if (importedApplication.getGitApplicationMetadata() != null) {
|
if (importedApplication.getGitApplicationMetadata() != null) {
|
||||||
final String defaultApplicationId =
|
final String defaultApplicationId =
|
||||||
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
|
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
|
||||||
actionCollectionsInBranchesMono = repository
|
actionCollectionsInBranchesMono = repository
|
||||||
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
|
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
|
||||||
.filter(actionCollection -> actionCollection.getGitSyncId() != null)
|
.filter(actionCollection -> actionCollection.getGitSyncId() != null)
|
||||||
.collectMap(ActionCollection::getGitSyncId);
|
.collectMap(ActionCollection::getGitSyncId);
|
||||||
} else {
|
} else {
|
||||||
actionCollectionsInBranchesMono = Mono.just(Collections.emptyMap());
|
actionCollectionsInBranchesMono = Mono.just(Collections.emptyMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Mono.zip(actionCollectionsInCurrentAppMono, actionCollectionsInBranchesMono)
|
return Mono.zip(actionCollectionsInCurrentAppMono, actionCollectionsInBranchesMono)
|
||||||
.flatMap(objects -> {
|
.flatMap(objects -> {
|
||||||
Map<String, ActionCollection> actionsCollectionsInCurrentApp = objects.getT1();
|
Map<String, ActionCollection> actionsCollectionsInCurrentApp = objects.getT1();
|
||||||
Map<String, ActionCollection> actionsCollectionsInBranches = objects.getT2();
|
Map<String, ActionCollection> actionsCollectionsInBranches = objects.getT2();
|
||||||
|
|
||||||
// set the existing action collections in the result DTO, this will be required in next phases
|
// set the existing action collections in the result DTO, this will be required in next
|
||||||
resultDTO.setExistingActionCollections(actionsCollectionsInCurrentApp.values());
|
// phases
|
||||||
|
resultDTO.setExistingActionCollections(actionsCollectionsInCurrentApp.values());
|
||||||
|
|
||||||
List<ActionCollection> newActionCollections = new ArrayList<>();
|
List<ActionCollection> newActionCollections = new ArrayList<>();
|
||||||
List<ActionCollection> existingActionCollections = new ArrayList<>();
|
List<ActionCollection> existingActionCollections = new ArrayList<>();
|
||||||
|
|
||||||
for (ActionCollection actionCollection : importedActionCollectionList) {
|
for (ActionCollection actionCollection : importedActionCollectionList) {
|
||||||
if (actionCollection.getUnpublishedCollection() == null
|
if (actionCollection.getUnpublishedCollection() == null
|
||||||
|| StringUtils.isEmpty(actionCollection
|
|| StringUtils.isEmpty(actionCollection
|
||||||
.getUnpublishedCollection()
|
.getUnpublishedCollection()
|
||||||
.getPageId())) {
|
.getPageId())) {
|
||||||
continue; // invalid action collection, skip it
|
continue; // invalid action collection, skip it
|
||||||
}
|
}
|
||||||
final String idFromJsonFile = actionCollection.getId();
|
final String idFromJsonFile = actionCollection.getId();
|
||||||
NewPage parentPage = new NewPage();
|
NewPage parentPage = new NewPage();
|
||||||
final ActionCollectionDTO unpublishedCollection =
|
final ActionCollectionDTO unpublishedCollection =
|
||||||
actionCollection.getUnpublishedCollection();
|
actionCollection.getUnpublishedCollection();
|
||||||
final ActionCollectionDTO publishedCollection = actionCollection.getPublishedCollection();
|
final ActionCollectionDTO publishedCollection =
|
||||||
|
actionCollection.getPublishedCollection();
|
||||||
|
|
||||||
// If pageId is missing in the actionCollectionDTO create a fallback pageId
|
// If pageId is missing in the actionCollectionDTO create a fallback pageId
|
||||||
final String fallbackParentPageId = unpublishedCollection.getPageId();
|
final String fallbackParentPageId = unpublishedCollection.getPageId();
|
||||||
|
|
||||||
if (unpublishedCollection.getName() != null) {
|
if (unpublishedCollection.getName() != null) {
|
||||||
unpublishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
|
unpublishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
|
||||||
.getUnpublishedCollectionIdToActionIdsMap()
|
.getUnpublishedCollectionIdToActionIdsMap()
|
||||||
.get(idFromJsonFile));
|
.get(idFromJsonFile));
|
||||||
unpublishedCollection.setPluginId(pluginMap.get(unpublishedCollection.getPluginId()));
|
unpublishedCollection.setPluginId(
|
||||||
parentPage = updatePageInActionCollection(unpublishedCollection, pageNameMap);
|
pluginMap.get(unpublishedCollection.getPluginId()));
|
||||||
}
|
parentPage = updatePageInActionCollection(unpublishedCollection, pageNameMap);
|
||||||
|
}
|
||||||
|
|
||||||
if (publishedCollection != null && publishedCollection.getName() != null) {
|
if (publishedCollection != null && publishedCollection.getName() != null) {
|
||||||
publishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
|
publishedCollection.setDefaultToBranchedActionIdsMap(importActionResultDTO
|
||||||
.getPublishedCollectionIdToActionIdsMap()
|
.getPublishedCollectionIdToActionIdsMap()
|
||||||
.get(idFromJsonFile));
|
.get(idFromJsonFile));
|
||||||
publishedCollection.setPluginId(pluginMap.get(publishedCollection.getPluginId()));
|
publishedCollection.setPluginId(
|
||||||
if (StringUtils.isEmpty(publishedCollection.getPageId())) {
|
pluginMap.get(publishedCollection.getPluginId()));
|
||||||
publishedCollection.setPageId(fallbackParentPageId);
|
if (StringUtils.isEmpty(publishedCollection.getPageId())) {
|
||||||
}
|
publishedCollection.setPageId(fallbackParentPageId);
|
||||||
NewPage publishedCollectionPage =
|
}
|
||||||
updatePageInActionCollection(publishedCollection, pageNameMap);
|
NewPage publishedCollectionPage =
|
||||||
parentPage = parentPage == null ? publishedCollectionPage : parentPage;
|
updatePageInActionCollection(publishedCollection, pageNameMap);
|
||||||
}
|
parentPage = parentPage == null ? publishedCollectionPage : parentPage;
|
||||||
|
}
|
||||||
|
|
||||||
actionCollection.makePristine();
|
actionCollection.makePristine();
|
||||||
actionCollection.setWorkspaceId(workspaceId);
|
actionCollection.setWorkspaceId(workspaceId);
|
||||||
actionCollection.setApplicationId(importedApplication.getId());
|
actionCollection.setApplicationId(importedApplication.getId());
|
||||||
|
|
||||||
// Check if the action has gitSyncId and if it's already in DB
|
// Check if the action has gitSyncId and if it's already in DB
|
||||||
if (actionCollection.getGitSyncId() != null
|
if (actionCollection.getGitSyncId() != null
|
||||||
&& actionsCollectionsInCurrentApp.containsKey(actionCollection.getGitSyncId())) {
|
&& actionsCollectionsInCurrentApp.containsKey(
|
||||||
|
actionCollection.getGitSyncId())) {
|
||||||
|
|
||||||
// Since the resource is already present in DB, just update resource
|
// Since the resource is already present in DB, just update resource
|
||||||
ActionCollection existingActionCollection =
|
ActionCollection existingActionCollection =
|
||||||
actionsCollectionsInCurrentApp.get(actionCollection.getGitSyncId());
|
actionsCollectionsInCurrentApp.get(actionCollection.getGitSyncId());
|
||||||
|
|
||||||
Set<Policy> existingPolicy = existingActionCollection.getPolicies();
|
Set<Policy> existingPolicy = existingActionCollection.getPolicies();
|
||||||
copyNestedNonNullProperties(actionCollection, existingActionCollection);
|
copyNestedNonNullProperties(actionCollection, existingActionCollection);
|
||||||
// Update branchName
|
// Update branchName
|
||||||
existingActionCollection.getDefaultResources().setBranchName(branchName);
|
existingActionCollection
|
||||||
// Recover the deleted state present in DB from imported actionCollection
|
.getDefaultResources()
|
||||||
existingActionCollection
|
.setBranchName(branchName);
|
||||||
.getUnpublishedCollection()
|
// Recover the deleted state present in DB from imported actionCollection
|
||||||
.setDeletedAt(actionCollection
|
existingActionCollection
|
||||||
.getUnpublishedCollection()
|
.getUnpublishedCollection()
|
||||||
.getDeletedAt());
|
.setDeletedAt(actionCollection
|
||||||
existingActionCollection.setDeletedAt(actionCollection.getDeletedAt());
|
.getUnpublishedCollection()
|
||||||
existingActionCollection.setDeleted(actionCollection.getDeleted());
|
.getDeletedAt());
|
||||||
existingActionCollection.setPolicies(existingPolicy);
|
existingActionCollection.setDeletedAt(actionCollection.getDeletedAt());
|
||||||
|
existingActionCollection.setDeleted(actionCollection.getDeleted());
|
||||||
|
existingActionCollection.setPolicies(existingPolicy);
|
||||||
|
|
||||||
existingActionCollection.updateForBulkWriteOperation();
|
existingActionCollection.updateForBulkWriteOperation();
|
||||||
existingActionCollections.add(existingActionCollection);
|
existingActionCollections.add(existingActionCollection);
|
||||||
resultDTO.getSavedActionCollectionIds().add(existingActionCollection.getId());
|
resultDTO.getSavedActionCollectionIds().add(existingActionCollection.getId());
|
||||||
resultDTO.getSavedActionCollectionMap().put(idFromJsonFile, existingActionCollection);
|
resultDTO
|
||||||
} else {
|
.getSavedActionCollectionMap()
|
||||||
if (!permissionProvider.canCreateAction(parentPage)) {
|
.put(idFromJsonFile, existingActionCollection);
|
||||||
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 {
|
} else {
|
||||||
DefaultResources defaultResources = new DefaultResources();
|
if (!permissionProvider.canCreateAction(parentPage)) {
|
||||||
defaultResources.setApplicationId(defaultApplicationId);
|
throw new AppsmithException(
|
||||||
defaultResources.setBranchName(branchName);
|
AppsmithError.ACL_NO_RESOURCE_FOUND,
|
||||||
actionCollection.setDefaultResources(defaultResources);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.info(
|
||||||
// this will generate the id and other auto generated fields e.g. createdAt
|
"Saving action collections in bulk. New: {}, Updated: {}",
|
||||||
actionCollection.updateForBulkWriteOperation();
|
newActionCollections.size(),
|
||||||
generateAndSetPolicies(parentPage, actionCollection);
|
existingActionCollections.size());
|
||||||
|
return repository
|
||||||
// create or update default resources for the action
|
.bulkInsert(newActionCollections)
|
||||||
// values already set to defaultResources are kept unchanged
|
.then(repository.bulkUpdate(existingActionCollections))
|
||||||
DefaultResourcesUtils.createDefaultIdsOrUpdateWithGivenResourceIds(
|
.thenReturn(resultDTO);
|
||||||
actionCollection, branchName);
|
});
|
||||||
|
})
|
||||||
// generate gitSyncId if it's not present
|
.onErrorResume(e -> {
|
||||||
if (actionCollection.getGitSyncId() == null) {
|
log.error("Error saving action collections", e);
|
||||||
actionCollection.setGitSyncId(
|
return Mono.error(e);
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -289,6 +289,7 @@ public class ApplicationServiceCEImpl extends BaseService<ApplicationRepository,
|
||||||
return applicationIdMono.flatMap(appId -> repository
|
return applicationIdMono.flatMap(appId -> repository
|
||||||
.updateById(appId, application, applicationPermission.getEditPermission())
|
.updateById(appId, application, applicationPermission.getEditPermission())
|
||||||
.onErrorResume(error -> {
|
.onErrorResume(error -> {
|
||||||
|
log.error("failed to update application {}", appId, error);
|
||||||
if (error instanceof DuplicateKeyException) {
|
if (error instanceof DuplicateKeyException) {
|
||||||
// Error message : E11000 duplicate key error collection: appsmith.application index:
|
// Error message : E11000 duplicate key error collection: appsmith.application index:
|
||||||
// workspace_app_deleted_gitApplicationMetadata dup key:
|
// workspace_app_deleted_gitApplicationMetadata dup key:
|
||||||
|
|
|
||||||
|
|
@ -1698,149 +1698,177 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
|
||||||
/* Mono.just(application) is created to avoid the eagerly fetching of existing actions
|
/* 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.
|
* during the pipeline construction. It should be fetched only when the pipeline is subscribed/executed.
|
||||||
*/
|
*/
|
||||||
return Mono.just(application).flatMap(importedApplication -> {
|
return Mono.just(application)
|
||||||
Mono<Map<String, NewAction>> actionsInCurrentAppMono = repository
|
.flatMap(importedApplication -> {
|
||||||
.findByApplicationId(importedApplication.getId())
|
Mono<Map<String, NewAction>> actionsInCurrentAppMono = repository
|
||||||
.filter(newAction -> newAction.getGitSyncId() != null)
|
.findByApplicationId(importedApplication.getId())
|
||||||
.collectMap(NewAction::getGitSyncId);
|
.filter(newAction -> newAction.getGitSyncId() != null)
|
||||||
|
.collectMap(NewAction::getGitSyncId);
|
||||||
|
|
||||||
// find existing actions in all the branches of this application and put them in a map
|
// find existing actions in all the branches of this application and put them in a map
|
||||||
Mono<Map<String, NewAction>> actionsInOtherBranchesMono;
|
Mono<Map<String, NewAction>> actionsInOtherBranchesMono;
|
||||||
if (importedApplication.getGitApplicationMetadata() != null) {
|
if (importedApplication.getGitApplicationMetadata() != null) {
|
||||||
final String defaultApplicationId =
|
final String defaultApplicationId =
|
||||||
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
|
importedApplication.getGitApplicationMetadata().getDefaultApplicationId();
|
||||||
actionsInOtherBranchesMono = repository
|
actionsInOtherBranchesMono = repository
|
||||||
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
|
.findByDefaultApplicationId(defaultApplicationId, Optional.empty())
|
||||||
.filter(newAction -> newAction.getGitSyncId() != null)
|
.filter(newAction -> newAction.getGitSyncId() != null)
|
||||||
.collectMap(NewAction::getGitSyncId);
|
.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);
|
|
||||||
} else {
|
} else {
|
||||||
// check whether user has permission to add new action
|
actionsInOtherBranchesMono = Mono.just(Collections.emptyMap());
|
||||||
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(
|
return Mono.zip(actionsInCurrentAppMono, actionsInOtherBranchesMono)
|
||||||
"Saving actions in bulk. New: {}, Updated: {}",
|
.flatMap(objects -> {
|
||||||
newNewActionList.size(),
|
Map<String, NewAction> actionsInCurrentApp = objects.getT1();
|
||||||
existingNewActionList.size());
|
Map<String, NewAction> actionsInOtherBranches = objects.getT2();
|
||||||
|
|
||||||
// Save all the new actions in bulk
|
List<NewAction> newNewActionList = new ArrayList<>();
|
||||||
return repository
|
List<NewAction> existingNewActionList = new ArrayList<>();
|
||||||
.bulkInsert(newNewActionList)
|
|
||||||
.then(repository.bulkUpdate(existingNewActionList))
|
final String workspaceId = importedApplication.getWorkspaceId();
|
||||||
.thenReturn(importActionResultDTO);
|
|
||||||
});
|
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
|
@Override
|
||||||
|
|
@ -1892,7 +1920,7 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
|
||||||
.get(newAction.getId())
|
.get(newAction.getId())
|
||||||
.get(0));
|
.get(0));
|
||||||
if (unpublishedAction.getDefaultResources() != null
|
if (unpublishedAction.getDefaultResources() != null
|
||||||
&& !StringUtils.hasLength(
|
&& org.apache.commons.lang3.StringUtils.isEmpty(
|
||||||
unpublishedAction.getDefaultResources().getCollectionId())) {
|
unpublishedAction.getDefaultResources().getCollectionId())) {
|
||||||
|
|
||||||
unpublishedAction
|
unpublishedAction
|
||||||
|
|
@ -1910,7 +1938,7 @@ public class NewActionServiceCEImpl extends BaseService<NewActionRepository, New
|
||||||
.get(0));
|
.get(0));
|
||||||
|
|
||||||
if (publishedAction.getDefaultResources() != null
|
if (publishedAction.getDefaultResources() != null
|
||||||
&& !StringUtils.hasLength(
|
&& org.apache.commons.lang3.StringUtils.isEmpty(
|
||||||
publishedAction.getDefaultResources().getCollectionId())) {
|
publishedAction.getDefaultResources().getCollectionId())) {
|
||||||
|
|
||||||
publishedAction
|
publishedAction
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -289,4 +289,33 @@ public class ApplicationSnapshotServiceUnitTest {
|
||||||
})
|
})
|
||||||
.verifyComplete();
|
.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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user