Java tutorial
package com.che.software.testato.domain.dao.jdbc.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.log4j.Logger; import org.springframework.stereotype.Repository; import com.che.software.testato.domain.dao.IScriptItemDAO; import com.che.software.testato.domain.dao.jdbc.adao.AbstractDAO; import com.che.software.testato.domain.dao.jdbc.exception.ScriptItemSearchDAOException; import com.che.software.testato.domain.entity.ScriptItem; import com.che.software.testato.domain.entity.search.ScriptItemSearch; /** * JDBC implementation of the DAO interface dedicated to the scripts items * management. * * @author Clement HELIOU (clement.heliou@che-software.com). * @copyright Che Software. * @license GNU General Public License. * @see AbstractDAO, IScriptItemDAO. * @since July, 2011. * * This file is part of Testato. * * Testato is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Testato is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License * along with Testato. If not, see <http://www.gnu.org/licenses/>. * * Testato's logo is a creation of Arrioch * (http://arrioch.deviantart.com/) and it's distributed under the terms * of the Creative Commons License. */ @Repository("scriptItemDAO") public class ScriptItemDAO extends AbstractDAO implements IScriptItemDAO { /** * Constants. */ private static final Logger LOGGER = Logger.getLogger(ScriptItemDAO.class); /** * Script item search from a bean of criterions. * * @author Clement HELIOU (clement.heliou@che-software.com). * @param searchBean the criterions to use for the search. * @return the resulting object list. * @since July, 2011. * @throws ScriptItemSearchDAOException if an error occurs during the * search. */ @Override public List<ScriptItem> searchScriptItem(ScriptItemSearch searchBean) throws ScriptItemSearchDAOException { LOGGER.debug("searchScriptItem()."); Connection connection = null; try { connection = getDataSource().getConnection(); List<Object> params = new ArrayList<Object>(); return getQueryRunner().query(connection, getScriptItemSearchQueryFromCriterion(searchBean, params), new BeanListHandler<ScriptItem>(ScriptItem.class), params.toArray()); } catch (SQLException e) { throw new ScriptItemSearchDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } } /** * Recovery of the script item search query from criterion. * * @author Clement HELIOU (clement.heliou@che-software.com). * @param searchBean the object containing the criterions. * @param params the parameters list corresponding to the built query. * @return the built query. * @since July, 2011. */ private String getScriptItemSearchQueryFromCriterion(ScriptItemSearch searchBean, List<Object> params) { LOGGER.debug("getScriptItemSearchQueryFromCriterion()."); setWhereClauseEnabled(false); StringBuilder sBuilder = new StringBuilder( "SELECT script_item_id AS scriptItemId, script_id AS scriptId, label, \"ORDER\" AS \"order\" FROM script_item "); if (null != searchBean && null != searchBean.getScriptId() && 0 != searchBean.getScriptId()) { sBuilder.append(getWhereClauseBegin()); sBuilder.append("script_id = ? "); params.add(searchBean.getScriptId()); } if (null != searchBean && null != searchBean.getWithoutChildren() && true == searchBean.getWithoutChildren()) { sBuilder.append(getWhereClauseBegin()); sBuilder.append( "NOT EXISTS( SELECT script_id FROM script WHERE parent_script_item = script_item_id ) "); } return sBuilder.toString(); } }