KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > shares > AppsSharePersistance


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// AppsSharePersistance
14
//
15
// NK 12.03.2001
16
//
17
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 JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Vector JavaDoc;
29
30
31 /**
32  * Persistance Storage of Applications share
33  *
34  * @author Khue ng
35  */

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     /**
44      * constructor
45      */

46     protected AppsSharePersistance () {
47
48     }
49
50
51     //--------------------------------------------------------------------------
52
/**
53      * return the singleton instance of this class
54      */

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     //--------------------------------------------------------------------------
65
/**
66      * return the list of all sites having access to an app
67      *
68      * @param int the app ident
69      *
70      * @return Enumeration , enumeration of site's keys
71      */

72     public Enumeration JavaDoc dbGetSites (int appID) throws JahiaException {
73
74         Connection JavaDoc dbConn = null;
75         Statement JavaDoc statement = null;
76
77         Vector JavaDoc vec = new Vector JavaDoc ();
78
79         try {
80             String JavaDoc 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 JavaDoc 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 JavaDoc (siteID));
91                     }
92                 }
93             }
94
95         } catch (SQLException JavaDoc se) {
96             String JavaDoc 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     //--------------------------------------------------------------------------
110
/**
111      * add a new share between a site and an app
112      *
113      * @param int , the app id
114      * @param int , the site id
115      */

116     public void dbAddShare (int appID, int siteID) throws JahiaException {
117
118         if (dbGetShare (appID, siteID) == null) {
119
120             try {
121
122                 StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
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 JavaDoc 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     //--------------------------------------------------------------------------
146
/**
147      * remove a share between a site and an app
148      *
149      * @param int the app id
150      * @param int the site id
151      */

152     public void dbRemoveShare (int appID, int siteID) throws JahiaException {
153
154         try {
155             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
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 JavaDoc 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     //--------------------------------------------------------------------------
170
/**
171      * remove all share referencing a site
172      *
173      * @param int, the site id
174      */

175     public void dbSiteRemoveShares (int siteID) throws JahiaException {
176
177         try {
178             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
179                     "DELETE FROM jahia_apps_share WHERE id_aps_siteid =");
180             sqlQuery.append (siteID);
181             executeQueryNoResultSet (sqlQuery.toString ());
182         } catch (JahiaException je) {
183             String JavaDoc 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     //--------------------------------------------------------------------------
194
/**
195      * remove all share referencing an app
196      *
197      * @param int, the app id
198      */

199     public void dbAppRemoveShares (int appID) throws JahiaException {
200
201         try {
202             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
203                     "DELETE FROM jahia_apps_share WHERE id_aps_appid =");
204             sqlQuery.append (appID);
205             executeQueryNoResultSet (sqlQuery.toString ());
206         } catch (JahiaException je) {
207             String JavaDoc 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     //--------------------------------------------------------------------------
216
/**
217      * return a share object or null if not found
218      *
219      * @param int, the app id
220      * @param int, the site id
221      *
222      * @return AppShare, the share bean
223      */

224     public AppShare dbGetShare (int appID, int siteID) throws JahiaException {
225
226         Connection JavaDoc dbConn = null;
227         Statement JavaDoc statement = null;
228
229         AppShare share = null;
230
231         try {
232             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
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 JavaDoc 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 JavaDoc se) {
251             String JavaDoc 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     //--------------------------------------------------------------------------
265
/**
266      * return a DOM document of application shares content
267      *
268      * @param int the site id
269      *
270      * @return JahiaDOMObject a DOM representation of this object
271      *
272      * @author NK
273      */

274     public JahiaDOMObject getApplicationSharesAsDOM (int siteID)
275             throws JahiaException {
276
277         Connection JavaDoc dbConn = null;
278         Statement JavaDoc statement = null;
279
280         String JavaDoc output = null;
281         JahiaDBDOMObject dom = null;
282
283         try {
284             /*
285             String sqlQuery = "SELECT * FROM jahia_apps_share"
286             +" WHERE id_aps_appid in"
287             +"(SELECT id_jahia_app_def FROM jahia_app_def where jahiaid_jahia_app_def="+siteID+")";
288             */

289
290             String JavaDoc 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 JavaDoc 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 JavaDoc se) {
307             String JavaDoc 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     //-------------------------------------------------------------------------
322
private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
323
324         Connection JavaDoc dbConn = null;
325         Statement JavaDoc 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 JavaDoc se) {
334             String JavaDoc 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     //-------------------------------------------------------------------------
346
private void closeStatement (Statement JavaDoc statement) {
347         // Close the opened statement
348
try {
349             if (statement != null) {
350                 statement.close ();
351             }
352         } catch (SQLException JavaDoc sqlEx) {
353             // just create an exception without raising it, just to notify it
354
// in the logs.
355
logger.error ("Error closing statement", sqlEx);
356         }
357     }
358
359 }
360
361
Popular Tags