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.dao; import com.sfs.beans.ObjectTypeBean; import com.sfs.beans.PrivilegesBean; import com.sfs.beans.UserBean; 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.log4j.Logger; import org.apache.commons.lang.StringUtils; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.RowMapper; /** * The Class ObjectTypeDAOImpl. */ public class ObjectTypeDAOImpl extends BaseDAOImpl implements ObjectTypeDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(ObjectTypeDAOImpl.class); /** The user dao. */ @Resource private UserDAO userDAO; /** * Sets the user dao. * * @param userDAORef the new user dao */ public final void setUserDAO(final UserDAO userDAORef) { this.userDAO = userDAORef; } /** * Load a collection of ObjectTypeBeans. * * @param type the type * * @return the collection< object type bean> * * @throws SFSDaoException the SFS dao exception */ @SuppressWarnings("unchecked") public final Collection<ObjectTypeBean> load(final String type) throws SFSDaoException { if (StringUtils.isBlank(type)) { throw new SFSDaoException("Error: type cannot be an empty string"); } dataLogger.info("List items for: " + type + " requested"); Collection<ObjectTypeBean> listItems = new ArrayList<ObjectTypeBean>(); try { listItems = this.getJdbcTemplateReader().query(this.getSQL().getValue("objectType/load"), new Object[] { type }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadObject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return listItems; } /** * Load an ObjectTypeBean for the supplied id. * * @param objectTypeId the object type id * * @return the object type bean * * @throws SFSDaoException the SFS dao exception */ @SuppressWarnings("unchecked") public final ObjectTypeBean load(final int objectTypeId) throws SFSDaoException { if (objectTypeId == 0) { throw new SFSDaoException("ObjectTypeId value cannot be 0"); } ObjectTypeBean objectType = null; try { objectType = (ObjectTypeBean) this.getJdbcTemplateReader().queryForObject( this.getSQL().getValue("objectType/loadId"), new Object[] { objectTypeId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadObject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return objectType; } /** * Load an ObjectTypeBean for the supplied parameters. * * @param object the object * @param objectName the object name * @param objectClass the object class * * @return the object type bean * * @throws SFSDaoException the SFS dao exception */ @SuppressWarnings("unchecked") public final ObjectTypeBean load(final String object, final String objectName, final String objectClass) throws SFSDaoException { if (object == null) { throw new SFSDaoException("Object definition cannot be null"); } if (objectName == null) { throw new SFSDaoException("Object Name cannot be null"); } if (objectClass == null) { throw new SFSDaoException("Object Class canot be null"); } ObjectTypeBean objectType = null; try { objectType = (ObjectTypeBean) this.getJdbcTemplateReader().queryForObject( this.getSQL().getValue("objectType/loadType"), new Object[] { object, objectName, objectClass }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadObject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } if (objectType == null) { throw new SFSDaoException("No objecttype found"); } return objectType; } /** * Load an ObjectTypeBean for the supplied parameters. * * @param object the object * @param objectAbbreviation the object abbreviation * @param objectName the object name * @param objectClass the object class * * @return the object type bean * * @throws SFSDaoException the SFS dao exception */ @SuppressWarnings("unchecked") public final ObjectTypeBean load(final String object, final String objectAbbreviation, final String objectName, final String objectClass) throws SFSDaoException { if (object == null) { throw new SFSDaoException("Object definition cannot be null"); } if (objectAbbreviation == null) { throw new SFSDaoException("Object Abbreviation cannot be null"); } if (objectName == null) { throw new SFSDaoException("Object Name cannot be null"); } if (objectClass == null) { throw new SFSDaoException("Object Class canot be null"); } ObjectTypeBean objectType = null; try { objectType = (ObjectTypeBean) this.getJdbcTemplateReader().queryForObject( this.getSQL().getValue("objectType/loadTypeAbbreviation"), new Object[] { object, objectAbbreviation, objectName, objectClass }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadObject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } if (objectType == null) { throw new SFSDaoException("No objecttype found"); } return objectType; } /** * Load the class name for ObjectTypeBean using the supplied abbreviation. * * @param object the object * @param abbreviation the abbreviation associated with the object * * @return the class name * * @throws SFSDaoException the SFS dao exception */ @SuppressWarnings("unchecked") public final String load(final String object, final String abbreviation) throws SFSDaoException { String className = ""; Collection<String> classNames = new ArrayList<String>(); try { classNames = this.getJdbcTemplateReader().query(this.getSQL().getValue("objectType/loadClassName"), new String[] { object, abbreviation.toUpperCase() }, new RowMapper() { public String mapRow(final ResultSet rs, final int rowNum) throws SQLException { String name = rs.getString("Class"); return name; } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } if (classNames != null) { for (String name : classNames) { if (StringUtils.isNotBlank(name)) { className = name; } } } return className; } /** * Creates an object type. * * @param objectType the object type * @param checkUser the check user * @param privileges the privileges * * @return true, if successful * * @throws SFSDaoException the SFS dao exception */ public final boolean create(final ObjectTypeBean objectType, final UserBean checkUser, final PrivilegesBean privileges) throws SFSDaoException { if (objectType.getObject() == null) { throw new SFSDaoException("ObjectType cannot have a null object type"); } if (objectType.getName() == null) { objectType.setName(""); } if (StringUtils.isBlank(objectType.getObject())) { throw new SFSDaoException("ObjectType cannot have an empty string " + "for its object type"); } if (StringUtils.isBlank(objectType.getClassName())) { throw new SFSDaoException("ObjectType cannot have an empty string " + "for a class"); } if (!privileges.getPrivilege(checkUser, "objectType", "create")) { throw new SFSDaoException("Insufficient user credentials to create new object type"); } boolean success = false; try { Calendar calendar = Calendar.getInstance(); Timestamp currentTime = new Timestamp(calendar.getTimeInMillis()); int insertCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("objectType/create"), new Object[] { objectType.getObject(), objectType.getName(), objectType.getClassName(), objectType.getAbbreviation(), objectType.getValue(), objectType.getSecurity(), objectType.getLdapMapping(1), objectType.getLdapMapping(2), objectType.getLdapMapping(3), currentTime, checkUser.getDN() }); if (insertCount > 0) { dataLogger.info(checkUser.getDN() + " successfully created " + "object type"); success = true; } } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return success; } /** * Modify an object type. * * @param objectType the object type * @param checkUser the check user * @param privileges the privileges * * @return true, if successful * * @throws SFSDaoException the SFS dao exception */ public final boolean modify(final ObjectTypeBean objectType, final UserBean checkUser, final PrivilegesBean privileges) throws SFSDaoException { if (objectType.getObjectTypeId() == 0) { throw new SFSDaoException("ObjectTypeId cannot be 0"); } if (objectType.getName() == null) { objectType.setName(""); } if (StringUtils.isBlank(objectType.getClassName())) { throw new SFSDaoException("ObjectType cannot have an empty string " + "for a class"); } if (!privileges.getPrivilege(checkUser, "objectType", "modify")) { throw new SFSDaoException("Insufficient user credentials to edit " + "object type"); } boolean success = false; try { Calendar calendar = Calendar.getInstance(); Timestamp currentTime = new Timestamp(calendar.getTimeInMillis()); int updateCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("objectType/modify"), new Object[] { objectType.getName(), objectType.getClassName(), objectType.getAbbreviation(), objectType.getValue(), objectType.getSecurity(), objectType.getLdapMapping(1), objectType.getLdapMapping(2), objectType.getLdapMapping(3), currentTime, checkUser.getDN(), objectType.getObjectTypeId() }); if (updateCount > 0) { dataLogger.info(checkUser.getDN() + " successfully updated " + "object type"); success = true; } } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return success; } /** * Delete an object type. * * @param objectType the object type * @param checkUser the check user * @param privileges the privileges * * @return true, if successful * * @throws SFSDaoException the SFS dao exception */ public final boolean delete(final ObjectTypeBean objectType, final UserBean checkUser, final PrivilegesBean privileges) throws SFSDaoException { if (objectType.getObjectTypeId() == 0) { throw new SFSDaoException("ObjectType requires a vaild " + "ObjectTypeId number"); } if (!privileges.getPrivilege(checkUser, "objectType", "delete")) { throw new SFSDaoException("Insufficient user credentials to delete " + "object type"); } boolean success = false; try { int deleteCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("objectType/delete"), new Object[] { objectType.getObjectTypeId() }); if (deleteCount > 0) { dataLogger.info(checkUser.getDN() + " successfully deleted " + "object type"); success = true; } } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return success; } /** * Load object. * * @param rs the rs * * @return the object type bean * * @throws SQLException the SQL exception */ private ObjectTypeBean loadObject(final ResultSet rs) throws SQLException { ObjectTypeBean objectType = new ObjectTypeBean(); // Create item bean and fill with dataset info. objectType.setObjectTypeId(rs.getInt("ObjectTypeId")); objectType.setObject(rs.getString("Object")); objectType.setName(rs.getString("Name")); objectType.setClassName(rs.getString("Class")); objectType.setAbbreviation(rs.getString("Abbreviation")); objectType.setValue(rs.getDouble("Value")); objectType.setSecurity(rs.getString("Security")); objectType.setLdapMapping(1, rs.getString("LdapMapping")); objectType.setLdapMapping(2, rs.getString("SecondLdapMapping")); objectType.setLdapMapping(3, rs.getString("ThirdLdapMapping")); try { objectType.setCreated(rs.getTimestamp("Created")); } catch (SQLException sqe) { dataLogger.info("Could not load Created date: " + sqe.getMessage()); } objectType.setCreatedBy(rs.getString("CreatedBy")); try { objectType.setModified(rs.getTimestamp("Modified")); } catch (SQLException sqe) { dataLogger.info("Could not load Modified date: " + sqe.getMessage()); } objectType.setModifiedBy(rs.getString("ModifiedBy")); if (objectType.getCreatedBy() != null && this.userDAO != null) { try { objectType.setCreatedByUser(this.userDAO.loadCached(objectType.getCreatedBy())); } catch (Exception e) { dataLogger.info("Could not load CreatedBy UserBean for " + "ObjectTypeId = " + objectType.getObjectTypeId()); } } if (objectType.getModifiedBy() != null && this.userDAO != null) { try { objectType.setModifiedByUser(this.userDAO.loadCached(objectType.getModifiedBy())); } catch (Exception e) { dataLogger.info("Could not load ModifiedBy UserBean for " + "ObjectTypeId = " + objectType.getObjectTypeId()); } } return objectType; } }