List of usage examples for org.apache.commons.fileupload FileItem getInputStream
InputStream getInputStream() throws IOException;
From source file:com.lecheng.cms.servlet.ConnectorServlet.java
/** * Manage the <code>POST</code> requests (<code>FileUpload</code>).<br /> * /*from www.ja va 2 s . co m*/ * The servlet accepts commands sent in the following format:<br /> * <code>connector?Command=<FileUpload>&Type=<ResourceType>&CurrentFolder=<FolderPath></code> * with the file in the <code>POST</code> body.<br /> * <br> * It stores an uploaded file (renames a file if another exists with the * same name) and then returns the JavaScript callback. */ @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // logger.debug("Entering Connector#doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); String commandStr = request.getParameter("Command"); String typeStr = request.getParameter("Type"); String currentFolderStr = request.getParameter("CurrentFolder"); // logger.debug("Parameter Command: {}", commandStr); // logger.debug("Parameter Type: {}", typeStr); // logger.debug("Parameter CurrentFolder: {}", currentFolderStr); UploadResponse ur; // if this is a QuickUpload request, 'commandStr' and 'currentFolderStr' // are empty if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) { commandStr = "QuickUpload"; currentFolderStr = "/"; } if (!RequestCycleHandler.isEnabledForFileUpload(request)) ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR, null, null, Messages.NOT_AUTHORIZED_FOR_UPLOAD); else if (!CommandHandler.isValidForPost(commandStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_COMMAND); else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_TYPE); else if (!UtilsFile.isValidPath(currentFolderStr)) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); String typePath = UtilsFile.constructServerSidePath(request, resourceType); String typeDirPath = getServletContext().getRealPath(typePath); File typeDir = new File(typeDirPath); UtilsFile.checkDirAndCreate(typeDir); File currentDir = new File(typeDir, currentFolderStr); if (!currentDir.exists()) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { String newFilename = null; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); // FileOperate fo = new FileOperate();//liwei // (+ ) upload.setHeaderEncoding("UTF-8");// liwei try { List<FileItem> items = upload.parseRequest(request); // We upload only one file at the same time FileItem uplFile = items.get(0); String rawName = UtilsFile.sanitizeFileName(uplFile.getName()); String filename = FilenameUtils.getName(rawName); String oldName = FilenameUtils.getName(rawName); String baseName = FilenameUtils.removeExtension(filename); String extension = FilenameUtils.getExtension(filename); // filename = fo.generateRandomFilename() + // extension;//liwei // (+ ) filename = UUID.randomUUID().toString() + "." + extension;// liwei // (UUID) // logger.warn(""+oldName+""+filename+""); if (!ExtensionsHandler.isAllowed(resourceType, extension)) ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); else { // construct an unique file name File pathToSave = new File(currentDir, filename); int counter = 1; while (pathToSave.exists()) { newFilename = baseName.concat("(").concat(String.valueOf(counter)).concat(")") .concat(".").concat(extension); pathToSave = new File(currentDir, newFilename); counter++; } if (Utils.isEmpty(newFilename)) ur = new UploadResponse(UploadResponse.SC_OK, UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()).concat(filename)); else ur = new UploadResponse(UploadResponse.SC_RENAMED, UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()).concat(newFilename), newFilename); // secure image check if (resourceType.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads()) { if (UtilsFile.isImage(uplFile.getInputStream())) uplFile.write(pathToSave); else { uplFile.delete(); ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); } } else uplFile.write(pathToSave); } } catch (Exception e) { ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR); } } } out.print(ur); out.flush(); out.close(); // logger.debug("Exiting Connector#doPost"); }
From source file:com.socialization.util.ConnectorServlet.java
/** * Manage the <code>POST</code> requests (<code>FileUpload</code>).<br /> * // w w w.j av a 2 s. c o m * The servlet accepts commands sent in the following format:<br /> * <code>connector?Command=<FileUpload>&Type=<ResourceType>&CurrentFolder=<FolderPath></code> * with the file in the <code>POST</code> body.<br /> * <br> * It stores an uploaded file (renames a file if another exists with the same name) and then * returns the JavaScript callback. */ @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.debug("Entering Connector#doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); String commandStr = request.getParameter("Command"); String typeStr = request.getParameter("Type"); String currentFolderStr = request.getParameter("CurrentFolder"); logger.debug("Parameter Command: {}", commandStr); logger.debug("Parameter Type: {}", typeStr); logger.debug("Parameter CurrentFolder: {}", currentFolderStr); UploadResponse ur; // if this is a QuickUpload request, 'commandStr' and 'currentFolderStr' // are empty if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) { commandStr = "QuickUpload"; currentFolderStr = "/"; } if (!RequestCycleHandler.isEnabledForFileUpload(request)) ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR, null, null, Messages.NOT_AUTHORIZED_FOR_UPLOAD); else if (!CommandHandler.isValidForPost(commandStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_COMMAND); else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_TYPE); else if (!UtilsFile.isValidPath(currentFolderStr)) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); String typeDirPath = null; if ("File".equals(typeStr)) { // ${application.path}/WEB-INF/userfiles/ typeDirPath = getServletContext().getRealPath("WEB-INF/userfiles/"); } else { String typePath = UtilsFile.constructServerSidePath(request, resourceType); typeDirPath = getServletContext().getRealPath(typePath); } File typeDir = new File(typeDirPath); UtilsFile.checkDirAndCreate(typeDir); File currentDir = new File(typeDir, currentFolderStr); if (!currentDir.exists()) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { String newFilename = null; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); try { List<FileItem> items = upload.parseRequest(request); // We upload only one file at the same time FileItem uplFile = items.get(0); String rawName = UtilsFile.sanitizeFileName(uplFile.getName()); String filename = FilenameUtils.getName(rawName); String baseName = FilenameUtils.removeExtension(filename); String extension = FilenameUtils.getExtension(filename); // ???? if (!ExtensionsHandler.isAllowed(resourceType, extension)) { ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); } // ?? else if (uplFile.getSize() > 1024 * 1024 * 3) { // ? ur = new UploadResponse(204); } // ?, ? else { // construct an unique file name // UUID ???, ? filename = UUID.randomUUID().toString() + "." + extension; filename = makeFileName(currentDir.getPath(), filename); File pathToSave = new File(currentDir, filename); int counter = 1; while (pathToSave.exists()) { newFilename = baseName.concat("(").concat(String.valueOf(counter)).concat(")") .concat(".").concat(extension); pathToSave = new File(currentDir, newFilename); counter++; } if (Utils.isEmpty(newFilename)) ur = new UploadResponse(UploadResponse.SC_OK, UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()).concat(filename)); else ur = new UploadResponse(UploadResponse.SC_RENAMED, UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()).concat(newFilename), newFilename); // secure image check if (resourceType.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads()) { if (UtilsFile.isImage(uplFile.getInputStream())) uplFile.write(pathToSave); else { uplFile.delete(); ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); } } else uplFile.write(pathToSave); } } catch (Exception e) { ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR); } } } out.print(ur); out.flush(); out.close(); logger.debug("Exiting Connector#doPost"); }
From source file:de.innovationgate.wgpublisher.webtml.form.TMLForm.java
private void parseFileItem(FileItem fi) { // create TempFileFolder and put TempFiles in them try {//from w ww. j av a 2 s. c o m String fiName = fi.getName(); // Strip of platform-specifiy file separator, prepending paths (even possible this happens?) if (fiName.indexOf(FILE_SEPARATOR_WIN32) != -1) { fiName = fiName.substring(fiName.lastIndexOf(FILE_SEPARATOR_WIN32) + 1); } // in case the file separator is UNIX-OS separator else { fiName = fiName.substring(fiName.lastIndexOf(FILE_SEPARATOR_UNIX) + 1); } fiName = WGUtils.normalizeUnicode(fiName); // Add to files of process context getProcessContext().addFile(fi.getInputStream(), fiName); // Set form field with name of the file TMLFormField field = (TMLFormField) _fields.get(fi.getFieldName()); if (field == null) { field = new TMLFormField(fi.getFieldName()); _fields.put(fi.getFieldName(), field); } field.addValue(fiName); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.shangde.common.util.FCKConnectorServlet.java
/** * Manage the <code>POST</code> requests (<code>FileUpload</code>).<br /> * /* w w w.j a va 2s .c om*/ * The servlet accepts commands sent in the following format:<br /> * <code>connector?Command=<FileUpload>&Type=<ResourceType>&CurrentFolder=<FolderPath></code> * with the file in the <code>POST</code> body.<br /> * <br> * It stores an uploaded file (renames a file if another exists with the * same name) and then returns the JavaScript callback. */ @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.debug("Entering Connector#doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); String commandStr = request.getParameter("Command"); String typeStr = request.getParameter("Type"); String currentFolderStr = request.getParameter("CurrentFolder"); logger.debug("Parameter Command: {}", commandStr); logger.debug("Parameter Type: {}", typeStr); logger.debug("Parameter CurrentFolder: {}", currentFolderStr); UploadResponse ur; // if this is a QuickUpload request, 'commandStr' and 'currentFolderStr' // are empty if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) { commandStr = "QuickUpload"; currentFolderStr = "/"; } if (!RequestCycleHandler.isEnabledForFileUpload(request)) ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR, null, null, Messages.NOT_AUTHORIZED_FOR_UPLOAD); else if (!CommandHandler.isValidForPost(commandStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_COMMAND); else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_TYPE); else if (!UtilsFile.isValidPath(currentFolderStr)) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); String typePath = null; String typeDirPath = null; String otherFilePath = this.getServletConfig().getServletContext().getRealPath("/"); System.out.println("===========" + otherFilePath); String fckSavePath = (String) request.getSession().getAttribute("fckSavePath"); if (fckSavePath != null && !fckSavePath.trim().equals("")) { typeDirPath = otherFilePath + "/upload/" + fckSavePath + "/"; } else { typeDirPath = otherFilePath + "/upload/public/"; } // if("article".equals((request.getSession().getAttribute("fckSavePath")))){//? feceditor.properties // typeDirPath = otherFilePath + "/static/images/"; // }else if("exam".equals((request.getSession().getAttribute("exam")))){//? feceditor.properties // typeDirPath = otherFilePath + "/static/images/"; // } // else{ // typeDirPath = otherFilePath + "/back/upload/fckimage/"; // } File typeDir = new File(typeDirPath); UtilsFile.checkDirAndCreate(typeDir); File currentDir = new File(typeDir, currentFolderStr); if (!currentDir.exists()) ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; else { String newFilename = null; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(request); // We upload only one file at the same time, FileItem uplFile = items.get(0); String rawName = UtilsFile.sanitizeFileName(uplFile.getName()); String filename = FilenameUtils.getName(rawName); String baseName = FilenameUtils.removeExtension(filename); String extension = FilenameUtils.getExtension(filename); filename = UUID.randomUUID() + "." + extension;// if (!ExtensionsHandler.isAllowed(resourceType, extension)) ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); else { // construct an unique file name File pathToSave = new File(currentDir, filename); int counter = 1; while (pathToSave.exists()) {//???? newFilename = baseName.concat("(").concat(String.valueOf(counter)).concat(")") .concat(".").concat(extension); pathToSave = new File(currentDir, newFilename); counter++; } if (Utils.isEmpty(newFilename)) {//???? if (fckSavePath != null && !fckSavePath.trim().equals("")) { String temp = "http://import.highso.org.cn/upload" + "/" + fckSavePath + "/" + filename; ur = new UploadResponse(UploadResponse.SC_OK, temp); } else { String temp = "http://import.highso.org.cn/upload/public/" + filename; ur = new UploadResponse(UploadResponse.SC_OK, temp); } } else {//???? if (fckSavePath != null && !fckSavePath.trim().equals("")) { String temp = "http://import.highso.org.cn/upload" + "/" + fckSavePath + "/" + filename; ur = new UploadResponse(UploadResponse.SC_RENAMED, temp); } else { String temp = "http://import.highso.org.cn/upload/public/" + filename; ur = new UploadResponse(UploadResponse.SC_RENAMED, temp); } } // secure image check if (resourceType.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads()) { if (UtilsFile.isImage(uplFile.getInputStream())) uplFile.write(pathToSave); else { uplFile.delete(); ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); } } else uplFile.write(pathToSave); } } catch (Exception e) { logger.error(e.getMessage()); ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR); } } } out.print(ur); out.flush(); out.close(); logger.debug("Exiting Connector#doPost"); }
From source file:importer.handler.post.ImporterPostHandler.java
/** * Parse the import params from the request * @param request the http request//from ww w . j a va 2s . c o m */ void parseImportParams(HttpServletRequest request) throws AeseException { try { FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List items = upload.parseRequest(request); for (int i = 0; i < items.size(); i++) { FileItem item = (FileItem) items.get(i); if (item.isFormField()) { String fieldName = item.getFieldName(); if (fieldName != null) { String contents = item.getString(); if (fieldName.equals(Params.DOCID)) { if (contents.startsWith("/")) { contents = contents.substring(1); int pos = contents.indexOf("/"); if (pos != -1) { database = contents.substring(0, pos); contents = contents.substring(pos + 1); } } docid = contents; } else if (fieldName.startsWith(Params.SHORT_VERSION)) nameMap.put(fieldName.substring(Params.SHORT_VERSION.length()), item.getString()); else if (fieldName.equals(Params.LC_STYLE) || fieldName.equals(Params.STYLE) || fieldName.equals(Params.CORFORM)) { jsonKeys.put(fieldName.toLowerCase(), contents); style = contents; } else if (fieldName.equals(Params.DEMO)) { if (contents != null && contents.equals("brillig")) demo = false; else demo = true; } else if (fieldName.equals(Params.TITLE)) title = contents; else if (fieldName.equals(Params.FILTER)) filterName = contents.toLowerCase(); else if (fieldName.equals(Params.SIMILARITY)) similarityTest = contents != null && contents.equals("1"); else if (fieldName.equals(Params.SPLITTER)) splitterName = contents; else if (fieldName.equals(Params.STRIPPER)) stripperName = contents; else if (fieldName.equals(Params.TEXT)) textName = contents.toLowerCase(); else if (fieldName.equals(Params.DICT)) dict = contents; else if (fieldName.equals(Params.ENCODING)) encoding = contents; else if (fieldName.equals(Params.HH_EXCEPTIONS)) hhExceptions = contents; else jsonKeys.put(fieldName, contents); } } else if (item.getName().length() > 0) { try { // assuming that the contents are text //item.getName retrieves the ORIGINAL file name String type = item.getContentType(); if (type != null && type.startsWith("image/")) { InputStream is = item.getInputStream(); ByteHolder bh = new ByteHolder(); while (is.available() > 0) { byte[] b = new byte[is.available()]; is.read(b); bh.append(b); } ImageFile iFile = new ImageFile(item.getName(), item.getContentType(), bh.getData()); images.add(iFile); } else { byte[] rawData = item.get(); guessEncoding(rawData); //System.out.println(encoding); File f = new File(item.getName(), new String(rawData, encoding)); files.add(f); } } catch (Exception e) { throw new AeseException(e); } } } if (style == null) style = DEFAULT_STYLE; } catch (Exception e) { throw new AeseException(e); } }
From source file:calliope.handler.post.AeseImportHandler.java
/** * Parse the import params from the request * @param request the http request/* ww w .j ava 2 s .c om*/ */ void parseImportParams(HttpServletRequest request) throws AeseException { try { FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List items = upload.parseRequest(request); for (int i = 0; i < items.size(); i++) { FileItem item = (FileItem) items.get(i); if (item.isFormField()) { String fieldName = item.getFieldName(); if (fieldName != null) { String contents = item.getString(); if (fieldName.equals(Params.DOCID)) { if (contents.startsWith("/")) { contents = contents.substring(1); int pos = contents.indexOf("/"); if (pos != -1) { database = contents.substring(0, pos); contents = contents.substring(pos + 1); } } docID = new DocID(contents); } else if (fieldName.startsWith(Params.SHORT_VERSION)) nameMap.put(fieldName.substring(Params.SHORT_VERSION.length()), item.getString()); else if (fieldName.equals(Params.LC_STYLE) || fieldName.equals(Params.STYLE)) { jsonKeys.put(fieldName.toLowerCase(), contents); style = contents; } else if (fieldName.equals(Params.DEMO)) { if (contents != null && contents.equals("brillig")) demo = false; else demo = true; } else if (fieldName.equals(Params.TITLE)) title = contents; else if (fieldName.equals(Params.FILTER)) filterName = contents.toLowerCase(); else if (fieldName.equals(Params.SIMILARITY)) similarityTest = contents != null && contents.equals("1"); else if (fieldName.equals(Params.SPLITTER)) splitterName = contents; else if (fieldName.equals(Params.STRIPPER)) stripperName = contents; else if (fieldName.equals(Params.TEXT)) textName = contents.toLowerCase(); else if (fieldName.equals(Params.XSLT)) xslt = getConfig(Config.xslt, contents); else if (fieldName.equals(Params.DICT)) dict = contents; else if (fieldName.equals(Params.ENCODING)) encoding = contents; else if (fieldName.equals(Params.HH_EXCEPTIONS)) hhExceptions = contents; else jsonKeys.put(fieldName, contents); } } else if (item.getName().length() > 0) { try { // assuming that the contents are text //item.getName retrieves the ORIGINAL file name String type = item.getContentType(); if (type != null && type.startsWith("image/")) { InputStream is = item.getInputStream(); ByteHolder bh = new ByteHolder(); while (is.available() > 0) { byte[] b = new byte[is.available()]; is.read(b); bh.append(b); } ImageFile iFile = new ImageFile(item.getName(), item.getContentType(), bh.getData()); images.add(iFile); } else { byte[] rawData = item.get(); guessEncoding(rawData); //System.out.println(encoding); File f = new File(item.getName(), new String(rawData, encoding)); files.add(f); } } catch (Exception e) { throw new AeseException(e); } } } if (style == null) style = DEFAULT_STYLE; } catch (Exception e) { throw new AeseException(e); } // System.out.println("Import params received:"); // System.out.println("docID="+docID.get()); // System.out.println("style="+style); // System.out.println("filterName="+filterName); // System.out.println("database="+database); // System.out.println("splitterName="+splitterName); // System.out.println("stripperName="+stripperName); // System.out.println("encoding="+encoding); // System.out.println("hhExceptions="+hhExceptions); // System.out.println("similarityTest="+similarityTest); // System.out.println("dict="+dict); // System.out.println("xslt="+xslt); // System.out.println("demo="+demo); // System.out.println("textName="+textName); }
From source file:com.znsx.cms.web.controller.DeviceController.java
@InterfaceDescription(logon = false, method = "Batch_Create_Camera", cmd = "2065") @RequestMapping("/batch_create_camera.json") public void batchCreateCamera(HttpServletRequest request, HttpServletResponse response) throws Exception { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // ?Filedata? boolean uploadFlag = false; // Excel?? InputStream in = null;/*w ww .jav a 2s .c om*/ // ID String organId = ""; // ? DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); Iterator iter = items.iterator(); // ?License License license = licenseManager.getLicense(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); // ??sessionId if ("sessionId".equals(fieldName)) { String sessionId = item.getString(); if (StringUtils.isBlank(sessionId)) { throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [sessionId]"); } // ?sessionId userManager.checkSession(sessionId); } if ("organId".equals(fieldName)) { organId = item.getString(); if (StringUtils.isBlank(organId)) { throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [organId]"); } } if ("Filedata".equals(fieldName)) { uploadFlag = true; in = item.getInputStream(); } } if (!uploadFlag) { throw new BusinessException(ErrorCode.MISSING_PARAMETER_FILEDATA, "Parameter [Filedata] not found !"); } Organ organ = organManger.getOrganById(organId); Workbook wb = deviceManager.checkoutIo(in); List<Dvr> dvrs = deviceManager.readDvrWb(wb, organ); List<Camera> cameras = deviceManager.readCameraWB(wb, organ, license, dvrs); // ?DvrID? // String[] dvrIds = deviceManager.batchDvrId("Dvr", dvrs.size()); // String[] cameraIds = null; // if (cameras.size() > 0) { // // ?CameraID? // cameraIds = deviceManager.batchGenerateId("Camera", // cameras.size()); // } deviceManager.batchInsertDvr(dvrs); // if (cameraIds != null) { deviceManager.batchInsertCamera(cameras); // } } else { throw new BusinessException(ErrorCode.NOT_MULTIPART_REQUEST, "Not multipart request !"); } BaseDTO dto = new BaseDTO(); dto.setCmd("2065"); dto.setMethod("Batch_Create_Camera"); writePage(response, dto); }
From source file:com.znsx.cms.web.controller.DeviceController.java
@InterfaceDescription(logon = false, method = "Batch_Create_Data_Device", cmd = "2067") @RequestMapping("/batch_create_data_device.json") public void batchCreateDataDevice(HttpServletRequest request, HttpServletResponse response) throws Exception { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // ?Filedata? boolean uploadFlag = false; // Excel?? InputStream in = null;//from ww w. ja v a 2s . c o m // ID String organId = ""; // String type = ""; // ? DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); Iterator iter = items.iterator(); // ?License License license = licenseManager.getLicense(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); // ??sessionId if ("sessionId".equals(fieldName)) { String sessionId = item.getString(); if (StringUtils.isBlank(sessionId)) { throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [sessionId]"); } // ?sessionId userManager.checkSession(sessionId); } if ("type".equals(fieldName)) { type = item.getString(); if (StringUtils.isBlank(type)) { throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [type]"); } } if ("Filedata".equals(fieldName)) { uploadFlag = true; in = item.getInputStream(); } } if (!uploadFlag) { throw new BusinessException(ErrorCode.MISSING_PARAMETER_FILEDATA, "Parameter [Filedata] not found !"); } // ?? Workbook wb = deviceManager.checkoutIo(in); // ?? if (type.equals(TypeDefinition.DEVICE_TYPE_FD + "")) { List<FireDetector> fds = tmDeviceManager.readFireDetectorWb(wb, license); tmDeviceManager.batchInsertFd(fds); } // else if (type.equals(TypeDefinition.DEVICE_TYPE_LIGHT + "")) { List<ControlDeviceLight> cdls = tmDeviceManager.readControlDeviceLightWb(wb, license); tmDeviceManager.batchInsertLight(cdls); } // else if (type.equals(TypeDefinition.DEVICE_TYPE_WP + "")) { List<ControlDeviceWp> cdws = tmDeviceManager.readControlDeviceWpWb(wb, license); tmDeviceManager.batchInsertWp(cdws); } // else if (type.equals(TypeDefinition.DEVICE_TYPE_PB + "")) { List<PushButton> pbs = tmDeviceManager.readPushButtonWb(wb, license); tmDeviceManager.batchInsertPb(pbs); } // ? else if (type.equals(TypeDefinition.DEVICE_TYPE_LIL + "")) { List<ControlDeviceLil> cdlis = tmDeviceManager.readControlDeviceLilWb(wb, license); tmDeviceManager.batchInsertLil(cdlis); } // else if (type.equals(TypeDefinition.DEVICE_TYPE_FAN + "")) { List<ControlDeviceFan> cdfs = tmDeviceManager.readControlDeviceFanWb(wb, license); tmDeviceManager.batchInsertFan(cdfs); } else { throw new BusinessException(ErrorCode.PARAMETER_INVALID, "Parameter type[" + type + "] invalid !"); } } else { throw new BusinessException(ErrorCode.NOT_MULTIPART_REQUEST, "Not multipart request !"); } BaseDTO dto = new BaseDTO(); dto.setCmd("2065"); dto.setMethod("Batch_Create_Camera"); writePage(response, dto); }
From source file:net.i2cat.csade.life2.backoffice.servlet.UserManagementService.java
/** * Funcin que se ejecuta cuando el servlet recibe los datos *///from ww w . ja v a2 s .c om protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ChangablePropertiesManager cpm = new ChangablePropertiesManager(this.getServletContext()); String operation = request.getParameter("operation"); PlatformUserManager pum = new PlatformUserManager(); String data = ""; if (operation != null && !"".equals(operation)) { if (operation.equals("savePicturePreference")) { String photo_hor = request.getParameter("photo_hor"); cpm.saveProperty("photo_hor", photo_hor); data = "{ \"message\": \"preferences saved.\" }"; } if (operation.equals("getPicturePreference")) { String photo_hor = cpm.getProperty("photo_hor"); data = "{ \"photo_hor\": \"" + photo_hor + "\"}"; } if (operation.equals("getPlatformUser")) { String login = request.getParameter("login"); try { data = pum.getUser(login).toJSON().toString(); } catch (RemoteException re) { data = "{ \"message\": \"Could not not retrieve user with login=" + login + " Reason:" + re.getMessage() + ".\" }"; } catch (ServiceException se) { data = "{ \"message\": \"Could not not retrieve user with login=" + login + " Reason:" + se.getMessage() + ".\" }"; } } if (operation.equals("delPlatformUser")) { String login = request.getParameter("login"); try { if (!request.isUserInRole("admin")) throw new ServiceException("You are not allowed to delete users"); if (login != null && login.equals(request.getUserPrincipal().getName())) throw new ServiceException("You cannot delete your own user"); pum.deleteUser(login); data = "{ \"message\": \"User with login " + login + " deleted.\" }"; } catch (RemoteException re) { data = "{ \"message\": \"Could not not delete user with login=" + login + " Reason:" + re.getMessage() + ".\" }"; } catch (ServiceException se) { data = "{ \"message\": \"Could not not delete user with login=" + login + " Reason:" + se.getMessage() + ".\" }"; } } if (operation.equals("savePlatformUser")) { FileItem uploadedFile = null; PlatformUser user = null; int res = 0; byte[] foto = null; try { if (!request.isUserInRole("admin")) throw new ServiceException("You are not allowed to upadte users"); user = new PlatformUser(); user.setNew(false); ServletFileUpload sfu = new ServletFileUpload(new DiskFileItemFactory()); sfu.setFileSizeMax(329000); sfu.setHeaderEncoding("UTF-8"); @SuppressWarnings("unchecked") List<FileItem> items = sfu.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { if (item.getFieldName().equals("login")) user.setLogin(item.getString()); if (item.getFieldName().equals("username")) user.setLogin(item.getString()); if (item.getFieldName().equals("password")) { user.setPass(item.getString()); } if (item.getFieldName().equals("idUser")) { if (item.getString() == null || "".equals(item.getString())) user.setNew(true); } if (item.getFieldName().equals("name")) { byte[] fnb = item.get(); String text = PasswordGenerator.utf8Decoder(fnb); user.setName(text); } if (item.getFieldName().equals("email")) { String mail = item.getString(); if (MailUtils.isValidEmail(mail)) user.setEmail(mail); else throw new ServiceException("El email del usuario es incorrecto"); } if (item.getFieldName().equals("telephonenumber")) user.setTelephonenumber(item.getString()); if (item.getFieldName().equals("role")) user.setRole(Integer.parseInt(item.getString())); if (item.getFieldName().equals("language")) user.setLanguage(item.getString()); if (item.getFieldName().equals("notification_level")) user.setNotification_level(item.getString()); if (item.getFieldName().equals("promoter_id")) user.setPromoter_id(item.getString()); if (item.getFieldName().equals("user_average_mark")) user.setUser_average_mark(item.getString()); if (item.getFieldName().equals("user_votes")) user.setUser_votes(item.getString()); if (item.getFieldName().equals("latitude")) user.setHome_area_lat(item.getString()); if (item.getFieldName().equals("longitude")) user.setHome_area_lon(item.getString()); if (item.getFieldName().equals("enabled")) user.setEnabled(item.getString().equals("0") ? 0 : 1); } else { uploadedFile = item; String inputExtension = FilenameUtils .getExtension(uploadedFile.getName().toLowerCase()); if ("jpg".equals(inputExtension) || "gif".equals(inputExtension) || "png".equals(inputExtension)) { InputStream filecontent = item.getInputStream(); foto = new byte[(int) uploadedFile.getSize()]; filecontent.read(foto, 0, (int) uploadedFile.getSize()); } //else // throw new FileUploadException("Extension not supported. Only jpg,gif or png files are allowed"); } } res = pum.saveUser(user); if (foto != null) { //String v=cpm.getProperty("photo_hor"); //byte[] resizedPhoto=ImageUtil.resizeImageAsJPG(foto, (v==null || "".equals(v)) ?200:Integer.parseInt(v)); pum.uploadFoto(user.getLogin(), foto); } data = "{ \"message\": \"User with login " + user.getLogin() + " (id=" + res + ") saved.\" }"; } catch (RemoteException exc) { data = "{ \"message\": \"Could not not save user with login=" + user.getLogin() + " Reason:" + exc.getMessage() + ".\" }"; } catch (ServiceException exc) { data = "{ \"message\": \"Could not not save user with login=" + user.getLogin() + " Reason:" + exc.getMessage() + ".\" }"; } catch (FileUploadException exc) { data = "{ \"message\": \"User with login " + user.getLogin() + " (id=" + res + ") saved, but there was a problem uploading picture:" + exc.getMessage() + "\" }"; } } if (operation.equals("listPlatformUsers")) { JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request); try { JSONObject jsonResponse = pum.getPlatformUsersJSON(param); data = jsonResponse.toString(); } catch (RemoteException re) { data = "{ \"message\": \"Could not not retrieve platform user listing. Reason:" + re.getMessage() + ".\" }"; } catch (ServiceException se) { data = "{ \"message\": \"Could not not retrieve platform user listing. Reason:" + se.getMessage() + ".\" }"; } } } response.setContentType("application/json;charset=UTF-8"); //response.setContentType("application/json"); response.getWriter().print(data); response.getWriter().close(); }
From source file:com.gtwm.pb.model.manageData.DataManagement.java
public int importCSV(HttpServletRequest request, TableInfo table, boolean updateExistingRecords, BaseField recordIdentifierField, boolean generateRowIds, char separator, char quotechar, int numHeaderLines, boolean useRelationDisplayValues, boolean importSequenceValues, boolean requireExactRelationMatches, boolean trim, boolean merge, List<FileItem> multipartItems, String csvContent) throws SQLException, InputRecordException, IOException, CantDoThatException, ObjectNotFoundException, DisallowedException, CodingErrorException { if (!FileUpload.isMultipartContent(new ServletRequestContext(request))) { if (csvContent == null) { throw new CantDoThatException( "To import CSV content, a file must be uploaded (form posted as multi-part) or csv_content specified"); }/*from w ww . j ava 2 s. c o m*/ } int numImportedRecords = 0; // get field set to import into. LinkedHashSet to ensure order is // retained so the right values are imported into the right fields LinkedHashSet<BaseField> fields = new LinkedHashSet<BaseField>(table.getFields()); // if row IDs aren't included in the data to import, remove ID from the // field set BaseField primaryKey = table.getPrimaryKey(); if (recordIdentifierField == null) { recordIdentifierField = primaryKey; } if (generateRowIds || (updateExistingRecords && !recordIdentifierField.equals(primaryKey))) { fields.remove(primaryKey); } Map<RelationField, Map<String, String>> relationLookups = new HashMap<RelationField, Map<String, String>>(); // Remove fields which shouldn't be modified during the import // For serial fields, if we need to set serial values explicitly, this // will have to be dealt with later for (BaseField field : table.getFields()) { if (field instanceof SequenceField && (!field.equals(primaryKey)) && (!importSequenceValues)) { fields.remove(field); } else if (field.getHidden()) { if (field.getFieldName().equals(HiddenFields.VIEW_COUNT.getFieldName()) || field.getFieldName().equals(HiddenFields.COMMENTS_FEED.getFieldName())) { fields.remove(field); } else if (updateExistingRecords) { if (field.getFieldName().equals(HiddenFields.DATE_CREATED.getFieldName()) || field.getFieldName().equals(HiddenFields.CREATED_BY.getFieldName())) { fields.remove(field); } } } else if (!field.getFieldCategory().savesData()) { fields.remove(field); } // Also, if importing relations by display value, look up // display/internal value mappings if (useRelationDisplayValues && field instanceof RelationField) { Map<String, String> displayToInternalValue = ((RelationFieldDefn) field).getItems(true, false); relationLookups.put((RelationField) field, displayToInternalValue); } } // Prepare SQL String insertSQLCode = null; String updateSQLCode = null; String logCreationSQLCode = null; // If updating, we'll need a record ID value. Depending on what the // identifier field is, this could be one of a couple of different types String recordIdentifierString = null; Integer recordIdentifierInteger = null; int recordIdentifierFieldNum = 0; DatabaseFieldType identifierFieldDbType = null; if (updateExistingRecords) { identifierFieldDbType = recordIdentifierField.getDbType(); if (!identifierFieldDbType.equals(DatabaseFieldType.VARCHAR) && !identifierFieldDbType.equals(DatabaseFieldType.INTEGER) && !identifierFieldDbType.equals(DatabaseFieldType.SERIAL)) { throw new CantDoThatException("The record identifier field has to be text or a whole number, " + recordIdentifierField + " is a " + identifierFieldDbType); } updateSQLCode = "UPDATE " + table.getInternalTableName() + " SET "; int fieldNum = 0; for (BaseField field : fields) { fieldNum += 1; if (merge) { // Update database only if there's a non-null value from the // spreadsheet updateSQLCode += field.getInternalFieldName() + " = COALESCE(?," + field.getInternalFieldName() + "), "; } else { updateSQLCode += field.getInternalFieldName() + " = ?, "; } if (field.equals(recordIdentifierField)) { recordIdentifierFieldNum = fieldNum; } } if (recordIdentifierFieldNum == 0) { throw new CantDoThatException("Can't find the field specified as record identifier (" + recordIdentifierField + ") in the list of table fields " + fields + " in table " + table); } updateSQLCode = updateSQLCode.substring(0, updateSQLCode.length() - 2); updateSQLCode += " WHERE " + recordIdentifierField.getInternalFieldName() + "=?"; logCreationSQLCode = "UPDATE " + table.getInternalTableName() + " SET " + table.getField(HiddenFields.DATE_CREATED.getFieldName()).getInternalFieldName() + "=?, " + table.getField(HiddenFields.CREATED_BY.getFieldName()).getInternalFieldName() + "=? WHERE " + primaryKey.getInternalFieldName() + "=?"; } insertSQLCode = "INSERT INTO " + table.getInternalTableName() + "("; String placeholders = ""; for (BaseField field : fields) { insertSQLCode += field.getInternalFieldName() + ", "; placeholders += "?, "; } placeholders = placeholders.substring(0, placeholders.length() - 2); insertSQLCode = insertSQLCode.substring(0, insertSQLCode.length() - 2) + ") VALUES (" + placeholders + ")"; // Find content to import Reader inputStreamReader = null; if (csvContent != null) { inputStreamReader = new StringReader(csvContent); } else { for (FileItem item : multipartItems) { // if item is a file if (!item.isFormField()) { if (item.getName().toLowerCase().endsWith(".xls")) { throw new CantDoThatException( "You need to upload as a CSV to import, Excel files can't be imported directly"); } inputStreamReader = new InputStreamReader(item.getInputStream()); break; } } } if (inputStreamReader == null) { throw new CantDoThatException("No file uploaded"); } CSVReader csvReader = new CSVReader(inputStreamReader, separator, quotechar, numHeaderLines); // returns a list of String arrays List<String[]> csvLines = (List<String[]>) csvReader.readAll(); // do db inserts Connection conn = null; PreparedStatement statement = null; // backupInsertStatement is for when an update returns 0 rows affected, // i.e. there's no matching row. In this case, do an insert PreparedStatement backupInsertStatement = null; PreparedStatement logCreationStatement = null; // These two variables used in exception handling int importLine = 0; BaseField fieldImported = null; Timestamp importTime = new Timestamp(System.currentTimeMillis()); AppUserInfo loggedInUser = authManager.getUserByUserName(request, request.getRemoteUser()); String fullname = loggedInUser.getForename() + " " + loggedInUser.getSurname() + " (" + loggedInUser.getUserName() + ")"; try { conn = this.dataSource.getConnection(); conn.setAutoCommit(false); if (updateExistingRecords) { statement = conn.prepareStatement(updateSQLCode); backupInsertStatement = conn.prepareStatement(insertSQLCode); logCreationStatement = conn.prepareStatement(logCreationSQLCode); } else { statement = conn.prepareStatement(insertSQLCode); } CSVLINE: for (String[] csvLineArray : csvLines) { // convert to an object rather than a primitive array - // easier to work with List<String> lineValues = Arrays.asList(csvLineArray); importLine++; // skip blank lines if (lineValues.size() == 1) { if (lineValues.get(0).length() == 0) { continue CSVLINE; } } int fieldNum = 0; for (BaseField field : fields) { fieldImported = field; fieldNum++; if (field.getHidden()) { String fieldName = field.getFieldName(); if (fieldName.equals(HiddenFields.LOCKED.getFieldName())) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else if (fieldName.equals(HiddenFields.DATE_CREATED.getFieldName()) || fieldName.equals(HiddenFields.LAST_MODIFIED.getFieldName())) { statement.setTimestamp(fieldNum, importTime); if (updateExistingRecords) { backupInsertStatement.setTimestamp(fieldNum, importTime); } } else if (fieldName.equals(HiddenFields.CREATED_BY.getFieldName()) || fieldName.equals(HiddenFields.MODIFIED_BY.getFieldName())) { statement.setString(fieldNum, fullname); if (updateExistingRecords) { backupInsertStatement.setString(fieldNum, fullname); } } } else if (fieldNum > lineValues.size()) { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } else { String lineValue = lineValues.get(fieldNum - 1); if (lineValue != null) { if (trim) { lineValue = lineValue.trim(); } if (lineValue.equals("")) { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } else { if ((field instanceof FileField) && (generateRowIds)) { throw new CantDoThatException( "Cannot generate row ids when importing file names. See line " + importLine + ", field '" + field.getFieldName() + "' with value '" + lineValue + "'"); } switch (field.getDbType()) { case VARCHAR: statement.setString(fieldNum, lineValue); if (updateExistingRecords) { backupInsertStatement.setString(fieldNum, lineValue); if (field.equals(recordIdentifierField)) { recordIdentifierString = lineValue; } } break; case TIMESTAMP: // deal with month and year // resolution dates exported if (lineValue.matches("^[a-zA-Z]{3}\\s\\d{2,4}$")) { lineValue = "01 " + lineValue; } else if (lineValue.matches("^\\d{2,4}")) { lineValue = "01 Jan " + lineValue; } try { Calendar calValue = CalendarParser.parse(lineValue, CalendarParser.DD_MM_YY); statement.setTimestamp(fieldNum, new Timestamp(calValue.getTimeInMillis())); if (updateExistingRecords) { backupInsertStatement.setTimestamp(fieldNum, new Timestamp(calValue.getTimeInMillis())); } } catch (CalendarParserException cpex) { throw new InputRecordException("Error importing line " + importLine + ", field " + field + ": " + cpex.getMessage(), field, cpex); } break; case FLOAT: lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); statement.setDouble(fieldNum, Double.valueOf(lineValue)); if (updateExistingRecords) { backupInsertStatement.setDouble(fieldNum, Double.valueOf(lineValue)); } break; case INTEGER: if ((field instanceof RelationField) && (useRelationDisplayValues)) { // find key value for display value RelationField relationField = (RelationField) field; Map<String, String> valueKeyMap = relationLookups.get(relationField); String internalValueString = valueKeyMap.get(lineValue); if (internalValueString == null) { if (!requireExactRelationMatches) { // A very basic fuzzy matching // algorithm String potentialDisplayValue = null; String lineValueLowerCase = lineValue.toLowerCase(); FUZZYMATCH: for (Map.Entry<String, String> entry : valueKeyMap .entrySet()) { potentialDisplayValue = entry.getKey(); if (potentialDisplayValue.toLowerCase() .contains(lineValueLowerCase)) { internalValueString = entry.getValue(); break FUZZYMATCH; } } } if (internalValueString == null) { throw new CantDoThatException("Error importing line " + importLine + ", field " + relationField + ": Can't find a related '" + relationField.getRelatedTable() + "' for " + relationField.getDisplayField() + " '" + lineValue + "'. "); } } int keyValue = Integer.valueOf(internalValueString); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } } else { lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); int keyValue = Integer.valueOf(lineValue); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } } break; case SERIAL: lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); int keyValue = Integer.valueOf(lineValue); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } break; case BOOLEAN: boolean filterValueIsTrue = Helpers.valueRepresentsBooleanTrue(lineValue); statement.setBoolean(fieldNum, filterValueIsTrue); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, filterValueIsTrue); } break; } } } else { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } } } if (updateExistingRecords) { // for potential error messages String recordIdentifierDescription = null; if (identifierFieldDbType.equals(DatabaseFieldType.INTEGER) || identifierFieldDbType.equals(DatabaseFieldType.SERIAL)) { if (recordIdentifierInteger == null) { throw new InputRecordException( "Can't find a record identifier value at line " + importLine, recordIdentifierField); } recordIdentifierDescription = recordIdentifierField.getFieldName() + " = " + recordIdentifierInteger; // Set the 'WHERE recordIdentifier = ?' clause statement.setInt(fields.size() + 1, recordIdentifierInteger); } else { if (recordIdentifierString == null) { throw new InputRecordException( "Can't find a record identifier value at line " + importLine, recordIdentifierField); } recordIdentifierDescription = recordIdentifierField.getFieldName() + " = '" + recordIdentifierString + "'"; // Set the 'WHERE recordIdentifier = ?' clause statement.setString(fields.size() + 1, recordIdentifierString); } int rowsAffected = statement.executeUpdate(); if (rowsAffected == 0) { // If can't find a match to update, insert a record // instead backupInsertStatement.executeUpdate(); // NB Postgres specific code to find Row ID of newly // inserted record, not cross-db compatible String newRowIdSQLCode = "SELECT currval('" + table.getInternalTableName() + "_" + primaryKey.getInternalFieldName() + "_seq')"; PreparedStatement newRowIdStatement = conn.prepareStatement(newRowIdSQLCode); ResultSet newRowIdResults = newRowIdStatement.executeQuery(); if (newRowIdResults.next()) { int newRowId = newRowIdResults.getInt(1); // Add creation metadata to the new row logCreationStatement.setTimestamp(1, importTime); logCreationStatement.setString(2, fullname); logCreationStatement.setInt(3, newRowId); int creationLogRowsAffected = logCreationStatement.executeUpdate(); if (creationLogRowsAffected == 0) { throw new SQLException( "Unable to update creation metadata of newly inserted record, using query " + logCreationStatement); } } else { newRowIdResults.close(); newRowIdStatement.close(); throw new SQLException("Row ID not found for the newly inserted record. '" + newRowIdStatement + "' didn't work"); } newRowIdResults.close(); newRowIdStatement.close(); } else if (rowsAffected > 1) { throw new InputRecordException("Error importing line " + importLine + ". The record identifier field " + recordIdentifierDescription + " should match only 1 record in the database but it actually matches " + rowsAffected, recordIdentifierField); } // reset to null for the next line recordIdentifierString = null; recordIdentifierInteger = null; } else { statement.executeUpdate(); } numImportedRecords += 1; } statement.close(); if (backupInsertStatement != null) { backupInsertStatement.close(); } if (logCreationStatement != null) { logCreationStatement.close(); } // reset the primary key ID sequence so new records can be added resetSequence((SequenceField) primaryKey, conn); // and any other sequence fields if (importSequenceValues) { for (BaseField field : table.getFields()) { if ((!field.equals(primaryKey)) && field instanceof SequenceField) { resetSequence((SequenceField) field, conn); } } } // ANALYZE the table after import if (numImportedRecords > 1000) { Statement analyzeStatement = conn.createStatement(); analyzeStatement.execute("ANALYZE " + table.getInternalTableName()); analyzeStatement.close(); } conn.commit(); } catch (SQLException sqlex) { String databaseErrorMessage = Helpers.replaceInternalNames(sqlex.getMessage(), table.getDefaultReport()); logger.warn("Import failed, statement is " + statement); logger.warn("Backup insert statement is " + backupInsertStatement); String errorMessage = "Error importing CSV line " + importLine; if (!fieldImported.getHidden()) { errorMessage += ", field '" + fieldImported + "'"; } errorMessage += ": " + databaseErrorMessage; throw new InputRecordException(errorMessage, fieldImported, sqlex); } catch (NumberFormatException nfex) { String causeMessage = nfex.getMessage(); causeMessage = causeMessage.replaceAll("For input string", "value"); String errorMessage = "Error parsing number when importing CSV line " + importLine; if (!fieldImported.getHidden()) { errorMessage += ", field '" + fieldImported + "'"; } errorMessage += ": " + causeMessage; throw new InputRecordException(errorMessage, fieldImported, nfex); } finally { if (conn != null) { conn.close(); } } this.logLastDataChangeTime(request); logLastTableDataChangeTime(table); UsageLogger usageLogger = new UsageLogger(this.dataSource); String logMessage = "" + numImportedRecords; if (updateExistingRecords) { logMessage += " records imported"; } else { logMessage += " new records imported"; } if (csvContent != null) { logMessage += " from file"; } usageLogger.logDataChange(loggedInUser, table, null, AppAction.CSV_IMPORT, -1, logMessage); UsageLogger.startLoggingThread(usageLogger); return numImportedRecords; }