KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > filemanager > JahiaFileFieldsManager


1 package org.jahia.services.filemanager;
2
3 import org.jahia.data.files.JahiaFile;
4 import org.jahia.data.files.JahiaFileField;
5 import org.jahia.exceptions.JahiaException;
6 import org.jahia.registries.ServicesRegistry;
7
8 import java.sql.*;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Properties JavaDoc;
11
12
13 /**
14  * <p>Title: </p>
15  * <p>Description: </p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: </p>
18  *
19  * @author Khue Nguyen
20  * @version 1.0
21  */

22
23 public class JahiaFileFieldsManager {
24
25     private static org.apache.log4j.Logger logger =
26             org.apache.log4j.Logger.getLogger (JahiaFileFieldsManager.class);
27
28     private static String JavaDoc CLASS_NAME = JahiaFileFieldsManager.class.getName ();
29
30     private static JahiaFileFieldsManager fileFieldsManager = null;
31
32     /** debug flag * */
33     public static boolean m_DebugQuery = false;
34
35     public JahiaFileFieldsManager () {
36     }
37
38     public static JahiaFileFieldsManager getInstance () {
39         if (fileFieldsManager == null) {
40             fileFieldsManager = new JahiaFileFieldsManager ();
41         }
42         return fileFieldsManager;
43     }
44
45     /**
46      * Return a Jahia File Field object looking at the id
47      *
48      * @param the id
49      *
50      * @return a (JahiaFileField) or null if not found
51      */

52     public JahiaFileField getJahiaFileField (int id) {
53         JahiaFileField fField = null;
54         Connection dbConn = null;
55         PreparedStatement stmt = null;
56         ResultSet rs = null;
57         String JavaDoc fileFieldTitle = null;
58
59         try {
60             // get a connection to the db
61
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
62             ;
63
64             // execute the query
65
String JavaDoc query = "select * from jahia_file_fields where id_jahia_filefield=?";
66             stmt = dbConn.prepareStatement (query);
67             stmt.setInt (1, id);
68             debugQuery (query);
69             rs = stmt.executeQuery ();
70             fField = getJahiaFileFieldFromResultSet (rs);
71         } catch (SQLException ex) {
72             processSQLException (ex);
73         } catch (JahiaException je) {
74             logger.debug (je.getMessage ());
75         } finally {
76             try {
77                 if (stmt != null) stmt.close ();
78             } catch (SQLException ex) {
79                 processSQLException (ex);
80             }
81         }
82         return fField;
83     }
84
85     /**
86      * @param the Jahia File Field
87      *
88      * @return true if no error
89      */

90     public boolean insertJahiaFileField (JahiaFileField fField)
91             throws JahiaException {
92         if (fField == null) {
93             return false;
94         }
95         Connection dbConn = null;
96         PreparedStatement pstmt = null;
97         boolean success = false;
98
99         try {
100             int id = fField.getID ();
101             if (id == -1) {
102                 id = ServicesRegistry.getInstance ()
103                         .getJahiaIncrementorsDBService ()
104                         .autoIncrement ("jahia_file_fields");
105             } else {
106                 // delete old value first
107
this.deleteJahiaFileField (fField.getID ());
108             }
109
110             // get a connection to the db
111
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
112
113             Properties JavaDoc properties = fField.getProperties ();
114
115             // prepare the file id properties to store
116
properties.setProperty (JahiaFileField.FIELD_FILE_FILEID_PROP,
117                     String.valueOf (fField.getFileID ()));
118
119             Enumeration JavaDoc propNames = properties.keys ();
120             while (propNames.hasMoreElements ()) {
121                 String JavaDoc curPropName = (String JavaDoc) propNames.nextElement ();
122                 String JavaDoc curPropValue = properties.getProperty (curPropName);
123                 pstmt = dbConn.prepareStatement ("INSERT INTO jahia_file_fields VALUES(?,?,?)");
124                 pstmt.setInt (1, id);
125                 pstmt.setString (2, curPropName);
126                 pstmt.setString (3, curPropValue);
127                 pstmt.execute ();
128             }
129             fField.setID (id);
130             success = true;
131         } catch (SQLException ex) {
132             processSQLException (ex);
133             success = false;
134         } finally {
135             try {
136                 if (pstmt != null) pstmt.close ();
137             } catch (SQLException ex) {
138                 processSQLException (ex);
139             }
140         }
141         return success;
142     }
143
144     /**
145      * Delete a Jahia File field from database
146      *
147      * @param id
148      *
149      * @return true if no error
150      */

151     public boolean deleteJahiaFileField (int id)
152             throws JahiaException {
153
154         Connection dbConn = null;
155         Statement stmt = null;
156         boolean success = false;
157
158         try {
159             // get a connection to the db
160
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
161
162             // execute the query
163
String JavaDoc query = " delete from jahia_file_fields where id_jahia_filefield="
164                     + id;
165
166             stmt = dbConn.createStatement ();
167             debugQuery (query);
168             stmt.execute (query);
169             success = true;
170         } catch (SQLException ex) {
171             processSQLException (ex);
172             success = false;
173         } finally {
174             try {
175                 if (stmt != null) stmt.close ();
176             } catch (SQLException ex) {
177                 processSQLException (ex);
178             }
179         }
180         return success;
181     }
182
183     /**
184      * Copy a file for a cloned page
185      *
186      * @param the Jahia File Field ID
187      * @param the cloned page ID
188      *
189      * @return true if no error
190      *
191      * @todo not implemented yet
192      */

193     public boolean copyFileField (int oldJahiaFileFieldID,
194                                   int newFieldID,
195                                   int pageID) throws JahiaException {
196         // @todo
197
return false;
198     }
199
200     /**
201      * Returns a JahiaFileField object from a resultset
202      *
203      * @param rs
204      *
205      * @return
206      *
207      * @throws JahiaException
208      */

209     protected JahiaFileField getJahiaFileFieldFromResultSet (ResultSet rs)
210             throws JahiaException {
211
212         if (rs == null)
213             throw new JahiaException (CLASS_NAME + ".getJahiaFileFieldFromResultSet",
214                     "Resultset is null",
215                     JahiaException.DATABASE_ERROR,
216                     JahiaException.ERROR_SEVERITY);
217
218         JahiaFileField fField = null;
219         int id = -1;
220         String JavaDoc name = "";
221         String JavaDoc value = "";
222         Properties JavaDoc properties = new Properties JavaDoc ();
223         try {
224             while (rs.next ()) {
225                 id = rs.getInt ("id_jahia_filefield");
226                 name = rs.getString ("prop_name_filefield");
227                 value = rs.getString ("prop_value_filefield");
228                 if (value == null) {
229                     value = "";
230                 }
231                 if (name != null) {
232                     properties.setProperty (name, value);
233                 }
234             }
235         } catch (SQLException se) {
236             String JavaDoc errorMsg = "DB Error : " + se.getMessage ();
237             logger.debug (errorMsg);
238             throw new JahiaException (CLASS_NAME + ".getJahiaFileFieldFromResultSet",
239                     errorMsg, JahiaException.DATABASE_ERROR,
240                     JahiaException.CRITICAL_SEVERITY);
241         }
242
243         JahiaFile aFile = null;
244         String JavaDoc val = properties.getProperty (JahiaFileField.FIELD_FILE_FILEID_PROP);
245         String JavaDoc versionVal = properties.getProperty (JahiaFileField.FIELD_FILE_VERSION_PROP);
246         if (val != null) {
247             try {
248                 aFile =
249                         ServicesRegistry.getInstance ()
250                         .getJahiaFilemanagerService ().getFileDB (Integer.parseInt (val),
251                                 versionVal);
252             } catch (Throwable JavaDoc t) {
253                 t.printStackTrace ();
254             }
255         }
256         if (aFile == null) {
257             aFile = new JahiaFile (-1, // filemanager id
258
-1, // folder id
259
"", // upload user
260
"", // realname
261
"", // storage name
262
0, // modif date
263
0, // size
264
"", // type
265
"", // title
266
"", // descr
267
String.valueOf (ServicesRegistry.getInstance ()
268                     .getJahiaVersionService ().getCurrentVersionID ()), // version
269
JahiaFile.STATE_ACTIVE);
270         }
271         fField = new JahiaFileField (aFile, properties);
272         fField.setID (id);
273         return fField;
274     }
275
276     /**
277      * Returns a properties as a String.
278      *
279      * @param id
280      * @param key
281      *
282      * @return
283      *
284      * @throws JahiaException
285      */

286     String JavaDoc getProperty (int id, String JavaDoc key)
287             throws JahiaException {
288
289         Connection dbConn = null;
290         PreparedStatement pstmt = null;
291         String JavaDoc value = null;
292
293         if ((key == null) || (key.trim ().equals ("")))
294             return null;
295
296         try {
297
298             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
299
300             pstmt =
301                     dbConn.prepareStatement (
302                             "SELECT prop_value_filefield FROM jahia_file_fields WHERE id_jahia_filefield=? AND prop_name_filefield=?");
303             pstmt.setInt (1, id);
304             pstmt.setString (2, key);
305             ResultSet rs = pstmt.executeQuery ();
306             if (rs != null) {
307                 if (rs.next ()) {
308                     value = rs.getString ("prop_value_filefield");
309                 }
310             }
311         } catch (SQLException se) {
312             logger.debug ("Exception retrieving a propertey from db : " + se.getMessage ());
313             throw new JahiaException (CLASS_NAME + ".getProperty(id,key)",
314                     "Cannot load file field prop from the database"
315                     + se.getMessage (),
316                     JahiaException.DATABASE_ERROR,
317                     JahiaException.CRITICAL_SEVERITY);
318         } finally {
319
320             closeStatement ((Statement) pstmt);
321         }
322
323         return value;
324
325     }
326
327     /**
328      * add a property . Do not check if there is already a same
329      * property in db.You must remove it first.
330      *
331      * @param id
332      * @param key
333      * @param value
334      *
335      * @throws JahiaException
336      */

337     void addProperty (int id, String JavaDoc key, String JavaDoc value) throws JahiaException {
338
339         if ((key == null) || (key.trim ().equals ("")))
340             return;
341
342         Connection dbConn = null;
343         PreparedStatement pstmt = null;
344
345         try {
346
347             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
348             pstmt = dbConn.prepareStatement ("INSERT INTO jahia_file_fields VALUES (?,?,?)");
349             pstmt.setInt (1, id);
350             pstmt.setString (2, key);
351             pstmt.setString (3, value);
352             pstmt.execute ();
353
354         } catch (SQLException se) {
355             String JavaDoc errorMsg = "Error adding property :" + se.getMessage ();
356             logger.debug (errorMsg);
357             throw new JahiaException ("Cannot add a property in the database",
358                     errorMsg,
359                     JahiaException.DATABASE_ERROR,
360                     JahiaException.CRITICAL_SEVERITY);
361         } finally {
362
363             closeStatement ((Statement) pstmt);
364         }
365     }
366
367     /**
368      * delete all properties for a given id
369      *
370      * @param id
371      */

372     void deleteProperties (int id) throws JahiaException {
373
374         try {
375             StringBuffer JavaDoc buff =
376                     new StringBuffer JavaDoc (
377                             "DELETE FROM jahia_file_fields WHERE id_jahia_filefield=");
378             buff.append (id);
379             executeQueryNoResultSet (buff.toString ());
380         } catch (JahiaException je) {
381             String JavaDoc errorMsg = "Error in deleteProperties(id) : ";
382             logger.debug (errorMsg);
383             throw new JahiaException (
384                     "Cannot delete all properties for the given file field: " + id,
385                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
386         }
387     }
388
389     /**
390      * Delete a property
391      *
392      * @param id
393      * @param key
394      *
395      * @throws JahiaException
396      */

397     void deleteProperty (int id, String JavaDoc key) throws JahiaException {
398
399         if ((key == null) || (key.trim ().equals ("")))
400             return;
401
402         Connection dbConn = null;
403         PreparedStatement pstmt = null;
404
405         try {
406             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
407             pstmt =
408                     dbConn.prepareStatement (
409                             "DELETE FROM jahia_file_fields WHERE id_jahia_filefield=? AND prop_name_filefield=? ");
410             pstmt.setInt (1, id);
411             pstmt.setString (2, key);
412         } catch (SQLException se) {
413             String JavaDoc errorMsg = "Error in deleteProperty(int id, String key) : "
414                     + se.getMessage ();
415             logger.debug (errorMsg);
416             throw new JahiaException ("Cannot delete a property in the database",
417                     errorMsg,
418                     JahiaException.DATABASE_ERROR,
419                     JahiaException.CRITICAL_SEVERITY);
420         } finally {
421
422             closeStatement ((Statement) pstmt);
423         }
424     }
425
426     /**
427      * Method debugQuery
428      * Outout the query for debug purpose
429      *
430      * @param query
431      */

432     protected static void debugQuery (String JavaDoc query) {
433         if (m_DebugQuery) {
434             logger.debug (query);
435         }
436     }
437
438     /**
439      * Method processSQLException
440      * For debug purpose
441      *
442      * @param the SQLException
443      */

444     protected void processSQLException (SQLException ex) {
445         if (m_DebugQuery) {
446             while (ex != null) {
447                 logger.debug ("SQL EXCEPTION: SqlState = " + ex.getSQLState ()
448                         + " Error code = " + ex.getErrorCode ()
449                         + " Error msg = " + ex.toString ());
450                 ex = ex.getNextException ();
451             }
452         }
453     }
454
455     private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
456         Connection dbConn = null;
457         Statement statement = null;
458
459         try {
460             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
461             statement = dbConn.createStatement ();
462             if (statement != null) {
463                 statement.executeUpdate (queryStr);
464             }
465         } catch (SQLException se) {
466             String JavaDoc errorMsg =
467                     "Error in executeQueryNoResultSet(String queryStr) : "
468                     + se.getMessage ();
469             logger.debug (errorMsg);
470             throw new JahiaException ("Cannot execute query" + queryStr,
471                     errorMsg,
472                     JahiaException.DATABASE_ERROR,
473                     JahiaException.CRITICAL_SEVERITY);
474         } finally {
475
476             closeStatement (statement);
477         }
478     }
479
480     //-------------------------------------------------------------------------
481
private void closeStatement (Statement statement) {
482         // Close the opened statement
483
try {
484             if (statement != null) {
485                 statement.close ();
486                 statement = null;
487             }
488         } catch (SQLException sqlEx) {
489             // just create an exception without raising it, just to notify it
490
// in the logs.
491
JahiaException je = new JahiaException ("Cannot close a statement",
492                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
493                     JahiaException.WARNING_SEVERITY);
494         }
495     }
496 }
Popular Tags