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 java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.dao.DataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.RowMapper; import com.sfs.beans.PrivilegesBean; import com.sfs.beans.ObjectTypeBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.PhoneBean; import com.sfs.whichdoctor.beans.IsbTransactionBean; /** * The Class PhoneDAOImpl. */ public class PhoneDAOImpl extends WhichDoctorBaseDAOImpl implements PhoneDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(PhoneDAOImpl.class); /** The isb transaction dao. */ @Resource private IsbTransactionDAO isbTransactionDAO; /** * Used to get a Collection of PhoneBeans for a specified GUID. * * @param guid the guid * @param allPhoneNumbers the all phone numbers * @return the collection * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final Collection<PhoneBean> load(final int guid, final boolean allPhoneNumbers) throws WhichDoctorDaoException { dataLogger.info("Phone numbers for GUID: " + guid + " requested"); // Do check whether required results are light // (only primary) or full (all phone numbers) String loadPhone = this.getSQL().getValue("phone/load") + " WHERE phone.Active = true AND phone.ReferenceGUID = ?"; if (!allPhoneNumbers) { loadPhone += " AND PrimaryPhone = true ORDER BY PrimaryPhone DESC"; } else { loadPhone += " ORDER BY PrimaryPhone DESC"; } Collection<PhoneBean> phoneNumbers = new ArrayList<PhoneBean>(); try { phoneNumbers = this.getJdbcTemplateReader().query(loadPhone, new Object[] { guid }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadPhone(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return phoneNumbers; } /** * Load the phone bean. * * @param phoneId the phone id * @return the phone bean * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final PhoneBean load(final int phoneId) throws WhichDoctorDaoException { dataLogger.info("Getting phoneId:" + phoneId); final String loadPhoneId = getSQL().getValue("phone/load") + " WHERE phone.PhoneId = ?"; PhoneBean phoneNumber = null; try { phoneNumber = (PhoneBean) this.getJdbcTemplateReader().queryForObject(loadPhoneId, new Object[] { phoneId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadPhone(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return phoneNumber; } /** * Creates the phone bean. * * @param phone the phone * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int create(final PhoneBean phone, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { phone.setActive(true); return save(phone, checkUser, privileges, "create"); } /** * Modify the phone bean. * * @param phone the phone * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int modify(final PhoneBean phone, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { phone.setActive(true); return save(phone, checkUser, privileges, "modify"); } /** * Delete the phone bean. * * @param phone the phone * @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 PhoneBean phone, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { boolean success = false; phone.setActive(false); int phoneId = save(phone, checkUser, privileges, "delete"); if (phoneId > 0) { success = true; } return success; } /** * Gets the country code based on the supplied country string. * * @param country the country * @return the country code */ public final int getCountryCode(final String country) { int countryCode = 0; if (StringUtils.isNotBlank(country)) { try { countryCode = this.getJdbcTemplateReader() .queryForInt(this.getSQL().getValue("phone/findCountryCode"), new Object[] { country }); } catch (DataAccessException dae) { dataLogger.error("Error loading country code: " + dae.getMessage()); } } return countryCode; } /** * Save the phone bean. * * @param phone the phone * @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 PhoneBean phone, final UserBean checkUser, final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException { /* Check required information within phone bean is present */ if (phone.getNumber() == null) { throw new WhichDoctorDaoException("Sorry invalid phone number"); } if (phone.getNumber().compareTo("") == 0) { throw new WhichDoctorDaoException("Sorry invalid phone number"); } if (phone.getReferenceGUID() == 0) { throw new WhichDoctorDaoException("Phone requires a valid Reference GUID number"); } if (!privileges.getPrivilege(checkUser, "phones", action)) { throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " phone entry"); } int phoneTypeId = 0; try { ObjectTypeBean object = this.getObjectTypeDAO().load("Phone Type", "", phone.getContactType()); phoneTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for phone type: " + e.getMessage()); throw new WhichDoctorDaoException("Phone requires a valid type"); } int phoneId = 0; Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis()); ArrayList<Object> parameters = new ArrayList<Object>(); parameters.add(phone.getReferenceGUID()); parameters.add(phoneTypeId); parameters.add(phone.getDescription()); parameters.add(phone.getNumber()); parameters.add(phone.getExtension()); parameters.add(phone.getAreaCode()); parameters.add(phone.getCountryCode()); parameters.add(phone.getPrimary()); parameters.add(phone.getActive()); parameters.add(sqlTimeStamp); parameters.add(checkUser.getDN()); parameters.add(phone.getLogMessage(action)); /* Begin the ISB transaction */ IsbTransactionBean isbTransaction = this.isbTransactionDAO.begin(phone.getReferenceGUID()); try { Integer[] result = this.performUpdate("phone", phone.getGUID(), parameters, "Phone", checkUser, action); /* Set the returned guid and id values */ phone.setGUID(result[0]); phoneId = result[1]; } catch (Exception e) { dataLogger.error("Error processing phone record: " + e.getMessage()); throw new WhichDoctorDaoException("Error processing phone record: " + e.getMessage()); } if (phoneId > 0) { // Update person city/region/primary // address details to reflect modification updatePrimary(phone, action); /* Commit the ISB transaction */ this.isbTransactionDAO.commit(isbTransaction); } return phoneId; } /** * If the PhoneBean is primary ensure no other * primary entry exists for the ReferenceGUID. * * @param phone the phone * @param action the action * @throws WhichDoctorDaoException the which doctor dao exception */ private void updatePrimary(final PhoneBean phone, final String action) throws WhichDoctorDaoException { if (phone.getPrimary()) { if (action.compareTo("delete") != 0) { /* Find old primary address and turn off flag */ this.getJdbcTemplateWriter().update(this.getSQL().getValue("phone/updatePrimary"), new Object[] { false, phone.getReferenceGUID(), phone.getGUID(), true }); } } } /** * Load phone bean from the result set. * * @param rs the rs * @return the phone bean * @throws SQLException the sQL exception */ private PhoneBean loadPhone(final ResultSet rs) throws SQLException { PhoneBean phoneNumber = new PhoneBean(); phoneNumber.setId(rs.getInt("PhoneId")); phoneNumber.setGUID(rs.getInt("GUID")); phoneNumber.setReferenceGUID(rs.getInt("ReferenceGUID")); phoneNumber.setContactType(rs.getString("ContactType")); phoneNumber.setDescription(rs.getString("Description")); phoneNumber.setNumber(rs.getString("PhoneNum")); phoneNumber.setExtension(rs.getString("Extension")); phoneNumber.setAreaCode(rs.getInt("AreaCode")); phoneNumber.setCountryCode(rs.getInt("CountryCode")); phoneNumber.setPrimary(rs.getBoolean("PrimaryPhone")); phoneNumber.setActive(rs.getBoolean("Active")); try { phoneNumber.setCreatedDate(rs.getTimestamp("CreatedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading CreatedDate: " + sqe.getMessage()); } phoneNumber.setCreatedBy(rs.getString("CreatedBy")); try { phoneNumber.setModifiedDate(rs.getTimestamp("ModifiedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ModifiedDate: " + sqe.getMessage()); } phoneNumber.setModifiedBy(rs.getString("ModifiedBy")); try { phoneNumber.setExportedDate(rs.getTimestamp("ExportedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ExportedDate: " + sqe.getMessage()); } phoneNumber.setExportedBy(rs.getString("ExportedBy")); return phoneNumber; } }