Guest User

Untitled

a guest
May 7th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. <%@ page language="java" import="javazoom.upload.*,java.util.*, com.booking.utility.*, com.booking.model.*" %>
  2. <%@ page errorPage="ExceptionHandler.jsp" %>
  3.  
  4. <jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >
  5. </jsp:useBean>
  6.  
  7. <%
  8. String appPath = application.getRealPath("/");
  9. upBean.setFolderstore(appPath + "csv");
  10. upBean.setOverwrite(true);
  11. upBean.setWhitelist("*.csv");
  12. %>
  13.  
  14. <html>
  15. <head>
  16. <title>Upload User CSV</title>
  17. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  18. </head>
  19. <body bgcolor="#FFFFFF" text="#000000">
  20. <div align="center">
  21. <%
  22. boolean error = true;
  23. String userFileName = "";
  24. String facilityFileName = "";
  25.  
  26. if (MultipartFormDataRequest.isMultipartFormData(request)) {
  27. MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
  28. String todo = null;
  29. if (mrequest != null) {
  30. todo = mrequest.getParameter("todo");
  31. }
  32. if ((todo != null) && (todo.equalsIgnoreCase("upload"))) {
  33. Hashtable files = mrequest.getFiles();
  34. UploadFile userFile = (UploadFile) files.get("userFile");
  35. UploadFile facilityFile = (UploadFile) files.get("facilityFile");
  36. if (userFile != null && userFile.getFileSize() > 0 && facilityFile != null && facilityFile.getFileSize() > 0) {
  37. userFileName = userFile.getFileName();
  38. facilityFileName = facilityFile.getFileName();
  39. upBean.store(mrequest);
  40. error = false;
  41. }
  42. %>
  43. <b>Upload status : </b>
  44. <ul>
  45. <%
  46. Vector history = upBean.getHistory();
  47. for (int i = 0; i < history.size(); i++) {
  48. UploadParameters up = (UploadParameters) history.elementAt(i);
  49. out.println("<li>Uploaded file : " + up.getFilename() + " (" + up.getFilesize() + " bytes)" + "<BR> Content Type : " + up.getContenttype());
  50. out.println("<BR>StoreModel : " + up.getStoremodelname() + " (" + up.getStoreinfo() + ")");
  51. }
  52. %>
  53. </ul>
  54. <%
  55. }
  56. }
  57. %>
  58. </div>
  59.  
  60. <div id="content" align="center">
  61. <%
  62. if (error == false) {
  63. //on windows shld be \\
  64. String userFullFilePath = appPath + "csv" + "/" + userFileName;
  65. String facilityFullFilePath = appPath + "csv" + "/" + facilityFileName;
  66.  
  67. //PARSE USER
  68.  
  69. List userList = ParseCSV.readCSVFile(userFullFilePath);
  70. Iterator iterUser = userList.iterator();
  71. String[] headerUser = (String[]) iterUser.next();
  72.  
  73. List facilityList = ParseCSV.readCSVFile(facilityFullFilePath);
  74. Iterator iterFac = facilityList.iterator();
  75. String[] headerFac = (String[]) iterFac.next();
  76.  
  77. boolean emailFound = false;
  78. boolean passwordFound = false;
  79. boolean nameFound = false;
  80. boolean typeFound = false;
  81. boolean capacityFound = false;
  82.  
  83. //find email index
  84. int email_index = 0;
  85. for (int i = 0; i < headerUser.length; i++) {
  86. if (headerUser[i].equals("email")) {
  87. email_index = i;
  88. emailFound = true;
  89. }
  90. }
  91.  
  92. int password_index = 0;
  93. for (int i = 0; i < headerUser.length; i++) {
  94. if (headerUser[i].equals("password")) {
  95. password_index = i;
  96. passwordFound = true;
  97. }
  98. }
  99.  
  100. int name_index = 0;
  101. for (int i = 0; i < headerFac.length; i++) {
  102. if (headerFac[i].equals("name")) {
  103. name_index = i;
  104. nameFound = true;
  105. }
  106. }
  107.  
  108. int type_index = 0;
  109. for (int i = 0; i < headerFac.length; i++) {
  110. if (headerFac[i].equals("type")) {
  111. type_index = i;
  112. typeFound = true;
  113. }
  114. }
  115.  
  116. int capacity_index = 0;
  117. for (int i = 0; i < headerFac.length; i++) {
  118. if (headerFac[i].equals("capacity")) {
  119. capacity_index = i;
  120. capacityFound = true;
  121. }
  122. }
  123.  
  124. if (passwordFound == false || emailFound == false || nameFound == false || typeFound == false || capacityFound == false) {
  125. response.sendRedirect("index.jsp?error=true");
  126. return;
  127. }
  128.  
  129. UserDAO uDAO = new UserDAO();
  130. RoomDAO rDAO = new RoomDAO();
  131.  
  132.  
  133. //success - file is correct
  134.  
  135. rDAO.clearRooms();
  136. uDAO.clearUsers();
  137.  
  138. int count = 0;
  139.  
  140. while (iterUser.hasNext()) {
  141. String[] line = (String[]) iterUser.next();
  142. String email = line[email_index];
  143. String password = line[password_index];
  144. //start adding to database
  145. int rows = uDAO.addUser(email, password);
  146. if (rows == 1) {
  147. //success
  148. out.println(email + " / " + password + " added<br/>");
  149. count++;
  150. } else {
  151. out.println(email + " / " + password + " add fail<br/>");
  152. }
  153. }
  154.  
  155. out.println("<br/>User CSV - " + count + " rows added<br/>");
  156. out.println("<br/><br/>");
  157.  
  158. count = 0;
  159.  
  160. while (iterFac.hasNext()) {
  161. String[] line = (String[]) iterFac.next();
  162. String name = line[name_index];
  163. String type = line[type_index];
  164. int capacity = Integer.parseInt(line[capacity_index]);
  165. //start adding to database
  166. int rows = rDAO.addRoom(name, type, capacity);
  167. if (rows == 1) {
  168. //success
  169. out.println(name + " / " + type + " / " + capacity + " added<br/>");
  170. count++;
  171. } else {
  172. out.println(name + " / " + type + " / " + capacity + " add fail<br/>");
  173. }
  174. }
  175.  
  176. out.println("<br/>Facility CSV - " + count + " rows added<br/>");
  177. out.println("<br/><a href=\"../index.jsp\">Back to main</a>");
  178.  
  179. } else {
  180. response.sendRedirect("index.jsp?error=true");
  181. return;
  182. }
  183. %>
  184.  
  185.  
  186. </div>
  187.  
  188. </body>
  189. </html>
Add Comment
Please, Sign In to add comment