1 18 19 package org.jahia.data.containers; 20 21 22 import java.io.Serializable ; 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.ArrayList ; 28 import java.util.BitSet ; 29 30 import org.jahia.exceptions.JahiaException; 31 import org.jahia.services.fields.ContentField; 32 import org.jahia.services.version.EntryLoadRequest; 33 import org.jahia.utils.JahiaConsole; 34 import org.jahia.utils.JahiaTools; 35 36 37 38 47 public class ContainerFilterByContainerDefinition implements Serializable , ContainerFilterInterface{ 48 49 private static org.apache.log4j.Logger logger = 50 org.apache.log4j.Logger.getLogger(ContainerFilterByContainerDefinition.class); 51 52 public static final String VERSION_ID = "version_id"; 53 public static final String WORKFLOW_STATE = "workflow_state"; 54 55 private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT; 56 57 private String containerDefinitionName; 58 59 private ContainerFilters containerFilters = null; 60 61 68 public ContainerFilterByContainerDefinition(String containerDefinitionName, 69 EntryLoadRequest entryLoadRequest){ 70 71 this.containerDefinitionName = containerDefinitionName; 72 if ( entryLoadRequest != null ){ 73 this.entryLoadRequest = entryLoadRequest; 74 } 75 76 } 77 78 84 public String getContainerDefinitionName() 85 { 86 return this.containerDefinitionName; 87 } 88 89 97 public BitSet doFilter(int ctnListID) 98 throws JahiaException 99 { 100 BitSet result = null; 101 102 result = doFiltering(ctnListID); 103 104 return result; 105 } 106 107 114 private BitSet doFiltering(int ctnListID) 115 throws JahiaException 116 { 117 String fieldFilterQuery = getSelect(ctnListID,true); 118 if ( fieldFilterQuery == null && !fieldFilterQuery.trim().equals("") ){ 119 return null; 120 } 121 122 BitSet bits = new BitSet (); 123 124 Connection dbConn = null; 125 Statement stmt = null; 126 ResultSet rs = null; 127 128 ArrayList deletedCtns = getDeletedContainers(ctnListID); 129 130 try 131 { 132 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 133 stmt = dbConn.createStatement(); 134 rs = stmt.executeQuery( fieldFilterQuery ); 135 while (rs.next()) 136 { 137 int ctnID = rs.getInt(1); 138 int workflowState = rs.getInt(2); 139 if ( this.entryLoadRequest.isCurrent() 140 || !deletedCtns.contains(new Integer (ctnID))){ 141 if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 142 workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE; 143 } 144 if ( this.entryLoadRequest.isCurrent() && 145 workflowState == EntryLoadRequest.ACTIVE_WORKFLOW_STATE){ 146 bits.set(ctnID); 147 } else if ( this.entryLoadRequest.isStaging() 148 && workflowState >= EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 149 bits.set(ctnID); 150 } 151 } 152 } 153 } 154 catch (SQLException se) 155 { 156 String errorMsg = "Error in doFiltering : " + se.getMessage(); 157 JahiaConsole.println ("ContainerFilterByContainerDefinition.doFiltering", errorMsg); 158 } finally { 159 160 closeStatement (stmt); 161 } 162 163 return bits; 164 } 165 166 177 public String getSelect(int ctnListID) 178 { 179 return getSelect (ctnListID, false); 180 } 181 182 189 protected String getSelect (int ctnListID, boolean ignoreLang) { 190 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries WHERE listid_jahia_ctn_entries="); 191 buff.append(ctnListID); 192 buff.append(" AND "); 193 buff.append(buildMultilangAndWorlflowQuery(this.entryLoadRequest,ignoreLang)); 194 buff.append(" ORDER BY id_jahia_ctn_entries, workflow_state"); 195 196 return buff.toString(); 197 } 198 205 public void setContainerFilters(ContainerFilters containerFilters){ 206 this.containerFilters = containerFilters; 207 } 208 209 214 static public String buildMultilangAndWorlflowQuery(EntryLoadRequest entryLoadRequest){ 215 216 return buildMultilangAndWorlflowQuery(entryLoadRequest, 217 false); 218 } 219 220 225 static public String buildMultilangAndWorlflowQuery(EntryLoadRequest entryLoadRequest, 226 boolean ignoreLang){ 227 228 StringBuffer strBuf = new StringBuffer (" "); 229 if ( entryLoadRequest.isCurrent()){ 230 strBuf.append(WORKFLOW_STATE); 231 strBuf.append("="); 232 strBuf.append(EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 233 } else if ( entryLoadRequest.isStaging() ) { 234 strBuf.append(WORKFLOW_STATE); 235 strBuf.append(">"); 236 strBuf.append(EntryLoadRequest.VERSIONED_WORKFLOW_STATE); 237 strBuf.append(" AND "); 238 strBuf.append("version_id"); 239 strBuf.append(" <> -1 "); 240 } else { 241 strBuf.append("version_id"); 242 strBuf.append("="); 243 strBuf.append(entryLoadRequest.getVersionID()); 244 } 245 if ( !ignoreLang ) { 246 String languageCode = entryLoadRequest.getFirstLocale(true). 247 toString(); 248 strBuf.append(" AND ("); 249 strBuf.append("language_code"); 250 strBuf.append("='"); 251 strBuf.append(JahiaTools.quote(languageCode)); 252 strBuf.append("' OR "); 253 strBuf.append("language_code"); 254 strBuf.append("='"); 255 strBuf.append(ContentField.SHARED_LANGUAGE); 256 strBuf.append("') "); 257 } 258 262 263 return strBuf.toString(); 264 265 } 266 267 275 static public ArrayList getDeletedContainers(int ctnListID) 276 throws JahiaException 277 { 278 279 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_ctn_entries FROM jahia_ctn_entries WHERE workflow_state>1 AND version_id=-1 AND listid_jahia_ctn_entries="); 280 buff.append(ctnListID); 281 282 Connection dbConn = null; 283 Statement stmt = null; 284 ResultSet rs = null; 285 286 ArrayList datas = new ArrayList (); 287 288 try 289 { 290 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 291 stmt = dbConn.createStatement(); 292 rs = stmt.executeQuery( buff.toString() ); 293 294 while (rs.next()) { 295 datas.add(new Integer (rs.getInt(1))); 296 } 297 } 298 catch (SQLException se) 299 { 300 String errorMsg = "Error in getDeletedContainers() : " + se.getMessage(); 301 JahiaConsole.println ("ContainerFilterBean.getDeletedContainers()", errorMsg); 302 } finally { 303 304 closeStatement (stmt); 305 } 306 return datas; 307 } 308 309 325 public BitSet doFilterBySite(int siteId, String containerDefinitionName, int listId) 326 throws JahiaException 327 { 328 329 BitSet result = null; 330 result = doFilteringBySite(siteId, containerDefinitionName,listId); 331 return result; 332 } 333 334 347 private BitSet doFilteringBySite(int siteId, 348 String containerDefinitionName, int listId) 349 throws JahiaException 350 { 351 String fieldFilterQuery = this.getSelectBySiteID(siteId, containerDefinitionName,true); 352 if ( fieldFilterQuery == null && !fieldFilterQuery.trim().equals("") ){ 353 return null; 354 } 355 356 BitSet bits = new BitSet (); 357 358 Connection dbConn = null; 359 Statement stmt = null; 360 ResultSet rs = null; 361 362 ArrayList deletedCtns = getDeletedContainersBySite(siteId, containerDefinitionName); 363 364 try 365 { 366 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 367 stmt = dbConn.createStatement(); 368 rs = stmt.executeQuery( fieldFilterQuery ); 369 while (rs.next()) 370 { 371 int ctnID = rs.getInt(1); 372 int workflowState = rs.getInt(2); 373 if ( this.entryLoadRequest.isCurrent() 374 || !deletedCtns.contains(new Integer (ctnID))){ 375 if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 376 workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE; 377 } 378 if ( this.entryLoadRequest.isCurrent() 379 && workflowState==EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 380 bits.set(ctnID); 381 } else if ( this.entryLoadRequest.isStaging() 382 && workflowState >= EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 383 bits.set(ctnID); 384 } 385 } 386 } 387 } 388 catch (SQLException se) 389 { 390 String errorMsg = "Error in doStringValueFiltering : " + se.getMessage(); 391 JahiaConsole.println ("ContainerFilterBean.doStringValueFiltering", errorMsg); 392 } finally { 393 394 closeStatement (stmt); 395 } 396 397 return bits; 398 } 399 400 416 public String getSelectBySiteID (int siteId, String containerDefinitionName) { 417 return getSelectBySiteID (siteId, containerDefinitionName, false); 418 } 419 420 432 public String getSelectBySiteID (int siteId, String containerDefinitionName, 433 boolean ignoreLang) { 434 435 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries a, jahia_ctn_def b WHERE "); 436 buff.append(buildMultilangAndWorlflowQuery(this.entryLoadRequest,ignoreLang)); 437 438 if ( siteId != -1 ){ 439 buff.append(" AND "); 440 buff.append(" jahiaid_jahia_ctn_entries="); 441 buff.append(siteId); 442 } 443 444 if ( containerDefinitionName != null && 445 !"".equals(containerDefinitionName.trim()) ){ 446 buff.append(" AND "); 447 buff.append(" a.ctndefid_jahia_ctn_entries = b.id_jahia_ctn_def "); 448 buff.append(" AND b.name_jahia_ctn_def='"); 449 buff.append(JahiaTools.quote(containerDefinitionName)); 450 buff.append("'"); 451 } 452 453 buff.append(" ORDER BY id_jahia_ctn_entries,"); 454 buff.append(WORKFLOW_STATE); 455 456 return buff.toString(); 457 } 458 459 469 static public ArrayList getDeletedContainersBySite(int siteId, 470 String containerDefinitionName) 471 throws JahiaException 472 { 473 474 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_ctn_entries FROM jahia_ctn_entries, jahia_ctn_def WHERE workflow_state>1 AND version_id=-1 "); 475 if ( siteId != -1 ){ 476 buff.append(" AND jahiaid_jahia_ctn_entries="); 477 buff.append(siteId); 478 } 479 480 if ( containerDefinitionName != null && 481 !"".equals(containerDefinitionName.trim()) ){ 482 buff.append(" AND "); 483 buff.append(" ctndefid_jahia_ctn_entries = id_jahia_ctn_def "); 484 buff.append(" AND name_jahia_ctn_def='"); 485 buff.append(JahiaTools.quote(containerDefinitionName)); 486 buff.append("'"); 487 } 488 489 Connection dbConn = null; 490 Statement stmt = null; 491 ResultSet rs = null; 492 493 ArrayList datas = new ArrayList (); 494 495 try 496 { 497 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 498 stmt = dbConn.createStatement(); 499 rs = stmt.executeQuery( buff.toString() ); 500 501 while (rs.next()) { 502 datas.add(new Integer (rs.getInt(1))); 503 } 504 } 505 catch (SQLException se) 506 { 507 String errorMsg = "Error in getDeletedContainers() : " + se.getMessage(); 508 JahiaConsole.println ("ContainerFilterBean.getDeletedContainers()", errorMsg); 509 } finally { 510 511 closeStatement (stmt); 512 } 513 return datas; 514 } 515 516 static private void closeStatement (Statement statement) 518 { 519 try { 521 if (statement!=null) { 522 statement.close(); 523 } 524 } 525 catch (SQLException sqlEx) { 526 logger.error("Error closing statement", sqlEx); 529 } 530 } 531 532 } 533 | Popular Tags |