1 13 package org.jahia.services.containers; 14 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.util.ArrayList ; 20 21 import org.jahia.content.ContentObject; 22 import org.jahia.exceptions.JahiaException; 23 import org.jahia.services.database.ConnectionDispenser; 24 import org.jahia.services.version.ContentObjectEntryState; 25 import org.jahia.services.version.EntryStateable; 26 27 36 class ContentContainerListDB { 37 38 private static org.apache.log4j.Logger logger = 39 org.apache.log4j.Logger.getLogger (ContentContainerListDB.class); 40 41 private static ContentContainerListDB singletonInstance = null; 42 43 46 public static synchronized ContentContainerListDB getInstance () { 47 if (singletonInstance == null) { 48 singletonInstance = new ContentContainerListDB (); 49 } 50 return singletonInstance; 51 } 52 53 55 protected ContentContainerList getContainerList (int containerListID) 56 throws JahiaException { 57 ContentContainerList resultContainerList = null; 58 Connection dbConn = null; 59 PreparedStatement stmt = null; 60 ResultSet rs = null; 61 62 final String selectFields = "parententryid_jahia_ctn_lists,pageid_jahia_ctn_lists,ctndefid_jahia_ctn_lists,rights_jahia_ctn_lists,version_id,workflow_state"; 63 64 try { 65 String sqlQuery = "SELECT " + 68 selectFields + 69 " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state>=1"; 70 71 dbConn = ConnectionDispenser.getConnection (); 72 stmt = dbConn.prepareStatement(sqlQuery); 73 stmt.setInt(1, containerListID); 74 rs = stmt.executeQuery (); 76 77 if (rs.next ()) { 79 int parentContainerID = rs.getInt ("parententryid_jahia_ctn_lists"); 81 int pageID = rs.getInt ("pageid_jahia_ctn_lists"); 82 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists"); 83 int rights = rs.getInt ("rights_jahia_ctn_lists"); 84 int versionID = rs.getInt ("version_id"); 85 int workflowState = rs.getInt ("workflow_state"); 86 87 ArrayList activeAndStagedVersions = new ArrayList (); 88 activeAndStagedVersions.add ( 89 new ContentObjectEntryState (workflowState, versionID, 90 ContentObject.SHARED_LANGUAGE)); 91 92 while (rs.next ()) { 95 versionID = rs.getInt ("version_id"); 96 workflowState = rs.getInt ("workflow_state"); 97 activeAndStagedVersions.add ( 98 new ContentObjectEntryState (workflowState, versionID, 99 ContentObject.SHARED_LANGUAGE)); 100 } 101 102 resultContainerList = 103 new ContentContainerList ( 104 containerListID, parentContainerID, pageID, ctnDefID, rights, 105 activeAndStagedVersions); 106 } else 107 { 110 stmt.close (); 111 sqlQuery = "SELECT " + 113 selectFields + 114 " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=?"; 115 stmt = dbConn.prepareStatement(sqlQuery); 116 stmt.setInt(1, containerListID); 117 rs = stmt.executeQuery (); 118 119 if (rs.next ()) { 121 int parentContainerID = rs.getInt ("parententryid_jahia_ctn_lists"); 122 int pageID = rs.getInt ("pageid_jahia_ctn_lists"); 123 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists"); 124 int rights = rs.getInt ("rights_jahia_ctn_lists"); 125 resultContainerList = 126 new ContentContainerList ( 127 containerListID, parentContainerID, pageID, ctnDefID, 128 rights, new ArrayList ()); 129 130 } else { 131 throw new JahiaException 133 ("Container list " + containerListID + " not found in database!", 134 "Container list " + containerListID + " not found in database!", 135 JahiaException.DATABASE_ERROR, 136 JahiaException.ERROR_SEVERITY); 137 } 138 } 139 140 } catch (SQLException se) { 141 throw new JahiaException ( 142 "Cannot load container list " + containerListID + "from the database", 143 se.getMessage (), 144 JahiaException.DATABASE_ERROR, 145 JahiaException.ERROR_SEVERITY, se); 146 } finally { 147 try { 148 if (stmt != null) 149 stmt.close (); 150 151 } catch (SQLException ex) { 152 throw new JahiaException ("Cannot free resources", 153 "Cannot free resources", 154 JahiaException.DATABASE_ERROR, 155 JahiaException.WARNING_SEVERITY, ex); 156 } 157 } 158 159 return resultContainerList; 160 } 161 162 protected ArrayList getVersionedEntryStates (int containerListID, boolean withActive) 163 throws JahiaException { 164 ArrayList resultEntryStates = new ArrayList (); 165 Connection dbConn = null; 166 PreparedStatement stmt = null; 167 ResultSet rs = null; 168 169 final String selectFields = "version_id,workflow_state"; 170 171 try { 172 String sqlQuery = "SELECT " + selectFields + 175 " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state<=?"; 176 177 dbConn = ConnectionDispenser.getConnection (); 178 stmt = dbConn.prepareStatement (sqlQuery); 179 stmt.setInt(1, containerListID); 180 stmt.setInt(2, withActive ? 1 : 0); 181 rs = stmt.executeQuery (); 183 184 if (rs.next ()) { 186 int versionID = rs.getInt ("version_id"); 188 int workflowState = rs.getInt ("workflow_state"); 189 190 resultEntryStates.add ( 191 new ContentObjectEntryState (workflowState, versionID, 192 ContentObject.SHARED_LANGUAGE)); 193 194 while (rs.next ()) { 197 versionID = rs.getInt ("version_id"); 198 workflowState = rs.getInt ("workflow_state"); 199 resultEntryStates.add ( 200 new ContentObjectEntryState (workflowState, versionID, 201 ContentObject.SHARED_LANGUAGE)); 202 } 203 204 } 205 } catch (SQLException se) { 206 throw new JahiaException ( 207 "Cannot load container list " + containerListID + "from the database", 208 se.getMessage (), 209 JahiaException.DATABASE_ERROR, 210 JahiaException.ERROR_SEVERITY, se); 211 } finally { 212 try { 213 214 if (stmt != null) stmt.close (); 215 } catch (SQLException ex) { 216 throw new JahiaException ("Cannot free resources", 217 "Cannot free resources", 218 JahiaException.DATABASE_ERROR, 219 JahiaException.WARNING_SEVERITY, ex); 220 } 221 } 222 223 return resultEntryStates; 224 } 225 226 protected void deleteEntry (int containerListID, EntryStateable entryState) 227 throws JahiaException { 228 229 Connection dbConn = null; 230 PreparedStatement stmt = null; 231 232 try { 233 String sqlQuery = 234 "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state=? AND version_id=?"; 235 dbConn = ConnectionDispenser.getConnection (); 236 stmt = dbConn.prepareStatement (sqlQuery); 237 stmt.setInt (1, containerListID); 238 stmt.setInt (2, entryState.getWorkflowState ()); 239 stmt.setInt (3, entryState.getVersionID ()); 240 int resultCount = stmt.executeUpdate (); 241 logger.debug (resultCount + " rows affected by delete of entry"); 242 243 } catch (SQLException se) { 244 throw new JahiaException ("Error while deleting entry", 245 se.getMessage (), 246 JahiaException.DATABASE_ERROR, 247 JahiaException.ERROR_SEVERITY, se); 248 249 } finally { 250 try { 251 if (stmt != null) 252 stmt.close (); 253 254 } catch (SQLException ex) { 255 throw new JahiaException ("Cannot free resources", 256 "Cannot free resources", 257 JahiaException.DATABASE_ERROR, 258 JahiaException.WARNING_SEVERITY, ex); 259 } 260 } 261 } 262 263 278 protected void copyEntry (int containerListID, 279 EntryStateable fromEntryState, 280 EntryStateable toEntryState) 281 throws JahiaException { 282 283 Connection dbConn = null; 284 PreparedStatement stmt = null; 285 286 try { 287 String sqlQuery = "SELECT parententryid_jahia_ctn_lists, pageid_jahia_ctn_lists, ctndefid_jahia_ctn_lists, rights_jahia_ctn_lists FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state=? AND version_id=?"; 288 dbConn = ConnectionDispenser.getConnection (); 289 stmt = dbConn.prepareStatement (sqlQuery); 290 stmt.setInt (1, containerListID); 291 stmt.setInt (2, fromEntryState.getWorkflowState ()); 292 stmt.setInt (3, fromEntryState.getVersionID ()); 293 ResultSet rs = stmt.executeQuery (); 294 if (rs.next ()) { 295 int parentEntryID = rs.getInt ("parententryid_jahia_ctn_lists"); 296 int pageID = rs.getInt ("pageid_jahia_ctn_lists"); 297 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists"); 298 int rights = rs.getInt ("rights_jahia_ctn_lists"); 299 stmt.close (); 300 301 String insertQuery = "INSERT INTO jahia_ctn_lists (id_jahia_ctn_lists, parententryid_jahia_ctn_lists, pageid_jahia_ctn_lists, ctndefid_jahia_ctn_lists, rights_jahia_ctn_lists, version_id, workflow_state) VALUES (?, ?, ?, ?, ?, ?, ?)"; 302 stmt = dbConn.prepareStatement (insertQuery); 303 stmt.setInt (1, containerListID); 304 stmt.setInt (2, parentEntryID); 305 stmt.setInt (3, pageID); 306 stmt.setInt (4, ctnDefID); 307 stmt.setInt (5, rights); 308 stmt.setInt (6, toEntryState.getVersionID ()); 309 stmt.setInt (7, toEntryState.getWorkflowState ()); 310 stmt.executeUpdate (); 311 } 312 } catch (SQLException se) { 313 logger.debug ("Error copying entry for ctnlist[" 314 + containerListID + "]\n From entry :" 315 + fromEntryState.toString () 316 + "\n To entry : " + toEntryState.toString ()); 317 throw new JahiaException ("Error while copying entry", 318 se.getMessage (), 319 JahiaException.DATABASE_ERROR, 320 JahiaException.ERROR_SEVERITY, se); 321 } finally { 322 try { 323 if (stmt != null) 324 stmt.close (); 325 } catch (SQLException ex) { 326 throw new JahiaException ("Cannot free resources", 327 "Cannot free resources", 328 JahiaException.DATABASE_ERROR, 329 JahiaException.WARNING_SEVERITY, ex); 330 } 331 } 332 } 333 334 } | Popular Tags |