1 19 20 package org.jahia.data.containers; 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 import java.util.Enumeration ; 30 import java.util.Vector ; 31 32 import org.jahia.exceptions.JahiaException; 33 import org.jahia.params.ParamBean; 34 import org.jahia.registries.ServicesRegistry; 35 import org.jahia.services.version.EntryLoadRequest; 36 import org.jahia.utils.JahiaConsole; 37 import org.jahia.utils.JahiaTools; 38 39 40 51 52 public class ContainerFilters implements Serializable { 53 54 private static org.apache.log4j.Logger logger = 55 org.apache.log4j.Logger.getLogger(ContainerFilters.class); 56 57 private static final String CLASS_NAME = ContainerFilters.class.getName(); 58 59 private int ctnListID = -1; 60 61 private int siteId = -1; 62 63 private String containerDefinitionName; 64 65 private boolean siteModeFiltering = false; 66 67 private BitSet bits; 68 69 private Vector containerFilters; 70 71 private String query; 72 73 private boolean isValid = false; 74 75 private boolean updated = false; 76 77 private long lastFilteringTime = -1; 78 79 private ArrayList stagingFields = null; 80 81 90 public ContainerFilters(int ctnListID, Vector containerFilters) 91 throws JahiaException 92 { 93 this.ctnListID = ctnListID; 94 this.containerFilters = containerFilters; 95 initContainerFilters(); 96 buildQuery(); 97 } 98 99 109 public ContainerFilters(String containerListName, ParamBean params, Vector containerFilters) 110 throws JahiaException 111 { 112 this.containerFilters = containerFilters; 114 initContainerFilters(); 115 116 if ( containerListName != null ){ 117 int clistID = ServicesRegistry.getInstance().getJahiaContainersService(). 118 getContainerListID( containerListName, params.getPage().getID() ); 119 if ( clistID != -1 ){ 120 this.ctnListID = clistID; 121 buildQuery(); 122 } 123 } 124 } 125 126 142 public ContainerFilters(Vector containerFilters, int siteId, 143 String containerDefinitionName) 144 throws JahiaException 145 { 146 this.siteId = siteId; 147 this.siteModeFiltering = true; 148 this.containerDefinitionName = containerDefinitionName; 149 150 this.containerFilters = containerFilters; 151 initContainerFilters(); 152 buildQuery(); 153 } 154 155 164 public BitSet doFilter() 165 throws JahiaException 166 { 167 if ( !isQueryValid() ) 168 return null; 169 170 172 BitSet result = new BitSet (); 173 174 BitSet bits = null; 175 ContainerFilterInterface containerFilter = null; 176 Vector v = getContainerFilters(); 177 int size = v.size(); 178 for ( int i=0 ; i<size ; i++ ) 179 { 180 bits = new BitSet (); 181 containerFilter = (ContainerFilterInterface)v.get(i); 182 if ( this.isSiteModeFiltering() ){ 183 bits = containerFilter.doFilterBySite(getSiteId(), 184 this.getContainerDefinitionName(),getCtnListID()); 185 } else { 186 bits = containerFilter.doFilter(getCtnListID()); 187 } 188 if ( bits != null ) 189 { 190 if ( i == 0 ){ 191 result.or(bits); 192 } else { 193 result.and(bits); 194 } 195 } 196 } 197 this.bits = result; 198 this.updated = true; 199 200 this.lastFilteringTime = System.currentTimeMillis(); 202 this.stagingFields = null; 203 return result; 204 } 205 206 213 public BitSet bits() 214 { 215 return this.bits; 216 } 217 218 225 public boolean isSiteModeFiltering() 226 { 227 return this.siteModeFiltering; 228 } 229 230 236 public int getCtnListID() 237 { 238 return this.ctnListID; 239 } 240 241 public void setCtnListID(int id){ 242 this.ctnListID = id; 243 } 244 245 250 public int getSiteId() 251 { 252 return this.siteId; 253 } 254 255 260 public String getContainerDefinitionName() 261 { 262 return this.containerDefinitionName; 263 } 264 265 274 public String getQuery() 275 { 276 return this.query; 277 } 278 279 285 public Vector getContainerFilters() 286 { 287 return this.containerFilters; 288 } 289 290 296 public boolean isQueryValid() 297 { 298 return this.isValid; 299 } 300 301 307 public long getLastFilteringTime() 308 { 309 return this.lastFilteringTime; 310 } 311 312 318 public boolean getUpdateStatus() 319 { 320 return this.updated; 321 } 322 323 328 public void setUpdateStatus() 329 { 330 this.updated = true; 331 } 332 333 338 public void resetUpdateStatus() 339 { 340 this.updated = false; 341 } 342 343 351 public ArrayList getStagingFields(boolean loadFromDB, 352 String containerDefinitionName, 353 EntryLoadRequest entryLoadRequest) 354 throws JahiaException 355 { 356 357 if ( !loadFromDB && this.stagingFields != null ){ 358 return this.stagingFields; 359 } 360 361 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b, jahia_ctn_def c WHERE "); 362 if ( !this.isSiteModeFiltering() ){ 363 buff.append(" a.listid_jahia_ctn_entries="); 364 buff.append(this.ctnListID); 365 buff.append(" AND "); 366 } else { 367 if ( this.getSiteId() != -1 ){ 368 buff.append("a.jahiaid_jahia_ctn_entries="); 369 buff.append(this.siteId); 370 buff.append(" AND "); 371 } 372 373 if ( containerDefinitionName != null && 374 !"".equals(containerDefinitionName.trim()) ){ 375 buff.append(" a.ctndefid_jahia_ctn_entries = c.id_jahia_ctn_def "); 376 buff.append(" AND c.name_jahia_ctn_def='"); 377 buff.append(JahiaTools.quote(containerDefinitionName)); 378 buff.append("' AND "); 379 } 380 381 } 382 buff.append(" a.id_jahia_ctn_entries=b.ctnid_jahia_fields_data AND b.workflow_state>1 "); 383 buff.append(" AND ( "); 384 buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(entryLoadRequest,false,true)); 385 buff.append(" )"); 386 387 Connection dbConn = null; 388 Statement stmt = null; 389 ResultSet rs = null; 390 391 ArrayList datas = new ArrayList (); 392 393 try 394 { 395 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 396 stmt = dbConn.createStatement(); 397 rs = stmt.executeQuery( buff.toString() ); 398 399 while (rs.next()) { 400 datas.add(new Integer (rs.getInt(1))); 401 } 402 } catch (SQLException se) { 403 String errorMsg = "Error in getStagingFields() : " + se.getMessage(); 404 JahiaConsole.println ("ContainerFilters.getStagingFields()", errorMsg); 405 } finally { 406 407 closeStatement (stmt); 408 } 409 this.stagingFields = datas; 410 return this.stagingFields; 411 } 412 413 419 public boolean compareQuery(String query) 420 { 421 if ( !isQueryValid() || query == null ){ 422 return false; 423 } 424 return (this.query.equals(query)); 425 } 426 427 431 private void initContainerFilters(){ 432 if ( this.containerFilters == null 433 || this.containerFilters.size() == 0 ){ 434 return; 435 } 436 int size = this.containerFilters.size(); 437 for ( int i=0 ; i<size ; i++ ){ 438 ContainerFilterInterface filterBean = 439 (ContainerFilterInterface)this.containerFilters.get(i); 440 if ( filterBean != null ){ 441 filterBean.setContainerFilters(this); 442 } 443 } 444 } 445 446 451 private void buildQuery() 452 { 453 454 456 if ( containerFilters == null || containerFilters.size()==0 ) 457 { 458 return; 459 } 460 461 StringBuffer buff = new StringBuffer (); 462 String fieldFilterQuery; 463 ContainerFilterInterface containerFilter; 464 int size = containerFilters.size(); 465 for ( int i=0 ; i<size ; i++ ) 466 { 467 containerFilter = (ContainerFilterInterface)containerFilters.get(i); 468 if ( containerFilter == null ) 469 return; 470 if ( this.isSiteModeFiltering() ){ 471 fieldFilterQuery = containerFilter.getSelectBySiteID(this.getSiteId(), this.getContainerDefinitionName()); 472 } else { 473 fieldFilterQuery = containerFilter.getSelect(this.getCtnListID()); 474 } 475 476 if ( fieldFilterQuery != null && !fieldFilterQuery.trim().equals("") ) 477 { 478 buff.append(fieldFilterQuery); 479 } 480 if ( i<size-1 ) 481 { 482 buff.append(" INTERSECT "); 483 } 484 } 485 486 this.query = buff.toString(); 487 489 this.isValid = true; 490 } 491 492 private void closeStatement (Statement statement) 494 { 495 try { 497 if (statement!=null) { 498 statement.close(); 499 } 500 } 501 catch (SQLException sqlEx) { 502 JahiaException je = new JahiaException ("Cannot close a statement", 505 "Cannot close a statement", JahiaException.DATABASE_ERROR, 506 JahiaException.WARNING_SEVERITY, sqlEx); 507 logger.error("Error:", je); 508 } 509 } 510 511 public String toString() { 512 StringBuffer result = new StringBuffer (); 513 if (containerFilters != null) { 514 Enumeration filterEnum = containerFilters.elements(); 515 result.append("Container filters:"); 516 result.append(containerFilters); 517 result.append("\n"); 518 } 519 if (bits != null) { 520 result.append("Bitset: "); 521 result.append(bits); 522 result.append("\n"); 523 } 524 if (stagingFields != null) { 525 result.append("Staging fields: "); 526 result.append(stagingFields); 527 result.append("\n"); 528 } 529 return result.toString(); 530 } 531 } 532 | Popular Tags |