1 package org.jahia.services.filemanager; 2 3 import org.jahia.data.files.JahiaFile; 4 import org.jahia.data.files.JahiaFileField; 5 import org.jahia.exceptions.JahiaException; 6 import org.jahia.registries.ServicesRegistry; 7 8 import java.sql.*; 9 import java.util.Enumeration ; 10 import java.util.Properties ; 11 12 13 22 23 public class JahiaFileFieldsManager { 24 25 private static org.apache.log4j.Logger logger = 26 org.apache.log4j.Logger.getLogger (JahiaFileFieldsManager.class); 27 28 private static String CLASS_NAME = JahiaFileFieldsManager.class.getName (); 29 30 private static JahiaFileFieldsManager fileFieldsManager = null; 31 32 33 public static boolean m_DebugQuery = false; 34 35 public JahiaFileFieldsManager () { 36 } 37 38 public static JahiaFileFieldsManager getInstance () { 39 if (fileFieldsManager == null) { 40 fileFieldsManager = new JahiaFileFieldsManager (); 41 } 42 return fileFieldsManager; 43 } 44 45 52 public JahiaFileField getJahiaFileField (int id) { 53 JahiaFileField fField = null; 54 Connection dbConn = null; 55 PreparedStatement stmt = null; 56 ResultSet rs = null; 57 String fileFieldTitle = null; 58 59 try { 60 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 62 ; 63 64 String query = "select * from jahia_file_fields where id_jahia_filefield=?"; 66 stmt = dbConn.prepareStatement (query); 67 stmt.setInt (1, id); 68 debugQuery (query); 69 rs = stmt.executeQuery (); 70 fField = getJahiaFileFieldFromResultSet (rs); 71 } catch (SQLException ex) { 72 processSQLException (ex); 73 } catch (JahiaException je) { 74 logger.debug (je.getMessage ()); 75 } finally { 76 try { 77 if (stmt != null) stmt.close (); 78 } catch (SQLException ex) { 79 processSQLException (ex); 80 } 81 } 82 return fField; 83 } 84 85 90 public boolean insertJahiaFileField (JahiaFileField fField) 91 throws JahiaException { 92 if (fField == null) { 93 return false; 94 } 95 Connection dbConn = null; 96 PreparedStatement pstmt = null; 97 boolean success = false; 98 99 try { 100 int id = fField.getID (); 101 if (id == -1) { 102 id = ServicesRegistry.getInstance () 103 .getJahiaIncrementorsDBService () 104 .autoIncrement ("jahia_file_fields"); 105 } else { 106 this.deleteJahiaFileField (fField.getID ()); 108 } 109 110 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 112 113 Properties properties = fField.getProperties (); 114 115 properties.setProperty (JahiaFileField.FIELD_FILE_FILEID_PROP, 117 String.valueOf (fField.getFileID ())); 118 119 Enumeration propNames = properties.keys (); 120 while (propNames.hasMoreElements ()) { 121 String curPropName = (String ) propNames.nextElement (); 122 String curPropValue = properties.getProperty (curPropName); 123 pstmt = dbConn.prepareStatement ("INSERT INTO jahia_file_fields VALUES(?,?,?)"); 124 pstmt.setInt (1, id); 125 pstmt.setString (2, curPropName); 126 pstmt.setString (3, curPropValue); 127 pstmt.execute (); 128 } 129 fField.setID (id); 130 success = true; 131 } catch (SQLException ex) { 132 processSQLException (ex); 133 success = false; 134 } finally { 135 try { 136 if (pstmt != null) pstmt.close (); 137 } catch (SQLException ex) { 138 processSQLException (ex); 139 } 140 } 141 return success; 142 } 143 144 151 public boolean deleteJahiaFileField (int id) 152 throws JahiaException { 153 154 Connection dbConn = null; 155 Statement stmt = null; 156 boolean success = false; 157 158 try { 159 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 161 162 String query = " delete from jahia_file_fields where id_jahia_filefield=" 164 + id; 165 166 stmt = dbConn.createStatement (); 167 debugQuery (query); 168 stmt.execute (query); 169 success = true; 170 } catch (SQLException ex) { 171 processSQLException (ex); 172 success = false; 173 } finally { 174 try { 175 if (stmt != null) stmt.close (); 176 } catch (SQLException ex) { 177 processSQLException (ex); 178 } 179 } 180 return success; 181 } 182 183 193 public boolean copyFileField (int oldJahiaFileFieldID, 194 int newFieldID, 195 int pageID) throws JahiaException { 196 return false; 198 } 199 200 209 protected JahiaFileField getJahiaFileFieldFromResultSet (ResultSet rs) 210 throws JahiaException { 211 212 if (rs == null) 213 throw new JahiaException (CLASS_NAME + ".getJahiaFileFieldFromResultSet", 214 "Resultset is null", 215 JahiaException.DATABASE_ERROR, 216 JahiaException.ERROR_SEVERITY); 217 218 JahiaFileField fField = null; 219 int id = -1; 220 String name = ""; 221 String value = ""; 222 Properties properties = new Properties (); 223 try { 224 while (rs.next ()) { 225 id = rs.getInt ("id_jahia_filefield"); 226 name = rs.getString ("prop_name_filefield"); 227 value = rs.getString ("prop_value_filefield"); 228 if (value == null) { 229 value = ""; 230 } 231 if (name != null) { 232 properties.setProperty (name, value); 233 } 234 } 235 } catch (SQLException se) { 236 String errorMsg = "DB Error : " + se.getMessage (); 237 logger.debug (errorMsg); 238 throw new JahiaException (CLASS_NAME + ".getJahiaFileFieldFromResultSet", 239 errorMsg, JahiaException.DATABASE_ERROR, 240 JahiaException.CRITICAL_SEVERITY); 241 } 242 243 JahiaFile aFile = null; 244 String val = properties.getProperty (JahiaFileField.FIELD_FILE_FILEID_PROP); 245 String versionVal = properties.getProperty (JahiaFileField.FIELD_FILE_VERSION_PROP); 246 if (val != null) { 247 try { 248 aFile = 249 ServicesRegistry.getInstance () 250 .getJahiaFilemanagerService ().getFileDB (Integer.parseInt (val), 251 versionVal); 252 } catch (Throwable t) { 253 t.printStackTrace (); 254 } 255 } 256 if (aFile == null) { 257 aFile = new JahiaFile (-1, -1, "", "", "", 0, 0, "", "", "", String.valueOf (ServicesRegistry.getInstance () 268 .getJahiaVersionService ().getCurrentVersionID ()), JahiaFile.STATE_ACTIVE); 270 } 271 fField = new JahiaFileField (aFile, properties); 272 fField.setID (id); 273 return fField; 274 } 275 276 286 String getProperty (int id, String key) 287 throws JahiaException { 288 289 Connection dbConn = null; 290 PreparedStatement pstmt = null; 291 String value = null; 292 293 if ((key == null) || (key.trim ().equals (""))) 294 return null; 295 296 try { 297 298 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 299 300 pstmt = 301 dbConn.prepareStatement ( 302 "SELECT prop_value_filefield FROM jahia_file_fields WHERE id_jahia_filefield=? AND prop_name_filefield=?"); 303 pstmt.setInt (1, id); 304 pstmt.setString (2, key); 305 ResultSet rs = pstmt.executeQuery (); 306 if (rs != null) { 307 if (rs.next ()) { 308 value = rs.getString ("prop_value_filefield"); 309 } 310 } 311 } catch (SQLException se) { 312 logger.debug ("Exception retrieving a propertey from db : " + se.getMessage ()); 313 throw new JahiaException (CLASS_NAME + ".getProperty(id,key)", 314 "Cannot load file field prop from the database" 315 + se.getMessage (), 316 JahiaException.DATABASE_ERROR, 317 JahiaException.CRITICAL_SEVERITY); 318 } finally { 319 320 closeStatement ((Statement) pstmt); 321 } 322 323 return value; 324 325 } 326 327 337 void addProperty (int id, String key, String value) throws JahiaException { 338 339 if ((key == null) || (key.trim ().equals (""))) 340 return; 341 342 Connection dbConn = null; 343 PreparedStatement pstmt = null; 344 345 try { 346 347 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 348 pstmt = dbConn.prepareStatement ("INSERT INTO jahia_file_fields VALUES (?,?,?)"); 349 pstmt.setInt (1, id); 350 pstmt.setString (2, key); 351 pstmt.setString (3, value); 352 pstmt.execute (); 353 354 } catch (SQLException se) { 355 String errorMsg = "Error adding property :" + se.getMessage (); 356 logger.debug (errorMsg); 357 throw new JahiaException ("Cannot add a property in the database", 358 errorMsg, 359 JahiaException.DATABASE_ERROR, 360 JahiaException.CRITICAL_SEVERITY); 361 } finally { 362 363 closeStatement ((Statement) pstmt); 364 } 365 } 366 367 372 void deleteProperties (int id) throws JahiaException { 373 374 try { 375 StringBuffer buff = 376 new StringBuffer ( 377 "DELETE FROM jahia_file_fields WHERE id_jahia_filefield="); 378 buff.append (id); 379 executeQueryNoResultSet (buff.toString ()); 380 } catch (JahiaException je) { 381 String errorMsg = "Error in deleteProperties(id) : "; 382 logger.debug (errorMsg); 383 throw new JahiaException ( 384 "Cannot delete all properties for the given file field: " + id, 385 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 386 } 387 } 388 389 397 void deleteProperty (int id, String key) throws JahiaException { 398 399 if ((key == null) || (key.trim ().equals (""))) 400 return; 401 402 Connection dbConn = null; 403 PreparedStatement pstmt = null; 404 405 try { 406 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 407 pstmt = 408 dbConn.prepareStatement ( 409 "DELETE FROM jahia_file_fields WHERE id_jahia_filefield=? AND prop_name_filefield=? "); 410 pstmt.setInt (1, id); 411 pstmt.setString (2, key); 412 } catch (SQLException se) { 413 String errorMsg = "Error in deleteProperty(int id, String key) : " 414 + se.getMessage (); 415 logger.debug (errorMsg); 416 throw new JahiaException ("Cannot delete a property in the database", 417 errorMsg, 418 JahiaException.DATABASE_ERROR, 419 JahiaException.CRITICAL_SEVERITY); 420 } finally { 421 422 closeStatement ((Statement) pstmt); 423 } 424 } 425 426 432 protected static void debugQuery (String query) { 433 if (m_DebugQuery) { 434 logger.debug (query); 435 } 436 } 437 438 444 protected void processSQLException (SQLException ex) { 445 if (m_DebugQuery) { 446 while (ex != null) { 447 logger.debug ("SQL EXCEPTION: SqlState = " + ex.getSQLState () 448 + " Error code = " + ex.getErrorCode () 449 + " Error msg = " + ex.toString ()); 450 ex = ex.getNextException (); 451 } 452 } 453 } 454 455 private void executeQueryNoResultSet (String queryStr) throws JahiaException { 456 Connection dbConn = null; 457 Statement statement = null; 458 459 try { 460 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 461 statement = dbConn.createStatement (); 462 if (statement != null) { 463 statement.executeUpdate (queryStr); 464 } 465 } catch (SQLException se) { 466 String errorMsg = 467 "Error in executeQueryNoResultSet(String queryStr) : " 468 + se.getMessage (); 469 logger.debug (errorMsg); 470 throw new JahiaException ("Cannot execute query" + queryStr, 471 errorMsg, 472 JahiaException.DATABASE_ERROR, 473 JahiaException.CRITICAL_SEVERITY); 474 } finally { 475 476 closeStatement (statement); 477 } 478 } 479 480 private void closeStatement (Statement statement) { 482 try { 484 if (statement != null) { 485 statement.close (); 486 statement = null; 487 } 488 } catch (SQLException sqlEx) { 489 JahiaException je = new JahiaException ("Cannot close a statement", 492 "Cannot close a statement", JahiaException.DATABASE_ERROR, 493 JahiaException.WARNING_SEVERITY); 494 } 495 } 496 } | Popular Tags |