1 package org.jahia.services.shares; 18 19 import org.jahia.data.JahiaDBDOMObject; 20 import org.jahia.data.JahiaDOMObject; 21 import org.jahia.exceptions.JahiaException; 22 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 30 31 36 public class AppsSharePersistance { 37 private static org.apache.log4j.Logger logger = 38 org.apache.log4j.Logger.getLogger (AppsSharePersistance.class); 39 40 private static AppsSharePersistance m_Instance = null; 41 42 43 46 protected AppsSharePersistance () { 47 48 } 49 50 51 55 public static synchronized AppsSharePersistance getInstance () { 56 57 if (m_Instance == null) { 58 m_Instance = new AppsSharePersistance (); 59 } 60 return m_Instance; 61 } 62 63 64 72 public Enumeration dbGetSites (int appID) throws JahiaException { 73 74 Connection dbConn = null; 75 Statement statement = null; 76 77 Vector vec = new Vector (); 78 79 try { 80 String sqlQuery = "SELECT * FROM jahia_apps_share where id_aps_appid=" + appID; 81 82 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 83 statement = dbConn.createStatement (); 84 if (statement != null) { 85 ResultSet rs = statement.executeQuery (sqlQuery); 86 if (rs != null) { 87 int siteID = -1; 88 while (rs.next ()) { 89 siteID = rs.getInt ("id_aps_siteid"); 90 vec.add (new Integer (siteID)); 91 } 92 } 93 } 94 95 } catch (SQLException se) { 96 String errorMsg = "Error in dbGetSites : " + se.getMessage (); 97 logger.error (errorMsg + " -> BAILING OUT"); 98 throw new JahiaException ("Cannot retrive site keys from the database", 99 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 100 } finally { 101 102 closeStatement (statement); 103 } 104 105 return vec.elements (); 106 } 107 108 109 116 public void dbAddShare (int appID, int siteID) throws JahiaException { 117 118 if (dbGetShare (appID, siteID) == null) { 119 120 try { 121 122 StringBuffer sqlQuery = new StringBuffer ( 123 "INSERT INTO jahia_apps_share VALUES("); 124 sqlQuery.append (appID); 125 sqlQuery.append (","); 126 sqlQuery.append (siteID); 127 sqlQuery.append (")"); 128 129 executeQueryNoResultSet (sqlQuery.toString ()); 130 131 logger.debug (sqlQuery.toString ()); 132 133 } catch (JahiaException je) { 134 String errorMsg = "Error in dbAddShare(int appid, String siteKey) : " + je.getMessage (); 135 logger.error (errorMsg + " -> BAILING OUT"); 136 throw new JahiaException ("Cannot add app share in the database", 137 errorMsg, JahiaException.DATABASE_ERROR, 138 JahiaException.CRITICAL_SEVERITY); 139 } 140 } 141 142 } 143 144 145 152 public void dbRemoveShare (int appID, int siteID) throws JahiaException { 153 154 try { 155 StringBuffer sqlQuery = new StringBuffer ( 156 "DELETE FROM jahia_apps_share WHERE id_aps_appid="); 157 sqlQuery.append (" and id_aps_siteid="); 158 sqlQuery.append (siteID); 159 executeQueryNoResultSet (sqlQuery.toString ()); 160 } catch (JahiaException je) { 161 String errorMsg = "Error in dbRemoveSite(int id) : " + je.getMessage (); 162 logger.error (errorMsg + " -> BAILING OUT"); 163 throw new JahiaException ("Cannot remove share in the database", 164 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 165 } 166 } 167 168 169 175 public void dbSiteRemoveShares (int siteID) throws JahiaException { 176 177 try { 178 StringBuffer sqlQuery = new StringBuffer ( 179 "DELETE FROM jahia_apps_share WHERE id_aps_siteid ="); 180 sqlQuery.append (siteID); 181 executeQueryNoResultSet (sqlQuery.toString ()); 182 } catch (JahiaException je) { 183 String errorMsg = "Error in dbRemoveShares(int siteID) : " + je.getMessage (); 184 logger.error (errorMsg + " -> BAILING OUT"); 185 throw new JahiaException ("Cannot remove share for a gived site in the database", 186 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 187 } 188 189 190 } 191 192 193 199 public void dbAppRemoveShares (int appID) throws JahiaException { 200 201 try { 202 StringBuffer sqlQuery = new StringBuffer ( 203 "DELETE FROM jahia_apps_share WHERE id_aps_appid ="); 204 sqlQuery.append (appID); 205 executeQueryNoResultSet (sqlQuery.toString ()); 206 } catch (JahiaException je) { 207 String errorMsg = "Error in dbRemoveShares(int appID) : " + je.getMessage (); 208 logger.error (errorMsg + " -> BAILING OUT"); 209 throw new JahiaException ("Cannot remove share for a gived app in the database", 210 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 211 } 212 } 213 214 215 224 public AppShare dbGetShare (int appID, int siteID) throws JahiaException { 225 226 Connection dbConn = null; 227 Statement statement = null; 228 229 AppShare share = null; 230 231 try { 232 StringBuffer sqlQuery = new StringBuffer ( 233 "SELECT * FROM jahia_apps_share where id_aps_appid="); 234 sqlQuery.append (appID); 235 sqlQuery.append (" and "); 236 sqlQuery.append (" id_aps_siteid= "); 237 sqlQuery.append (siteID); 238 239 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 240 statement = dbConn.createStatement (); 241 if (statement != null) { 242 ResultSet rs = statement.executeQuery (sqlQuery.toString ()); 243 244 while (rs.next ()) { 245 share = new AppShare (rs.getInt ("id_aps_appid"), 246 rs.getInt ("id_aps_siteid")); 247 } 248 } 249 250 } catch (SQLException se) { 251 String errorMsg = "Error in dbGetShare : " + se.getMessage (); 252 logger.error (errorMsg + " -> BAILING OUT"); 253 throw new JahiaException ("Cannot retrieve site keys from the database", 254 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 255 } finally { 256 257 closeStatement (statement); 258 } 259 260 return share; 261 } 262 263 264 274 public JahiaDOMObject getApplicationSharesAsDOM (int siteID) 275 throws JahiaException { 276 277 Connection dbConn = null; 278 Statement statement = null; 279 280 String output = null; 281 JahiaDBDOMObject dom = null; 282 283 try { 284 289 290 String sqlQuery = "SELECT DISTINCT jahia_apps_share.id_aps_appid,jahia_apps_share.id_aps_siteid" 291 + " FROM jahia_apps_share,jahia_app_def" 292 + " WHERE jahia_apps_share.id_aps_appid=" 293 + "jahia_app_def.id_jahia_app_def AND jahia_app_def.jahiaid_jahia_app_def=" + siteID; 294 295 296 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 297 statement = dbConn.createStatement (); 298 if (statement != null) { 299 ResultSet rs = statement.executeQuery (sqlQuery); 300 if (rs != null) { 301 dom = new JahiaDBDOMObject (); 302 dom.addTable ("jahia_apps_share", rs); 303 return dom; 304 } 305 } 306 } catch (SQLException se) { 307 String errorMsg = "Error in getApplicationSharesAsDOM(int siteID) : " + se.getMessage (); 308 logger.error (errorMsg); 309 throw new JahiaException ("Cannot load data from the database", 310 errorMsg, JahiaException.DATABASE_ERROR, 311 JahiaException.CRITICAL_SEVERITY); 312 } finally { 313 314 closeStatement (statement); 315 } 316 317 return dom; 318 } 319 320 321 private void executeQueryNoResultSet (String queryStr) throws JahiaException { 323 324 Connection dbConn = null; 325 Statement statement = null; 326 327 try { 328 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 329 statement = dbConn.createStatement (); 330 if (statement != null) { 331 statement.executeUpdate (queryStr); 332 } 333 } catch (SQLException se) { 334 String errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage (); 335 logger.error (errorMsg + " -> BAILING OUT"); 336 throw new JahiaException ("Cannot execute query" + queryStr, 337 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 338 } finally { 339 340 closeStatement (statement); 341 } 342 343 } 344 345 private void closeStatement (Statement statement) { 347 try { 349 if (statement != null) { 350 statement.close (); 351 } 352 } catch (SQLException sqlEx) { 353 logger.error ("Error closing statement", sqlEx); 356 } 357 } 358 359 } 360 361 | Popular Tags |