Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page language="java" import="javazoom.upload.*,java.util.*, com.booking.utility.*, com.booking.model.*" %>
- <%@ page errorPage="ExceptionHandler.jsp" %>
- <jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >
- </jsp:useBean>
- <%
- String appPath = application.getRealPath("/");
- upBean.setFolderstore(appPath + "csv");
- upBean.setOverwrite(true);
- upBean.setWhitelist("*.csv");
- %>
- <html>
- <head>
- <title>Upload User CSV</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
- </head>
- <body bgcolor="#FFFFFF" text="#000000">
- <div align="center">
- <%
- boolean error = true;
- String userFileName = "";
- String facilityFileName = "";
- if (MultipartFormDataRequest.isMultipartFormData(request)) {
- MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
- String todo = null;
- if (mrequest != null) {
- todo = mrequest.getParameter("todo");
- }
- if ((todo != null) && (todo.equalsIgnoreCase("upload"))) {
- Hashtable files = mrequest.getFiles();
- UploadFile userFile = (UploadFile) files.get("userFile");
- UploadFile facilityFile = (UploadFile) files.get("facilityFile");
- if (userFile != null && userFile.getFileSize() > 0 && facilityFile != null && facilityFile.getFileSize() > 0) {
- userFileName = userFile.getFileName();
- facilityFileName = facilityFile.getFileName();
- upBean.store(mrequest);
- error = false;
- }
- %>
- <b>Upload status : </b>
- <ul>
- <%
- Vector history = upBean.getHistory();
- for (int i = 0; i < history.size(); i++) {
- UploadParameters up = (UploadParameters) history.elementAt(i);
- out.println("<li>Uploaded file : " + up.getFilename() + " (" + up.getFilesize() + " bytes)" + "<BR> Content Type : " + up.getContenttype());
- out.println("<BR>StoreModel : " + up.getStoremodelname() + " (" + up.getStoreinfo() + ")");
- }
- %>
- </ul>
- <%
- }
- }
- %>
- </div>
- <div id="content" align="center">
- <%
- if (error == false) {
- //on windows shld be \\
- String userFullFilePath = appPath + "csv" + "/" + userFileName;
- String facilityFullFilePath = appPath + "csv" + "/" + facilityFileName;
- //PARSE USER
- List userList = ParseCSV.readCSVFile(userFullFilePath);
- Iterator iterUser = userList.iterator();
- String[] headerUser = (String[]) iterUser.next();
- List facilityList = ParseCSV.readCSVFile(facilityFullFilePath);
- Iterator iterFac = facilityList.iterator();
- String[] headerFac = (String[]) iterFac.next();
- boolean emailFound = false;
- boolean passwordFound = false;
- boolean nameFound = false;
- boolean typeFound = false;
- boolean capacityFound = false;
- //find email index
- int email_index = 0;
- for (int i = 0; i < headerUser.length; i++) {
- if (headerUser[i].equals("email")) {
- email_index = i;
- emailFound = true;
- }
- }
- int password_index = 0;
- for (int i = 0; i < headerUser.length; i++) {
- if (headerUser[i].equals("password")) {
- password_index = i;
- passwordFound = true;
- }
- }
- int name_index = 0;
- for (int i = 0; i < headerFac.length; i++) {
- if (headerFac[i].equals("name")) {
- name_index = i;
- nameFound = true;
- }
- }
- int type_index = 0;
- for (int i = 0; i < headerFac.length; i++) {
- if (headerFac[i].equals("type")) {
- type_index = i;
- typeFound = true;
- }
- }
- int capacity_index = 0;
- for (int i = 0; i < headerFac.length; i++) {
- if (headerFac[i].equals("capacity")) {
- capacity_index = i;
- capacityFound = true;
- }
- }
- if (passwordFound == false || emailFound == false || nameFound == false || typeFound == false || capacityFound == false) {
- response.sendRedirect("index.jsp?error=true");
- return;
- }
- UserDAO uDAO = new UserDAO();
- RoomDAO rDAO = new RoomDAO();
- //success - file is correct
- rDAO.clearRooms();
- uDAO.clearUsers();
- int count = 0;
- while (iterUser.hasNext()) {
- String[] line = (String[]) iterUser.next();
- String email = line[email_index];
- String password = line[password_index];
- //start adding to database
- int rows = uDAO.addUser(email, password);
- if (rows == 1) {
- //success
- out.println(email + " / " + password + " added<br/>");
- count++;
- } else {
- out.println(email + " / " + password + " add fail<br/>");
- }
- }
- out.println("<br/>User CSV - " + count + " rows added<br/>");
- out.println("<br/><br/>");
- count = 0;
- while (iterFac.hasNext()) {
- String[] line = (String[]) iterFac.next();
- String name = line[name_index];
- String type = line[type_index];
- int capacity = Integer.parseInt(line[capacity_index]);
- //start adding to database
- int rows = rDAO.addRoom(name, type, capacity);
- if (rows == 1) {
- //success
- out.println(name + " / " + type + " / " + capacity + " added<br/>");
- count++;
- } else {
- out.println(name + " / " + type + " / " + capacity + " add fail<br/>");
- }
- }
- out.println("<br/>Facility CSV - " + count + " rows added<br/>");
- out.println("<br/><a href=\"../index.jsp\">Back to main</a>");
- } else {
- response.sendRedirect("index.jsp?error=true");
- return;
- }
- %>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment