package com.vst.webapp.action;
import com.vst.dao.Dao;
import com.vst.model.Loader;
import com.vst.webapp.listener.FileUploadListener;
import com.vst.util.db2sql;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Iterator;
import java.io.File;
/**
* Created by IntelliJ IDEA.
* User: ASchukin
* Date: 04.12.2007
* Time: 21:43:36
* To change this template use File | Settings | File Templates.
*/
public class LoadFormController extends BaseFormController {
private Dao dao = null;
public void setDao(Dao dao) {
this.dao = dao;
}
public LoadFormController() {
setCommandName("loader");
setCommandClass(Loader.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
Loader loader = new Loader();
return loader;
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors)
throws Exception {
/*
// create file upload factory and upload servlet
FileItemFactory
factory = new DiskFileItemFactory();
ServletFileUpload
upload = new ServletFileUpload(factory);
// set file upload progress listener
FileUploadListener
listener = new FileUploadListener();
HttpSession
session = request.getSession();
session.setAttribute("LISTENER", listener);
// upload servlet allows to set upload listener
upload.setProgressListener(listener);
List
uploadedItems = null;
FileItem
fileItem = null;
String
filePath = "c:\\temp"; // Path to store file on local system
// tttttttttttttttttttttttttttttttttttttt
File dumpFile = null;
// tttttttttttttttttttttttttttttttttttttt
try
{
// iterate over all uploaded files
uploadedItems = upload.parseRequest(request);
Iterator i = uploadedItems.iterator();
while (i.hasNext())
{
fileItem = (FileItem) i.next();
if (fileItem.isFormField() == false)
{
if (fileItem.getSize() > 0)
{
File
uploadedFile = null;
String
myFullFileName = fileItem.getName(),
myFileName = "",
slashType = (myFullFileName.lastIndexOf("\\") > 0) ? "\\" : "/"; // Windows or UNIX
int
startIndex = myFullFileName.lastIndexOf(slashType);
// Ignore the path and get the filename
myFileName = myFullFileName.substring(startIndex + 1, myFullFileName.length());
// Create new File object
uploadedFile = new File(filePath, myFileName);
dumpFile = new File(filePath, myFileName);
// Write the uploaded file to the system
fileItem.write(uploadedFile);
}
}
}
// tttttttttttttttttttttttttttttttttttttt
request.setAttribute("isLoaded", "true");
// if (!db2sql.execDump(FileHelper.loadFileAsString(dumpFile,"UTF-8"),null, request, response)) {
// request.setAttribute("sqlMistake", "true");
// }
db2sql.test();
if( dumpFile != null )
dumpFile.delete();
else
request.setAttribute("noFile", "true");
// tttttttttttttttttttttttttttttttttttttt
response.sendRedirect("/loading.html");
}
catch (FileUploadException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
*/
return new ModelAndView("loadForm");
}
}