com.sfs.whichdoctor.analysis.TrainingSummaryAnalysisDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.analysis.TrainingSummaryAnalysisDAOImpl.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.analysis;

import com.sfs.DataFilter;
import com.sfs.beans.BuilderBean;
import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.TrainingSummaryBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.RotationBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.beans.SearchResultsBean;
import com.sfs.whichdoctor.beans.SpecialtyBean;
import com.sfs.whichdoctor.dao.BaseDAOImpl;
import com.sfs.whichdoctor.search.SearchDAO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * The Class TrainingSummaryAnalysisDAOImpl.
 */
public class TrainingSummaryAnalysisDAOImpl extends BaseDAOImpl implements TrainingSummaryAnalysisDAO {

    /** The data logger. */
    private static Logger dataLogger = Logger.getLogger(TrainingSummaryAnalysisDAOImpl.class);

    /** The search dao. */
    @Resource
    private SearchDAO searchDAO;

    /**
     * Perform a training analysis search.
     *
     * @param trainingSummary the existing training summary search
     *
     * @return the training summary bean
     *
     * @throws WhichDoctorAnalysisDaoException the which doctor analysis dao
     *             exception
     */
    public final TrainingSummaryBean search(final TrainingSummaryBean trainingSummary)
            throws WhichDoctorAnalysisDaoException {

        TrainingSummaryBean search = trainingSummary.clone();

        if (search == null) {
            throw new NullPointerException("The training summary cannot be null");
        }

        /* Clear any existing results or people */
        search.clearResults();

        // Perform a PersonSearchDAO based on the array
        // of people GUIDs in the search object

        Collection<Object> peopleGUIDs = new ArrayList<Object>();

        if (search.getPeople() != null) {
            for (String name : search.getPeople().keySet()) {
                PersonBean person = search.getPeople().get(name);
                peopleGUIDs.add(person.getGUID());
            }
        }

        // Load the people who are part of this rotation summary
        SearchBean findPeople = this.searchDAO.initiate("person", new UserBean());
        findPeople.setLimit(0);
        findPeople.setSearchArray(peopleGUIDs, "People in training summary set");

        BuilderBean personDetails = new BuilderBean();
        // Set search to load training summary details
        for (String type : search.getTrainingTypes()) {
            if (type.compareToIgnoreCase("Basic Training") == 0) {
                personDetails.setParameter("TRAINING_BASIC", true);
            }
            if (type.compareToIgnoreCase("Advanced Training") == 0) {
                personDetails.setParameter("TRAINING_ADVANCED", true);
            }
            if (type.compareToIgnoreCase("Post-FRACP Training") == 0) {
                personDetails.setParameter("TRAINING_POSTFRACP", true);
            }
        }
        personDetails.setParameter("MEMBERSHIP", true);
        personDetails.setParameter("SPECIALTY", true);

        try {
            SearchResultsBean personResults = this.searchDAO.search(findPeople, personDetails);

            // Set the training summary people
            if (personResults != null) {
                Collection<PersonBean> people = new ArrayList<PersonBean>();
                for (Object result : personResults.getSearchResults()) {
                    people.add((PersonBean) result);
                }
                search.setLoadedPeople(people);
            }
        } catch (Exception e) {
            dataLogger.error("Error performing person search: " + e.getMessage());
            throw new WhichDoctorAnalysisDaoException("Error performing person search: " + e.getMessage());
        }

        /**
         * Iterate through all the loaded people Put them into the relevant
         * specialty committee groups Depending on specialty status - training
         */
        for (Integer guid : search.getLoadedPeople().keySet()) {
            PersonBean person = search.getLoadedPeople().get(guid);
            if (person.getSpecialtyList() != null) {
                for (SpecialtyBean specialty : person.getSpecialtyList()) {
                    if (StringUtils.equalsIgnoreCase(specialty.getStatus(), "Training for specialty")) {
                        search = addPersonToCommittee(search, specialty.getTrainingProgram(), person);
                    }
                }
            }
        }

        SearchBean findRotations = this.searchDAO.initiate("rotation", new UserBean());
        findRotations.setLimit(0);

        // Set the search criteria to be the list of people GUIDs
        RotationBean rotationCriteria = (RotationBean) findRotations.getSearchCriteria();
        RotationBean rotationConstraint = (RotationBean) findRotations.getSearchConstraints();

        dataLogger.info("Setting " + peopleGUIDs.size() + " people GUID values");
        Collection<Integer> people = new ArrayList<Integer>();
        for (Object guid : peopleGUIDs) {
            people.add((Integer) guid);
        }
        rotationCriteria.setPeopleGUIDs(people);
        rotationCriteria.setSummaryTypes(search.getTrainingTypes());

        /* Set the start date for the rotations */
        rotationCriteria.setStartDate(search.getStartDate());
        rotationConstraint.setStartDate(DataFilter.parseDate("31/12/2037", false));
        /* Set the end date for the rotations */
        rotationCriteria.setEndDate(search.getEndDate());
        rotationConstraint.setEndDate(DataFilter.parseDate("1/1/1900", false));

        findRotations.setSearchCriteria(rotationCriteria);
        findRotations.setSearchConstraints(rotationConstraint);

        BuilderBean rotationDetails = new BuilderBean();
        rotationDetails.setParameter("ASSESSMENTS", true);
        rotationDetails.setParameter("SUPERVISORS", true);

        try {
            SearchResultsBean rotationResults = this.searchDAO.search(findRotations, rotationDetails);

            if (rotationResults != null) {
                Collection<RotationBean> rotations = new ArrayList<RotationBean>();
                if (rotationResults.getSearchResults() != null) {
                    for (Object result : rotationResults.getSearchResults()) {
                        rotations.add((RotationBean) result);
                    }
                }
                /* Set the training summary rotations */
                search.setResults(rotations);
            }

        } catch (Exception e) {
            dataLogger.error("Error performing rotation search: " + e.getMessage());
            throw new WhichDoctorAnalysisDaoException("Error performing rotation search: " + e.getMessage());
        }

        return search;
    }

    /**
     * Adds the person to committee.
     *
     * @param summary the summary
     * @param committee the committee
     * @param person the person
     *
     * @return the training summary bean
     */
    private TrainingSummaryBean addPersonToCommittee(final TrainingSummaryBean summary, final String committee,
            final PersonBean person) {

        if (committee != null && person != null) {
            /* Get existing committee tree map */
            Map<String, Collection<RotationBean>> committeeMap = summary.getResults(committee);

            if (committeeMap != null) {
                String orderIndex = summary.getOrder(person);

                if (!committeeMap.containsKey(orderIndex)) {
                    // The person does not exist in this committee, add to it
                    committeeMap.put(orderIndex, new ArrayList<RotationBean>());
                    summary.setResults(committee, committeeMap);
                }
            }
        }
        return summary;
    }
}