package vn.mobileid.jackrabbit.uat.db; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DatabaseFunctionImpl implements DatabaseFunction { final static Logger logger = LoggerFactory.getLogger(DatabaseFunctionImpl.class); private static DatabaseFunction instance = null; private static ReentrantLock lock = new ReentrantLock(); private DatabaseFunctionImpl() { } public static DatabaseFunction getInstance() { if(instance != null) return instance; lock.lock(); if(instance != null){ lock.unlock(); return instance; } instance = new DatabaseFunctionImpl(); lock.unlock(); return instance; } @Override public boolean insertFileUUID(int hoten, String prefix, String folderName, String uuid) throws SQLException { // TODO Auto-generated method stub CallableStatement proc_stmt = null; Connection connection = null; boolean response = false; try { connection = DatabaseConnection.getInstance().createConnection(); proc_stmt = connection.prepareCall("{ call ADD_FILE_UUID(?,?,?,?) }"); proc_stmt.setInt(DataBaseFECConstants.pHoTen, hoten); proc_stmt.setString(DataBaseFECConstants.pFileUUID, uuid); proc_stmt.setString(DataBaseFECConstants.pFolderPrefix, prefix); proc_stmt.setString(DataBaseFECConstants.pFolderName, folderName); proc_stmt.executeQuery(); response = true; }finally { if(connection != null) connection.close(); if (proc_stmt != null) proc_stmt.close(); } return response; } @Override public FileManagerConfig getFileManagerConfig(String prefix) throws SQLException { // TODO Auto-generated method stub CallableStatement proc_stmt = null; Connection connection = null; ResultSet rs = null; FileManagerConfig response = null; try{ connection = DatabaseConnection.getInstance().createConnection(); proc_stmt = connection.prepareCall("{ call GET_MAX_INDEX(?) }"); proc_stmt.setString(DataBaseFECConstants.pFolderPrefix, prefix); //proc_stmt.registerOutParameter(DataBaseFECConstants.IndexMax, java.sql.Types.BIGINT); rs = proc_stmt.executeQuery(); response = new FileManagerConfig(); if(rs.first()){ response.setIndex(rs.getInt(DataBaseFECConstants.IndexMax)); response.setCountFolder(rs.getInt(DataBaseFECConstants.countFolder)); response.setFolderName(rs.getString(DataBaseFECConstants.folderName)); } }finally { if(connection != null) connection.close(); if (proc_stmt != null) proc_stmt.close(); if(rs != null) rs.close(); } return response; } /* (non-Javadoc) * @see vn.mobileid.jackrabbit.uat.db.DatabaseFunction#findUUID(long) */ @Override public String findUUID(long index) throws SQLException { // TODO Auto-generated method stub CallableStatement proc_stmt = null; Connection connection = null; ResultSet rs = null; String response = null; try{ connection = DatabaseConnection.getInstance().createConnection(); proc_stmt = connection.prepareCall("{ call FIND_UUID(?) }"); proc_stmt.setLong(DataBaseFECConstants.pTB_INDEX, index); rs = proc_stmt.executeQuery(); if(rs.first()){ response = rs.getString(DataBaseFECConstants.FILE_UUID); } }catch (Exception e) { // TODO: handle exception }finally { if(connection != null) connection.close(); if (proc_stmt != null) proc_stmt.close(); if(rs != null) rs.close(); } return response; } }