com.sfs.whichdoctor.importer.Importer.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.importer.Importer.java

Source

/*******************************************************************************
 * 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.beans.UserBean;
import com.sfs.beans.PrivilegesBean;
import com.sfs.whichdoctor.dao.ExamDAO;
import com.sfs.whichdoctor.dao.PersonDAO;

import java.io.IOException;
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;
import org.springframework.context.ApplicationContext;

/**
 * The Class Importer.
 *
 * @author David Harrison 14th June 2007
 */
public class Importer extends ImporterBase {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The import logger. */
    private static Logger importLogger = Logger.getLogger(Importer.class);

    /** The column map. */
    private TreeMap<Integer, String> columnMap = new TreeMap<Integer, String>();

    /** The include rows. */
    private TreeMap<Integer, String> includeRows = new TreeMap<Integer, String>();

    /** The description map. */
    private TreeMap<String, List<String>> descriptionMap = new TreeMap<String, List<String>>();

    /**
     * Sets the column map.
     *
     * @param columnMapVal the column map val
     */
    public final void setColumnMap(final TreeMap<Integer, String> columnMapVal) {
        columnMap = columnMapVal;
    }

    /**
     * Gets the column map.
     *
     * @return the column map
     */
    public final TreeMap<Integer, String> getColumnMap() {
        return columnMap;
    }

    /**
     * Sets the include rows.
     *
     * @param includeRowsVal the include rows val
     */
    public final void setIncludeRows(final TreeMap<Integer, String> includeRowsVal) {
        includeRows = includeRowsVal;
    }

    /**
     * Gets the include rows.
     *
     * @return the include rows
     */
    public final TreeMap<Integer, String> getIncludeRows() {
        return includeRows;
    }

    /**
     * Sets the description map.
     *
     * @param descriptionMapVal the description map val
     */
    public final void setDescriptionMap(final TreeMap<String, List<String>> descriptionMapVal) {
        descriptionMap = descriptionMapVal;
    }

    /**
     * Gets the description map.
     *
     * @return the description map
     */
    public final TreeMap<String, List<String>> getDescriptionMap() {
        return descriptionMap;
    }

    /**
     * Sets the column map.
     *
     * @param type the type
     * @param data the data
     * @param includeRowsVal the include rows
     */
    public final void setColumnMap(final String type, final TreeMap<Integer, TreeMap<Integer, String>> data,
            final TreeMap<Integer, String> includeRowsVal) {

        TreeMap<Integer, String> columnMapVal = new TreeMap<Integer, String>();

        List<String> fields = new ArrayList<String>();

        if (StringUtils.equalsIgnoreCase(type, "exam")) {
            ExamImporter examImporter = new ExamImporter();
            fields = examImporter.getFields();
        }

        // Inspect the first row of data supplied
        Integer rowIndex = data.keySet().iterator().next();

        TreeMap<Integer, String> firstRow = data.get(rowIndex);

        int fieldMatches = 0;

        for (Integer columnNumber : firstRow.keySet()) {
            String dataField = firstRow.get(columnNumber);
            String fieldName = "";

            // Iterate through each field to see if there is a match
            // If there is more than two matches then the first row
            // is indicating column field names
            for (int i = 0; i < fields.size(); i++) {
                String field = (String) fields.get(i);
                if (StringUtils.equalsIgnoreCase(dataField, field)) {
                    // Matching field
                    fieldName = dataField;
                    fieldMatches++;
                }
            }
            columnMapVal.put(columnNumber, fieldName);
        }
        if (fieldMatches > 2) {
            // There were more than two field matches
            // Deselect the first column from the list of imports
            if (includeRowsVal.containsKey(rowIndex)) {
                includeRowsVal.remove(rowIndex);
            }
        }

        setIncludeRows(includeRowsVal);
        setColumnMap(columnMapVal);

    }

    /**
     * Process data.
     *
     * @param type the type
     * @param data the data
     * @param columns the columns
     * @param applicationContext the application context
     *
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public final void processData(final String type, final TreeMap<Integer, TreeMap<Integer, String>> data,
            final TreeMap<Integer, String> columns, final ApplicationContext applicationContext)
            throws IOException {
        HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>();

        if (data != null && columns != null) {
            // Iterate through the columns assigning values to the dataMap
            for (Integer columnNumber : columns.keySet()) {
                String columnField = columns.get(columnNumber);

                if (StringUtils.isNotBlank(columnField)) {
                    // The column has been associated to some data
                    ArrayList<String> dataValues = new ArrayList<String>();

                    for (Integer rowNumber : data.keySet()) {
                        TreeMap<Integer, String> rowData = data.get(rowNumber);
                        if (rowData.containsKey(columnNumber)) {
                            // Get the value of the column and add it array
                            String dataField = rowData.get(columnNumber);
                            dataValues.add(dataField);
                        }
                    }
                    // Add the data name and array to the dataMap
                    dataMap.put(columnField, dataValues);
                }
            }
            // Store the completed dataMap
            setDataMap(dataMap);
        }

        // Take the dataMap and load the relevant objects into the bean map
        if (StringUtils.equalsIgnoreCase(type, "exam")) {
            importLogger.debug("Processing exam import");
            try {
                ExamImporter examImporter = new ExamImporter();
                examImporter.setDataMap(getDataMap());

                /* Process the data map and turn it into a bean map */
                final PersonDAO personDAO = (PersonDAO) applicationContext.getBean("personDAO");

                setBeanMap(examImporter.assignData(personDAO));
                setDescriptionMap(examImporter.getDescriptionMap());
                setImportMessage(examImporter.getImportMessage());

            } catch (Exception e) {
                importLogger.error("Error processing exam import: " + e.getMessage());
                throw new IOException("Error processing exam import: " + e.getMessage());
            }
        }
    }

    /**
     * Commit.
     *
     * @param type the type
     * @param user the user
     * @param privileges the privileges
     * @param applicationContext the application context
     *
     * @return the int
     */
    public final int commit(final String type, final UserBean user, final PrivilegesBean privileges,
            final ApplicationContext applicationContext) {
        int newEntries = 0;

        HashMap<Object, List<Object>> beanMap = getBeanMap();
        if (beanMap != null) {
            // Bean map exists for committing
            // Execute the corresponding commit action

            if (StringUtils.equalsIgnoreCase(type, "exam")) {
                ExamImporter examImporter = new ExamImporter();
                examImporter.setBeanMap(beanMap);

                final ExamDAO examDAO = (ExamDAO) applicationContext.getBean("examDAO");

                newEntries = examImporter.commit(user, privileges, examDAO);
                setSuccessMap(examImporter.getSuccessMap());
                setFailureMap(examImporter.getFailureMap());
            }
        }
        return newEntries;
    }
}