package modules.admin.DataMaintenance.actions;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.skyve.CORE;
import org.skyve.domain.messages.UploadException;
import org.skyve.domain.messages.UploadException.Problem;
import org.skyve.metadata.controller.UploadAction;
import org.skyve.util.Util;
import org.skyve.web.WebContext;

import modules.admin.domain.DataMaintenance;

public class UploadBackup extends UploadAction<DataMaintenance> {

	private static final long serialVersionUID = -7270254238606857719L;

	@Override
	public DataMaintenance upload(final DataMaintenance bean, final UploadedFile file,
			final UploadException exception, final WebContext webContext) throws Exception {
		
		// create the backup upload file
		File backup = new File(String.format("%sbackup_%s%s%s", 
				Util.getContentDirectory(),
				CORE.getUser().getCustomerName(),
				File.separator,
				file.getFileName()));
		
		// check that a backup with this filename doesn't already exist
		if(backup.exists()) {
			exception.addError(new Problem(
					String.format("A backup with the name %s already exists.", file.getFileName()), null));
			throw exception;
		}
		
		// copy the input file to the backup location		
		Files.copy(file.getInputStream(), Paths.get(backup.getAbsolutePath()));
		if(backup.exists()) {
			Util.LOGGER.info("Uploaded backup " + backup.getAbsolutePath());
		}
		
		// refresh the list of backups to include the uploaded one
		bean.setRefreshBackups(Boolean.TRUE);
		
		return bean;
	}
}

@@ -1182,24 +1182,398 @@
 ion;%0A%09%09%7D%0A%09%09%0A
+%09%09//check that the backup folder exists, and if not, create it%0A%09%09File backup_directory = new File(String.format(%22%25sbackup_%25s%22, %0A%09%09%09%09Util.getContentDirectory(),%0A%09%09%09%09CORE.getUser().getCustomerName()));%0A%09%09if (!backup_directory.exists()) %7B%0A%09%09    try%7B%0A%09%09        backup_directory.mkdir();%0A%09%09    %7D %0A%09%09    catch(SecurityException se)%7B%0A%09%09        //handle it%0A%09%09    %7D        %0A%09%09%7D%09%09%0A%09%09%0A
 %09%09// copy th

Create content backup directory if it doesn't exist during upload backup