package modules.admin.DataMaintenance.actions;

import java.io.File;

import org.skyve.CORE;
import org.skyve.impl.backup.ExternalBackup;
import org.skyve.metadata.controller.ServerSideAction;
import org.skyve.metadata.controller.ServerSideActionResult;
import org.skyve.util.FileUtil;
import org.skyve.util.Util;
import org.skyve.web.WebContext;

import modules.admin.domain.DataMaintenance;

public class DeleteBackup implements ServerSideAction<DataMaintenance> {
	private static final long serialVersionUID = 5306067916641877356L;

	@Override
	public ServerSideActionResult<DataMaintenance> execute(DataMaintenance bean, WebContext webContext)
	throws Exception {
		String customerName = CORE.getUser().getCustomerName();
		File backup = new File(String.format("%sbackup_%s%s%s",
												Util.getContentDirectory(),
												customerName,
												File.separator,
												bean.getSelectedBackupName()));
		if (backup.exists()) {
			Util.LOGGER.info("Deleting backup " + backup.getAbsolutePath());
			if (ExternalBackup.areExternalBackupsEnabled()) {
				ExternalBackup.getInstance().deleteBackup(bean.getSelectedBackupName());
			} else {
				FileUtil.delete(backup);
			}
			Util.LOGGER.info("Deleted backup " + backup.getAbsolutePath());
		}
		else {
			Util.LOGGER.info("Backup " + backup.getAbsolutePath() + " no longer exists");
		}

		bean.setSelectedBackupName(null); // deselect the deleted backup
		bean.setRefreshBackups(Boolean.TRUE);

		return new ServerSideActionResult<>(bean);
	}
}

@@ -721,16 +721,560 @@
 me();%0A%09%09
+%0A%09%09// delete external backup if enabled%0A%09%09%09%09if (ExternalBackup.areExternalBackupsEnabled()) %7B%0A%09%09%09%09%09String backupName = bean.getSelectedBackupName();%0A%09%09%09%09%09if (ExternalBackup.getInstance().exists(backupName)) %7B%0A%09%09%09%09%09%09Util.LOGGER.info(%22Deleting backup %22 + backupName);%0A%09%09%09%09%09%09ExternalBackup.getInstance().deleteBackup(bean.getSelectedBackupName());%0A%09%09%09%09%09%09Util.LOGGER.info(%22Deleted backup %22 + backupName);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09Util.LOGGER.info(%22Backup %22 + backupName + %22 no longer exists%22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09// delete from local content%0A%09%09%09%09%09
 File bac
@@ -1328,21 +1328,16 @@
 %0A%09%09%09%09%09%09%09
-%09%09%09%09%09
 Util.get
@@ -1363,21 +1363,16 @@
 %0A%09%09%09%09%09%09%09
-%09
-%09%09%09%09
 customer
@@ -1384,21 +1384,16 @@
 %0A%09%09%09%09%09%09%09
-%09%09%09
-%09%09
 File.sep
@@ -1407,21 +1407,16 @@
 %0A%09%09%09%09%09%09%09
-%09%09%09%09
-%09
 bean.get
@@ -1439,16 +1439,19 @@
 me()));%0A
+%09%09%09
 %09%09if (ba
@@ -1467,16 +1467,19 @@
 ts()) %7B%0A
+%09%09%09
 %09%09%09Util.
@@ -1544,148 +1544,8 @@
 ;%0A%09%09
-%09if (ExternalBackup.areExternalBackupsEnabled()) %7B%0A%09%09%09%09ExternalBackup.getInstance().deleteBackup(bean.getSelectedBackupName());%0A%09%09%09%7D else %7B%0A
 %09%09%09%09
@@ -1572,18 +1572,16 @@
 up);%0A%09%09%09
-%7D%0A
 %09%09%09Util.
@@ -1645,19 +1645,23 @@
 ;%0A%09%09
+%09
-%7D%0A
 %09%09
+%7D 
 else %7B%0A
+%09%09%09
 %09%09%09U
@@ -1743,11 +1743,57 @@
 ;%0A%09%09
-%7D%0A%0A
+%09%09%09%7D%0A%09%09%09%09%7D%0A%0A%09%09%09%09// deselect the deleted backup%0A%09%09
 %09%09be
@@ -1827,40 +1827,11 @@
 ll);
- // deselect the deleted backup%0A
+%0A%09%09
 %09%09be

Update DeleteBackup when external backups are enabled to check if the backup exists externally before trying to delete