KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > JahiaGroupDBUtils


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 package org.jahia.services.usermanager;
14
15 import org.jahia.exceptions.JahiaException;
16 import org.jahia.exceptions.database.JahiaDatabaseConnectionException;
17 import org.jahia.exceptions.database.JahiaDatabaseException;
18
19 import java.sql.*;
20 import java.util.Properties JavaDoc;
21
22
23 /**
24  * @author Khue Nguyen
25  * @version 1.1
26  */

27 class JahiaGroupDBUtils {
28     static private JahiaGroupDBUtils mObject = null;
29
30
31     //--------------------------------------------------------------------------
32
private JahiaGroupDBUtils () {
33     }
34
35
36     //--------------------------------------------------------------------------
37
/**
38      * Get the user database utilities object reference.
39      *
40      * @return The user database utilities reference.
41      */

42     static public JahiaGroupDBUtils getInstance () {
43         if (mObject == null) {
44             mObject = new JahiaGroupDBUtils ();
45         }
46         return mObject;
47     }
48
49
50     //--------------------------------------------------------------------------
51
/**
52      * Remove the specified property from the properties list.
53      *
54      * @param key Property's name.
55      *
56      * @return Return true on success or false on any failure.
57      */

58     public synchronized boolean removeProperty (String JavaDoc propertyKey, int grpID,
59                                                 String JavaDoc providerName, String JavaDoc groupKey)
60             throws JahiaDatabaseException,
61             JahiaDatabaseConnectionException,
62             JahiaException {
63         boolean result = false;
64
65         String JavaDoc query = "DELETE FROM jahia_grp_prop WHERE" +
66                 " id_jahia_grp=" + grpID +
67                 " AND name_jahia_grp_prop='" + propertyKey + "'" +
68                 " AND provider_jahia_grp_prop='" + providerName + "'" +
69                 " AND grpkey_jahia_grp_prop='" + groupKey + "'";
70
71         return makeQuery (query);
72     }
73
74
75     //--------------------------------------------------------------------------
76
/**
77      * Add (or update if not already in the property list) a property key-value
78      * pair in the grp's properties list.
79      *
80      * @param key Property's name.
81      * @param value Property's value.
82      *
83      * @return Return true on success or false on any failure.
84      */

85     public synchronized boolean addProperty (String JavaDoc key, String JavaDoc value,
86                                              int grpID, String JavaDoc providerName, String JavaDoc groupKey)
87             throws JahiaDatabaseException,
88             JahiaDatabaseConnectionException,
89             JahiaException {
90         boolean result = false;
91
92         String JavaDoc query = "INSERT INTO jahia_grp_prop (id_jahia_grp, " +
93                 "name_jahia_grp_prop, value_jahia_grp_prop, provider_jahia_grp_prop, grpkey_jahia_grp_prop)" +
94                 " VALUES (" + grpID + ",'" + key + "','" + value + "','" + providerName + "','" + groupKey + "')";
95
96         return makeQuery (query);
97     }
98
99
100     //--------------------------------------------------------------------------
101
/**
102      * Add (or update if not already in the property list) a property key-value
103      * pair in the grp's properties list.
104      *
105      * @param key Property's name.
106      * @param value Property's value.
107      *
108      * @return Return true on success or false on any failure.
109      */

110     public synchronized boolean updateProperty (String JavaDoc key, String JavaDoc value,
111                                                 int grpID, String JavaDoc providerName,
112                                                 String JavaDoc groupKey)
113             throws JahiaDatabaseException,
114             JahiaDatabaseConnectionException,
115             JahiaException {
116         boolean result = false;
117
118         String JavaDoc query = "UPDATE jahia_grp_prop SET " +
119                 "value_jahia_grp_prop='" + value + "'" +
120                 " WHERE id_jahia_grp=" + grpID +
121                 " AND name_jahia_grp_prop='" + key + "'" +
122                 " AND provider_jahia_grp_prop='" + providerName + "'" +
123                 " AND grpkey_jahia_grp_prop='" + groupKey + "'";
124
125         return makeQuery (query);
126     }
127
128     public Properties JavaDoc getGroupProperties (int groupID, String JavaDoc providerName, String JavaDoc groupKey)
129             throws JahiaDatabaseException {
130         // Get all the group attributes
131
final String JavaDoc query = "SELECT name_jahia_grp_prop, value_jahia_grp_prop FROM jahia_grp_prop WHERE id_jahia_grp=? AND provider_jahia_grp_prop=? AND grpkey_jahia_grp_prop=?";
132
133         Properties JavaDoc properties = new Properties JavaDoc ();
134         // Get all the user attributes
135
PreparedStatement statement = null;
136         try {
137
138             Connection dbConn = org.jahia.services.database.ConnectionDispenser.
139                     getConnection ();
140             if (dbConn == null) {
141                 return null;
142             }
143             // execute the SELECT query
144
statement = dbConn.prepareStatement (query);
145             statement.setInt (1, groupID);
146             statement.setString (2, providerName);
147             statement.setString (3, groupKey);
148
149             ResultSet rs = statement.executeQuery ();
150
151             if (rs != null) {
152
153                 String JavaDoc propName = null;
154                 String JavaDoc propVal = null;
155
156                 while (rs.next ()) {
157
158                     propName = rs.getString ("name_jahia_grp_prop");
159                     propVal = rs.getString ("value_jahia_grp_prop");
160                     if (propVal == null) {
161                         propVal = "";
162                     }
163
164                     if (propName != null) {
165                         properties.put (propName, propVal);
166                     }
167                 }
168             }
169
170         } catch (SQLException sqlEx) {
171             throw new JahiaDatabaseException (
172                     "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY);
173         } finally {
174             CloseStatement (statement);
175         }
176         return properties;
177     }
178
179     //--------------------------------------------------------------------------
180
// Executes and INSERT, UPDATE or DELETE SQL operation. This method should not
181
// be used with and SELECT operation. This method lock the object on database
182
// write access.
183
private boolean makeQuery (String JavaDoc query)
184             throws JahiaDatabaseException,
185             JahiaDatabaseConnectionException,
186             JahiaException {
187         // Get a database connection
188
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
189         if (dbConn == null) {
190             return false;
191         }
192
193         boolean result = false;
194         Statement statement = null;
195
196         try {
197             statement = dbConn.createStatement ();
198             if (statement != null) {
199                 synchronized (this) {
200                     statement.executeUpdate (query);
201                     result = true;
202                 }
203             }
204         } catch (SQLException sqlEx) {
205             throw new JahiaDatabaseException (
206                     "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY);
207         } finally {
208             CloseStatement (statement);
209         }
210
211         return result;
212     }
213
214
215     //-------------------------------------------------------------------------
216
private void CloseStatement (Statement statement)
217             throws JahiaDatabaseException {
218         // Close the opened statement
219
try {
220             if (statement != null) {
221                 statement.close ();
222             }
223         } catch (SQLException sqlEx) {
224             throw new JahiaDatabaseException (
225                     "Could not close a statement in JahiaGroupDBUtils",
226                     sqlEx, JahiaDatabaseException.ERROR_SEVERITY);
227         }
228     }
229
230 }
231
Popular Tags