Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.quantros.qsupport;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import jxl.Cell;
- import org.apache.commons.fileupload.DiskFileUpload;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.lang.StringUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import com.infoobjectsinc.util.QGlobals;
- import com.infoobjectsinc.util.QUser;
- import com.infoobjectsinc.util.QuantrosException;
- import com.quantros.AppModules.IOFunctions;
- import com.quantros.AppModules.ProcessDataHandler;
- import com.quantros.qsupport.DAO.BatchUploadDAO;
- import com.quantros.qxplore.Globals;
- import com.quantros.util.ServerUtil;
- public class BatchUpload implements ProcessDataHandler{
- public void processData(HttpServletRequest req, HttpServletResponse res, ServletContext servctxt) throws ServletException, Exception {
- HttpSession session = req.getSession(true);
- IOFunctions iof = new IOFunctions(QGlobals.debugger);
- QUser qUser = (QUser) session.getAttribute("quser");
- BatchUploadDAO dao = new BatchUploadDAO();
- if (qUser == null) {
- iof.forwardTo(req, res, servctxt, "/qsupport/jsp/index.jsp");
- return;
- }
- try {
- String uploadUsersID = qUser.getIdAsString();
- String action = iof.getParameter(req, "action", "");
- String forEntitiesID = iof.getParameter(req, "ENTITIESID", "");
- String uploadType = iof.getParameter(req, "uploadType", "");
- if (action.equalsIgnoreCase("uploadFile")) {
- JSONObject jsonResponse = new JSONObject();
- JSONObject uploadResult = uploadFile(qUser, uploadType, iof, req, res, servctxt);
- ParseExcel parser = null;
- String uploadID = "";
- if (uploadType.equalsIgnoreCase("Departments")) {
- parser = new ParseExcelDepartments();
- //parser.setIsDebug(true);
- } else if (uploadType.equalsIgnoreCase("Facilities")) {
- parser = new ParseExcelFacilities();
- //parser.setIsDebug(true);
- } else if (uploadType.equalsIgnoreCase("Users")) {
- parser = new ParseExcelUsers();
- } else if (uploadType.equalsIgnoreCase("SeverityAlertNotificationUserAssignment")){
- parser = new ParseExcelSeverityAlertUsers();
- } else {
- throw new Exception("upload type " + uploadType + " is not known.");
- }
- uploadID = dao.insertFileIntoTable(uploadUsersID, forEntitiesID, uploadResult.getString("jobname"), uploadResult.getString("FILE_PATH"), uploadResult.getString("ORIGINAL_FILE_PATH"), uploadType);
- jsonResponse.put("success", true);
- res.setContentType("text/html");
- res.getWriter().print(jsonResponse);
- } else if (action.equalsIgnoreCase("getUploadHistory")) {
- JSONObject jsonResponse = new JSONObject();
- jsonResponse.put("success", true);
- JSONArray ar = getUploadHistory(forEntitiesID,dao,uploadType);
- jsonResponse.put("uploadHistory", ar);
- res.setContentType("application/json");
- res.getWriter().print(jsonResponse);
- } else if (action.equalsIgnoreCase("downloadOriginalFile")) {
- String uploadID = iof.getParameter(req, "uploadID", "");
- uploadType = iof.getParameter(req, "uploadType", "");
- File of = ServerUtil.getFile(dao.getUploadedFile(uploadID));
- String fileName = dao.getClientSideFilePath(uploadID);
- String[] components = fileName.split("[\\/]");
- int len = components.length;
- fileName = components[len - 1];
- res.setHeader("Expires", "0");
- res.setHeader("Cache-Control","private, must-revalidate, max-age=300, post-check=0, pre-check=0");
- res.setContentType("application/vnd.ms-excel");
- res.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
- res.setHeader("Content-Length", String.valueOf(of.length()));
- ServletOutputStream servletoutputstream = res.getOutputStream();
- int bufferSize = 10240; // 10KB
- FileInputStream fileinputstream = ServerUtil.getFileInputStream(of);
- try {
- int i = fileinputstream.available();
- byte numOfBytes[] = new byte[i];
- int j = fileinputstream.read(numOfBytes);
- fileinputstream.close();
- servletoutputstream.write(numOfBytes);
- } catch (Exception e) {
- fileinputstream.close();
- QGlobals.debugger.error("BatchUpload","processData",e);
- throw new QuantrosException(e);
- }
- servletoutputstream.flush();
- } else if (action.equalsIgnoreCase("downloadBadRecords")) {
- String uploadID = iof.getParameter(req, "uploadID", "");
- uploadType = iof.getParameter(req, "uploadType", "");
- ParseExcel parser = null;
- if (uploadType.equalsIgnoreCase("Departments")) {
- parser = new ParseExcelDepartments();
- } else if (uploadType.equalsIgnoreCase("Facilities")) {
- parser = new ParseExcelFacilities();
- //parser.setIsDebug(true);
- } else if (uploadType.equalsIgnoreCase("Users")) {
- parser = new ParseExcelUsers();
- parser.setIsDebug(true);
- // UploadFacilitiesParseExcel parser = new UploadFacilitiesParseExcel();
- } else if (uploadType.equalsIgnoreCase("SeverityAlertNotificationUserAssignment")) {
- parser = new ParseExcelSeverityAlertUsers();
- parser.setIsDebug(true);
- } else {
- throw new Exception("upload type " + uploadType + " is not known.");
- }
- File badFile = ServerUtil.getFile(dao.getBadRecordFilePath(uploadID));
- String fileName = badFile.getName();
- QGlobals.debugger.log("BatchUpload","processData", "Bad record file name:" + fileName, 4);
- res.setHeader("Expires", "0");
- res.setHeader("Cache-Control","private, must-revalidate, max-age=300, post-check=0, pre-check=0");
- res.setContentType("application/vnd.ms-excel");
- res.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
- res.setHeader("Content-Length", String.valueOf(badFile.length()));
- ServletOutputStream servletoutputstream = res.getOutputStream();
- int bufferSize = 10240; // 10KB
- FileInputStream fileinputstream = ServerUtil.getFileInputStream(badFile);
- QGlobals.debugger.log("BatchUpload","processData", "Before sending excel content", 4);
- try {
- int i = fileinputstream.available();
- byte numOfBytes[] = new byte[i];
- int j = fileinputstream.read(numOfBytes);
- fileinputstream.close();
- servletoutputstream.write(numOfBytes);
- } catch (Exception e) {
- fileinputstream.close();
- QGlobals.debugger.error("BatchUpload","processData",e);
- throw new QuantrosException(e);
- }
- servletoutputstream.flush();
- }
- } catch (Exception e) {
- QGlobals.debugger.error("BatchUpload", "processData", e);
- throw new QuantrosException(e);
- }
- }
- private JSONArray getUploadHistory(String entitiesID, BatchUploadDAO dao, String uploadType) throws Exception {
- List lst = dao.getUploadHistory(entitiesID, uploadType);
- JSONArray ar = new JSONArray();
- Iterator itr = lst.iterator();
- Map m;
- int badCount = 0;
- int totalRecordCount = 0;
- String downloadLink = "";
- while (itr.hasNext()) {
- m = (Map) itr.next();
- badCount = Integer.parseInt(m.get("BAD_RECORD_COUNT").toString());
- totalRecordCount = Integer.parseInt(m.get("RECORD_COUNT").toString());
- if (badCount > 0) {
- downloadLink = "<a class='badRecordsDownloadLink' onclick='badRecordLinkClicked();' href='/qsupport/controller/QSupportController?ServName=BatchUpload&action=downloadBadRecords&uploadID=" +
- m.get("UPLOADID").toString() + "&uploadType=" + uploadType + "'>" + badCount + "</a>";
- m.put("BAD_RECORD_COUNT", downloadLink);
- }
- downloadLink = "<a class='totalRecordsDownloadLink' onclick='totalRecordLinkClicked();' href='/qsupport/controller/QSupportController?ServName=BatchUpload&action=downloadOriginalFile&uploadID=" +
- m.get("UPLOADID").toString() + "&uploadType=" + uploadType + "'>" + totalRecordCount + "</a>";
- m.put("RECORD_COUNT", downloadLink);
- ar.put(new JSONObject(m));
- }
- return ar;
- }
- /*
- * This method builds a JSONObject for the given record.
- */
- public JSONObject cellsToJSONObject(int index, String[] headerNames, Cell[] dataCells) throws QuantrosException {
- JSONObject jsonRecord = new JSONObject();
- try {
- jsonRecord.put("s_no", index);
- for(int i = 0; i < dataCells.length; i++) {
- jsonRecord.put(headerNames[i], dataCells[i].getContents());
- }
- } catch(Exception e) {
- QGlobals.debugger.error("ExcelImport", "cellsToJSONObject", e);
- throw new QuantrosException(e);
- }
- return jsonRecord;
- }
- private JSONObject uploadFile(QUser qUser, String uploadType, IOFunctions iof, HttpServletRequest req, HttpServletResponse res, ServletContext servctxt) throws ServletException, Exception {
- String entitiesID = iof.getParameter(req,"ENTITIESID", "");
- String jobName = iof.getParameter(req,"jobname", "");
- boolean isMultipart = ServletFileUpload.isMultipartContent(req);
- Globals.debugger.log("BatchUpload", "uploadFile", "isMultipart:" + isMultipart, 4);
- JSONObject result = new JSONObject();
- File f = null;
- DiskFileUpload upload = new DiskFileUpload();
- List items = upload.parseRequest(req);
- Iterator itr = items.iterator();
- while(itr.hasNext()) {
- FileItem item = (FileItem)itr.next();
- if (! item.isFormField()) {
- String usersID = qUser.getIdAsString();
- String originalFileName = item.getName().substring(item.getName().lastIndexOf(File.separatorChar) + 1);
- String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
- String fileName = "" + System.currentTimeMillis() + "." + extension;
- ArrayList pathComponents = new ArrayList();
- pathComponents.add((Globals.splusInfo.myProps.getProperty("ATTACH_DOC_DRIVE")).trim() + "QSUPPORT");
- pathComponents.add("BatchUploads");
- pathComponents.add(uploadType);
- pathComponents.add(entitiesID);
- pathComponents.add(usersID);
- pathComponents.add(fileName);
- fileName = StringUtils.join(pathComponents , File.separatorChar);
- f = ServerUtil.getFile(fileName);
- f.getParentFile().mkdirs();
- item.write(f);
- result.put("FILE_PATH", f.getAbsolutePath());
- result.put("ORIGINAL_FILE_PATH", item.getName());
- } else {
- String fieldName = item.getFieldName();
- String value = item.getString();
- if (fieldName.equalsIgnoreCase("jobname")) {
- result.put("jobname", value);
- }
- }
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment