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 info.toegepaste.controller; import info.toegepaste.model.Classgroup; import info.toegepaste.model.Course; import info.toegepaste.model.Exam; import info.toegepaste.model.Score; import info.toegepaste.model.Student; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; import javax.servlet.http.Part; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.persistence.Query; import org.primefaces.event.FileUploadEvent; import org.primefaces.model.UploadedFile; import info.toegepaste.service.ClassgroupService; import info.toegepaste.service.CourseService; import info.toegepaste.service.ExamService; import info.toegepaste.service.ScoreService; import info.toegepaste.service.StudentsService; import java.util.Calendar; import javax.ejb.EJB; /** * * @author Gustave */ @ManagedBean(name = "UploadController") public class UploadController { /** * Creates a new instance of UploadController */ @EJB private ClassgroupService classgroupService; @EJB private ExamService examService; @EJB private StudentsService studentsService; @EJB private ScoreService scoreService; @EJB private CourseService courseService; private UploadedFile file; public void fileUploadListener(FileUploadEvent e) { // Get uploaded file from the FileUploadEvent to use with primefaces this.file = e.getFile(); // Get uploaded file to use with Apache POI InputStream POIFile; XSSFWorkbook workbook = null; try { POIFile = e.getFile().getInputstream(); //Create workbook workbook = new XSSFWorkbook(POIFile); } catch (IOException ex) { } // Print out the information of the file System.out.println( "Uploaded File Name Is :: " + file.getFileName() + " :: Uploaded File Size :: " + file.getSize()); //Create a worksheet (needed to get rows) XSSFSheet worksheet = workbook.getSheetAt(0); //Divide worksheet into rows Iterator<Row> rowIterator = worksheet.iterator(); //Get Classgroup from line 1 cell 2 XSSFRow currentRow = (XSSFRow) rowIterator.next(); Iterator<Cell> klasIter = currentRow.cellIterator(); XSSFCell klasCell = currentRow.getCell(1); //Get Course from line 2 cell 2 currentRow = (XSSFRow) rowIterator.next(); Iterator<Cell> courseIter = currentRow.cellIterator(); XSSFCell courseCell = currentRow.getCell(1); //Get subject from line 3 cell 2 System.out.println("Stuff"); currentRow = (XSSFRow) rowIterator.next(); Iterator<Cell> subjectIter = currentRow.cellIterator(); XSSFCell subjectCell = currentRow.getCell(1); System.out.println("Subject:" + subjectCell.toString()); System.out.println("Subject:" + subjectCell.getStringCellValue()); //Get total possible score from line 4 cell 2 currentRow = (XSSFRow) rowIterator.next(); Iterator<Cell> totalScoreIter = currentRow.cellIterator(); XSSFCell totalScoreCell = currentRow.getCell(1); //Skip line 5 & 6 currentRow = (XSSFRow) rowIterator.next(); currentRow = (XSSFRow) rowIterator.next(); currentRow = (XSSFRow) rowIterator.next(); List<Classgroup> group = classgroupService.getWithName(klasCell.getStringCellValue()); //Persist new exam to database Exam newExam = new Exam(); Classgroup newGroup = new Classgroup(); //Check if classgroup already exists, create if it doesnt if (group.isEmpty()) { newGroup.setName(klasCell.getStringCellValue()); newGroup.setCourses(null); classgroupService.insert(newGroup); } else { newGroup = group.get(0); } newExam.setClassgroup(newGroup); List<Course> course = courseService.getWithName(courseCell.getStringCellValue()); Course newCourse = new Course(); //Check if course exists, if not create if (course.isEmpty()) { newCourse.setName(courseCell.getStringCellValue()); int year = Calendar.getInstance().get(Calendar.YEAR); newCourse.setYear(year); newCourse.setClassgroup(newGroup); int maand = Calendar.getInstance().get(Calendar.MONTH); if (maand <= 6 && maand >= 1) { newCourse.setSemester(2); } else { newCourse.setSemester(1); } courseService.insert(newCourse); } else { newCourse = course.get(0); } newExam.setCourse(newCourse); newExam.setName(subjectCell.getStringCellValue()); // double totalScoreValue = ; // String totalScoreWorkaround =String.valueOf(totalScoreValue); newExam.setTotal((int) totalScoreCell.getNumericCellValue()); examService.insert(newExam); //Read file to end, cell 0 student number, cell 1 name, cell 2 score while (rowIterator.hasNext()) { XSSFCell userNrCell = currentRow.getCell(0); System.out.println(userNrCell.toString()); int StudentNumber = 0; // String StudentNumberWorkaround =userNrCell.getStringCellValue(); StudentNumber = (int) userNrCell.getNumericCellValue(); List<Student> currentStudent = studentsService.getStudentInListByNumber(StudentNumber); Student newStudent = new Student(); XSSFCell userNameCell = currentRow.getCell(1); //Check if student exists, else create if (currentStudent.isEmpty()) { String fullName = userNameCell.getStringCellValue(); String nameArray[] = fullName.split(" "); newStudent.setFirstname(nameArray[0]); newStudent.setLastname(nameArray[1]); newStudent.setNumber(StudentNumber); newStudent.setEmail("r0" + StudentNumber + "@student.thomasmore.be"); newStudent.setClassgroup(newGroup); newStudent.setPassword(null); studentsService.insert(newStudent); } else { newStudent = currentStudent.get(0); } //Add score to student List<Score> currentScore = scoreService.checkIfScoreExists(newStudent, newExam); XSSFCell scoreCell = currentRow.getCell(2); Score scoreEntry = new Score(); if (currentScore.isEmpty()) { scoreEntry.setExam(newExam); scoreEntry.setScore((int) scoreCell.getNumericCellValue()); if (currentStudent.isEmpty()) { scoreEntry.setStudent(newStudent); } else { scoreEntry.setStudent(currentStudent.get(0)); } scoreService.insert(scoreEntry); } else { scoreEntry = currentScore.get(0); } currentRow = (XSSFRow) rowIterator.next(); } } public void validateFile(FacesContext ctx, UIComponent comp, Object value) { List<FacesMessage> msgs = new ArrayList<FacesMessage>(); if (file.getSize() > 1024) { msgs.add(new FacesMessage("file too big")); } if (!"text/plain".equals(file.getContentType())) { msgs.add(new FacesMessage("not a text file")); } if (!msgs.isEmpty()) { throw new ValidatorException(msgs); } } }