All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.gdface.facedb.dborm.image.FdImageManager Maven / Gradle / Ivy

// ______________________________________________________
// Generated by sql2java - https://github.com/10km/sql2java-2-6-7 (custom branch) 
// modified by guyadong from
// sql2java original version https://sourceforge.net/projects/sql2java/ 
// JDBC driver used at code generation time: com.mysql.jdbc.Driver
// template: manager.java.vm
// ______________________________________________________
package net.gdface.facedb.dborm.image;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

import net.gdface.facedb.dborm.Constant;
import net.gdface.facedb.dborm.Manager;
import net.gdface.facedb.dborm.TableListener;
import net.gdface.facedb.dborm.TableManager;
import net.gdface.facedb.dborm.exception.DaoException;
import net.gdface.facedb.dborm.exception.DataAccessException;
import net.gdface.facedb.dborm.exception.DataRetrievalException;
import net.gdface.facedb.dborm.exception.ObjectRetrievalException;
import net.gdface.facedb.dborm.face.FdFaceBean;
import net.gdface.facedb.dborm.face.FdFaceManager;

/**
 * Handles database calls (save, load, count, etc...) for the fd_image table.
* Remarks: 图像信息存储表,用于存储系统中所有用到的图像数据,表中只包含图像基本信息,图像二进制源数据存在在fd_store中(md5对应)
* @author sql2java */ public class FdImageManager extends TableManager.BaseAdapter { /** * Tablename. */ public static final String TABLE_NAME="fd_image"; /** * Contains all the primary key fields of the fd_image table. */ public static final String[] PRIMARYKEY_NAMES = { "md5" }; @Override public String getTableName() { return TABLE_NAME; } @Override public String getFields() { return FD_IMAGE_FIELDS; } @Override public String getFullFields() { return FD_IMAGE_FULL_FIELDS; } @Override public String[] getPrimarykeyNames() { return PRIMARYKEY_NAMES; } private static FdImageManager singleton = new FdImageManager(); protected FdImageManager(){} /** * Get the FdImageManager singleton. * * @return FdImageManager */ public static FdImageManager getInstance() { return singleton; } /** * Creates a new FdImageBean instance. * * @return the new FdImageBean */ public FdImageBean createBean() { return new FdImageBean(); } @Override protected Class beanType(){ return FdImageBean.class; } protected FdFaceManager instanceOfFdFaceManager(){ return FdFaceManager.getInstance(); } ////////////////////////////////////// // PRIMARY KEY METHODS ////////////////////////////////////// //1 /** * Loads a {@link FdImageBean} from the fd_image using primary key fields. * * @param md5 String - PK# 1 * @return a unique FdImageBean or {@code null} if not found or have null argument * @throws DaoException */ public FdImageBean loadByPrimaryKey(String md5) throws DaoException { try{ return loadByPrimaryKeyChecked(md5); }catch(ObjectRetrievalException e){ // not found return null; } } //1.1 /** * Loads a {@link FdImageBean} from the fd_image using primary key fields. * * @param md5 String - PK# 1 * @return a unique FdImageBean * @throws ObjectRetrievalException if not found * @throws DaoException */ @SuppressWarnings("unused") public FdImageBean loadByPrimaryKeyChecked(String md5) throws DaoException { if(null == md5){ throw new ObjectRetrievalException(new NullPointerException()); } Connection c = null; PreparedStatement ps = null; try { c = this.getConnection(); StringBuilder sql = new StringBuilder("SELECT " + FD_IMAGE_FIELDS + " FROM fd_image WHERE md5=?"); // System.out.println("loadByPrimaryKey: " + sql); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); if (md5 == null) { ps.setNull(FD_IMAGE_ID_MD5 + 1, Types.CHAR); } else { ps.setString(FD_IMAGE_ID_MD5 + 1, md5); } List pReturn = this.loadByPreparedStatementAsList(ps); if (1 == pReturn.size()) { return pReturn.get(0); } else { throw new ObjectRetrievalException(); } } catch(ObjectRetrievalException e) { throw e; } catch(SQLException e) { throw new DataRetrievalException(e); } finally { this.getManager().close(ps); this.freeConnection(c); } } //1.2 @Override public FdImageBean loadByPrimaryKey(FdImageBean bean) throws DaoException { return bean==null?null:loadByPrimaryKey(bean.getMd5()); } //1.2.2 @Override public FdImageBean loadByPrimaryKeyChecked(FdImageBean bean) throws DaoException { if(null == bean){ throw new NullPointerException(); } return loadByPrimaryKeyChecked(bean.getMd5()); } //1.3 /** * Loads a {@link FdImageBean} from the fd_image using primary key fields. * @param keys primary keys value:
* @return a unique {@link FdImageBean} or {@code null} if not found * @see {@link #loadByPrimaryKey(String md5)} */ @Override public FdImageBean loadByPrimaryKey(Object ...keys) throws DaoException{ if(null == keys){ throw new NullPointerException(); } if(keys.length != FD_IMAGE_PK_COUNT){ throw new IllegalArgumentException("argument number mismatch with primary key number"); } if(null == keys[0]){ return null; } return loadByPrimaryKey((String)keys[0]); } //1.3.2 @Override public FdImageBean loadByPrimaryKeyChecked(Object ...keys) throws DaoException{ if(null == keys){ throw new NullPointerException(); } if(keys.length != FD_IMAGE_PK_COUNT){ throw new IllegalArgumentException("argument number mismatch with primary key number"); } if(! (keys[0] instanceof String)){ throw new IllegalArgumentException("invalid type for the No.1 argument,expected type:String"); } return loadByPrimaryKeyChecked((String)keys[0]); } //1.4 /** * Returns true if this fd_image contains row with primary key fields. * @param md5 String - PK# 1 * @throws DaoException */ @SuppressWarnings("unused") public boolean existsPrimaryKey(String md5) throws DaoException { if(null == md5){ return false; } Connection c = null; PreparedStatement ps = null; try{ c = this.getConnection(); StringBuilder sql = new StringBuilder("SELECT COUNT(*) AS MCOUNT FROM fd_image WHERE md5=?"); // System.out.println("loadByPrimaryKey: " + sql); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); if (md5 == null) { ps.setNull(FD_IMAGE_ID_MD5 + 1, Types.CHAR); } else { ps.setString(FD_IMAGE_ID_MD5 + 1, md5); } return 1 == this.countByPreparedStatement(ps); }catch(SQLException e){ throw new ObjectRetrievalException(e); }finally{ this.getManager().close(ps); this.freeConnection(c); } } //1.6 /** * Return true if this fd_image contains row with primary key fields. * @param bean * @throws DaoException * @return false if primary kes has null * @see #countUsingTemplate(FdImageBean) */ @Override public boolean existsByPrimaryKey(FdImageBean bean) throws DaoException { if(null == bean || null == bean.getMd5()){ return false; } long modified = bean.getModified(); try{ bean.resetModifiedExceptPrimaryKeys(); return 1 == countUsingTemplate(bean); }finally{ bean.setModified(modified); } } //1.7 @Override public FdImageBean checkDuplicate(FdImageBean bean) throws DaoException{ if(!existsByPrimaryKey(bean)){ throw new ObjectRetrievalException("Duplicate entry ("+ bean.getMd5() +") for key 'PRIMARY'"); } return bean; } //1.4.1 /** * Check duplicated row by primary keys,if row exists throw {@link ObjectRetrievalException} * @param md5 String * @throws DaoException * @see #existsPrimaryKey(String md5) */ public String checkDuplicate(String md5) throws DaoException { if(existsPrimaryKey(md5)){ throw new ObjectRetrievalException("Duplicate entry '"+ md5 +"' for key 'PRIMARY'"); } return md5; } //2 /** * Delete row according to its primary keys.
* all keys must not be null * * @param md5 String - PK# 1 * @return the number of deleted rows * @throws DaoException * @see {@link #delete(FdImageBean)} */ public int deleteByPrimaryKey(String md5) throws DaoException { FdImageBean bean=createBean(); bean.setMd5(md5); return this.delete(bean); } //2.2 /** * Delete row according to primary keys of bean.
* * @param bean will be deleted ,all keys must not be null * @return the number of deleted rows,0 returned if bean is null * @throws DaoException */ @Override public int delete(FdImageBean bean) throws DaoException { if(null == bean || null == bean.getMd5()){ return 0; } Connection c = null; PreparedStatement ps = null; try { // listener callback this.listenerContainer.beforeDelete(bean); c = this.getConnection(); StringBuilder sql = new StringBuilder("DELETE FROM fd_image WHERE md5=?"); // System.out.println("deleteByPrimaryKey: " + sql); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); if (bean.getMd5() == null) { ps.setNull(FD_IMAGE_ID_MD5 + 1, Types.CHAR); } else { ps.setString(FD_IMAGE_ID_MD5 + 1, bean.getMd5()); } int rows=ps.executeUpdate(); if(rows>0){ // listener callback this.listenerContainer.afterDelete(bean); } return rows; } catch(SQLException e) { throw new DataAccessException(e); } finally { // listener callback this.listenerContainer.done(); this.getManager().close(ps); this.freeConnection(c); } } //2.1 /** * Delete row according to its primary keys. * * @param keys primary keys value:
* @return the number of deleted rows * @see {@link #delete(FdImageBean)} */ @Override public int deleteByPrimaryKey(Object ...keys) throws DaoException{ if(null == keys){ throw new NullPointerException(); } if(keys.length != FD_IMAGE_PK_COUNT){ throw new IllegalArgumentException("argument number mismatch with primary key number"); } FdImageBean bean = createBean(); if(null != keys[0] && !(keys[0] instanceof String)){ throw new IllegalArgumentException("invalid type for the No.1 argument,expected type:String"); } bean.setMd5((String)keys[0]); return delete(bean); } ////////////////////////////////////// // IMPORT KEY GENERIC METHOD ////////////////////////////////////// private static final Class[] IMPORTED_BEAN_TYPES = new Class[]{FdFaceBean.class}; /** * @see #getImportedBeansAsList(FdImageBean,int) */ @SuppressWarnings("unchecked") @Override public > T[] getImportedBeans(FdImageBean bean, int ikIndex) throws DaoException { return getImportedBeansAsList(bean, ikIndex).toArray((T[])java.lang.reflect.Array.newInstance(IMPORTED_BEAN_TYPES[ikIndex],0)); } /** * Retrieves imported T objects by ikIndex.
* @param *
    *
  • {@link Constant#FD_IMAGE_IK_FD_FACE_IMAGE_MD5} -> {@link FdFaceBean}
  • *
* @param bean the {@link FdImageBean} object to use * @param ikIndex valid values: {@link Constant#FD_IMAGE_IK_FD_FACE_IMAGE_MD5} * @return the associated T beans or {@code null} if {@code bean} is {@code null} * @throws DaoException */ @SuppressWarnings("unchecked") @Override public > List getImportedBeansAsList(FdImageBean bean,int ikIndex)throws DaoException{ switch(ikIndex){ case FD_IMAGE_IK_FD_FACE_IMAGE_MD5: return (List)this.getFaceBeansByImageMd5AsList(bean); default: throw new IllegalArgumentException(String.format("invalid ikIndex %d", ikIndex)); } } /** * Set the T objects as imported beans of bean object by ikIndex.
* @param see also {@link #getImportedBeansAsList(FdImageBean,int)} * @param bean the {@link FdImageBean} object to use * @param importedBeans the FdFaceBean array to associate to the {@link FdImageBean} * @param ikIndex valid values: see also {@link #getImportedBeansAsList(FdImageBean,int)} * @return importedBeans always * @throws DaoException */ @SuppressWarnings("unchecked") @Override public > T[] setImportedBeans(FdImageBean bean,T[] importedBeans,int ikIndex)throws DaoException{ switch(ikIndex){ case FD_IMAGE_IK_FD_FACE_IMAGE_MD5: return (T[])setFaceBeansByImageMd5(bean,(FdFaceBean[])importedBeans); default: throw new IllegalArgumentException(String.format("invalid ikIndex %d", ikIndex)); } } /** * Set the importedBeans associates to the bean by ikIndex
* @param see also {@link #getImportedBeansAsList(FdImageBean,int)} * @param bean the {@link FdImageBean} object to use * @param importedBeans the object to associate to the {@link FdImageBean} * @param ikIndex valid values: see also {@link #getImportedBeansAsList(FdImageBean,int)} * @return importedBeans always * @throws DaoException */ @SuppressWarnings("unchecked") @Override public ,C extends java.util.Collection> C setImportedBeans(FdImageBean bean,C importedBeans,int ikIndex)throws DaoException{ switch(ikIndex){ case FD_IMAGE_IK_FD_FACE_IMAGE_MD5: return (C)setFaceBeansByImageMd5(bean,(java.util.Collection)importedBeans); default: throw new IllegalArgumentException(String.format("invalid ikIndex %d", ikIndex)); } } ////////////////////////////////////// // GET/SET IMPORTED KEY BEAN METHOD ////////////////////////////////////// //3.1 GET IMPORTED /** * Retrieves the {@link FdFaceBean} object from the fd_face.image_md5 field.
* FK_NAME : fd_face_ibfk_1 * @param bean the {@link FdImageBean} * @return the associated {@link FdFaceBean} beans or {@code null} if {@code bean} is {@code null} * @throws DaoException */ public FdFaceBean[] getFaceBeansByImageMd5(FdImageBean bean) throws DaoException { return getFaceBeansByImageMd5AsList(bean).toArray(new FdFaceBean[0]); } //3.1.2 GET IMPORTED /** * Retrieves the {@link FdFaceBean} object from the fd_face.image_md5 field.
* FK_NAME : fd_face_ibfk_1 * @param md5 String - PK# 1 * @return the associated {@link FdFaceBean} beans or {@code null} if {@code bean} is {@code null} * @throws DaoException */ public FdFaceBean[] getFaceBeansByImageMd5(String md5OfImage) throws DaoException { FdImageBean bean = createBean(); bean.setMd5(md5OfImage); return getFaceBeansByImageMd5(bean); } //3.2 GET IMPORTED /** * Retrieves the {@link FdFaceBean} object from fd_face.image_md5 field.
* FK_NAME:fd_face_ibfk_1 * @param bean the {@link FdImageBean} * @return the associated {@link FdFaceBean} beans * @throws DaoException */ public List getFaceBeansByImageMd5AsList(FdImageBean bean) throws DaoException { return getFaceBeansByImageMd5AsList(bean,1,-1); } //3.2.2 GET IMPORTED /** * Retrieves the {@link FdFaceBean} object from fd_face.image_md5 field.
* FK_NAME:fd_face_ibfk_1 * @param md5 String - PK# 1 * @return the associated {@link FdFaceBean} beans * @throws DaoException */ public List getFaceBeansByImageMd5AsList(String md5OfImage) throws DaoException { FdImageBean bean = createBean(); bean.setMd5(md5OfImage); return getFaceBeansByImageMd5AsList(bean); } //3.2.4 GET IMPORTED /** * Retrieves the {@link FdFaceBean} object from fd_face.image_md5 field, * given the start row and number of rows.
* FK_NAME:fd_face_ibfk_1 * @param bean the {@link FdImageBean} * @param startRow the start row to be used (first row = 1, last row=-1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @return the associated {@link FdFaceBean} beans * @throws DaoException */ public List getFaceBeansByImageMd5AsList(FdImageBean bean,int startRow, int numRows) throws DaoException { if(null == bean){ return new java.util.ArrayList(); } FdFaceBean other = new FdFaceBean(); other.setImageMd5(bean.getMd5()); return instanceOfFdFaceManager().loadUsingTemplateAsList(other,startRow,numRows); } //3.3 SET IMPORTED /** * set the {@link FdFaceBean} object array associate to FdImageBean by the fd_face.image_md5 field.
* FK_NAME : fd_face_ibfk_1 * @param bean the referenced {@link FdImageBean} * @param importedBeans imported beans from fd_face * @return importedBeans always * @throws DaoException * @see {@link FdFaceManager#setReferencedByImageMd5(FdFaceBean, FdImageBean) */ public FdFaceBean[] setFaceBeansByImageMd5(FdImageBean bean , FdFaceBean[] importedBeans) throws DaoException { if(null != importedBeans){ for( FdFaceBean importBean : importedBeans ){ instanceOfFdFaceManager().setReferencedByImageMd5(importBean , bean); } } return importedBeans; } //3.4 SET IMPORTED /** * set the {@link FdFaceBean} object collection associate to FdImageBean by the fd_face.image_md5 field.
* FK_NAME:fd_face_ibfk_1 * @param bean the referenced {@link FdImageBean} * @param importedBeans imported beans from fd_face * @return importedBeans always * @throws DaoException * @see {@link FdFaceManager#setReferencedByImageMd5(FdFaceBean, FdImageBean) */ public > C setFaceBeansByImageMd5(FdImageBean bean , C importedBeans) throws DaoException { if(null != importedBeans){ for( FdFaceBean importBean : importedBeans ){ instanceOfFdFaceManager().setReferencedByImageMd5(importBean , bean); } } return importedBeans; } //3.5 SYNC SAVE /** * Save the FdImageBean bean and referenced beans and imported beans into the database. * * @param bean the {@link FdImageBean} bean to be saved * @param impFaceByImageMd5 the {@link FdFaceBean} beans refer to {@link FdImageBean} * @return the inserted or updated {@link FdImageBean} bean * @throws DaoException */ public FdImageBean save(FdImageBean bean , FdFaceBean[] impFaceByImageMd5 ) throws DaoException { if(null == bean) { return null; } bean = this.save( bean ); this.setFaceBeansByImageMd5(bean,impFaceByImageMd5); instanceOfFdFaceManager().save( impFaceByImageMd5 ); return bean; } //3.6 SYNC SAVE AS TRANSACTION /** * Transaction version for sync save * @see {@link #save(FdImageBean , FdFaceBean[] )} */ public FdImageBean saveAsTransaction(final FdImageBean bean ,final FdFaceBean[] impFaceByImageMd5 ) throws DaoException { return this.runAsTransaction(new Callable(){ @Override public FdImageBean call() throws Exception { return save(bean , impFaceByImageMd5 ); }}); } //3.7 SYNC SAVE /** * Save the FdImageBean bean and referenced beans and imported beans into the database. * * @param bean the {@link FdImageBean} bean to be saved * @param impFaceByImageMd5 the {@link FdFaceBean} bean refer to {@link FdImageBean} * @return the inserted or updated {@link FdImageBean} bean * @throws DaoException */ public FdImageBean save(FdImageBean bean , java.util.Collection impFaceByImageMd5 ) throws DaoException { if(null == bean) { return null; } bean = this.save( bean ); this.setFaceBeansByImageMd5(bean,impFaceByImageMd5); instanceOfFdFaceManager().save( impFaceByImageMd5 ); return bean; } //3.8 SYNC SAVE AS TRANSACTION /** * Transaction version for sync save * @see {@link #save(FdImageBean , java.util.Collection )} */ public FdImageBean saveAsTransaction(final FdImageBean bean ,final java.util.Collection impFaceByImageMd5 ) throws DaoException { return this.runAsTransaction(new Callable(){ @Override public FdImageBean call() throws Exception { return save(bean , impFaceByImageMd5 ); }}); } private static final int SYNC_SAVE_ARG_LEN = 1; private static final int SYNC_SAVE_ARG_0 = 0; //3.9 SYNC SAVE /** * Save the FdImageBean bean and referenced beans and imported beans (array) into the database. * * @param bean the {@link FdImageBean} bean to be saved * @param args referenced beans or imported beans
* see also {@link #save(FdImageBean , FdFaceBean[] )} * @return the inserted or updated {@link FdImageBean} bean * @throws DaoException */ @Override public FdImageBean save(FdImageBean bean,Object ...inputs) throws DaoException { if(null == inputs){ return save(bean); } if(inputs.length > SYNC_SAVE_ARG_LEN){ throw new IllegalArgumentException("too many dynamic arguments,max dynamic arguments number: 1"); } Object[] args = new Object[SYNC_SAVE_ARG_LEN]; System.arraycopy(inputs, 0, args, 0, inputs.length); if( null != args[SYNC_SAVE_ARG_0] && !(args[SYNC_SAVE_ARG_0] instanceof FdFaceBean[])){ throw new IllegalArgumentException("invalid type for the No.1 dynamic argument,expected type:FdFaceBean[]"); } return save(bean, (FdFaceBean[])args[SYNC_SAVE_ARG_0]); } //3.10 SYNC SAVE /** * Save the FdImageBean bean and referenced beans and imported beans (collection) into the database. * * @param bean the {@link FdImageBean} bean to be saved * @param args referenced beans or imported beans
* see also {@link #save(FdImageBean , java.util.Collection )} * @return the inserted or updated {@link FdImageBean} bean * @throws DaoException */ @SuppressWarnings("unchecked") @Override public FdImageBean saveCollection(FdImageBean bean,Object ...inputs) throws DaoException { if(null == inputs){ return save(bean); } if(inputs.length > SYNC_SAVE_ARG_LEN){ throw new IllegalArgumentException("too many dynamic arguments,max dynamic arguments number: 1"); } Object[] args = new Object[SYNC_SAVE_ARG_LEN]; System.arraycopy(inputs, 0, args, 0, inputs.length); if( null != args[SYNC_SAVE_ARG_0] && !(args[SYNC_SAVE_ARG_0] instanceof java.util.Collection)){ throw new IllegalArgumentException("invalid type for the No.1 argument,expected type:java.util.Collection"); } return save(bean, (java.util.Collection)args[SYNC_SAVE_ARG_0]); } ////////////////////////////////////// // SQL 'WHERE' METHOD ////////////////////////////////////// //11 /** * Deletes rows from the fd_image table using a 'where' clause. * It is up to you to pass the 'WHERE' in your where clauses. *
Attention, if 'WHERE' is omitted it will delete all records. * * @param where the sql 'where' clause * @return the number of deleted rows * @throws DaoException */ @Override public int deleteByWhere(String where) throws DaoException { if( !this.listenerContainer.isEmpty()){ final DeleteBeanAction action = new DeleteBeanAction(); this.loadByWhere(where,action); return action.getCount(); } Connection c = null; PreparedStatement ps = null; try { c = this.getConnection(); StringBuilder sql = new StringBuilder("DELETE FROM fd_image " + where); // System.out.println("deleteByWhere: " + sql); ps = c.prepareStatement(sql.toString()); return ps.executeUpdate(); } catch(SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(c); } } //_____________________________________________________________________ // // SAVE //_____________________________________________________________________ //13 @Override public FdImageBean insert(FdImageBean bean) throws DaoException { // mini checks if (null == bean || !bean.isModified()) { return bean; } if (!bean.isNew()){ return this.update(bean); } Connection c = null; PreparedStatement ps = null; StringBuilder sql = null; try { c = this.getConnection(); // listener callback this.listenerContainer.beforeInsert(bean); int dirtyCount = 0; sql = new StringBuilder("INSERT into fd_image ("); if (bean.checkMd5Modified()) { if (dirtyCount>0) { sql.append(","); } sql.append("md5"); dirtyCount++; } if (bean.checkFormatModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("format"); dirtyCount++; } if (bean.checkWidthModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("width"); dirtyCount++; } if (bean.checkHeightModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("height"); dirtyCount++; } if (bean.checkDepthModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("depth"); dirtyCount++; } if (bean.checkFaceNumModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("face_num"); dirtyCount++; } if (bean.checkUpdateTimeModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("update_time"); dirtyCount++; } if (bean.checkCreateTimeModified()) { if (dirtyCount>0) { sql.append(","); } sql.append("create_time"); dirtyCount++; } sql.append(") values ("); if(dirtyCount > 0) { sql.append("?"); for(int i = 1; i < dirtyCount; i++) { sql.append(",?"); } } sql.append(")"); // System.out.println("insert : " + sql.toString()); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.fillPreparedStatement(ps, bean, SEARCH_EXACT,true); ps.executeUpdate(); bean.isNew(false); bean.resetIsModified(); // listener callback this.listenerContainer.afterInsert(bean); return bean; } catch(SQLException e) { throw new DataAccessException(e); } finally { // listener callback this.listenerContainer.done(); sql = null; this.getManager().close(ps); this.freeConnection(c); } } //14 @Override public FdImageBean update(FdImageBean bean) throws DaoException { // mini checks if (null == bean || !bean.isModified()) { return bean; } if (bean.isNew()){ return this.insert(bean); } Connection c = null; PreparedStatement ps = null; StringBuilder sql = null; try { c = this.getConnection(); // listener callback this.listenerContainer.beforeUpdate(bean); sql = new StringBuilder("UPDATE fd_image SET "); boolean useComma=false; if (bean.checkMd5Modified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("md5=?"); } if (bean.checkFormatModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("format=?"); } if (bean.checkWidthModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("width=?"); } if (bean.checkHeightModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("height=?"); } if (bean.checkDepthModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("depth=?"); } if (bean.checkFaceNumModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("face_num=?"); } if (bean.checkUpdateTimeModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("update_time=?"); } if (bean.checkCreateTimeModified()) { if (useComma) { sql.append(", "); } else { useComma=true; } sql.append("create_time=?"); } sql.append(" WHERE "); sql.append("md5=?"); // System.out.println("update : " + sql.toString()); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); int dirtyCount = this.fillPreparedStatement(ps, bean, SEARCH_EXACT,true); if (dirtyCount == 0) { // System.out.println("The bean to look is not initialized... do not update."); return bean; } if (bean.getMd5() == null) { ps.setNull(++dirtyCount, Types.CHAR); } else { ps.setString(++dirtyCount, bean.getMd5()); } ps.executeUpdate(); // listener callback this.listenerContainer.afterUpdate(bean); bean.resetIsModified(); return bean; } catch(SQLException e) { throw new DataAccessException(e); } finally { // listener callback this.listenerContainer.done(); sql = null; this.getManager().close(ps); this.freeConnection(c); } } //_____________________________________________________________________ // // USING TEMPLATE //_____________________________________________________________________ //18 @Override public FdImageBean loadUniqueUsingTemplate(FdImageBean bean) throws DaoException { List beans = this.loadUsingTemplateAsList(bean); switch(beans.size()){ case 0: return null; case 1: return beans.get(0); default: throw new ObjectRetrievalException("More than one element !!"); } } //18-1 @Override public FdImageBean loadUniqueUsingTemplateChecked(FdImageBean bean) throws DaoException { List beans = this.loadUsingTemplateAsList(bean); switch(beans.size()){ case 0: throw new ObjectRetrievalException("Not found element !!"); case 1: return beans.get(0); default: throw new ObjectRetrievalException("More than one element !!"); } } //20-5 @Override public int loadUsingTemplate(FdImageBean bean, int[] fieldList, int startRow, int numRows,int searchType, Action action) throws DaoException { // System.out.println("loadUsingTemplate startRow:" + startRow + ", numRows:" + numRows + ", searchType:" + searchType); StringBuilder sqlWhere = new StringBuilder(""); String sql=createSelectSql(fieldList,this.fillWhere(sqlWhere, bean, searchType) > 0?" WHERE "+sqlWhere.toString():null); PreparedStatement ps = null; Connection connection = null; try { connection = this.getConnection(); ps = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); this.fillPreparedStatement(ps, bean, searchType,false); return this.loadByPreparedStatement(ps, fieldList, startRow, numRows, action); } catch (DaoException e) { throw e; }catch (SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(connection); } } //21 @Override public int deleteUsingTemplate(FdImageBean bean) throws DaoException { if(bean.checkMd5Initialized() && null != bean.getMd5()){ return this.deleteByPrimaryKey(bean.getMd5()); } if( !this.listenerContainer.isEmpty()){ final DeleteBeanAction action=new DeleteBeanAction(); this.loadUsingTemplate(bean,action); return action.getCount(); } Connection c = null; PreparedStatement ps = null; StringBuilder sql = new StringBuilder("DELETE FROM fd_image "); StringBuilder sqlWhere = new StringBuilder(""); try { if (this.fillWhere(sqlWhere, bean, SEARCH_EXACT) > 0) { sql.append(" WHERE ").append(sqlWhere); } else { // System.out.println("The bean to look is not initialized... deleting all"); } // System.out.println("deleteUsingTemplate: " + sql.toString()); c = this.getConnection(); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.fillPreparedStatement(ps, bean, SEARCH_EXACT, false); return ps.executeUpdate(); } catch(SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(c); sql = null; sqlWhere = null; } } //_____________________________________________________________________ // // COUNT //_____________________________________________________________________ //25 @Override public int countWhere(String where) throws DaoException { String sql = new StringBuffer("SELECT COUNT(*) AS MCOUNT FROM fl_person ") .append(null == where ? "" : where).toString(); // System.out.println("countWhere: " + sql); Connection c = null; Statement st = null; ResultSet rs = null; try { int iReturn = -1; c = this.getConnection(); st = c.createStatement(); rs = st.executeQuery(sql); if (rs.next()) { iReturn = rs.getInt("MCOUNT"); } if (iReturn != -1) { return iReturn; } } catch(SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(st, rs); this.freeConnection(c); sql = null; } throw new DataAccessException("Error in countWhere where=[" + where + "]"); } //26 /** * Retrieves the number of rows of the table fd_image with a prepared statement. * * @param ps the PreparedStatement to be used * @return the number of rows returned * @throws DaoException */ private int countByPreparedStatement(PreparedStatement ps) throws DaoException { ResultSet rs = null; try { int iReturn = -1; rs = ps.executeQuery(); if (rs.next()) { iReturn = rs.getInt("MCOUNT"); } if (iReturn != -1) { return iReturn; } } catch(SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(rs); } throw new DataAccessException("Error in countByPreparedStatement"); } //20 /** * count the number of elements of a specific FdImageBean bean given the search type * * @param bean the FdImageBean template to look for * @param searchType exact ? like ? starting like ? * @return the number of rows returned * @throws DaoException */ @Override public int countUsingTemplate(FdImageBean bean, int searchType) throws DaoException { Connection c = null; PreparedStatement ps = null; StringBuilder sql = new StringBuilder("SELECT COUNT(*) AS MCOUNT FROM fd_image"); StringBuilder sqlWhere = new StringBuilder(""); try { if (this.fillWhere(sqlWhere, bean, SEARCH_EXACT) > 0) { sql.append(" WHERE ").append(sqlWhere); } else { // System.out.println("The bean to look is not initialized... counting all..."); } // System.out.println("countUsingTemplate: " + sql.toString()); c = this.getConnection(); ps = c.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.fillPreparedStatement(ps, bean, searchType,false); return this.countByPreparedStatement(ps); } catch(SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(c); sql = null; sqlWhere = null; } } /** * fills the given StringBuilder with the sql where clauses constructed using the bean and the search type * @param sqlWhere the StringBuilder that will be filled * @param bean the bean to use for creating the where clauses * @param searchType exact ? like ? starting like ? * @return the number of clauses returned */ protected int fillWhere(StringBuilder sqlWhere, FdImageBean bean, int searchType) { if (bean == null) { return 0; } int dirtyCount = 0; String sqlEqualsOperation = searchType == SEARCH_EXACT ? "=" : " like "; try { if (bean.checkMd5Modified()) { dirtyCount ++; if (bean.getMd5() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("md5 IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("md5 ").append(sqlEqualsOperation).append("?"); } } if (bean.checkFormatModified()) { dirtyCount ++; if (bean.getFormat() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("format IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("format ").append(sqlEqualsOperation).append("?"); } } if (bean.checkWidthModified()) { dirtyCount ++; if (bean.getWidth() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("width IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("width = ?"); } } if (bean.checkHeightModified()) { dirtyCount ++; if (bean.getHeight() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("height IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("height = ?"); } } if (bean.checkDepthModified()) { dirtyCount ++; if (bean.getDepth() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("depth IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("depth = ?"); } } if (bean.checkFaceNumModified()) { dirtyCount ++; if (bean.getFaceNum() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("face_num IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("face_num = ?"); } } if (bean.checkUpdateTimeModified()) { dirtyCount ++; if (bean.getUpdateTime() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("update_time IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("update_time = ?"); } } if (bean.checkCreateTimeModified()) { dirtyCount ++; if (bean.getCreateTime() == null) { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("create_time IS NULL"); } else { sqlWhere.append((sqlWhere.length() == 0) ? " " : " AND ").append("create_time = ?"); } } } finally { sqlEqualsOperation = null; } return dirtyCount; } /** * fill the given prepared statement with the bean values and a search type * @param ps the PreparedStatement that will be filled * @param bean the bean to use for creating the where clauses * @param searchType exact ? like ? starting like ? * @return the number of clauses returned * @throws DaoException */ protected int fillPreparedStatement(PreparedStatement ps, FdImageBean bean, int searchType,boolean fillNull) throws DaoException { if (bean == null) { return 0; } int dirtyCount = 0; try { if (bean.checkMd5Modified()) { switch (searchType) { case SEARCH_EXACT: // System.out.println("Setting for " + dirtyCount + " [" + bean.getMd5() + "]"); if (bean.getMd5() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.CHAR);} } else { ps.setString(++dirtyCount, bean.getMd5()); } break; case SEARCH_LIKE: // System.out.println("Setting for " + dirtyCount + " [%" + bean.getMd5() + "%]"); if ( bean.getMd5() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.CHAR);} } else { ps.setString(++dirtyCount, SQL_LIKE_WILDCARD + bean.getMd5() + SQL_LIKE_WILDCARD); } break; case SEARCH_STARTING_LIKE: // System.out.println("Setting for " + dirtyCount + " [%" + bean.getMd5() + "]"); if ( bean.getMd5() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.CHAR);} } else { ps.setString(++dirtyCount, SQL_LIKE_WILDCARD + bean.getMd5()); } break; case SEARCH_ENDING_LIKE: // System.out.println("Setting for " + dirtyCount + " [" + bean.getMd5() + "%]"); if (bean.getMd5() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.CHAR);} } else { ps.setString(++dirtyCount, bean.getMd5() + SQL_LIKE_WILDCARD); } break; default: throw new DaoException("Unknown search type " + searchType); } } if (bean.checkFormatModified()) { switch (searchType) { case SEARCH_EXACT: // System.out.println("Setting for " + dirtyCount + " [" + bean.getFormat() + "]"); if (bean.getFormat() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.VARCHAR);} } else { ps.setString(++dirtyCount, bean.getFormat()); } break; case SEARCH_LIKE: // System.out.println("Setting for " + dirtyCount + " [%" + bean.getFormat() + "%]"); if ( bean.getFormat() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.VARCHAR);} } else { ps.setString(++dirtyCount, SQL_LIKE_WILDCARD + bean.getFormat() + SQL_LIKE_WILDCARD); } break; case SEARCH_STARTING_LIKE: // System.out.println("Setting for " + dirtyCount + " [%" + bean.getFormat() + "]"); if ( bean.getFormat() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.VARCHAR);} } else { ps.setString(++dirtyCount, SQL_LIKE_WILDCARD + bean.getFormat()); } break; case SEARCH_ENDING_LIKE: // System.out.println("Setting for " + dirtyCount + " [" + bean.getFormat() + "%]"); if (bean.getFormat() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.VARCHAR);} } else { ps.setString(++dirtyCount, bean.getFormat() + SQL_LIKE_WILDCARD); } break; default: throw new DaoException("Unknown search type " + searchType); } } if (bean.checkWidthModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getWidth() + "]"); if (bean.getWidth() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.INTEGER);} } else { Manager.setInteger(ps, ++dirtyCount, bean.getWidth()); } } if (bean.checkHeightModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getHeight() + "]"); if (bean.getHeight() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.INTEGER);} } else { Manager.setInteger(ps, ++dirtyCount, bean.getHeight()); } } if (bean.checkDepthModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getDepth() + "]"); if (bean.getDepth() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.INTEGER);} } else { Manager.setInteger(ps, ++dirtyCount, bean.getDepth()); } } if (bean.checkFaceNumModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getFaceNum() + "]"); if (bean.getFaceNum() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.INTEGER);} } else { Manager.setInteger(ps, ++dirtyCount, bean.getFaceNum()); } } if (bean.checkUpdateTimeModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getUpdateTime() + "]"); if (bean.getUpdateTime() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.TIMESTAMP);} } else { ps.setTimestamp(++dirtyCount, new java.sql.Timestamp(bean.getUpdateTime().getTime())); } } if (bean.checkCreateTimeModified()) { // System.out.println("Setting for " + dirtyCount + " [" + bean.getCreateTime() + "]"); if (bean.getCreateTime() == null) {if(fillNull){ ps.setNull(++dirtyCount, Types.TIMESTAMP);} } else { ps.setTimestamp(++dirtyCount, new java.sql.Timestamp(bean.getCreateTime().getTime())); } } } catch(SQLException e) { throw new DataAccessException(e); } return dirtyCount; } //_____________________________________________________________________ // // DECODE RESULT SET //_____________________________________________________________________ //28 /** * decode a resultset in an array of FdImageBean objects * * @param rs the resultset to decode * @param fieldList table of the field's associated constants * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @return the resulting FdImageBean table * @throws DaoException */ public FdImageBean[] decodeResultSet(ResultSet rs, int[] fieldList, int startRow, int numRows) throws DaoException { return this.decodeResultSetAsList(rs, fieldList, startRow, numRows).toArray(new FdImageBean[0]); } //28-1 /** * decode a resultset in a list of FdImageBean objects * * @param rs the resultset to decode * @param fieldList table of the field's associated constants * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @return the resulting FdImageBean table * @throws DaoException */ public List decodeResultSetAsList(ResultSet rs, int[] fieldList, int startRow, int numRows) throws DaoException { ListAction action = new ListAction(); actionOnResultSet(rs, fieldList, numRows, numRows, action); return action.getList(); } //28-2 /** decode a resultset and call action * @param rs the resultset to decode * @param fieldList table of the field's associated constants * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @param action interface obj for do something * @return the count dealt by action * @throws DaoException * @throws IllegalArgumentException */ public int actionOnResultSet(ResultSet rs, int[] fieldList, int startRow, int numRows, Action action) throws DaoException{ try{ int count = 0; if(0!=numRows){ if( startRow<1 ){ throw new IllegalArgumentException("invalid argument:startRow (must >=1)"); } if( null==action || null==rs ){ throw new IllegalArgumentException("invalid argument:action OR rs (must not be null)"); } for(;startRow > 1 && rs.next();){ --startRow; //skip to last of startRow } if (fieldList == null) { if(numRows<0){ for(;rs.next();++count){ action.call(decodeRow(rs, action.getBean())); } }else{ for(;rs.next() && count loadByPreparedStatementAsList(PreparedStatement ps) throws DaoException { return this.loadByPreparedStatementAsList(ps, null); } //33 /** * Loads all the elements using a prepared statement specifying a list of fields to be retrieved. * * @param ps the PreparedStatement to be used * @param fieldList table of the field's associated constants * @return an array of FdImageBean * @throws DaoException */ public FdImageBean[] loadByPreparedStatement(PreparedStatement ps, int[] fieldList) throws DaoException { return this.loadByPreparedStatementAsList(ps, fieldList).toArray(new FdImageBean[0]); } //33 /** * Loads all the elements using a prepared statement specifying a list of fields to be retrieved. * * @param ps the PreparedStatement to be used * @param fieldList table of the field's associated constants * @return an array of FdImageBean * @throws DaoException */ public List loadByPreparedStatementAsList(PreparedStatement ps, int[] fieldList) throws DaoException { return loadByPreparedStatementAsList(ps,fieldList,1,-1); } //34 /** * Loads all the elements using a prepared statement specifying a list of fields to be retrieved, * and specifying the start row and the number of rows. * * @param ps the PreparedStatement to be used * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @param fieldList table of the field's associated constants * @return an array of FdImageBean * @throws DaoException */ public FdImageBean[] loadByPreparedStatement(PreparedStatement ps, int[] fieldList, int startRow, int numRows) throws DaoException { return loadByPreparedStatementAsList(ps,fieldList,startRow,numRows).toArray(new FdImageBean[0]); } //34-1 /** * Loads all the elements using a prepared statement specifying a list of fields to be retrieved, * and specifying the start row and the number of rows. * * @param ps the PreparedStatement to be used * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @param fieldList table of the field's associated constants * @return an array of FdImageBean * @throws DaoException */ public List loadByPreparedStatementAsList(PreparedStatement ps, int[] fieldList, int startRow, int numRows) throws DaoException { ListAction action = new ListAction(); loadByPreparedStatement(ps,fieldList,startRow,numRows,action); return action.getList(); } //34-2 /** * Loads each element using a prepared statement specifying a list of fields to be retrieved, * and specifying the start row and the number of rows * and dealt by action. * * @param ps the PreparedStatement to be used * @param startRow the start row to be used (first row = 1, last row = -1) * @param numRows the number of rows to be retrieved (all rows = a negative number) * @param fieldList table of the field's associated constants * @param action Action object for do something(not null) * @return the count dealt by action * @throws DaoException */ public int loadByPreparedStatement(PreparedStatement ps, int[] fieldList, int startRow, int numRows,Action action) throws DaoException { ResultSet rs = null; try { ps.setFetchSize(100); rs = ps.executeQuery(); return this.actionOnResultSet(rs, fieldList, startRow, numRows, action); } catch (DaoException e) { throw e; } catch (SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(rs); } } //_____________________________________________________________________ // // LISTENER //_____________________________________________________________________ private final TableListener.ListenerContainer listenerContainer = new TableListener.ListenerContainer(); //35 @Override public TableListener registerListener(TableListener listener) { this.listenerContainer.add(listener); return listener; } //36 /** * remove listener. */ @Override public void unregisterListener(TableListener listener) { this.listenerContainer.remove(listener); } //37 @Override public void fire(TableListener.Event event, FdImageBean bean) throws DaoException{ if(null == event){ throw new NullPointerException(); } event.fire(listenerContainer, bean); } //37-1 @Override public void fire(int event, FdImageBean bean) throws DaoException{ try{ fire(TableListener.Event.values()[event],bean); }catch(ArrayIndexOutOfBoundsException e){ throw new IllegalArgumentException("invalid event id " + event); } } //37-2 /** * bind foreign key listener to foreign table:
*/ public void bindForeignKeyListenerForDeleteRule(){ } //37-3 /** * unbind foreign key listener from all of foreign tables
* @see #bindForeignKeyListenerForDeleteRule() */ public void unbindForeignKeyListenerForDeleteRule(){ } //_____________________________________________________________________ // // UTILS //_____________________________________________________________________ //40 /** * Retrieves the manager object used to get connections. * * @return the manager used */ private Manager getManager() { return Manager.getInstance(); } //41 /** * Frees the connection. * * @param c the connection to release */ private void freeConnection(Connection c) { // back to pool this.getManager().releaseConnection(c); } //42 /** * Gets the connection. */ private Connection getConnection() throws DaoException { try { return this.getManager().getConnection(); } catch(SQLException e) { throw new DataAccessException(e); } } //43 @Override public boolean isPrimaryKey(String column){ for(String c:PRIMARYKEY_NAMES){ if(c.equalsIgnoreCase(column)){ return true; } } return false; } /** * Fill the given prepared statement with the values in argList * @param ps the PreparedStatement that will be filled * @param argList the arguments to use fill given prepared statement * @throws DaoException */ private void fillPrepareStatement(PreparedStatement ps, Object[] argList) throws DaoException{ try { if (!(argList == null || ps == null)) { for (int i = 0; i < argList.length; i++) { if (argList[i].getClass().equals(byte[].class)) { ps.setBytes(i + 1, (byte[]) argList[i]); } else { ps.setObject(i + 1, argList[i]); } } } } catch (SQLException e) { throw new DaoException(e); } } @Override public int loadBySqlForAction(String sql, Object[] argList, int[] fieldList,int startRow, int numRows,Action action) throws DaoException{ PreparedStatement ps = null; Connection connection = null; // logger.debug("sql string:\n" + sql + "\n"); try { connection = this.getConnection(); ps = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); fillPrepareStatement(ps, argList); return this.loadByPreparedStatement(ps, fieldList, startRow, numRows, action); } catch (DaoException e) { throw e; }catch (SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(connection); } } @Override public T runAsTransaction(Callable fun) throws DaoException{ return Manager.getInstance().runAsTransaction(fun); } class DeleteBeanAction extends Action.BaseAdapter{ private final AtomicInteger count=new AtomicInteger(0); @Override public void call(FdImageBean bean) throws DaoException { FdImageManager.this.delete(bean); count.incrementAndGet(); } int getCount(){ return count.get(); } } //45 /** * return a primary key list from {@link FdImageBean} array * @param array */ public List toPrimaryKeyList(FdImageBean... array){ if(null == array){ return new java.util.ArrayList(); } java.util.ArrayList list = new java.util.ArrayList(array.length); for(FdImageBean bean:array){ list.add(null == bean ? null : bean.getMd5()); } return list; } //46 /** * return a primary key list from {@link FdImageBean} collection * @param array */ public List toPrimaryKeyList(java.util.Collection collection){ if(null == collection){ return new java.util.ArrayList(); } java.util.ArrayList list = new java.util.ArrayList(collection.size()); for(FdImageBean bean:collection){ list.add(null == bean ? null : bean.getMd5()); } return list; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy