Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package it.unitn.elisco.utils; import it.unitn.elisco.bean.hibernate.Person; import it.unitn.elisco.bean.hibernate.Question; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.mindrot.jbcrypt.BCrypt; /** * * @author Andrea Marcolin */ public class Utilities { // Password managing functions (BCrypt for Java - JBCrypt) public static String cryptPassword(String password) { return BCrypt.hashpw(password, BCrypt.gensalt()); } public static boolean checkPassword(String password, Person person) { return BCrypt.checkpw(password, person.getPassword()); } // Hashing function public static String generateToken(String string) { return String.valueOf((string + String.valueOf(new Date().getTime())).hashCode()); } // Date parsing public static String parseCalendarDate(Calendar date, String format) { DateFormat df = new SimpleDateFormat(format); return df.format(date.getTime()); } public static Workbook getExcelFromQuestionList(String workbookName, List<Question> questions) { // Create EXCEL File (Workbook with sheets) Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(workbookName); // Create styles for cells CellStyle questionStyle = workbook.createCellStyle(); questionStyle.setWrapText(true); questionStyle.setVerticalAlignment(VerticalAlignment.CENTER); CellStyle othersStyle = workbook.createCellStyle(); othersStyle.setAlignment(HorizontalAlignment.CENTER); othersStyle.setVerticalAlignment(VerticalAlignment.CENTER); // Create header row Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("ID"); headerRow.createCell(1).setCellValue("TAG"); headerRow.createCell(2).setCellValue("DOMANDA"); headerRow.createCell(3).setCellValue("APRROVATA (SI/NO)"); headerRow.getCell(0).setCellStyle(othersStyle); headerRow.getCell(1).setCellStyle(othersStyle); headerRow.getCell(2).setCellStyle(othersStyle); headerRow.getCell(3).setCellStyle(othersStyle); int rownum = 1; for (Question question : questions) { // Create a row Row row = sheet.createRow(rownum++); // Create cells for id and question and set their values row.createCell(0).setCellValue(question.getId()); row.createCell(1).setCellValue(question.getTag()); row.createCell(2).setCellValue(question.getBody()); // Create empty cell for admin input row.createCell(3); // Set cell styles row.getCell(0).setCellStyle(othersStyle); row.getCell(1).setCellStyle(othersStyle); row.getCell(2).setCellStyle(questionStyle); row.getCell(3).setCellStyle(othersStyle); } sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.setColumnWidth(2, 100 * 256); // Set questionStyle column width to 100 characters sheet.autoSizeColumn(3); return workbook; } public static List<Long> getApprovedQuestionListFromExcel(InputStream fileStream) throws IOException { // Build the document from stream Workbook workbook = new XSSFWorkbook(fileStream); // Get the sheet with data Sheet sheet = workbook.getSheetAt(0); // Create a list for results List<Long> approvedQuestions = new ArrayList<>(); int rownum = 1; Row row; Cell approvedCell; Cell idCell; while (sheet.getRow(rownum) != null) { row = sheet.getRow(rownum); rownum++; approvedCell = row.getCell(3); idCell = row.getCell(0); if (approvedCell != null && idCell != null && approvedCell.getStringCellValue().equalsIgnoreCase("SI")) { approvedQuestions.add(new Double(idCell.getNumericCellValue()).longValue()); } } return approvedQuestions; } }