1 31 package com.opencms.defaults.master.genericsql; 32 33 import org.opencms.db.CmsDbContext; 34 import org.opencms.db.CmsDbPool; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProject; 37 import org.opencms.main.CmsLog; 38 import org.opencms.util.CmsStringUtil; 39 40 import java.sql.Connection ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.sql.Statement ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Properties ; 48 49 67 public class CmsSqlManager extends org.opencms.db.generic.CmsSqlManager { 68 69 70 72 75 public CmsSqlManager(String dbPoolUrl, Class currentClass) { 76 77 if (!dbPoolUrl.startsWith(CmsDbPool.DBCP_JDBC_URL_PREFIX)) { 78 dbPoolUrl = CmsDbPool.DBCP_JDBC_URL_PREFIX + dbPoolUrl; 79 } 80 m_poolUrl = dbPoolUrl; 81 82 m_queries = new HashMap (); 83 m_cachedQueries = new HashMap (); 84 85 loadQueries(currentClass); 86 combineQueries(); 87 88 } 89 90 97 protected void loadQueries(Class currentClass) { 98 99 Properties properties = new Properties (); 100 101 String className = currentClass.getName(); 104 String queryFilename = className.substring(0, className.lastIndexOf('.')); 105 queryFilename = queryFilename.replace('.','/') + "/query.properties"; 106 Class superClass = currentClass.getSuperclass(); 108 if(superClass != java.lang.Object .class) { 109 loadQueries(superClass); 110 } 111 try { 112 properties.load(getClass().getClassLoader().getResourceAsStream(queryFilename)); 115 m_queries.putAll(properties); 116 } catch(Exception exc) { 117 if(CmsLog.getLog(this).isErrorEnabled()) { 119 CmsLog.getLog(this).error("Couldn't load " + queryFilename, exc); 120 } 121 } 122 } 123 124 130 protected void combineQueries() { 131 Iterator keys = m_queries.keySet().iterator(); 132 while(keys.hasNext()) { 133 String key = (String )keys.next(); 134 while(replace(key)); 136 } 137 } 138 139 145 public Connection getConnection() throws SQLException { 146 147 return getConnection(0); 148 } 149 150 153 public Connection getConnection(int projectId) throws SQLException { 154 155 return super.getConnection(projectId); 156 } 157 158 166 public Connection getConnection(CmsProject project) throws SQLException { 167 168 return super.getConnection(project.getId()); 169 } 170 171 177 protected boolean replace(String key) { 178 boolean retValue = false; 179 String value = (String )m_queries.get(key); 180 String newValue = new String (); 181 int index = 0; 182 int lastIndex = 0; 183 while(index != -1) { 185 index = value.indexOf("${", lastIndex); 186 if(index != -1) { 187 retValue = true; 188 int nextIndex = value.indexOf('}', index); 189 if(nextIndex != -1) { 190 String replacer = value.substring(index+2, nextIndex); 192 newValue += value.substring(lastIndex, index); 194 String replacerStr = (String )m_queries.get(replacer); 196 if (replacerStr == null) { 197 replacerStr = ""; 198 } 199 newValue += replacerStr; 200 lastIndex = nextIndex+1; 202 } else { 203 newValue += value.substring(lastIndex, index+2); 205 lastIndex = index+2; 207 } 208 } else { 209 newValue += value.substring(lastIndex); 211 } 212 } 213 m_queries.put(key, newValue); 215 return retValue; 217 } 218 219 226 public PreparedStatement sqlPrepare(CmsObject cms, Connection conn, String queryKey) throws SQLException { 227 return sqlPrepare(cms, conn, queryKey, null); 228 } 229 230 240 public PreparedStatement sqlPrepare(CmsObject cms, Connection conn, String queryKey, HashMap optionalSqlTokens) throws SQLException { 241 String statement = null; 242 String moduleMaster = "CMS_MODULE_MASTER"; 243 String channelRel = "CMS_MODULE_CHANNEL_REL"; 244 String media = "CMS_MODULE_MEDIA"; 245 246 String statementStr = (String )m_queries.get(queryKey); 248 if(statementStr == null) { 249 statementStr = ""; 250 } 251 statement = statementStr; 252 253 if (cms.getRequestContext().currentProject().isOnlineProject()) { 255 moduleMaster = "CMS_MODULE_ONLINE_MASTER"; 256 channelRel = "CMS_MODULE_ONLINE_CHANNEL_REL"; 257 media = "CMS_MODULE_ONLINE_MEDIA"; 258 } 259 260 statement = CmsStringUtil.substitute(statement, "$CMS_MODULE_MASTER", moduleMaster); 262 statement = CmsStringUtil.substitute(statement, "$CMS_MODULE_CHANNEL_REL", channelRel); 263 statement = CmsStringUtil.substitute(statement, "$CMS_MODULE_MEDIA", media); 264 265 if (optionalSqlTokens != null) { 267 Iterator optionalSqlKeys = optionalSqlTokens.keySet().iterator(); 268 while (optionalSqlKeys.hasNext()) { 269 String currentKey = (String ) optionalSqlKeys.next(); 270 String currentValue = (String ) optionalSqlTokens.get(currentKey); 271 statement = CmsStringUtil.substitute(statement, currentKey, currentValue); 272 } 273 } 274 275 return conn.prepareStatement(statement); 276 } 277 278 284 public String readQuery(String queryKey) { 285 String value = null; 286 if ((value = (String )m_queries.get(queryKey)) == null) { 287 if (CmsLog.getLog(this).isErrorEnabled()) { 288 CmsLog.getLog(this).error("Query '" + queryKey + "' not found"); 289 } 290 } 291 return value; 292 } 293 294 297 public void closeAll(CmsDbContext dbc, Connection con, Statement stmnt, ResultSet res) { 298 299 if (dbc == null) { 300 dbc = new CmsDbContext(); 301 } 302 super.closeAll(dbc, con, stmnt, res); 303 } 304 305 } 306 | Popular Tags |