1 13 package org.jahia.services.containers; 14 15 import org.jahia.content.ContentObject; 16 import org.jahia.exceptions.JahiaException; 17 import org.jahia.services.version.ContentObjectEntryState; 18 import org.jahia.services.version.EntryStateable; 19 20 import java.sql.*; 21 import java.util.ArrayList ; 22 23 32 33 class ContentContainerDB { 34 35 private static org.apache.log4j.Logger logger = 36 org.apache.log4j.Logger.getLogger (ContentContainerDB.class); 37 38 private static ContentContainerDB singletonInstance = null; 39 40 43 public static synchronized ContentContainerDB getInstance () { 44 if (singletonInstance == null) { 45 singletonInstance = new ContentContainerDB (); 46 } 47 return singletonInstance; 48 } 49 50 52 protected ContentContainer getContainer (int containerID) throws JahiaException { 53 ContentContainer resultContainer = null; 54 Connection dbConn = null; 55 PreparedStatement stmt = null; 56 ResultSet rs = null; 57 58 try { 59 final String sqlQuery = "SELECT DISTINCT jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state>=1"; 62 63 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 64 stmt = dbConn.prepareStatement(sqlQuery); 65 stmt.setInt(1, containerID); 66 rs = stmt.executeQuery (); 68 69 if (rs.next ()) { 71 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries"); 73 int pageID = rs.getInt ("pageid_jahia_ctn_entries"); 74 int listID = rs.getInt ("listid_jahia_ctn_entries"); 75 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries"); 76 int rank = rs.getInt ("rank_jahia_ctn_entries"); 77 int rights = rs.getInt ("rights_jahia_ctn_entries"); 78 int versionID = rs.getInt ("version_id"); 79 int workflowState = rs.getInt ("workflow_state"); 80 81 ArrayList activeAndStagedVersions = new ArrayList (); 82 activeAndStagedVersions.add ( 83 new ContentObjectEntryState (workflowState, versionID, 84 ContentObject.SHARED_LANGUAGE)); 85 86 while (rs.next ()) { 89 versionID = rs.getInt ("version_id"); 90 workflowState = rs.getInt ("workflow_state"); 91 activeAndStagedVersions.add ( 92 new ContentObjectEntryState (workflowState, versionID, 93 ContentObject.SHARED_LANGUAGE)); 94 } 95 96 rs.close(); 97 98 resultContainer = 99 new ContentContainer ( 100 containerID, jahiaID, pageID, ctnDefID, listID, rights, 101 activeAndStagedVersions); 102 } else 103 { 106 final String sqlQuery2 = "SELECT DISTINCT jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?"; 108 rs.close(); 111 stmt.close(); 112 stmt = dbConn.prepareStatement(sqlQuery2); 113 stmt.setInt(1, containerID); 114 rs = stmt.executeQuery (); 115 116 if (rs.next ()) { 118 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries"); 119 int pageID = rs.getInt ("pageid_jahia_ctn_entries"); 120 int listID = rs.getInt ("listid_jahia_ctn_entries"); 121 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries"); 122 int rank = rs.getInt ("rank_jahia_ctn_entries"); 123 int rights = rs.getInt ("rights_jahia_ctn_entries"); 124 int versionID = rs.getInt ("version_id"); 125 int workflowState = rs.getInt ("workflow_state"); 126 resultContainer = 127 new ContentContainer ( 128 containerID, jahiaID, pageID, ctnDefID, listID, rights, 129 new ArrayList ()); 130 } else { 131 140 } 141 } 142 143 } catch (SQLException se) { 144 throw new JahiaException ( 145 "Cannot load container " + containerID + "from the database", 146 se.getMessage (), 147 JahiaException.DATABASE_ERROR, 148 JahiaException.ERROR_SEVERITY, se); 149 } finally { 150 try { 151 152 if (stmt != null) stmt.close (); 153 } catch (SQLException ex) { 154 throw new JahiaException ("Cannot free resources", 155 "Cannot free resources", 156 JahiaException.DATABASE_ERROR, 157 JahiaException.WARNING_SEVERITY, ex); 158 } 159 } 160 161 return resultContainer; 162 } 163 164 protected ArrayList getVersionedEntryStates (int containerID, boolean withActive) 165 throws JahiaException { 166 ArrayList resultEntryStates = new ArrayList (); 167 Connection dbConn = null; 168 Statement stmt = null; 169 ResultSet rs = null; 170 171 final String selectFields = "jahiaid_jahia_ctn_entries," + 172 "pageid_jahia_ctn_entries," + 173 "listid_jahia_ctn_entries," + 174 "ctndefid_jahia_ctn_entries," + 175 "rank_jahia_ctn_entries," + 176 "rights_jahia_ctn_entries," + 177 "version_id," + 178 "workflow_state"; 179 180 try { 181 String sqlQuery = "SELECT " + 184 selectFields + 185 " FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=" + containerID; 186 if (withActive) { 187 sqlQuery += " AND workflow_state<=1"; 188 } else { 189 sqlQuery += " AND workflow_state<=0"; 190 } 191 192 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 193 stmt = dbConn.createStatement (); 194 rs = stmt.executeQuery (sqlQuery); 196 197 if (rs.next ()) { 199 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries"); 201 int pageID = rs.getInt ("pageid_jahia_ctn_entries"); 202 int listID = rs.getInt ("listid_jahia_ctn_entries"); 203 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries"); 204 int rank = rs.getInt ("rank_jahia_ctn_entries"); 205 int rights = rs.getInt ("rights_jahia_ctn_entries"); 206 int versionID = rs.getInt ("version_id"); 207 int workflowState = rs.getInt ("workflow_state"); 208 209 resultEntryStates.add (new ContentObjectEntryState ( 210 workflowState, versionID, ContentObject.SHARED_LANGUAGE)); 211 212 while (rs.next ()) { 215 versionID = rs.getInt ("version_id"); 216 workflowState = rs.getInt ("workflow_state"); 217 resultEntryStates.add (new ContentObjectEntryState ( 218 workflowState, versionID, ContentObject.SHARED_LANGUAGE)); 219 } 220 221 } 222 223 } catch (SQLException se) { 224 throw new JahiaException ( 225 "Cannot load container " + containerID + "from the database", 226 se.getMessage (), 227 JahiaException.DATABASE_ERROR, 228 JahiaException.ERROR_SEVERITY, se); 229 } finally { 230 try { 231 232 if (stmt != null) stmt.close (); 233 } catch (SQLException ex) { 234 throw new JahiaException ("Cannot free resources", 235 "Cannot free resources", 236 JahiaException.DATABASE_ERROR, 237 JahiaException.WARNING_SEVERITY, ex); 238 } 239 } 240 241 return resultEntryStates; 242 } 243 244 protected void deleteEntry (int containerID, EntryStateable entryState) 245 throws JahiaException { 246 247 Connection dbConn = null; 248 PreparedStatement stmt = null; 249 ResultSet rs = null; 250 251 try { 252 String sqlQuery = 253 "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?" + 254 " AND workflow_state=? AND version_id=?"; 255 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 256 stmt = dbConn.prepareStatement (sqlQuery); 257 stmt.setInt (1, containerID); 258 stmt.setInt (2, entryState.getWorkflowState ()); 259 stmt.setInt (3, entryState.getVersionID ()); 260 int resultCount = stmt.executeUpdate (); 261 logger.debug (resultCount + " rows affected by delete of entry"); 262 } catch (SQLException se) { 263 throw new JahiaException ("Error while deleting entry", 264 se.getMessage (), 265 JahiaException.DATABASE_ERROR, 266 JahiaException.ERROR_SEVERITY, se); 267 } finally { 268 try { 269 if (stmt != null) 270 stmt.close (); 271 } catch (SQLException ex) { 272 throw new JahiaException ("Cannot free resources", 273 "Cannot free resources", 274 JahiaException.DATABASE_ERROR, 275 JahiaException.WARNING_SEVERITY, ex); 276 } 277 } 278 } 279 280 295 protected void copyEntry (int containerID, 296 EntryStateable fromEntryState, 297 EntryStateable toEntryState) 298 throws JahiaException { 299 300 Connection dbConn = null; 301 PreparedStatement stmt = null; 302 303 try { 304 String sqlQuery = 305 "SELECT jahiaid_jahia_ctn_entries, " + 306 "pageid_jahia_ctn_entries, " + 307 "listid_jahia_ctn_entries, " + 308 "ctndefid_jahia_ctn_entries, " + 309 "rank_jahia_ctn_entries," + 310 "rights_jahia_ctn_entries " + 311 "FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?" + 312 " AND workflow_state=? AND version_id=?"; 313 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 314 stmt = dbConn.prepareStatement (sqlQuery); 315 stmt.setInt (1, containerID); 316 stmt.setInt (2, fromEntryState.getWorkflowState ()); 317 stmt.setInt (3, fromEntryState.getVersionID ()); 318 ResultSet rs = stmt.executeQuery (); 319 if (rs.next ()) { 320 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries"); 321 int pageID = rs.getInt ("pageid_jahia_ctn_entries"); 322 int listID = rs.getInt ("listid_jahia_ctn_entries"); 323 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries"); 324 int rank = rs.getInt ("rank_jahia_ctn_entries"); 325 int rights = rs.getInt ("rights_jahia_ctn_entries"); 326 stmt.close (); 327 String insertQuery = "INSERT INTO jahia_ctn_entries (" + 328 "id_jahia_ctn_entries, " + 329 "jahiaid_jahia_ctn_entries, " + 330 "pageid_jahia_ctn_entries, " + 331 "listid_jahia_ctn_entries, " + 332 "ctndefid_jahia_ctn_entries, " + 333 "rank_jahia_ctn_entries, " + 334 "rights_jahia_ctn_entries, " + 335 "version_id, " + 336 "workflow_state" + 337 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 338 stmt = dbConn.prepareStatement (insertQuery); 339 stmt.setInt (1, containerID); 340 stmt.setInt (2, jahiaID); 341 stmt.setInt (3, pageID); 342 stmt.setInt (4, listID); 343 stmt.setInt (5, ctnDefID); 344 stmt.setInt (6, rank); 345 stmt.setInt (7, rights); 346 stmt.setInt (8, toEntryState.getVersionID ()); 347 stmt.setInt (9, toEntryState.getWorkflowState ()); 348 stmt.executeUpdate (); 349 } 350 } catch (SQLException se) { 351 throw new JahiaException ("Error while copying entry", 352 se.getMessage (), 353 JahiaException.DATABASE_ERROR, 354 JahiaException.ERROR_SEVERITY, se); 355 } finally { 356 try { 357 if (stmt != null) 358 stmt.close (); 359 } catch (SQLException ex) { 360 throw new JahiaException ("Cannot free resources", 361 "Cannot free resources", 362 JahiaException.DATABASE_ERROR, 363 JahiaException.WARNING_SEVERITY, ex); 364 } 365 } 366 } 367 368 } 369 | Popular Tags |