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.dao; import com.sfs.beans.BuilderBean; import com.sfs.beans.FinancialTypeBean; import com.sfs.beans.PrivilegesBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.ExpenseClaimBean; 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.ReimbursementBean; import com.sfs.whichdoctor.beans.SearchBean; import com.sfs.whichdoctor.beans.SearchResultsBean; import com.sfs.whichdoctor.search.WhichDoctorSearchDaoException; import java.sql.Date; import java.sql.ResultSet; import java.sql.Timestamp; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.TreeMap; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; /** * The Class ReimbursementDAOImpl. * * @author David Harrison */ public class ReimbursementDAOImpl extends WhichDoctorFinancialObjectDAOImpl implements ReimbursementDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(ReimbursementDAOImpl.class); /** The expense claim dao. */ @Resource private ExpenseClaimDAO expenseClaimDAO; /** * Get a ReimbursementBean for a specified reimbursementId. * * @param reimbursementId the reimbursement id * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean load(final int reimbursementId) throws WhichDoctorDaoException { return load(reimbursementId, new BuilderBean(), false); } /** * Get a ReimbursementBean for a specified reimbursementId with the supplied * load details. * * @param reimbursementId the reimbursement id * @param loadDetails the load details * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean load(final int reimbursementId, final BuilderBean loadDetails) throws WhichDoctorDaoException { return load(reimbursementId, loadDetails, false); } /** * Get a ReimbursementBean for a specified reimbursementId with the supplied * load details. A boolean parameter identifies whether to use the default * reader connection or optional writer connection datasource. * * @param reimbursementId the reimbursement id * @param loadDetails the load details * @param useWriterConn the use writer conn * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ private ReimbursementBean load(final int reimbursementId, final BuilderBean loadDetails, final boolean useWriterConn) throws WhichDoctorDaoException { dataLogger.info("Reimbursement Id: " + reimbursementId + " requested"); ReimbursementBean reimbursement = null; final String loadSQL = this.getSQL().getValue("reimbursement/load") + " AND reimbursement.ReimbursementId = ? " + "GROUP BY reimbursement.ReimbursementId"; JdbcTemplate jdbcTemplate = this.getJdbcTemplateReader(); if (useWriterConn) { jdbcTemplate = this.getJdbcTemplateWriter(); } try { reimbursement = (ReimbursementBean) jdbcTemplate.queryForObject(loadSQL, new Object[] { reimbursementId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadReimbursement(rs, loadDetails); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } return reimbursement; } /** * Get a ReimbursementBean for a specified name. * * @param strReimbursement the str reimbursement * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean load(final String strReimbursement) throws WhichDoctorDaoException { return load(strReimbursement, new BuilderBean()); } /** * Get a ReimbursementBean for a specified name and supplied load details. * * @param strReimbursement the str reimbursement * @param loadDetails the load details * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean load(final String strReimbursement, final BuilderBean loadDetails) throws WhichDoctorDaoException { dataLogger.info("Reimbursement Name: " + strReimbursement + " requested"); int reimbursementGUID = 0; final String loadSQL = getSQL().getValue("reimbursement/loadName"); try { reimbursementGUID = this.getJdbcTemplateReader().queryForInt(loadSQL, new Object[] { strReimbursement }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } if (reimbursementGUID > 0) { return loadGUID(reimbursementGUID, loadDetails); } else { throw new WhichDoctorDaoException( "Sorry no reimbursement matching " + "those details could be identified"); } } /** * Get a ReimbursementBean for a specified guid. * * @param guid the guid * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean loadGUID(final int guid) throws WhichDoctorDaoException { return loadGUID(guid, new BuilderBean()); } /** * Get a ReimbursementBean for a specified guid and supplied load details. * * @param guid the guid * @param loadDetails the load details * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean loadGUID(final int guid, final BuilderBean loadDetails) throws WhichDoctorDaoException { dataLogger.info("Reimbursement GUID: " + guid + " requested"); ReimbursementBean reimbursement = null; final String loadSQL = getSQL().getValue("reimbursement/load") + " AND reimbursement.Active = true AND reimbursement.GUID = ?" + " GROUP BY reimbursement.ReimbursementId"; try { reimbursement = (ReimbursementBean) this.getJdbcTemplateReader().queryForObject(loadSQL, new Object[] { guid }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadReimbursement(rs, loadDetails); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } return reimbursement; } /** * Creates the ReimbursementBean. * * @param reimbursement the reimbursement * @param checkUser the check user * @param privileges the privileges * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean create(final ReimbursementBean reimbursement, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { reimbursement.setActive(true); final int reimbursementId = save(reimbursement, checkUser, privileges, "create"); return load(reimbursementId, new BuilderBean(), true); } /** * Modify. * * @param reimbursement the reimbursement * @param checkUser the check user * @param privileges the privileges * * @return the reimbursement bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final ReimbursementBean modify(final ReimbursementBean reimbursement, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { reimbursement.setActive(true); final int reimbursementId = save(reimbursement, checkUser, privileges, "modify"); return load(reimbursementId, new BuilderBean(), true); } /** * Delete the ReimbursementBean. * * @param reimbursement the reimbursement * @param checkUser the check user * @param privileges the privileges * * @return true, if successful * * @throws WhichDoctorDaoException the which doctor dao exception */ public final boolean delete(final ReimbursementBean reimbursement, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { boolean success = false; if (privileges.getPrivilege(checkUser, "reimbursements", "delete")) { // Before deleting the reimbursement remove its child objects final BuilderBean loadDetails = new BuilderBean(); loadDetails.setParameter("LOAD_ALL", true); try { final ReimbursementBean deleteObj = this.loadGUID(reimbursement.getGUID(), loadDetails); deleteAssociatedObjects(deleteObj, checkUser, privileges); } catch (WhichDoctorDaoException wde) { dataLogger.error("Error deleting children: " + wde.getMessage(), wde); throw new WhichDoctorDaoException("Error deleting children objects: " + wde.getMessage()); } } reimbursement.setActive(false); int deleteId = save(reimbursement, checkUser, privileges, "delete"); if (deleteId > 0) { success = true; } return success; } /** * Save. * * @param reimbursement the reimbursement * @param checkUser the check user * @param privileges the privileges * @param action the action * * @return the int * * @throws WhichDoctorDaoException the which doctor dao exception */ private int save(final ReimbursementBean reimbursement, final UserBean checkUser, final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException { /* Create reimbursement requires all the essential information */ if (reimbursement.getDescription() == null) { throw new WhichDoctorDaoException("Reimbursement description field " + "cannot be null"); } if (reimbursement.getDescription().compareTo("") == 0) { throw new WhichDoctorDaoException("Reimbursement description field " + "cannot be an empty string"); } if (reimbursement.getPersonId() == 0 && reimbursement.getOrganisationId() == 0) { throw new WhichDoctorDaoException("Reimbursement must be " + "attributed to a person or organisation"); } if (!privileges.getPrivilege(checkUser, "reimbursements", action)) { throw new WhichDoctorDaoException("Insufficient user credentials " + "to " + action + " reimbursement"); } /* Find Reimbursement Type Id */ int typeId = 0; try { FinancialTypeBean financialObject = this.financialTypeDAO.load("Reimbursement", reimbursement.getTypeName(), reimbursement.getClassName()); typeId = financialObject.getFinancialTypeId(); } catch (Exception e) { dataLogger.error("Error loading financial type for reimbursement: " + e.getMessage()); throw new WhichDoctorDaoException("Reimbursement requires a type"); } /* Check if the reimbursement number exists, if not create one */ reimbursement .setNumber(this.checkNumber("reimbursement", reimbursement.getNumber(), reimbursement.getIssued())); int reimbursementId = 0; Date issued = new Date(Calendar.getInstance().getTimeInMillis()); Date meetingDate = null; if (reimbursement.getIssued() != null) { issued = new Date(reimbursement.getIssued().getTime()); } if (reimbursement.getMeetingDate() != null) { meetingDate = new Date(reimbursement.getMeetingDate().getTime()); } Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis()); ArrayList<Object> parameters = new ArrayList<Object>(); parameters.add(reimbursement.getDescription()); parameters.add(reimbursement.getLocation()); parameters.add(meetingDate); parameters.add(reimbursement.getNumber()); parameters.add(reimbursement.getCancelled()); parameters.add(reimbursement.getPersonId()); parameters.add(reimbursement.getOrganisationId()); parameters.add(issued); parameters.add(typeId); parameters.add(reimbursement.getActive()); parameters.add(sqlTimeStamp); parameters.add(checkUser.getDN()); parameters.add(reimbursement.getLogMessage(action)); try { Integer[] result = this.performUpdate("reimbursement", reimbursement.getGUID(), parameters, "Reimbursement", checkUser, action); /* Set the returned guid and id values */ reimbursement.setGUID(result[0]); reimbursementId = result[1]; } catch (Exception e) { dataLogger.error("Error processing reimbursement record: " + e.getMessage()); throw new WhichDoctorDaoException("Error processing reimbursement " + "record: " + e.getMessage()); } if (reimbursementId > 0) { dataLogger.info(checkUser.getDN() + " created reimbursementId: " + String.valueOf(reimbursementId)); } return reimbursementId; } /** * Delete the objects associated with the supplied ReimbursementBean. * * @param deleteObjects the delete objects * @param checkUser the check user * @param privileges the privileges */ private void deleteAssociatedObjects(final ReimbursementBean deleteObjects, final UserBean checkUser, final PrivilegesBean privileges) { if (deleteObjects.getExpenseClaims() != null) { // Delete expense claims for (ExpenseClaimBean expense : deleteObjects.getExpenseClaims()) { try { expense.setLogMessage(expense.getLogMessage("delete")); this.expenseClaimDAO.delete(expense, checkUser, privileges); } catch (WhichDoctorDaoException wde) { dataLogger.error("Error deleting expense claim: " + wde.getMessage()); } } } deleteMemos(deleteObjects, checkUser, privileges); deleteGroups(deleteObjects, checkUser, privileges); } /** * Load reimbursement. * * @param rs the rs * @param loadDetails the load details * * @return the reimbursement bean * * @throws SQLException the SQL exception */ private ReimbursementBean loadReimbursement(final ResultSet rs, final BuilderBean loadDetails) throws SQLException { ReimbursementBean reimbursement = new ReimbursementBean(); reimbursement.setId(rs.getInt("ReimbursementId")); reimbursement.setGUID(rs.getInt("GUID")); reimbursement.setAbbreviation(rs.getString("Abbreviation")); reimbursement.setClassName(rs.getString("ReimbursementClass")); reimbursement.setTypeName(rs.getString("ReimbursementType")); reimbursement.setNumber(rs.getString("ReimbursementNo")); reimbursement.setDescription(rs.getString("Description")); reimbursement.setLocation(rs.getString("Location")); try { reimbursement.setMeetingDate(rs.getDate("MeetingDate")); } catch (SQLException e) { reimbursement.setMeetingDate(null); } reimbursement.setPersonId(rs.getInt("PersonId")); if (reimbursement.getPersonId() > 0) { PersonBean person = new PersonBean(); person.setGUID(reimbursement.getPersonId()); person.setPersonIdentifier(rs.getInt("PersonIdentifier")); person.setPreferredName(rs.getString("PreferredName")); person.setFirstName(rs.getString("FirstName")); person.setLastName(rs.getString("LastName")); person.setTitle(rs.getString("Title")); person.setGender(rs.getString("Gender")); // Load address. if (loadDetails.getBoolean("ADDRESS")) { try { person.setAddress(this.getAddressDAO().load(person.getGUID(), false, loadDetails.getString("ADDRESS_CLASS"), loadDetails.getString("ADDRESS_TYPE"))); } catch (Exception e) { dataLogger.error("Error loading address for reimbursement: " + e.getMessage()); } } reimbursement.setPerson(person); } reimbursement.setOrganisationId(rs.getInt("OrganisationId")); if (reimbursement.getOrganisationId() > 0) { OrganisationBean organisation = new OrganisationBean(); organisation.setGUID(reimbursement.getOrganisationId()); organisation.setName(rs.getString("OrganisationName")); if (loadDetails.getBoolean("ADDRESS")) { try { organisation.setAddress(this.getAddressDAO().load(organisation.getGUID(), false, loadDetails.getString("ADDRESS_CLASS"), loadDetails.getString("ADDRESS_TYPE"))); } catch (Exception e) { dataLogger.error("Error loading address for reimbursement: " + e.getMessage()); } } reimbursement.setOrganisation(organisation); } try { reimbursement.setIssued(rs.getDate("Issued")); } catch (SQLException e) { reimbursement.setIssued(null); } reimbursement.setValue(rs.getDouble("ReimbursementValue")); reimbursement.setNetValue(rs.getDouble("ReimbursementTotalValue")); reimbursement.setCancelled(rs.getBoolean("Cancelled")); reimbursement.setSecurity(rs.getString("Security")); if (loadDetails.getBoolean("EXPENSECLAIM")) { try { reimbursement.setExpenseClaims(this.expenseClaimDAO.load(reimbursement.getGUID(), loadDetails.getBoolean("EXPENSECLAIM_FULL"))); } catch (Exception e) { dataLogger.error("Error loading expense claims for reimbursement: " + e.getMessage()); } } reimbursement.setActive(rs.getBoolean("Active")); if (loadDetails.getBoolean("HISTORY")) { try { reimbursement.setCreatedDate(rs.getTimestamp("CreatedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading CreatedDate: " + sqe.getMessage()); } reimbursement.setCreatedBy(rs.getString("CreatedBy")); try { reimbursement.setModifiedDate(rs.getTimestamp("ModifiedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ModifiedDate: " + sqe.getMessage()); } reimbursement.setModifiedBy(rs.getString("ModifiedBy")); try { reimbursement.setExportedDate(rs.getTimestamp("ExportedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ExportedDate: " + sqe.getMessage()); } reimbursement.setExportedBy(rs.getString("ExportedBy")); } if (loadDetails.getBoolean("TAGS")) { try { reimbursement.setTags( this.getTagDAO().load(reimbursement.getGUID(), loadDetails.getString("USERDN"), true)); } catch (Exception e) { dataLogger.error("Error loading tags for reimbursement: " + e.getMessage()); } } if (loadDetails.getBoolean("MEMO")) { try { reimbursement.setMemo( this.getMemoDAO().load(reimbursement.getGUID(), loadDetails.getBoolean("MEMO_FULL"))); } catch (Exception e) { dataLogger.error("Error loading memos for reimbursement: " + e.getMessage()); } } if (loadDetails.getBoolean("GROUPS")) { reimbursement.setGroups(loadGroups(reimbursement.getGUID())); } if (loadDetails.getBoolean("CREATED")) { UserBean user = new UserBean(); user.setDN(rs.getString("CreatedBy")); user.setPreferredName(rs.getString("CreatedFirstName")); user.setLastName(rs.getString("CreatedLastName")); reimbursement.setCreatedUser(user); } if (loadDetails.getBoolean("MODIFIED")) { UserBean user = new UserBean(); user.setDN(rs.getString("ModifiedBy")); user.setPreferredName(rs.getString("ModifiedFirstName")); user.setLastName(rs.getString("ModifiedLastName")); reimbursement.setModifiedUser(user); } if (loadDetails.getBoolean("EXPORTED")) { UserBean user = new UserBean(); user.setDN(rs.getString("ExportedBy")); user.setPreferredName(rs.getString("ExportedFirstName")); user.setLastName(rs.getString("ExportedLastName")); reimbursement.setExportedUser(user); } return reimbursement; } /** * Load groups. * * @param guid the guid * * @return the collection< group bean> */ private Collection<GroupBean> loadGroups(final int guid) { ArrayList<GroupBean> groups = new ArrayList<GroupBean>(); // Create new SearchBean of type Reimbursement and default values SearchResultsBean results = new SearchResultsBean(); SearchBean groupSearch = this.getSearchDAO().initiate("group", null); groupSearch.setLimit(0); GroupBean searchCriteria = (GroupBean) groupSearch.getSearchCriteria(); searchCriteria.setObjectType("Reimbursements"); ItemBean item = new ItemBean(); item.setObject2GUID(guid); TreeMap<String, ItemBean> items = new TreeMap<String, ItemBean>(); items.put("Reimbursement", item); searchCriteria.setItems(items); groupSearch.setSearchCriteria(searchCriteria); try { BuilderBean loadGroup = new BuilderBean(); loadGroup.setParameter("ITEMS", true); loadGroup.setParameter("REFERENCEID", String.valueOf(guid)); results = this.getSearchDAO().search(groupSearch, loadGroup); } catch (WhichDoctorSearchDaoException wdse) { dataLogger.error("Error loading groups for reimbursement: " + wdse.getMessage()); } for (Object group : results.getSearchResults()) { groups.add((GroupBean) group); } return groups; } }