1 13 14 21 22 package org.jahia.services.containers; 23 24 import org.jahia.data.JahiaDBDOMObject; 25 import org.jahia.data.JahiaDOMObject; 26 import org.jahia.data.containers.JahiaContainerStructure; 27 import org.jahia.data.containers.JahiaContainerSubDefinition; 28 import org.jahia.exceptions.JahiaException; 29 import org.jahia.services.database.ConnectionDispenser; 30 31 import java.sql.Connection ; 32 import java.sql.PreparedStatement ; 33 import java.sql.ResultSet ; 34 import java.sql.SQLException ; 35 import java.sql.Statement ; 36 import java.util.Enumeration ; 37 import java.util.Vector ; 38 39 40 47 public class JahiaContainerStructuresDB { 48 49 private static org.apache.log4j.Logger logger = 50 org.apache.log4j.Logger.getLogger (JahiaContainerStructuresDB.class); 51 52 53 56 public JahiaContainerStructuresDB () { 57 } 58 59 60 70 public Vector db_load_container_structure (int subDefID) 71 throws JahiaException { 72 Connection dbConn = null; 73 PreparedStatement stmt = null; 74 ResultSet rs = null; 75 Vector theStructure = new Vector (); 76 try { 77 String sqlQuery = "SELECT * FROM jahia_ctn_struct WHERE ctnsubdefid_jahia_ctn_struct=? ORDER BY rank_jahia_ctn_struct ASC"; 78 79 dbConn = ConnectionDispenser.getConnection (); 80 stmt = dbConn.prepareStatement (sqlQuery); 81 stmt.setInt(1, subDefID); 82 rs = stmt.executeQuery (); 83 84 while (rs.next ()) { 85 int ctnsubdefid = rs.getInt ("ctnsubdefid_jahia_ctn_struct"); 86 int objTypeID = rs.getInt ("objtype_jahia_ctn_struct"); 87 int objDefID = rs.getInt ("objdefid_jahia_ctn_struct"); 88 int rank = rs.getInt ("rank_jahia_ctn_struct"); 89 theStructure.add (new JahiaContainerStructure ( 90 ctnsubdefid, objTypeID, objDefID, rank)); 91 } 92 93 } catch (SQLException se) { 94 String errorMsg = "Error in db_load_container_structure : " + se.getMessage () + " -> BAILING OUT"; 95 logger.error (errorMsg); 96 throw new JahiaException ("Cannot load structures from the database", 97 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, 98 se); 99 } finally { 100 closeStatement(stmt); 101 } 102 if (theStructure.size () > 0) { 103 return theStructure; 104 } else { 105 return null; 106 } 107 } 109 110 117 public void db_create_container_structure (JahiaContainerSubDefinition theSubDef) 118 throws JahiaException { 119 Connection dbConn = null; 120 PreparedStatement stmt = null; 121 String sqlQuery = 122 "INSERT INTO jahia_ctn_struct (ctnsubdefid_jahia_ctn_struct, objtype_jahia_ctn_struct, objdefid_jahia_ctn_struct, rank_jahia_ctn_struct) VALUES(?,?,?,?)"; 123 124 try { 125 dbConn = ConnectionDispenser.getConnection (); 127 128 stmt = dbConn.prepareStatement (sqlQuery); 129 130 Vector structList = theSubDef.getStructure (); 132 if (structList != null) { 133 Enumeration structure = structList.elements (); 134 while (structure.hasMoreElements ()) { 135 JahiaContainerStructure theStruct = (JahiaContainerStructure) structure.nextElement (); 136 theStruct.setSubctndefid (theSubDef.getID ()); 137 stmt.setInt(1, theSubDef.getID ()); 138 stmt.setInt(2, theStruct.getObjectType ()); 139 stmt.setInt(3, theStruct.getObjectDefID ()); 140 stmt.setInt(4, theStruct.getRank ()); 141 stmt.executeUpdate (); 142 } 143 } 144 145 } catch (SQLException se) { 146 String errorMsg = "Error in db_create_container_structure : " + se.getMessage () + " -> BAILING OUT"; 147 logger.error (errorMsg); 148 throw new JahiaException ("Cannot create structures in the database", 149 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, 150 se); 151 } finally { 152 closeStatement(stmt); 153 } 154 155 } 157 158 167 public void db_update_container_structure (JahiaContainerSubDefinition theSubDef) 168 throws JahiaException { 169 db_delete_container_structure (theSubDef.getID ()); 171 db_create_container_structure (theSubDef); 173 } 175 176 185 public void db_delete_container_structure (int theSubDefID) 186 throws JahiaException { 187 Connection dbConn = null; 188 Statement stmt = null; 189 String sqlQuery = ""; 190 try { 191 dbConn = ConnectionDispenser.getConnection (); 193 stmt = dbConn.createStatement (); 194 195 sqlQuery = "DELETE FROM jahia_ctn_struct "; 196 sqlQuery += "WHERE ctnsubdefid_jahia_ctn_struct=" + theSubDefID; 197 stmt.executeUpdate (sqlQuery); 198 199 } catch (SQLException se) { 200 String errorMsg = "Error in db_delete_container_structure : " + se.getMessage () + " -> BAILING OUT"; 201 logger.error (errorMsg); 202 throw new JahiaException ("Cannot delete structures in the database", 203 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, 204 se); 205 } finally { 206 closeStatement(stmt); 207 } 208 } 209 210 211 218 public JahiaDOMObject getContainerStructsAsDOM (int siteID) 219 throws JahiaException { 220 221 Connection dbConn = null; 222 Statement statement = null; 223 JahiaDBDOMObject dom = null; 224 225 try { 226 227 String sqlQuery = "SELECT DISTINCT jahia_ctn_struct.ctnsubdefid_jahia_ctn_struct," 228 + "jahia_ctn_struct.objtype_jahia_ctn_struct,jahia_ctn_struct.objdefid_jahia_ctn_struct," 229 + "jahia_ctn_struct.rank_jahia_ctn_struct FROM jahia_ctn_struct,jahia_fields_def,jahia_ctn_def WHERE " 230 + "( jahia_ctn_struct.objtype_jahia_ctn_struct=1 AND jahia_ctn_struct.objdefid_jahia_ctn_struct=" 231 + "jahia_fields_def.id_jahia_fields_def AND jahia_fields_def.jahiaid_jahia_fields_def=" + siteID + " ) " 232 + "OR ( jahia_ctn_struct.objtype_jahia_ctn_struct=2 AND jahia_ctn_struct.objdefid_jahia_ctn_struct=" 233 + "jahia_ctn_def.id_jahia_ctn_def AND jahia_ctn_def.jahiaid_jahia_ctn_def=" + siteID + " )"; 234 235 236 dbConn = ConnectionDispenser.getConnection (); 237 statement = dbConn.createStatement (); 238 if (statement != null) { 239 ResultSet rs = statement.executeQuery (sqlQuery); 240 if (rs != null) { 241 dom = new JahiaDBDOMObject (); 242 dom.addTable ("jahia_ctn_struct", rs); 243 return dom; 244 } 245 } 246 } catch (SQLException se) { 247 String errorMsg = "Error in getContainerStructsAsDOM(int siteID) : " + se.getMessage (); 248 logger.error (errorMsg + " -> BAILING OUT"); 249 throw new JahiaException ("Cannot load container structures from the database", 250 errorMsg, JahiaException.DATABASE_ERROR, 251 JahiaException.CRITICAL_SEVERITY); 252 } finally { 253 closeStatement (statement); 254 } 255 256 return dom; 257 } 258 259 private void closeStatement (Statement statement) { 260 try { 262 if (statement != null) { 263 statement.close (); 264 } 265 266 } catch (SQLException sqlEx) { 267 logger.error ("Cannot close a statement", sqlEx); 268 } 269 } 270 271 272 } 273 | Popular Tags |