📄 folderdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.filemanager;import java.io.*;import java.util.*;import java.sql.*;import org.jahia.tools.files.*;import org.jahia.utils.*; // JahiaConsoleimport org.jahia.exceptions.*; // JahiaInitializationExceptionimport org.jahia.services.*;import org.jahia.registries.*;import org.jahia.data.files.*;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * class FolderDB * * @author khue ng * @version 1.0 */public class FolderDB{ private static boolean m_debugQuery = false; private static FolderDB m_instance = null; /** * Constructor */ protected FolderDB() { } /** * Method getInstance * Give a unique point to get a reference to the unique instance of this class * * @return a reference to the unique instance of this class */ public static FolderDB getInstance() { if ( m_instance == null ) { m_instance = new FolderDB(); } return m_instance; } /** * Method insert * Insert a new row in jahia_filemgr_folders table * * @param the folder object to create * @return true if no exception occurs */ public boolean insert(Folder folder) { boolean success = false; Connection dbConn = null; Statement stmt = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); int id = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahia_filemgr_folders" ); // execute the query String query = " insert into jahia_filemgr_folders (id_jahia_fld,filemanagerid_jahia_fld, parent_jahia_fld, fldname_jahia_fld)" + " values(" + id + "," + folder.getFilemanagerID() + "," + folder.getParentID() + "," + "'" + filter(folder.getFolderName()) + "')"; stmt = dbConn.createStatement(); debugQuery(query); stmt.execute(query); success = true; } catch (SQLException ex) { processSQLException("FolderDB::insert()",ex); } catch (JahiaException je) { JahiaConsole.println("FolderDB::insert()",je.getMessage()); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { processSQLException("FolderDB:: insert() freeing connection fails",ex); } } return success; } /** * Method update * Update a row in jahia_filemgr_folders table * * @param a folder object * @return true if no exception occurs */ public boolean update(Folder folder) { boolean success = false; Connection dbConn = null; Statement stmt = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); // execute the query String query = " update jahia_filemgr_folders set " + "filemanagerid_jahia_fld=" + folder.getFilemanagerID() + "," + "parent_jahia_fld=" + folder.getParentID() + "," + "fldname_jahia_fld='" + filter(folder.getFolderName()) + "'" + " where id_jahia_fld=" + folder.getFolderID(); stmt = dbConn.createStatement(); debugQuery(query); ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query); success = true; } catch (SQLException ex) { processSQLException("FolderDB::update()",ex); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { processSQLException("FolderDB:: update() freeing connection fails",ex); } } return success; } /** * Method delete * Delete a row in jahia_filemgr_folders table * * @param folderID * @return false on any error else true */ public boolean delete(int folderID) { boolean success = false; Connection dbConn = null; Statement stmt = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); // execute the query String query = " delete from jahia_filemgr_folders where id_jahia_fld=" + folderID; stmt = dbConn.createStatement(); debugQuery(query); stmt.execute(query); success = true; } catch (SQLException ex) { processSQLException("FolderDB::delete()",ex); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { processSQLException("FolderDB:: delete() freeing connection fails",ex); } return success; } } /** * Method getFolder * Return a folder looking at it's id * * @param id * @return a folder object else null */ public Folder getFolder(int folderID) { Folder folder = null; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); // execute the query String query = " select * from jahia_filemgr_folders where id_jahia_fld=" + folderID; stmt = dbConn.createStatement(); debugQuery(query); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); while ( rs.next() ) { int filemanagerID = rs.getInt("filemanagerid_jahia_fld"); int parentID = rs.getInt("parent_jahia_fld"); String folderName = rs.getString("fldname_jahia_fld"); folder = new Folder( filemanagerID, parentID, folderName); folder.setFolderID(folderID); } } catch (SQLException ex) { processSQLException("FolderDB::getFolder(id)",ex); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); if ( rs != null ) rs.close(); } catch ( SQLException ex ) { processSQLException("FolderDB::getFolder(folderID) freeing connection fails",ex); } } return folder; } /** * Method getFolderByFile * Return a folder looking at a gived file id * * @param file id * @return a folder object else null */ public Folder getFolderByFile(int fileID) { Folder folder = null; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); // execute the query String query = " select * from jahia_filemgr_files where id_jahia_file=" + fileID; stmt = dbConn.createStatement(); debugQuery(query); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); if ( rs.next() ) { int folderID = rs.getInt("fldid_jahia_file"); try{ if ( rs != null ) rs.close(); if ( stmt != null ) stmt.close(); } catch ( Throwable t ){ } ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); folder = FolderDB.getInstance().getFolder(folderID); } } catch (SQLException ex) { processSQLException("FolderDB::getFolder(id)",ex); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { processSQLException("FolderDB::getFolder(folderID) freeing connection fails",ex); } } return folder; } /** * Method getFoldersByParent * Return a Vector of folder for a gived parent folderID * * @param parent folder id * @return a Vector of folder objects else null */ public Vector getFoldersByParent(int parentID) { Folder folder = null; Vector folders = null; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // get a connection to the db dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(1); // execute the query String query = "select * from jahia_filemgr_folders where bm_parent_id=" + parentID; stmt = dbConn.createStatement(); debugQuery(query); rs =ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); while ( rs.next() ) { int filemanagerID = rs.getInt("filemanagerid_jahia_fld"); int folderID = rs.getInt("fld_folder_id"); String folderName = rs.getString("fldname_jahia_fld"); folder = new Folder( filemanagerID, parentID, folderName); folder.setFolderID(folderID); folders.add(folder); } } catch (SQLException ex) { processSQLException("FolderDB::getFoldersByParent(id)",ex); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { processSQLException("FolderDB:: getFoldersByParent(parentID) freeing connection fails",ex); } } return folders; } /** * Method getFolders * Return a Vector of folders depending on a where query * * @param whereSql a sql where statement * @param orderSql a sql order statement * @return a Vector of folder objects else null */ public Vector getFolders(String whereSql, String orderSql) { Folder folder = null; Vector folders = new Vector(); Connection dbConn = null; Statement stmt = null; ResultSet rs = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -