Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.importer; import com.sfs.Formatter; import com.sfs.DataFilter; import com.sfs.beans.BuilderBean; import com.sfs.beans.UserBean; import com.sfs.beans.PrivilegesBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.ExamBean; import com.sfs.whichdoctor.dao.ExamDAO; import com.sfs.whichdoctor.dao.PersonDAO; import com.sfs.whichdoctor.formatter.OutputFormatter; import java.util.ArrayList; import java.util.List; import java.util.TreeMap; import java.util.HashMap; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; /** * The Class ExamImporter. * * @author David Harrison 14th June 2007 */ public class ExamImporter extends ImporterBase { /** The unique serial version UID for the class. */ private static final long serialVersionUID = 7780236869616583234L; /** The import logger. */ private static Logger importLogger = Logger.getLogger(ExamImporter.class); /** * Instantiates a new exam importer. */ public ExamImporter() { String[] keyFields = { "MIN", "Candidate No." }; String[] requiredFields = { "Exam Type", "Result" }; String[] optionalFields = { "Exam Date", "Exam Venue", "Result Band" }; setKeyFields(keyFields); setRequiredFields(requiredFields); setOptionalFields(optionalFields); } /** * Assign data. * * @param personDAO the person dao * * @return the hash map< object, list< object>> * * @throws WhichDoctorImporterException the which doctor importer exception */ protected final HashMap<Object, List<Object>> assignData(final PersonDAO personDAO) throws WhichDoctorImporterException { importLogger.debug("Getting data map"); HashMap<String, List<String>> dataMap = this.getDataMap(); if (dataMap == null) { throw new WhichDoctorImporterException("Error importing " + "- the data map cannot be null"); } List<Integer> indexValues = new ArrayList<Integer>(); TreeMap<Integer, PersonBean> keyMap = new TreeMap<Integer, PersonBean>(); // Load the index values if (dataMap.containsKey("MIN") || dataMap.containsKey("Candidate No.")) { importLogger.debug("Datamap contains index key"); // The submitted data has a key field, load associated people if (dataMap.containsKey("MIN")) { // Load person based on MIN for (String strIdentifier : dataMap.get("MIN")) { try { Integer identifier = new Integer(strIdentifier); if (!keyMap.containsKey(identifier)) { PersonBean person = personDAO.loadIdentifier(identifier, new BuilderBean()); if (person != null) { keyMap.put(identifier, person); } } indexValues.add(identifier); } catch (Exception e) { setImportMessage("Error loading person with MIN: " + strIdentifier); importLogger.error("Error loading person with MIN: " + e.getMessage()); } } } else { // dataMap has Candidate number but not MIN for (String strCandidateNo : dataMap.get("Candidate No.")) { try { Integer candidateNo = new Integer(strCandidateNo); if (!keyMap.containsKey(candidateNo)) { PersonBean person = personDAO.loadCandidateNumber(candidateNo.intValue(), new BuilderBean()); if (person != null) { keyMap.put(candidateNo, person); } } indexValues.add(candidateNo); } catch (Exception e) { setImportMessage("Error loading person with Candidate " + "Number: " + strCandidateNo); importLogger.error("Error loading person with " + "Candidate Number: " + e.getMessage()); } } } } // With the index values loaded cycle through and create the actual // beans for (int i = 0; i < indexValues.size(); i++) { Integer index = (Integer) indexValues.get(i); if (keyMap.containsKey(index)) { PersonBean person = (PersonBean) keyMap.get(index); if (person == null) { throw new WhichDoctorImporterException("Person is null"); } try { // Set the values of the exam object ExamBean exam = new ExamBean(); exam.setReferenceGUID(person.getGUID()); exam.setLogMessage("Created via automated import process"); if (dataMap.containsKey("Exam Type")) { List<String> values = dataMap.get("Exam Type"); importLogger.info("Exam Type: " + values.get(i)); exam.setType(values.get(i)); } if (dataMap.containsKey("Exam Date")) { List<String> values = dataMap.get("Exam Date"); importLogger.info("Exam Date: " + DataFilter.parseDate(values.get(i), true)); exam.setDateSat(DataFilter.parseDate(values.get(i), true)); } if (dataMap.containsKey("Exam Venue")) { List<String> values = dataMap.get("Exam Venue"); importLogger.info("Exam Venue: " + values.get(i)); exam.setLocation(values.get(i)); } if (dataMap.containsKey("Result")) { List<String> values = dataMap.get("Result"); importLogger.info("Result: " + values.get(i)); String status = checkInput("Result", values.get(i)); exam.setStatus(status); } if (dataMap.containsKey("Result Band")) { List<String> values = dataMap.get("Result Band"); importLogger.info("Result Band: " + values.get(i)); exam.setStatusLevel(values.get(i)); } setBeanArray(person, exam); } catch (Exception e) { setImportMessage("Error setting values for exam associated to: " + OutputFormatter.toFormattedName(person)); importLogger.error("Error setting values for exam: " + e.getMessage()); } } } return this.getBeanMap(); } /** * Commit. * * @param user the user * @param privileges the privileges * @param examDAO the exam dao * * @return the int */ public final int commit(final UserBean user, final PrivilegesBean privileges, final ExamDAO examDAO) { int newEntries = 0; HashMap<Object, List<Object>> beanMap = this.getBeanMap(); if (beanMap != null) { for (Object objPerson : beanMap.keySet()) { PersonBean person = (PersonBean) objPerson; List<Object> exams = beanMap.get(person); for (Object objExam : exams) { ExamBean exam = (ExamBean) objExam; importLogger.info("Creating exam: " + exam.getReferenceGUID() + ": " + exam.getType() + ": " + exam.getDateSat()); try { int examId = examDAO.create(exam, user, privileges); if (examId > 0) { // Successfully created, add to success map setSuccess(getIndexDescription(person), getValueDescription(exam)); newEntries++; } else { // Failed to create, add to failed map setFailure(getIndexDescription(person), getValueDescription(exam)); } } catch (Exception e) { importLogger.error("Error creating exam: " + e.getMessage()); // Add to failed map setFailure(getIndexDescription(person), getValueDescription(exam)); } } } } return newEntries; } /** * Gets the description map. * * @return the description map */ public final TreeMap<String, List<String>> getDescriptionMap() { TreeMap<String, List<String>> descriptionMap = new TreeMap<String, List<String>>(); HashMap<Object, List<Object>> beanMap = this.getBeanMap(); for (Object objPerson : beanMap.keySet()) { PersonBean person = (PersonBean) objPerson; String indexDescription = getIndexDescription(person); List<String> descriptions = new ArrayList<String>(); for (Object objExam : beanMap.get(person)) { ExamBean exam = (ExamBean) objExam; descriptions.add(getValueDescription(exam)); } descriptionMap.put(indexDescription, descriptions); } return descriptionMap; } /** * Gets the index description. * * @param person the person * * @return the index description */ private String getIndexDescription(final PersonBean person) { return OutputFormatter.toFormattedName(person); } /** * Gets the value description. * * @param exam the exam * * @return the value description */ private String getValueDescription(final ExamBean exam) { ArrayList<String[]> descriptiveFields = new ArrayList<String[]>(); String[] examType = { "Exam type", exam.getType() }; String[] examResult = { "Exam result", exam.getStatus() + " " + exam.getStatusLevel() }; String[] examVenue = { "Exam venue", exam.getLocation() + " on " + Formatter.numericDate(exam.getDateSat()) }; descriptiveFields.add(examType); descriptiveFields.add(examResult); descriptiveFields.add(examVenue); return generateDescription(descriptiveFields); } /** * Check the input for consistency. * * @param type the type * @param input the input * * @return the string */ private String checkInput(final String type, final String input) { String result = ""; if (StringUtils.isNotBlank(input)) { // The input is not null or empty, set and check if it needs to be modified result = input; if (StringUtils.equalsIgnoreCase(type, "Result")) { if (input.toLowerCase().startsWith("pas")) { result = "Passed"; } else { result = "Failed"; } } } return result; } }