/** * Gets userdata from row. * * @param aRow {@link HSSFRow} to get userdata from. * @param aColumns Array of {@link Integer} that specify the userdata columns. See {@code * getUserdataColumnsFromSheet()}. * @param aHouse {@link String} of users house name. * @return {@link User} if data found, {@code null} otherwise. */ private User getUser(HSSFRow aRow, int[] aColumns, String aHouse) { if (aRow != null && aColumns.length > 3) { // convert cell types to string convertCellTypes(aRow, Arrays.copyOf(aColumns, 3), HSSFCell.CELL_TYPE_STRING); // convertCellTypes( aRow, new int[]{aColumns[3]}, HSSFCell.CELL_TYPE_NUMERIC ); // get cells HSSFCell vCellName = aRow.getCell(aColumns[0]); HSSFCell vCellFloor = aRow.getCell(aColumns[1]); HSSFCell vCellRoom = aRow.getCell(aColumns[2]); HSSFCell vCellBirthdate = aRow.getCell(aColumns[3]); // check cells and create user if (vCellName != null && vCellFloor != null && vCellRoom != null && vCellBirthdate != null) { try { String vName = vCellName.getStringCellValue().trim(); String vFloor = vCellFloor.getStringCellValue().trim(); String vRoom = vCellRoom.getStringCellValue().trim(); Date vBirthdate = (new SimpleDateFormat("dd.MM.yyyy")).parse("00.00.0000"); if (vCellBirthdate.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { vBirthdate = vCellBirthdate.getDateCellValue(); } if (vName.length() > 0) { User vUser = new User(-1); vUser.setName(vName); vUser.setHouse(aHouse); vUser.setFloor(vFloor); vUser.setRoom(vRoom); if (vBirthdate != null) { SimpleDateFormat vDateFormat = new SimpleDateFormat("dd.MM.yyyy"); String vDateString = vDateFormat.format(vBirthdate); vUser.setBirthdate(vDateString); } return vUser; } } catch (Exception e) { e.printStackTrace(); logger.error("Error importing data from row " + aRow.getRowNum()); System.exit(0); } } } return null; }
/** * Imports user data from csv file. * * @param aCsvFile {@link File} csv file to import. * @return {@link List} of {@link User} from csv file. */ private List<User> getUsers(File aCsvFile) { List<User> vUsers = new ArrayList<User>(); if (aCsvFile != null & aCsvFile.exists() && aCsvFile.isFile()) { try { BufferedReader vReader = new BufferedReader(new InputStreamReader(new FileInputStream(aCsvFile), "UTF-16")); // get header String vLine; if ((vLine = vReader.readLine()) != null) { int[] vColumns = getUserdataColumnsFromSheet(vLine.toLowerCase().split("\t")); logger.debug("Extracted file columns: "); for (int i : vColumns) logger.debug("\t" + i); if (validateColumnsData(vColumns)) { // read user data while ((vLine = vReader.readLine()) != null) { String[] vUserData = vLine.split("\t"); if (getMaxColumnNum(vColumns) < vUserData.length) { String vPreName = vUserData[vColumns[0]].trim(); String vLastName = vUserData[vColumns[1]].trim(); String vHouse = vUserData[vColumns[2]].replace("Haus", "").trim(); String vRoom = vUserData[vColumns[3]].trim(); String vBirthdate = vUserData[vColumns[4]].trim(); if (vLastName.length() > 0) { User vUser = new User(-1); vUser.setName(vLastName + ", " + vPreName); vUser.setHouse(vHouse); vUser.setRoom(vRoom); vUser.setBirthdate(vBirthdate); vUsers.add(vUser); } } else { logger.warn("Line doesn't contain number of required information:\n\t" + vLine); } } } } else { logger.warn("Cannot read header from file " + aCsvFile.getPath()); } vReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error("Cannot find file for import: " + aCsvFile.getPath()); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } } return vUsers; }