KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > JahiaApplicationsPersistanceBaseService


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 JavaDoc;
17
18 /**
19  * Handles all the storage and retrieval of application data from the persistant
20  * storage area, in this implementation from a JDBC database.
21  * You should not directly access this service, but rather through the JahiaApplicationManagerService !
22  *
23  * @author Eric Vassali
24  * @author Serge Huber
25  * @version 1.0
26  */

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 JavaDoc serviceName;
35     private static JahiaApplicationsPersistanceBaseService theObject = null;
36
37     private static final String JavaDoc DELETE_WEBAPP_QUERY =
38             "DELETE FROM jahia_app_def WHERE id_jahia_app_def=?";
39     private static final String JavaDoc 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     /**
42      * Constructor is protected because of Singleton pattern
43      */

44     protected JahiaApplicationsPersistanceBaseService () {
45         logger.debug ("***** Starting *****");
46     } // end constructor
47

48     /**
49      * Retrieves the single instance of the service. If the instance does not
50      * exist it creates it.
51      *
52      * @return Singleton instance of the service
53      */

54     public static synchronized JahiaApplicationsPersistanceBaseService
55             getInstance () {
56         if (theObject == null) {
57             theObject = new JahiaApplicationsPersistanceBaseService ();
58         }
59         return theObject;
60     } // end getInstance
61

62     /**
63      * Retrieves the number of distinct jahia website
64      * exist it creates it.
65      *
66      * @return Singleton instance of the service
67      */

68     public Vector JavaDoc getWebSites ()
69             throws JahiaException {
70
71         Connection dbConn = null;
72         Statement stmt = null;
73         ResultSet rs = null;
74         Vector JavaDoc webSites = new Vector JavaDoc ();
75         try {
76             String JavaDoc 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 JavaDoc (newID));
89                     jahiaID = newID;
90                 }
91             }
92         } catch (SQLException se) {
93             String JavaDoc 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     } // end getWebSites
113

114     /**
115      * return the list of applications
116      *
117      * @param visibleOnly if true return only applications with visible status = 1
118      */

119     public Vector JavaDoc get_applications_list (boolean visibleOnly)
120             throws JahiaException {
121         Connection dbConn = null;
122         Statement stmt = null;
123         ResultSet rs = null;
124         Vector JavaDoc appList = new Vector JavaDoc ();
125         try {
126             String JavaDoc 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 JavaDoc 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 JavaDoc newList = new Vector JavaDoc ();
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     } // end get_applications_list
171

172     /**
173      * return the list of applications for a gived site
174      *
175      * @param visibleOnly if true return only applications with visible status = 1
176      */

177     public Vector JavaDoc get_applications_list (int jahiaID, boolean visibleOnly)
178             throws JahiaException {
179         Connection dbConn = null;
180         Statement stmt = null;
181         ResultSet rs = null;
182         Vector JavaDoc appList = new Vector JavaDoc ();
183         try {
184             String JavaDoc 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 JavaDoc 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 JavaDoc newList = new Vector JavaDoc ();
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     } // end get_applications_list
233

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 JavaDoc 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 JavaDoc 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     } // end get_application_definition
271

272     /**
273      * return an Application Definition , looking at the context property
274      */

275     public ApplicationBean get_application_definition (String JavaDoc context)
276             throws JahiaException {
277         Connection dbConn = null;
278         Statement stmt = null;
279         ResultSet rs = null;
280         ApplicationBean appBean = null;
281         try {
282             String JavaDoc 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 JavaDoc 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     } // end get_application_definition
315

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 JavaDoc 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     } // end get_application_from_resultset
339

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             // gets the field id
348
int theAppID = ServicesRegistry.getInstance ().
349                     getJahiaIncrementorsDBService ().autoIncrement ("jahia_app_def");
350             theApp.setID (theAppID);
351
352             // creates empty line
353
String JavaDoc 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             // executes the query
361
stmt.execute (sqlQuery);
362
363             success = true;
364         } catch (SQLException se) {
365             // catches error if cannot auto-increment id, or if cannot update values
366
String JavaDoc 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             // enters values with update_jahia_field
385
update_application (theApp);
386         }
387     } // end add_application
388

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 JavaDoc 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     } // end update_application
431

432     public void setName (String JavaDoc 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             // delete application role groups
449
Vector JavaDoc vec = GroupsTools.getGroups (true);
450
451             JahiaGroupManagerService gms = ServicesRegistry.getInstance ()
452                     .getJahiaGroupManagerService ();
453
454             int size = vec.size ();
455             JahiaGroup grp = null;
456             String JavaDoc 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             // the following check is here because previous versions of Jahia
468
// had corrupted rights fields that always used ACL#1, which is
469
// normally attached to the first site. We do not want to delete
470
// this ACL if it is set to 1 (and it should never have this value)
471
// because we might corrupt the first site. The Jahia Doctor has
472
// a module to correct this problem so it is recommended that it
473
// is run on old Jahia databases.
474
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 JavaDoc 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     //--------------------------------------------------------------------------
509
/**
510      * return a DOM document of applications definitions
511      *
512      * @param siteID the site id
513      *
514      * @return JahiaDOMObject a DOM representation of this object
515      */

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 JavaDoc 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 JavaDoc 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     //-------------------------------------------------------------------------
555
private void closeStatement (Statement statement) {
556         // Close the opened statement
557
try {
558             if (statement != null) {
559                 statement.close ();
560             }
561         } catch (SQLException sqlEx) {
562             // just create an exception without raising it, just to notify it
563
// in the logs.
564
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     ///////////////////////////////////////////////////////////////////////////////////////////////
572
// FIXME -Fulco-
573
//
574
// The purpose of the method is only to add the creator of an application
575
// (application adding on a page) into the groups "manager" and "administrator".
576
//
577
// REMOVE THIS METHOD AS SOON AS POSSIBLE !!!!!!
578
//
579
///////////////////////////////////////////////////////////////////////////////////////////////
580
public void fake_create_application_groups (
581             ApplicationBean theApp,
582             int fieldID,
583             JahiaUser user)
584             throws JahiaException {
585         String JavaDoc groupname;
586         JahiaGroup group;
587
588         // Add the user to the "Administrator" group
589
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         // Add the user to the "manager" group
599
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     ///////////////////////////////////////////////////////////////////////////////////////////////
610

611 } // end JahiaApplicationsPersistanceBaseService
Popular Tags