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 ; 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 import java.util.Enumeration ; 12 import java.util.Properties ; 13 14 15 24 class FieldDefinitionPropertiesPersistence { 25 26 static private final String GET_ALL_PROP_BYID_QUERY = "SELECT * FROM jahia_fields_def_extprop WHERE id_jahia_fields_def =?"; 27 static private final String INSERT_PROP_QUERY = "INSERT INTO jahia_fields_def_extprop (id_jahia_fields_def, prop_name, prop_value) VALUES (?, ?, ?)"; 28 static private final String UPDATE_PROP_QUERY = "UPDATE jahia_fields_def_extprop SET prop_value=? WHERE id_jahia_fields_def=? AND prop_name=?"; 29 static private final String 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 42 public static synchronized FieldDefinitionPropertiesPersistence getInstance () { 43 if (instance == null) { 44 instance = new FieldDefinitionPropertiesPersistence (); 45 } 46 return instance; 47 } 48 49 protected Properties loadFieldDefinitionProperties (int id) 50 throws JahiaException { 51 52 Properties properties = new Properties (); 53 Connection dbConn = null; 54 PreparedStatement stmt = null; 55 ResultSet 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 propName = rs.getString (2); 66 String propValue = rs.getString (3); 67 properties.setProperty (propName, propValue); 68 } 69 70 } catch (SQLException 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 91 protected void deleteFieldDefinitionProperties (int id) 92 throws JahiaException { 93 94 Connection dbConn = null; 95 PreparedStatement 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 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 propName, String propValue) 116 throws JahiaException { 117 118 Connection dbConn = null; 119 PreparedStatement 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 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 propName, String propValue) throws JahiaException { 144 145 Connection dbConn = null; 146 PreparedStatement 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 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 176 protected void insertFieldDefinitionProperties (int id, Properties properties) 177 throws JahiaException { 178 179 Connection dbConn = null; 180 PreparedStatement stmt = null; 181 String propName = null; 182 String propValue = null; 183 try { 184 185 dbConn = ConnectionDispenser.getConnection (); 186 stmt = dbConn.prepareStatement (INSERT_PROP_QUERY); 187 Enumeration enumeration = properties.propertyNames (); 188 while (enumeration.hasMoreElements ()) { 189 propName = (String ) 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 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 218 protected void saveFieldDefinitionProperties (int id, Properties properties) 219 throws JahiaException { 220 221 Enumeration enumeration = properties.propertyNames (); 222 while (enumeration.hasMoreElements ()) { 223 String name = (String ) enumeration.nextElement (); 224 updateFieldDefinitionProperty (id, name, properties.getProperty (name)); 225 } 226 } 227 228 private void closeStatement (Statement statement) { 229 try { 231 if (statement != null) { 232 statement.close (); 233 } 234 } catch (SQLException sqlEx) { 235 logger.error ("Cannot close a statement", sqlEx); 236 } 237 } 238 239 } | Popular Tags |