FileParser.java
package edu.rit.coopeval.importing;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import edu.rit.coopeval.dao.*;
import edu.rit.coopeval.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by anusharma on 3/10/15.
*/
@Component
public class FileParser {
@Autowired
private EvaluationRepository evaluationRepo;
@Autowired
private DepartmentRepository departmentRepo;
@Autowired
private FormQuestionRepository formQuestionRepo;
@Autowired
private DepartmentTermFormRepository departmentTermFormRepo;
@Autowired
private StudentQuestionAnswerRepository studentQuestionAnswerRepo;
@Autowired
private EmployerQuestionAnswerRepository employerQuestionAnswerRepo;
@Autowired
private EmployerUserRepository employerUserRepo;
@Autowired
private EvaluationStatusRepository evaluationStatusRepo;
private Evaluation evaluation;
private EmployerUser employerUser;
private Department department;
private DepartmentTermForm departmentTermForm;
private StudentQuestionAnswer studentQuestionAnswer;
private EmployerQuestionAnswer employerQuestionAnswer;
private Form form;
private Timestamp dateTime = new Timestamp(new Date().getTime());
private String username;
public void parseLineByLine(File importFile, String Username) throws IOException {
try{
username = Username;
Scanner scan = new Scanner(importFile);
while (scan.hasNextLine()){
parseLine(scan.nextLine());
if(evaluation != null) {
evaluationRepo.save(evaluation);
studentQuestionAnswerRepo.save(studentQuestionAnswer);
employerQuestionAnswerRepo.save(employerQuestionAnswer);
}
if(department != null)
departmentRepo.save(department);
if(employerUser != null)
employerUserRepo.save(employerUser);
}
}
catch (IOException e){
e.printStackTrace();
}
}
public void parseLine(String line){
HashMap<String, String> mapping = new HashMap<String, String>();
try{
Scanner scan = new Scanner(line).useDelimiter("\t");
while (scan.hasNext()) {
mapping.put("placementId", scan.next());
mapping.put("studentUid", scan.next());
mapping.put("employerId", scan.next());
mapping.put("contactId", scan.next());
mapping.put("placementTitle", scan.next());
mapping.put("studentLastName", scan.next());
mapping.put("studentFirstName", scan.next());
mapping.put("programCode", scan.next());
mapping.put("quarterCode", scan.next());
mapping.put("accountManager", scan.next());
mapping.put("blockCode", scan.next());
mapping.put("studentEmail", scan.next());
mapping.put("JobType", scan.next());
mapping.put("gender", scan.next());
mapping.put("currentYear", scan.next());
mapping.put("employerName", scan.next());
mapping.put("employerLocation", scan.next());
mapping.put("contactLastName", scan.next());
mapping.put("contactFirstName", scan.next());
mapping.put("contactEmail", scan.next());
mapping.put("studentDce", scan.next());
mapping.put("contactPhone", scan.next());
mapping.put("gradDate", scan.next());
mapping.put("coopsUpToNow", scan.next());
mapping.put("advisorFirstName", scan.next());
mapping.put("advisorLastName", scan.next());
mapping.put("advisorDce", scan.next());
}
importEvaluation(mapping);
// importEmployerUser(mapping);
// importDepartment(mapping);
if(evaluation != null) {
importQuestionAnswers();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public Evaluation importEvaluation(HashMap<String,String> data){
if(evaluationRepo.findByPlacementId(data.get("placementId")) == null) {
Department dept = departmentRepo.findByDepartmentCode(data.get("programCode"));
departmentTermForm = departmentTermFormRepo.findByDepartmentId(dept);
form = departmentTermForm.getFormByStudentFormId();
evaluation = new Evaluation();
evaluation.setPlacementId(data.get("placementId"));
evaluation.setStudentUid(data.get("studentUid"));
evaluation.setJobTitle(data.get("placementTitle"));
evaluation.setStudentFirstName(data.get("studentFirstName"));
evaluation.setStudentLastName(data.get("studentLastName"));
//evaluation.setTerm(data.get("quarterCode"));
evaluation.setStudentEmail(data.get("studentEmail"));
evaluation.setGender(data.get("gender"));
evaluation.setCurrentYear(Long.parseLong(data.get("currentYear")));
evaluation.setCompanyLocation(data.get("employerLocation"));
evaluation.setCompanyPhoneNumber(data.get("contactPhone"));
evaluation.setGradYear(data.get("gradDate"));
evaluation.setCoopsUpToNow(Long.parseLong(data.get("coopsUpToNow")));
evaluation.setAdvisorFirstName(data.get("advisorFirstName"));
evaluation.setAdvisorLastName(data.get("advisorLastName"));
evaluation.setAdvisorDCE(data.get("advisorDce"));
//Making assumption that studentFormId and employerFormId would be same for a department
evaluation.setFormByStudentFormId(form);
evaluation.setFormByEmployerFormId(form);
evaluation.setEmployerUserByEmployerUserId(importEmployerUser(data));
evaluation.setDepartmentByDepartmentId(importDepartment(data));
evaluation.setEvaluationStatusByEmployerEvaluationStatusId(evaluationStatusRepo.findByName("Pending"));
evaluation.setEvaluationStatusByEmployerEvaluationStatusId(evaluationStatusRepo.findByName("Pending"));
//evaluation.setEvaluationApprovalByEvaluationId(); // To do
return evaluation;
}
else
return evaluationRepo.findByPlacementId(data.get("placementId"));
}
public EmployerUser importEmployerUser(HashMap<String, String> data){
if(employerUserRepo.getEmployerUserById(Long.parseLong((data.get("contactId")))) == null) {
employerUser = new EmployerUser();
employerUser.setEmployerUserId(data.get("contactId"));
employerUser.setCompanyName(data.get("employerName"));
employerUser.setEmployerFirstName(data.get("contactFirstName"));
employerUser.setEmployerLastName(data.get("contactLastName"));
employerUser.setEmployerEmail(data.get("contactEmail"));
employerUser.setCreateDate(dateTime);
employerUser.setModDate(dateTime);
employerUser.setCreateBy(username);
employerUser.setModBy(username);
return employerUser;
}
else
return employerUserRepo.getEmployerUserById(Long.parseLong((data.get("contactId"))));
}
public Department importDepartment(HashMap<String, String> data){
if(departmentRepo.findByDepartmentCode(data.get("programCode")) == null) {
department = new Department();
department.setDepartmentCode(data.get("programCode"));
//department.setCollegeByCollegeId(); //Is College imported before Department? How the relationship works?
return department;
}
else
return departmentRepo.findByDepartmentCode(data.get("programCode"));
}
public void importQuestionAnswers(){
List<FormQuestion> formQuestions = formQuestionRepo.findByForm(form);
for(FormQuestion formQuestion: formQuestions){
importStudentQuestionAnswers(formQuestion);
importEmployerQuestionAnswers(formQuestion);
}
}
public void importStudentQuestionAnswers(FormQuestion formQuestion){
studentQuestionAnswer = new StudentQuestionAnswer();
studentQuestionAnswer.setEvaluationByEvaluationId(evaluation);
studentQuestionAnswer.setFormQuestionByFormQuestionId(formQuestion);
}
public void importEmployerQuestionAnswers( FormQuestion formQuestion){
employerQuestionAnswer = new EmployerQuestionAnswer();
employerQuestionAnswer.setEvaluationByEvaluationId(evaluation);
employerQuestionAnswer.setFormQuestionByFormQuestionId(formQuestion);
}
}