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.analysis; import java.util.ArrayList; import java.util.Collection; import java.util.TreeMap; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.sfs.beans.BuilderBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.CreditBean; import com.sfs.whichdoctor.beans.DebitBean; import com.sfs.whichdoctor.beans.GroupAnalysisBean; import com.sfs.whichdoctor.beans.GroupBean; import com.sfs.whichdoctor.beans.ItemBean; import com.sfs.whichdoctor.beans.OrganisationBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.ReceiptBean; import com.sfs.whichdoctor.beans.ReimbursementBean; import com.sfs.whichdoctor.beans.RotationBean; import com.sfs.whichdoctor.beans.SearchBean; import com.sfs.whichdoctor.beans.SearchResultsBean; import com.sfs.whichdoctor.dao.BaseDAOImpl; import com.sfs.whichdoctor.search.SearchDAO; import com.sfs.whichdoctor.search.WhichDoctorSearchDaoException; /** * The Class GroupAnalysisDAOImpl. * * @author David Harrison */ public class GroupAnalysisDAOImpl extends BaseDAOImpl implements GroupAnalysisDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(GroupAnalysisDAOImpl.class); /** The search dao. */ @Resource private SearchDAO searchDAO; /** * Analyse a set of groups. * * @param groups the groups * @param user the user * * @return the group analysis bean * * @throws WhichDoctorAnalysisDaoException the which doctor analysis dao * exception */ public final GroupAnalysisBean analyse(final Collection<Integer> groups, final UserBean user) throws WhichDoctorAnalysisDaoException { return analyse(groups, new BuilderBean(), user); } /** * Analyse a set of groups. * * @param groups the groups * @param loadDetails the load details * @param user the user * * @return the group analysis bean * * @throws WhichDoctorAnalysisDaoException the which doctor analysis dao * exception */ public final GroupAnalysisBean analyse(final Collection<Integer> groups, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorAnalysisDaoException { GroupAnalysisBean groupAnalysis = new GroupAnalysisBean(); Collection<Object> variables = new ArrayList<Object>(); for (Integer groupGUID : groups) { dataLogger.debug("Analyising Group GUID: " + groupGUID); variables.add(groupGUID); } dataLogger.debug("Group variables size: " + variables.size()); if (variables.size() == 0) { dataLogger.error("Cannot perform group analysis with no groups"); throw new WhichDoctorAnalysisDaoException("Cannot perform group analysis with no groups"); } SearchBean search = this.searchDAO.initiate("group", user); search.setSearchArray(variables, "Group GUIDs"); search.setLimit(0); try { BuilderBean groupDetails = new BuilderBean(); groupDetails.setParameter("ITEMS", true); SearchResultsBean results = this.searchDAO.search(search, groupDetails); Collection<Object> searchResults = results.getSearchResults(); if (searchResults != null) { for (Object group : searchResults) { groupAnalysis.addGroup((GroupBean) group); } } } catch (Exception e) { dataLogger.error("Error performing Group Analysis search: " + e.getMessage()); } /* Iterate through each group's itembeans, adding them to the item map */ for (Integer groupGUID : groupAnalysis.getGroups().keySet()) { GroupBean group = groupAnalysis.getGroups().get(groupGUID); TreeMap<String, ItemBean> items = group.getItems(); if (items != null) { for (String key : items.keySet()) { ItemBean item = items.get(key); groupAnalysis.addReferenceGUID(item.getObject2GUID(), group.getGUID()); } } } /* * Build the CombinedGroup map using the groupGUID and referenceGUID * maps */ for (Integer referenceGUID : groupAnalysis.getReferenceGUIDs().keySet()) { ArrayList<Integer> groupGUIDs = groupAnalysis.getReferenceGUIDs().get(referenceGUID); groupAnalysis.addCombinedGroup(referenceGUID, groupGUIDs); } /* * With the referenceGUIDs/groupGUIDs organised load the referenceGUID * objects */ try { groupAnalysis.setReferenceObjects(loadReferenceObjects(groupAnalysis.getReferenceGUIDs(), loadDetails, user, groupAnalysis.getGroupClass())); } catch (Exception e) { dataLogger.error("Error loading reference objects: " + e.getMessage()); } /* With the objects loaded build the order map */ groupAnalysis .setReferenceOrder(buildOrder(groupAnalysis.getReferenceObjects(), groupAnalysis.getGroupClass())); return groupAnalysis; } /** * Load reference objects. * * @param referenceGUIDs the reference gui ds * @param loadDetails the load details * @param user the user * @param groupClass the group class * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao * exception */ private TreeMap<Integer, Object> loadReferenceObjects(final TreeMap<Integer, ArrayList<Integer>> referenceGUIDs, final BuilderBean loadDetails, final UserBean user, final String groupClass) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); Collection<Object> searchGUIDs = new ArrayList<Object>(); for (Integer referenceGUID : referenceGUIDs.keySet()) { searchGUIDs.add(String.valueOf(referenceGUID)); } dataLogger .info("Loading reference objects for: " + groupClass + ", with a size of: " + searchGUIDs.size()); if (StringUtils.equalsIgnoreCase(groupClass, "people") || StringUtils.equalsIgnoreCase(groupClass, "members")) { referenceObjects = loadPeople(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "organisations")) { referenceObjects = loadOrganisations(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "debits")) { referenceObjects = loadDebits(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "credits")) { referenceObjects = loadCredits(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "receipts")) { referenceObjects = loadReceipts(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "reimbursements")) { referenceObjects = loadReimbursements(searchGUIDs, loadDetails, user); } if (StringUtils.equalsIgnoreCase(groupClass, "rotations")) { referenceObjects = loadRotations(searchGUIDs, loadDetails, user); } return referenceObjects; } /** * Builds the order. * * @param referenceObjects the reference objects * @param groupClass the group class * * @return the tree map< string, integer> */ private TreeMap<String, Integer> buildOrder(final TreeMap<Integer, Object> referenceObjects, final String groupClass) { if (referenceObjects == null) { throw new NullPointerException("The reference objects map cannot be null"); } if (groupClass == null) { throw new NullPointerException("The group class string cannot be null"); } dataLogger.debug("Building order for " + groupClass + ", with a size of: " + referenceObjects.size()); TreeMap<String, Integer> orderMap = new TreeMap<String, Integer>(); for (Integer referenceGUID : referenceObjects.keySet()) { Object reference = referenceObjects.get(referenceGUID); String orderKey = GroupAnalysisBean.getOrderKey(reference, groupClass); orderMap.put(orderKey, referenceGUID); } return orderMap; } /** * Load people. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadPeople(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform a person search SearchBean search = this.searchDAO.initiate("person", user); search.setSearchArray(searchGUIDs, "People GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object personObj : referenceResults) { if (personObj != null) { PersonBean person = (PersonBean) personObj; referenceObjects.put(person.getGUID(), person); } } } return referenceObjects; } /** * Load organisations. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadOrganisations(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform an organisation search SearchBean search = this.searchDAO.initiate("organisation", user); search.setSearchArray(searchGUIDs, "Organisation GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object orgObj : referenceResults) { if (orgObj != null) { OrganisationBean organisation = (OrganisationBean) orgObj; referenceObjects.put(organisation.getGUID(), organisation); } } } return referenceObjects; } /** * Load debits. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadDebits(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform an debit search SearchBean search = this.searchDAO.initiate("debit", user); search.setSearchArray(searchGUIDs, "Debit GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object debitObj : referenceResults) { if (debitObj != null) { DebitBean debit = (DebitBean) debitObj; referenceObjects.put(debit.getGUID(), debit); } } } return referenceObjects; } /** * Load credits. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadCredits(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform an credit search SearchBean search = this.searchDAO.initiate("credit", user); search.setSearchArray(searchGUIDs, "Credit GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object creditObj : referenceResults) { if (creditObj != null) { CreditBean credit = (CreditBean) creditObj; referenceObjects.put(credit.getGUID(), credit); } } } return referenceObjects; } /** * Load receipts. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadReceipts(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform an receipt search SearchBean search = this.searchDAO.initiate("receipt", user); search.setSearchArray(searchGUIDs, "Receipt GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object receiptObj : referenceResults) { if (receiptObj != null) { ReceiptBean receipt = (ReceiptBean) receiptObj; referenceObjects.put(receipt.getGUID(), receipt); } } } return referenceObjects; } /** * Load reimbursements. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadReimbursements(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); /* Perform an reimbursement search */ SearchBean search = this.searchDAO.initiate("reimbursement", user); search.setSearchArray(searchGUIDs, "Reimbursement GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object reimbObj : referenceResults) { if (reimbObj != null) { ReimbursementBean reimbursement = (ReimbursementBean) reimbObj; referenceObjects.put(reimbursement.getGUID(), reimbursement); } } } return referenceObjects; } /** * Load rotations. * * @param searchGUIDs the search gui ds * @param loadDetails the load details * @param user the user * * @return the tree map< integer, object> * * @throws WhichDoctorSearchDaoException the which doctor search dao exception */ private TreeMap<Integer, Object> loadRotations(final Collection<Object> searchGUIDs, final BuilderBean loadDetails, final UserBean user) throws WhichDoctorSearchDaoException { TreeMap<Integer, Object> referenceObjects = new TreeMap<Integer, Object>(); // Perform an rotation search SearchBean search = this.searchDAO.initiate("rotation", user); search.setSearchArray(searchGUIDs, "Rotation GUIDs"); search.setLimit(0); SearchResultsBean results = this.searchDAO.search(search, loadDetails); if (results != null) { Collection<Object> referenceResults = results.getSearchResults(); for (Object rotationObj : referenceResults) { if (rotationObj != null) { RotationBean rotation = (RotationBean) rotationObj; referenceObjects.put(rotation.getGUID(), rotation); } } } return referenceObjects; } }