1 package org.jahia.services.applications; 2 3 import org.jahia.data.JahiaDBDOMObject; 4 import org.jahia.data.JahiaDOMObject; 5 import org.jahia.data.applications.ApplicationBean; 6 import org.jahia.exceptions.JahiaException; 7 import org.jahia.registries.ServicesRegistry; 8 import org.jahia.services.acl.JahiaBaseACL; 9 import org.jahia.services.usermanager.GroupsTools; 10 import org.jahia.services.usermanager.JahiaGroup; 11 import org.jahia.services.usermanager.JahiaGroupManagerService; 12 import org.jahia.services.usermanager.JahiaUser; 13 import org.jahia.utils.JahiaTools; 14 15 import java.sql.*; 16 import java.util.Vector ; 17 18 27 public class JahiaApplicationsPersistanceBaseService 28 extends JahiaApplicationsPersistanceService { 29 30 private static org.apache.log4j.Logger logger = 31 org.apache.log4j.Logger.getLogger ( 32 JahiaApplicationsPersistanceBaseService.class); 33 34 private String serviceName; 35 private static JahiaApplicationsPersistanceBaseService theObject = null; 36 37 private static final String DELETE_WEBAPP_QUERY = 38 "DELETE FROM jahia_app_def WHERE id_jahia_app_def=?"; 39 private static final String UPDATE_WEBAPP_QUERY = "UPDATE jahia_app_def SET jahiaid_jahia_app_def=?,name_jahia_app_def=?,context_jahia_app_def=?,visible_jahia_app_def=?,shared_jahia_app_def=?,rights_jahia_app_def=?,filename_jahia_app_def=?,desc_jahia_app_def=? WHERE id_jahia_app_def=?"; 40 41 44 protected JahiaApplicationsPersistanceBaseService () { 45 logger.debug ("***** Starting *****"); 46 } 48 54 public static synchronized JahiaApplicationsPersistanceBaseService 55 getInstance () { 56 if (theObject == null) { 57 theObject = new JahiaApplicationsPersistanceBaseService (); 58 } 59 return theObject; 60 } 62 68 public Vector getWebSites () 69 throws JahiaException { 70 71 Connection dbConn = null; 72 Statement stmt = null; 73 ResultSet rs = null; 74 Vector webSites = new Vector (); 75 try { 76 String sqlQuery = "SELECT distinct(jahiaid_jahia_app_def) FROM jahia_app_def order by jahiaid_jahia_app_def"; 77 dbConn = org.jahia.services.database.ConnectionDispenser. 78 getConnection (); 79 stmt = dbConn.createStatement (); 80 rs = stmt.executeQuery (sqlQuery); 81 int jahiaID = -1; 82 int newID = 0; 83 84 while (rs.next ()) { 85 86 newID = rs.getInt ("jahiaid_jahia_app_def"); 87 if (jahiaID != newID) { 88 webSites.add (new Integer (newID)); 89 jahiaID = newID; 90 } 91 } 92 } catch (SQLException se) { 93 String errorMsg = 94 "Error while retrieving sites used by web applications"; 95 logger.error (errorMsg, se); 96 throw new JahiaException ( 97 "Cannot retrieve the number of jahia websites ", 98 errorMsg, JahiaException.DATABASE_ERROR, 99 JahiaException.CRITICAL_SEVERITY, se); 100 } finally { 101 try { 102 if (rs != null) 103 rs = null; 104 if (stmt != null) 105 stmt.close (); 106 } catch (SQLException ex) { 107 logger.error ("Error while freeing resources", ex); 108 } 109 } 110 return webSites; 111 112 } 114 119 public Vector get_applications_list (boolean visibleOnly) 120 throws JahiaException { 121 Connection dbConn = null; 122 Statement stmt = null; 123 ResultSet rs = null; 124 Vector appList = new Vector (); 125 try { 126 String sqlQuery = "SELECT * FROM jahia_app_def "; 127 if (visibleOnly) { 128 sqlQuery += " where visible_jahia_app_def=1 "; 129 } 130 131 sqlQuery += " order by name_jahia_app_def"; 132 133 dbConn = org.jahia.services.database.ConnectionDispenser. 134 getConnection (); 135 stmt = dbConn.createStatement (); 136 rs = stmt.executeQuery (sqlQuery); 137 138 while (rs.next ()) { 139 ApplicationBean appBean = get_application_from_resultset (rs); 140 appList.add (appBean); 141 } 142 } catch (SQLException se) { 143 String errorMsg = "Error while retrieving list of web applications"; 144 logger.error (errorMsg, se); 145 throw new JahiaException ( 146 "Cannot load external application info from the database", 147 errorMsg, JahiaException.DATABASE_ERROR, 148 JahiaException.CRITICAL_SEVERITY, se 149 ); 150 } finally { 151 try { 152 if (rs != null) 153 rs = null; 154 if (stmt != null) 155 stmt.close (); 156 } catch (SQLException ex) { 157 logger.error ("Error while freeing resources", ex); 158 } 159 } 160 161 Vector newList = new Vector (); 162 int size = appList.size (); 163 for (int i = 0; i < size; i++) { 164 ApplicationBean app = (ApplicationBean) appList.get (i); 165 newList.add (app); 166 } 167 168 return newList; 169 170 } 172 177 public Vector get_applications_list (int jahiaID, boolean visibleOnly) 178 throws JahiaException { 179 Connection dbConn = null; 180 Statement stmt = null; 181 ResultSet rs = null; 182 Vector appList = new Vector (); 183 try { 184 String sqlQuery = "SELECT * FROM jahia_app_def "; 185 sqlQuery += "WHERE (jahiaid_jahia_app_def=" + jahiaID + 186 " or shared_jahia_app_def=1)"; 187 188 if (visibleOnly) { 189 sqlQuery += " and visible_jahia_app_def=1 "; 190 } 191 192 sqlQuery += " order by name_jahia_app_def"; 193 194 dbConn = org.jahia.services.database.ConnectionDispenser. 195 getConnection (); 196 stmt = dbConn.createStatement (); 197 rs = stmt.executeQuery (sqlQuery); 198 199 while (rs.next ()) { 200 ApplicationBean appBean = get_application_from_resultset (rs); 201 appList.add (appBean); 202 } 203 } catch (SQLException se) { 204 String errorMsg = 205 "Error while retrieving web application list for site " + 206 jahiaID; 207 logger.error (errorMsg, se); 208 throw new JahiaException ( 209 "Cannot load external application info from the database", 210 errorMsg, JahiaException.DATABASE_ERROR, 211 JahiaException.CRITICAL_SEVERITY, se); 212 } finally { 213 try { 214 if (rs != null) 215 rs = null; 216 if (stmt != null) 217 stmt.close (); 218 } catch (SQLException ex) { 219 logger.error ("Error while freeing resources", ex); 220 } 221 } 222 223 Vector newList = new Vector (); 224 int size = appList.size (); 225 for (int i = 0; i < size; i++) { 226 ApplicationBean app = (ApplicationBean) appList.get (i); 227 newList.add (app); 228 } 229 230 return newList; 231 232 } 234 public ApplicationBean get_application_definition (int appID) 235 throws JahiaException { 236 Connection dbConn = null; 237 Statement stmt = null; 238 ResultSet rs = null; 239 ApplicationBean appBean = null; 240 try { 241 String sqlQuery = "SELECT * FROM jahia_app_def "; 242 sqlQuery += "WHERE id_jahia_app_def=" + appID; 243 244 dbConn = org.jahia.services.database.ConnectionDispenser. 245 getConnection (); 246 stmt = dbConn.createStatement (); 247 rs = stmt.executeQuery (sqlQuery); 248 if (rs.next ()) { 249 appBean = get_application_from_resultset (rs); 250 } 251 252 } catch (SQLException se) { 253 String errorMsg = "Error while retrieving web application " + appID; 254 logger.error (errorMsg, se); 255 throw new JahiaException ( 256 "Cannot load external application info from the database", 257 errorMsg, JahiaException.DATABASE_ERROR, 258 JahiaException.CRITICAL_SEVERITY, se); 259 } finally { 260 try { 261 if (stmt != null) 262 stmt.close (); 263 } catch (SQLException ex) { 264 logger.error ("Error while freeing resources", ex); 265 } 266 } 267 268 return appBean; 269 270 } 272 275 public ApplicationBean get_application_definition (String context) 276 throws JahiaException { 277 Connection dbConn = null; 278 Statement stmt = null; 279 ResultSet rs = null; 280 ApplicationBean appBean = null; 281 try { 282 String sqlQuery = "SELECT * FROM jahia_app_def "; 283 sqlQuery += "WHERE context_jahia_app_def='" + 284 JahiaTools.quote (context) + "' "; 285 286 dbConn = org.jahia.services.database.ConnectionDispenser. 287 getConnection (); 288 stmt = dbConn.createStatement (); 289 rs = stmt.executeQuery (sqlQuery); 290 291 if (rs.next ()) { 292 appBean = get_application_from_resultset (rs); 293 } 294 295 } catch (SQLException se) { 296 String errorMsg = 297 "Error while retrieving web application for context " + context; 298 logger.error (errorMsg, se); 299 throw new JahiaException ( 300 "Cannot load external application info from the database", 301 errorMsg, JahiaException.DATABASE_ERROR, 302 JahiaException.CRITICAL_SEVERITY, se); 303 } finally { 304 try { 305 if (stmt != null) 306 stmt.close (); 307 } catch (SQLException ex) { 308 logger.error ("Error while freeing resources", ex); 309 } 310 } 311 312 return appBean; 313 314 } 316 private ApplicationBean get_application_from_resultset (ResultSet rs) 317 throws JahiaException { 318 try { 319 return new ApplicationBean (rs.getInt ("id_jahia_app_def"), 320 rs.getInt ("jahiaid_jahia_app_def"), 321 rs.getString ("name_jahia_app_def"), 322 rs.getString ("context_jahia_app_def"), 323 rs.getInt ("visible_jahia_app_def"), 324 (rs.getInt ("shared_jahia_app_def") == 1), 325 rs.getInt ("rights_jahia_app_def"), 326 rs.getString ("filename_jahia_app_def"), 327 rs.getString ("desc_jahia_app_def")); 328 } catch (SQLException se) { 329 String errorMsg = 330 "Error while reading web application row from result set"; 331 logger.error (errorMsg, se); 332 throw new JahiaException ( 333 "Cannot load external application info from the database", 334 errorMsg, JahiaException.DATABASE_ERROR, 335 JahiaException.CRITICAL_SEVERITY, se); 336 } 337 338 } 340 public void add_application (ApplicationBean theApp) 341 throws JahiaException { 342 Connection dbConn = null; 343 Statement stmt = null; 344 boolean success = false; 345 try { 346 347 int theAppID = ServicesRegistry.getInstance (). 349 getJahiaIncrementorsDBService ().autoIncrement ("jahia_app_def"); 350 theApp.setID (theAppID); 351 352 String sqlQuery = "INSERT INTO jahia_app_def(id_jahia_app_def) "; 354 sqlQuery += "VALUES(" + theApp.getID () + ")"; 355 356 dbConn = org.jahia.services.database.ConnectionDispenser. 357 getConnection (); 358 stmt = dbConn.createStatement (); 359 360 stmt.execute (sqlQuery); 362 363 success = true; 364 } catch (SQLException se) { 365 String errorMsg = "Error while adding web application " + 367 theApp.getName () + " to the database"; 368 logger.error (errorMsg, se); 369 throw new JahiaException ( 370 "Cannot insert new application in the database", 371 errorMsg, JahiaException.DATABASE_ERROR, 372 JahiaException.CRITICAL_SEVERITY, se); 373 374 } finally { 375 try { 376 if (stmt != null) 377 stmt.close (); 378 } catch (SQLException ex) { 379 logger.error ("Error while freeing resources", ex); 380 } 381 } 382 383 if (success) { 384 update_application (theApp); 386 } 387 } 389 public void update_application (ApplicationBean theApp) 390 throws JahiaException { 391 Connection dbConn = null; 392 PreparedStatement stmt = null; 393 try { 394 395 dbConn = org.jahia.services.database.ConnectionDispenser. 396 getConnection (); 397 stmt = dbConn.prepareStatement (UPDATE_WEBAPP_QUERY); 398 stmt.setInt (1, theApp.getJahiaID ()); 399 stmt.setString (2, theApp.getName ()); 400 stmt.setString (3, theApp.getContext ()); 401 stmt.setInt (4, theApp.getVisibleStatus ()); 402 if (theApp.isShared ()) { 403 stmt.setInt (5, 1); 404 } else { 405 stmt.setInt (5, 0); 406 } 407 stmt.setInt (6, theApp.getRights ()); 408 stmt.setString (7, theApp.getFilename ()); 409 stmt.setString (8, theApp.getdesc ()); 410 stmt.setInt (9, theApp.getID ()); 411 412 stmt.executeUpdate (); 413 414 } catch (SQLException se) { 415 String errorMsg = "Error while updating application " + 416 theApp.getID () + " in database"; 417 logger.error (errorMsg, se); 418 throw new JahiaException ( 419 "Cannot update application definitions in the database", 420 errorMsg, JahiaException.DATABASE_ERROR, 421 JahiaException.CRITICAL_SEVERITY, se); 422 } finally { 423 try { 424 if (stmt != null) 425 stmt.close (); 426 } catch (SQLException ex) { 427 logger.error ("Error while freeing resources", ex); 428 } 429 } 430 } 432 public void setName (String name) { 433 serviceName = name; 434 } 435 436 public void removeApplication (ApplicationBean theApp) 437 throws JahiaException { 438 Connection dbConn = null; 439 PreparedStatement stmt = null; 440 try { 441 442 dbConn = org.jahia.services.database.ConnectionDispenser. 443 getConnection (); 444 stmt = dbConn.prepareStatement (DELETE_WEBAPP_QUERY); 445 stmt.setInt (1, theApp.getID ()); 446 stmt.executeUpdate (); 447 448 Vector vec = GroupsTools.getGroups (true); 450 451 JahiaGroupManagerService gms = ServicesRegistry.getInstance () 452 .getJahiaGroupManagerService (); 453 454 int size = vec.size (); 455 JahiaGroup grp = null; 456 String appIDStr = null; 457 for (int i = 0; i < size; i++) { 458 grp = (JahiaGroup) vec.get (i); 459 appIDStr = GroupsTools.getAppIDPart (grp.getGroupname ()); 460 if (appIDStr != null && 461 (Integer.parseInt (appIDStr) == theApp.getID ())) { 462 gms.deleteGroup (grp); 463 } 464 } 465 466 JahiaBaseACL appACL = new JahiaBaseACL (theApp.getRights ()); 467 if (appACL.getID () != 1) { 475 appACL.delete (); 476 } else { 477 logger.error ("Web application " + theApp.getID () + 478 " has ACL ID#" + appACL.getID () + 479 " which is an error, not deleting ACL"); 480 } 481 482 } catch (SQLException se) { 483 String errorMsg = "Error while removing web application " + 484 theApp.getID () + " from the database"; 485 logger.error (errorMsg, se); 486 throw new JahiaException ( 487 "Cannot remove application definitions from the database", 488 errorMsg, JahiaException.DATABASE_ERROR, 489 JahiaException.CRITICAL_SEVERITY, se); 490 } finally { 491 try { 492 if (stmt != null) 493 stmt.close (); 494 } catch (SQLException ex) { 495 logger.error ("Error while freeing resources", ex); 496 } 497 } 498 } 499 500 public void removeApplication (int appID) 501 throws JahiaException { 502 ApplicationBean appBean = get_application_definition (appID); 503 if (appBean != null) { 504 removeApplication (appBean); 505 } 506 } 507 508 516 public JahiaDOMObject getApplicationDefsAsDOM (int siteID) 517 throws JahiaException { 518 519 Connection dbConn = null; 520 Statement statement = null; 521 522 JahiaDBDOMObject dom = null; 523 524 try { 525 String sqlQuery = "SELECT * FROM jahia_app_def" 526 + " WHERE jahiaid_jahia_app_def=" + siteID; 527 528 dbConn = org.jahia.services.database.ConnectionDispenser. 529 getConnection (); 530 statement = dbConn.createStatement (); 531 if (statement != null) { 532 ResultSet rs = statement.executeQuery (sqlQuery); 533 if (rs != null) { 534 dom = new JahiaDBDOMObject (); 535 dom.addTable ("jahia_app_def", rs); 536 return dom; 537 } 538 } 539 } catch (SQLException se) { 540 String errorMsg = 541 "Error while retrieving web application definition for site " + 542 siteID; 543 logger.error (errorMsg, se); 544 throw new JahiaException ("Cannot load data from the database", 545 errorMsg, JahiaException.DATABASE_ERROR, 546 JahiaException.CRITICAL_SEVERITY, se); 547 } finally { 548 closeStatement (statement); 549 } 550 551 return dom; 552 } 553 554 private void closeStatement (Statement statement) { 556 try { 558 if (statement != null) { 559 statement.close (); 560 } 561 } catch (SQLException sqlEx) { 562 JahiaException je = new JahiaException ("Cannot close a statement", 565 "Cannot close a statement", JahiaException.DATABASE_ERROR, 566 JahiaException.WARNING_SEVERITY, sqlEx); 567 logger.error ("Error:", je); 568 } 569 } 570 571 public void fake_create_application_groups ( 581 ApplicationBean theApp, 582 int fieldID, 583 JahiaUser user) 584 throws JahiaException { 585 String groupname; 586 JahiaGroup group; 587 588 groupname = Integer.toString (theApp.getID ()) + "_" + 590 Integer.toString (fieldID) + 591 "_administrator"; 592 group = ServicesRegistry.getInstance ().getJahiaGroupManagerService (). 593 lookupGroup (groupname); 594 if (group != null) { 595 group.addMember (user); 596 } 597 598 groupname = Integer.toString (theApp.getID ()) + "_" + 600 Integer.toString (fieldID) + 601 "_manager"; 602 group = ServicesRegistry.getInstance ().getJahiaGroupManagerService (). 603 lookupGroup (groupname); 604 if (group != null) { 605 group.addMember (user); 606 } 607 608 } 609 611 } | Popular Tags |