KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fields > FieldDefinitionPropertiesPersistence


1 package org.jahia.services.fields;
2
3 import org.jahia.exceptions.JahiaException;
4 import org.jahia.services.database.ConnectionDispenser;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Statement JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Properties JavaDoc;
13
14
15 /**
16  * <p>Title: Field Definition Properties DB persistence</p>
17  * <p>Description: Field Definition Properties DB persistence</p>
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: </p>
20  *
21  * @author Khue Nguyen
22  * @version 1.0
23  */

24 class FieldDefinitionPropertiesPersistence {
25
26     static private final String JavaDoc GET_ALL_PROP_BYID_QUERY = "SELECT * FROM jahia_fields_def_extprop WHERE id_jahia_fields_def =?";
27     static private final String JavaDoc INSERT_PROP_QUERY = "INSERT INTO jahia_fields_def_extprop (id_jahia_fields_def, prop_name, prop_value) VALUES (?, ?, ?)";
28     static private final String JavaDoc UPDATE_PROP_QUERY = "UPDATE jahia_fields_def_extprop SET prop_value=? WHERE id_jahia_fields_def=? AND prop_name=?";
29     static private final String JavaDoc REMOVE_ALL_PROP_BYID_QUERY = "DELETE FROM jahia_fields_def_extprop WHERE id_jahia_fields_def=?";
30
31     private FieldDefinitionPropertiesPersistence () {
32     }
33
34     private static org.apache.log4j.Logger logger =
35             org.apache.log4j.Logger.getLogger (FieldDefinitionPropertiesPersistence.class);
36
37     private static FieldDefinitionPropertiesPersistence instance = null;
38
39     /**
40      * @return an instance of this class
41      */

42     public static synchronized FieldDefinitionPropertiesPersistence getInstance () {
43         if (instance == null) {
44             instance = new FieldDefinitionPropertiesPersistence ();
45         }
46         return instance;
47     }
48
49     protected Properties JavaDoc loadFieldDefinitionProperties (int id)
50             throws JahiaException {
51
52         Properties JavaDoc properties = new Properties JavaDoc ();
53         Connection JavaDoc dbConn = null;
54         PreparedStatement JavaDoc stmt = null;
55         ResultSet JavaDoc rs = null;
56
57         try {
58
59             dbConn = ConnectionDispenser.getConnection ();
60             stmt = dbConn.prepareStatement (GET_ALL_PROP_BYID_QUERY);
61             stmt.setInt (1, id);
62             rs = stmt.executeQuery ();
63
64             while (rs.next ()) {
65                 String JavaDoc propName = rs.getString (2);
66                 String JavaDoc propValue = rs.getString (3);
67                 properties.setProperty (propName, propValue);
68             }
69
70         } catch (SQLException JavaDoc se) {
71             throw new JahiaException ("Cannot load field definition prop for " + id +
72                     "from the database",
73                     se.getMessage (),
74                     JahiaException.DATABASE_ERROR,
75                     JahiaException.ERROR_SEVERITY, se);
76         } finally {
77             closeStatement(stmt);
78         }
79         return properties;
80     }
81
82     /**
83      * Delete All properties form a given id
84      *
85      * @param id
86      *
87      * @return
88      *
89      * @throws JahiaException
90      */

91     protected void deleteFieldDefinitionProperties (int id)
92             throws JahiaException {
93
94         Connection JavaDoc dbConn = null;
95         PreparedStatement JavaDoc stmt = null;
96
97         try {
98
99             dbConn = ConnectionDispenser.getConnection ();
100             stmt = dbConn.prepareStatement (REMOVE_ALL_PROP_BYID_QUERY);
101             stmt.setInt (1, id);
102             stmt.executeUpdate ();
103
104         } catch (SQLException JavaDoc se) {
105             throw new JahiaException ("Cannot delete field definition props for " + id +
106                     "from the database",
107                     se.getMessage (),
108                     JahiaException.DATABASE_ERROR,
109                     JahiaException.ERROR_SEVERITY, se);
110         } finally {
111             closeStatement(stmt);
112         }
113     }
114
115     protected void insertFieldDefinitionProperty (int id, String JavaDoc propName, String JavaDoc propValue)
116             throws JahiaException {
117
118         Connection JavaDoc dbConn = null;
119         PreparedStatement JavaDoc stmt = null;
120
121         try {
122
123             dbConn = ConnectionDispenser.getConnection ();
124             stmt = dbConn.prepareStatement (INSERT_PROP_QUERY);
125             stmt.setInt (1, id);
126             stmt.setString (2, propName);
127             stmt.setString (3, propValue);
128             stmt.executeUpdate ();
129
130         } catch (SQLException JavaDoc se) {
131             throw new JahiaException ("Cannot insert field definition prop for id="
132                     + id + " name=" + propName + " propValue="
133                     + propValue +
134                     "from the database",
135                     se.getMessage (),
136                     JahiaException.DATABASE_ERROR,
137                     JahiaException.ERROR_SEVERITY, se);
138         } finally {
139             closeStatement(stmt);
140         }
141     }
142
143     protected void updateFieldDefinitionProperty(int id, String JavaDoc propName, String JavaDoc propValue) throws JahiaException {
144
145         Connection JavaDoc dbConn = null;
146         PreparedStatement JavaDoc stmt = null;
147
148         try {
149             dbConn = ConnectionDispenser.getConnection();
150             stmt = dbConn.prepareStatement(UPDATE_PROP_QUERY);
151             stmt.setString(1, propValue);
152             stmt.setInt(2, id);
153             stmt.setString(3, propName);
154             int rowCount = stmt.executeUpdate();
155             if (rowCount == 0){
156                 insertFieldDefinitionProperty(id, propName, propValue);
157             }
158         } catch (SQLException JavaDoc se) {
159             throw new JahiaException("Cannot insert field definition prop for id=" + id + " name=" + propName
160                     + " propValue=" + propValue + "from the database", se.getMessage(), JahiaException.DATABASE_ERROR,
161                     JahiaException.ERROR_SEVERITY, se);
162         } finally {
163             closeStatement(stmt);
164         }
165     }
166
167     /**
168      * Inserts the property values for the given id
169      *
170      * @param properties
171      *
172      * @return
173      *
174      * @throws JahiaException
175      */

176     protected void insertFieldDefinitionProperties (int id, Properties JavaDoc properties)
177             throws JahiaException {
178
179         Connection JavaDoc dbConn = null;
180         PreparedStatement JavaDoc stmt = null;
181         String JavaDoc propName = null;
182         String JavaDoc propValue = null;
183         try {
184
185             dbConn = ConnectionDispenser.getConnection ();
186             stmt = dbConn.prepareStatement (INSERT_PROP_QUERY);
187             Enumeration JavaDoc enumeration = properties.propertyNames ();
188             while (enumeration.hasMoreElements ()) {
189                 propName = (String JavaDoc) enumeration.nextElement ();
190                 propValue = properties.getProperty (propName);
191                 stmt.setInt (1, id);
192                 stmt.setString (2, propName);
193                 stmt.setString (3, propValue);
194                 stmt.executeUpdate ();
195             }
196         } catch (SQLException JavaDoc se) {
197             throw new JahiaException ("Cannot insert field definition prop for id="
198                     + id + " name=" + propName + " propValue="
199                     + propValue +
200                     "from the database",
201                     se.getMessage (),
202                     JahiaException.DATABASE_ERROR,
203                     JahiaException.ERROR_SEVERITY, se);
204         } finally {
205             closeStatement(stmt);
206         }
207     }
208     
209     /**
210      * Update/insert the property values for the given id
211      *
212      * @param properties
213      *
214      * @return
215      *
216      * @throws JahiaException
217      */

218     protected void saveFieldDefinitionProperties (int id, Properties JavaDoc properties)
219             throws JahiaException {
220
221         Enumeration JavaDoc enumeration = properties.propertyNames ();
222         while (enumeration.hasMoreElements ()) {
223             String JavaDoc name = (String JavaDoc) enumeration.nextElement ();
224             updateFieldDefinitionProperty (id, name, properties.getProperty (name));
225         }
226     }
227     
228     private void closeStatement (Statement JavaDoc statement) {
229         // Close the opened statement
230
try {
231             if (statement != null) {
232                 statement.close ();
233             }
234         } catch (SQLException JavaDoc sqlEx) {
235             logger.error ("Cannot close a statement", sqlEx);
236         }
237     }
238
239 }
Popular Tags