1 13 23 24 package org.jahia.services.fields; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 36 import org.apache.log4j.Logger; 37 import org.jahia.data.JahiaDBDOMObject; 38 import org.jahia.data.JahiaDOMObject; 39 import org.jahia.data.fields.JahiaFieldDefinition; 40 import org.jahia.data.fields.JahiaFieldSubDefinition; 41 import org.jahia.exceptions.JahiaException; 42 import org.jahia.registries.ServicesRegistry; 43 import org.jahia.services.database.ConnectionDispenser; 44 import org.jahia.utils.FileUtils; 45 import org.jahia.utils.JahiaTools; 46 47 48 public class JahiaFieldDefinitionsDB { 49 50 public static final String defaultValueFlatFileMarker = "<defvalue_in_flat_file>"; 51 52 private static Logger logger = Logger.getLogger (JahiaFieldDefinitionsDB.class); 53 54 57 public JahiaFieldDefinitionsDB () { 58 } 60 61 72 public JahiaFieldDefinition db_load_field_definition (int theDefID) 73 throws JahiaException { 74 Connection dbConn = null; 75 PreparedStatement stmt1 = null; 76 PreparedStatement stmt2 = null; 77 ResultSet rs = null; 78 ResultSet rs2 = null; 79 JahiaFieldDefinition theFieldDef = null; 80 try { 81 dbConn = ConnectionDispenser.getConnection (); 83 84 String sqlQuery = "SELECT * FROM jahia_fields_def "; 86 sqlQuery += "WHERE id_jahia_fields_def = ?"; 87 stmt1 = dbConn.prepareStatement (sqlQuery); 88 stmt1.setInt(1, theDefID); 89 rs = stmt1.executeQuery (); 90 if (rs.next ()) { 91 int ID = rs.getInt ("id_jahia_fields_def"); 92 int jahiaID = rs.getInt ("jahiaid_jahia_fields_def"); 93 String name = rs.getString ("name_jahia_fields_def"); 94 95 sqlQuery = "SELECT * FROM jahia_fields_def_prop "; 96 sqlQuery += "WHERE flddefid_jahia_fields_def_prop=?"; 97 stmt2 = dbConn.prepareStatement (sqlQuery); 98 stmt2.setInt(1, ID); 99 rs2 = stmt2.executeQuery (); 100 101 Hashtable subDefs = new Hashtable (); 102 103 while (rs2.next ()) { 104 int subDefID = rs2.getInt ("id_jahia_fields_def_prop"); 105 int templateID = rs2.getInt ("pdefid_jahia_fields_def_prop"); 106 String title = rs2.getString ("title_jahia_fields_def_prop"); 107 int type = rs2.getInt ("type_jahia_fields_def_prop"); 108 String defaultValue = rs2.getString ("default_jahia_fields_def_prop"); 109 if (defaultValue.equals ("<empty>")) { 110 defaultValue = ""; 111 } else { 112 String filePath = ServicesRegistry.getInstance () 113 .getJahiaFieldService () 114 .composeFieldDefDefaultValueFilePath (jahiaID, name); 115 File f = new File (filePath); 116 if (f.exists ()) { 117 defaultValue = 118 FileUtils.getInstance ().readFile (f.getAbsolutePath ()); 119 } 120 } 121 subDefs.put (new Integer (templateID), 122 new JahiaFieldSubDefinition (subDefID, ID, templateID, title, type, 123 defaultValue)); 124 } 125 126 theFieldDef = new JahiaFieldDefinition (ID, jahiaID, name, subDefs); 127 } 128 } catch (SQLException se) { 129 String errorMsg = "Error in db_load_field_definition : " + se.getMessage (); 130 logger.error (errorMsg + " -> BAILING OUT"); 131 throw new JahiaException ("Cannot load fields from the database", 132 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 133 } finally { 134 try { 135 136 if (stmt1 != null) stmt1.close (); 137 if (stmt2 != null) stmt2.close (); 138 } catch (SQLException ex) { 139 new JahiaException ("Cannot free resources", 140 "db_load_field_definition : cannot free resources", 141 JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY); 142 } 143 } 144 if (theFieldDef != null) { 145 theFieldDef.setProperties (FieldDefinitionPropertiesPersistence 146 .getInstance () 147 .loadFieldDefinitionProperties (theFieldDef.getID ())); 148 } 149 return theFieldDef; 150 151 } 153 154 166 public JahiaFieldDefinition db_load_field_definition (int siteID, String name) 167 throws JahiaException { 168 Connection dbConn = null; 169 PreparedStatement stmt = null; 170 PreparedStatement stmt2 = null; 171 ResultSet rs = null; 172 ResultSet rs2 = null; 173 JahiaFieldDefinition theFieldDef = null; 174 try { 175 dbConn = ConnectionDispenser.getConnection (); 177 178 stmt = dbConn.prepareStatement("SELECT * FROM jahia_fields_def WHERE jahiaid_jahia_fields_def = ? and name_jahia_fields_def=?"); 179 stmt.setInt(1, siteID); 180 stmt.setString(2, name); 181 rs = stmt.executeQuery (); 182 if (rs.next ()) { 183 int ID = rs.getInt ("id_jahia_fields_def"); 184 int jahiaID = rs.getInt ("jahiaid_jahia_fields_def"); 185 name = rs.getString ("name_jahia_fields_def"); 186 187 stmt2 = dbConn.prepareStatement("SELECT * FROM jahia_fields_def_prop WHERE flddefid_jahia_fields_def_prop=?"); 188 stmt2.setInt(1, ID); 189 190 rs2 = stmt2.executeQuery (); 191 192 Hashtable subDefs = new Hashtable (); 193 194 while (rs2.next ()) { 195 int subDefID = rs2.getInt ("id_jahia_fields_def_prop"); 196 int templateID = rs2.getInt ("pdefid_jahia_fields_def_prop"); 197 String title = rs2.getString ("title_jahia_fields_def_prop"); 198 int type = rs2.getInt ("type_jahia_fields_def_prop"); 199 String defaultValue = rs2.getString ("default_jahia_fields_def_prop"); 200 if (defaultValue.equals ("<empty>")) { 201 defaultValue = ""; 202 } else if (defaultValue.equals ( 203 JahiaFieldDefinitionsDB.defaultValueFlatFileMarker)) { 204 String filePath = ServicesRegistry.getInstance () 205 .getJahiaFieldService () 206 .composeFieldDefDefaultValueFilePath (siteID, name); 207 File f = new File (filePath); 208 if (f.exists ()) { 209 defaultValue = 210 FileUtils.getInstance ().readFile (f.getAbsolutePath ()); 211 } 212 } 213 subDefs.put (new Integer (templateID), 214 new JahiaFieldSubDefinition (subDefID, ID, templateID, title, type, 215 defaultValue)); 216 } 217 218 theFieldDef = new JahiaFieldDefinition (ID, jahiaID, name, subDefs); 219 } 220 } catch (SQLException se) { 221 String errorMsg = "Error in db_load_field_definition : " + se.getMessage (); 222 logger.error (errorMsg + " -> BAILING OUT"); 223 throw new JahiaException ("Cannot load fields from the database", 224 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 225 } finally { 226 closeStatement(stmt); 227 closeStatement(stmt2); 228 } 229 if (theFieldDef != null) { 230 theFieldDef.setProperties (FieldDefinitionPropertiesPersistence 231 .getInstance () 232 .loadFieldDefinitionProperties (theFieldDef.getID ())); 233 } 234 return theFieldDef; 235 236 } 238 239 248 public void db_create_field_definition (JahiaFieldDefinition theFieldDef) 249 throws JahiaException { 250 Connection dbConn = null; 251 PreparedStatement stmt = null; 252 try { 253 ServicesRegistry sr = ServicesRegistry.getInstance (); 255 int theFieldDefID = sr.getJahiaIncrementorsDBService ().autoIncrement ( 256 "jahia_fields_def"); 257 258 theFieldDef.setID (theFieldDefID); 259 String sqlQuery = 260 "INSERT INTO jahia_fields_def (id_jahia_fields_def,jahiaid_jahia_fields_def,name_jahia_fields_def) VALUES(?,?,?)"; 261 dbConn = ConnectionDispenser.getConnection (); 263 stmt = dbConn.prepareStatement (sqlQuery); 264 stmt.setInt(1, theFieldDef.getID ()); 265 stmt.setInt(2, theFieldDef.getJahiaID ()); 266 stmt.setString(3, theFieldDef.getName ()); 267 268 stmt.executeUpdate (); 270 271 db_update_field_defprop (theFieldDef); 273 } 274 catch (SQLException se) { 276 String errorMsg = "Error in create_jahia_field_definition : " + se.getMessage (); 277 logger.error (errorMsg + " -> BAILING OUT"); 278 throw new JahiaException ("Cannot insert new fields definitions in the database", 279 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 280 281 } finally { 282 closeStatement(stmt); 283 } 284 } 286 public void db_create_field_defprop(JahiaFieldDefinition theFieldDef) throws JahiaException { 287 Connection dbConn = null; 288 PreparedStatement stmt = null; 289 String sqlQuery = ""; 290 try { 291 292 dbConn = ConnectionDispenser.getConnection(); 294 295 sqlQuery = "INSERT INTO jahia_fields_def_prop (id_jahia_fields_def_prop,flddefid_jahia_fields_def_prop,pdefid_jahia_fields_def_prop,"; 296 sqlQuery += "title_jahia_fields_def_prop,type_jahia_fields_def_prop,default_jahia_fields_def_prop) VALUES(?,?,?,?,?,?)"; 297 stmt = dbConn.prepareStatement(sqlQuery); 298 299 Hashtable subDefs = theFieldDef.getSubDefs(); 301 Enumeration subKeys = subDefs.keys(); 302 while (subKeys.hasMoreElements()) { 303 int pageDefID = ((Integer ) subKeys.nextElement()).intValue(); 304 305 JahiaFieldSubDefinition theSubDef = (JahiaFieldSubDefinition) subDefs.get(new Integer (pageDefID)); 306 if (theSubDef.getID() == 0) { 308 int theSubDefID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( 309 "jahia_fields_def_prop"); 310 theSubDef.setID(theSubDefID); 311 theSubDef.setFieldDefID(theFieldDef.getID()); 312 } 313 314 String defaultVal = theFieldDef.getDefaultValue(pageDefID); 315 if (defaultVal == null || "".equals(defaultVal)) { 316 defaultVal = "<empty>"; } else if (defaultVal.length() > 250) { 319 String filePath = ServicesRegistry.getInstance().getJahiaFieldService() 320 .composeFieldDefDefaultValueFilePath(theFieldDef.getJahiaID(), theFieldDef.getName()); 321 File f = new File (filePath); 322 f.createNewFile(); 323 FileUtils.getInstance().writeFile(f.getAbsolutePath(), theFieldDef.getDefaultValue(pageDefID)); 324 defaultVal = JahiaFieldDefinitionsDB.defaultValueFlatFileMarker; 325 } 326 327 if (theSubDef.getTitle().length() > 0 && theSubDef.getType() != -1) { 328 stmt.setInt(1, theSubDef.getID()); 330 stmt.setInt(2, theFieldDef.getID()); 331 stmt.setInt(3, pageDefID); 332 stmt.setString(4, JahiaTools.quote(theSubDef.getTitle())); 333 stmt.setInt(5, theSubDef.getType()); 334 stmt.setString(6, JahiaTools.quote(defaultVal)); 335 336 stmt.executeUpdate(); 337 } 338 } 339 340 } 341 catch (IOException ioe) { 343 String errorMsg = "Error in db_update_field_definition : " + ioe.getMessage(); 344 logger.error(errorMsg + " -> BAILING OUT"); 345 throw new JahiaException("Cannot update fields definitions in the database", errorMsg, 346 JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 347 } catch (SQLException se) { 348 String errorMsg = "Error in db_update_field_definition : " + se.getMessage(); 349 logger.error(errorMsg + " -> BAILING OUT"); 350 throw new JahiaException("Cannot update fields definitions in the database", errorMsg, 351 JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 352 } finally { 353 closeStatement(stmt); 354 } 355 } 357 366 public void db_update_field_defprop (JahiaFieldDefinition theFieldDef) 367 throws JahiaException { 368 Connection dbConn = null; 369 PreparedStatement stmt = null; 370 String sqlQuery = ""; 371 try { 372 373 dbConn = ConnectionDispenser.getConnection (); 375 376 sqlQuery = "DELETE FROM jahia_fields_def_prop WHERE flddefid_jahia_fields_def_prop=?"; 378 stmt = dbConn.prepareStatement (sqlQuery); 379 stmt.setInt(1, theFieldDef.getID()); 380 381 stmt.executeUpdate (); 382 383 db_create_field_defprop(theFieldDef); 384 385 } catch (SQLException se) { 386 String errorMsg = "Error in db_update_field_definition : " + se.getMessage (); 387 logger.error (errorMsg + " -> BAILING OUT"); 388 throw new JahiaException ("Cannot update fields definitions in the database", 389 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 390 } finally { 391 closeStatement(stmt); 392 } 393 394 if (theFieldDef != null) { 395 FieldDefinitionPropertiesPersistence.getInstance () 396 .saveFieldDefinitionProperties (theFieldDef.getID (), 397 theFieldDef.getProperties ()); 398 } 399 400 } 402 403 414 public void db_delete_field_definition (int fieldDefID) 415 throws JahiaException { 416 Connection dbConn = null; 417 Statement stmt = null; 418 try { 419 420 String sqlQuery = "DELETE FROM " + jahia_fields_def + 422 " WHERE " + FIELD_FIELD_ID + "=" + fieldDefID; 423 424 dbConn = ConnectionDispenser.getConnection (); 426 stmt = dbConn.createStatement (); 427 stmt.executeUpdate (sqlQuery); 428 429 } 430 catch (SQLException se) { 432 String errorMsg = "Error in db_delete_field_definition : " + se.getMessage (); 433 logger.error (errorMsg + " -> BAILING OUT"); 434 throw new JahiaException ("Cannot delete fields definitions in the database", 435 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 436 } finally { 437 closeStatement(stmt); 438 } 439 FieldDefinitionPropertiesPersistence.getInstance () 440 .deleteFieldDefinitionProperties (fieldDefID); 441 442 } 444 445 453 public void db_delete_field_sub_definition (int pageDefID) 454 throws JahiaException { 455 Connection dbConn = null; 456 Statement stmt = null; 457 try { 458 459 String sqlQuery = "DELETE FROM jahia_fields_def_prop " + 461 " WHERE pdefid_jahia_fields_def_prop=" + pageDefID; 462 463 dbConn = ConnectionDispenser.getConnection (); 465 stmt = dbConn.createStatement (); 466 stmt.executeUpdate (sqlQuery); 467 468 } 469 catch (SQLException se) { 471 String errorMsg = "Error in db_delete_field_sub_definition : " + se.getMessage (); 472 logger.error (errorMsg + " -> BAILING OUT"); 473 throw new JahiaException ("Cannot delete fields sub definitions in the database", 474 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 475 } finally { 476 closeStatement(stmt); 477 } 478 479 } 481 482 492 public JahiaDOMObject getFieldDefsAsDOM (int siteID) throws JahiaException { 493 494 Connection dbConn = null; 495 Statement statement = null; 496 497 JahiaDBDOMObject dom = null; 498 499 try { 500 String sqlQuery = "SELECT * FROM jahia_fields_def where jahiaid_jahia_fields_def=" + siteID; 501 502 dbConn = ConnectionDispenser.getConnection (); 503 statement = dbConn.createStatement (); 504 if (statement != null) { 505 ResultSet rs = statement.executeQuery (sqlQuery); 506 if (rs != null) { 507 dom = new JahiaDBDOMObject (); 508 dom.addTable ("jahia_fields_def", rs); 509 return dom; 510 } 511 } 512 } catch (SQLException se) { 513 String errorMsg = "Error in getFieldDefsAsDOM(int siteID) : " + se.getMessage (); 514 logger.error (errorMsg + " -> BAILING OUT"); 515 throw new JahiaException ("Cannot load field def from the database", 516 errorMsg, JahiaException.DATABASE_ERROR, 517 JahiaException.CRITICAL_SEVERITY); 518 } finally { 519 520 closeStatement (statement); 521 } 522 523 return dom; 524 } 525 526 536 public JahiaDOMObject getFieldDefPropsAsDOM (int siteID) 537 throws JahiaException { 538 539 Connection dbConn = null; 540 Statement statement = null; 541 542 JahiaDBDOMObject dom = null; 543 544 try { 545 546 String sqlQuery = "SELECT DISTINCT jahia_fields_def_prop.id_jahia_fields_def_prop," 547 + "jahia_fields_def_prop.flddefid_jahia_fields_def_prop,jahia_fields_def_prop.pdefid_jahia_fields_def_prop," 548 + "jahia_fields_def_prop.title_jahia_fields_def_prop,jahia_fields_def_prop.type_jahia_fields_def_prop," 549 + "jahia_fields_def_prop.default_jahia_fields_def_prop" 550 + " FROM jahia_fields_def_prop,jahia_fields_def" 551 + " where jahia_fields_def_prop.flddefid_jahia_fields_def_prop=" 552 + " jahia_fields_def.id_jahia_fields_def AND jahia_fields_def.jahiaid_jahia_fields_def=" + siteID; 553 554 dbConn = ConnectionDispenser.getConnection (); 555 statement = dbConn.createStatement (); 556 if (statement != null) { 557 ResultSet rs = statement.executeQuery (sqlQuery); 558 if (rs != null) { 559 dom = new JahiaDBDOMObject (); 560 dom.addTable ("jahia_fields_def_prop", rs); 561 return dom; 562 } 563 } 564 } catch (SQLException se) { 565 String errorMsg = "Error in getFieldDefPropsAsDOM(int siteID) : " + se.getMessage (); 566 logger.error (errorMsg + " -> B AILING OUT"); 567 throw new JahiaException ("Cannot load field def props from the database", 568 errorMsg, JahiaException.DATABASE_ERROR, 569 JahiaException.CRITICAL_SEVERITY); 570 } finally { 571 572 closeStatement (statement); 573 } 574 575 return dom; 576 } 577 578 579 private void closeStatement (Statement statement) { 581 try { 583 if (statement != null) { 584 statement.close (); 585 } 586 } catch (SQLException sqlEx) { 587 logger.error ("Cannot close a statement", sqlEx); 588 } 589 } 590 591 592 593 597 private static final String jahia_fields_def = "jahia_fields_def"; 598 private static final String FIELD_FIELD_ID = "id_" + jahia_fields_def; 599 600 } 602 | Popular Tags |