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.text.Collator ; 28 import java.util.ArrayList ; 29 import java.util.BitSet ; 30 import java.util.Collections ; 31 import java.util.Comparator ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Locale ; 35 import java.util.Vector ; 36 37 import org.jahia.bin.Jahia; 38 import org.jahia.data.fields.ExpressionMarker; 39 import org.jahia.exceptions.JahiaException; 40 import org.jahia.resourcebundle.ResourceBundleMarker; 41 import org.jahia.services.fields.ContentField; 42 import org.jahia.services.version.EntryLoadRequest; 43 import org.jahia.utils.JahiaConsole; 44 import org.jahia.utils.JahiaTools; 45 46 54 55 public class ContainerSorterByContainerDefinition implements Serializable , ContainerSorterInterface { 56 57 private static org.apache.log4j.Logger logger = 58 org.apache.log4j.Logger.getLogger(ContainerSorterByContainerDefinition.class); 59 60 protected int listId = -1; 61 62 protected int siteId = -1; 63 64 protected String fieldName; 65 66 protected String containerDefinitionName; 67 68 protected boolean updated = false; 69 70 protected long lastSortingTime = -1; 71 72 protected boolean numberSort = false; 73 74 protected String numberFormat = NumberFormats.LONG_FORMAT; 75 76 protected boolean isValid = false; 77 78 protected boolean ASC_Ordering = true; 80 protected Vector result; 82 83 protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT; 84 85 public int getCtnListID(){ 86 return this.listId; 87 } 88 89 public void setCtnListID(int id){ 90 this.listId = id; 91 } 92 93 104 public ContainerSorterByContainerDefinition(int siteId, String fieldName, 105 String containerDefinitionName, EntryLoadRequest entryLoadRequest, 106 boolean numberSort) 107 throws JahiaException 108 { 109 this(siteId,fieldName,containerDefinitionName,numberSort,entryLoadRequest); 110 } 111 112 122 public ContainerSorterByContainerDefinition(int siteId, String fieldName, 123 String containerDefinitionName, boolean numberSort, 124 EntryLoadRequest entryLoadRequest) 125 throws JahiaException 126 { 127 this(siteId,fieldName,containerDefinitionName,numberSort,null,entryLoadRequest); 128 } 129 130 140 public ContainerSorterByContainerDefinition(int siteId, String fieldName, 141 String containerDefinitionName, 142 boolean numberSort, 143 String numberFormat, 144 EntryLoadRequest entryLoadRequest ) 145 throws JahiaException 146 { 147 if ( fieldName != null && !fieldName.trim().equals("") ) 148 { 149 this.siteId = siteId; 150 this.fieldName = fieldName; 151 this.containerDefinitionName = containerDefinitionName; 152 this.isValid = true; 153 this.numberSort = numberSort; 154 if ( NumberFormats.isValidFormat(numberFormat) ){ 155 this.numberFormat = numberFormat; 156 } 157 } 158 if (entryLoadRequest != null){ 159 this.entryLoadRequest = entryLoadRequest; 160 } 161 } 162 163 170 public Vector doSort(BitSet bits) 171 { 172 this.result = null; 173 174 try { 175 Vector ctnIds = null; 176 177 if ( this.isValid ){ 178 if ( this.numberSort ) 180 { 181 this.result = doNumberSort(bits); 182 } else { 183 this.result = doStringSort(bits); 184 } 185 } 186 } catch ( Throwable t ){ 187 JahiaConsole.println("ContainerSorterByContainerDefinition.doSort", 188 "Exception occured :" + t.getMessage()); 189 t.printStackTrace(); 190 } 191 192 this.lastSortingTime = System.currentTimeMillis(); 194 195 return this.result; 196 } 197 198 199 204 public Vector result() 205 { 206 return this.result; 207 } 208 209 214 public boolean isAscOrdering() 215 { 216 return this.ASC_Ordering; 217 } 218 219 224 public boolean isNumberOrdering() 225 { 226 return this.numberSort; 227 } 228 229 234 public boolean setNumberOrdering(boolean val) 235 { 236 return this.numberSort = val; 237 } 238 239 244 public void setDescOrdering() 245 { 246 this.ASC_Ordering = false; 247 } 248 249 254 public boolean setAscOrdering() 255 { 256 return this.ASC_Ordering = true; 257 } 258 259 264 public boolean setAscOrdering(boolean val) 265 { 266 return this.ASC_Ordering = val; 267 } 268 269 275 public int getSiteId() 276 { 277 return this.siteId; 278 } 279 280 286 public String getSortingFieldName() 287 { 288 return this.fieldName; 289 } 290 291 297 public long getLastSortingTime() 298 { 299 return this.lastSortingTime; 300 } 301 302 308 public boolean isValid() 309 { 310 return this.isValid; 311 } 312 313 319 public boolean getUpdateStatus() 320 { 321 return this.updated; 322 } 323 324 329 public void setUpdateStatus() 330 { 331 this.updated = true; 332 } 333 334 public EntryLoadRequest getEntryLoadRequest(){ 336 return this.entryLoadRequest; 337 } 338 339 344 public void resetUpdateStatus() 345 { 346 this.updated = false; 347 } 348 349 358 protected Vector getFieldValues(BitSet bits) 359 throws JahiaException 360 { 361 362 ArrayList deletedCtns = ContainerFilterByContainerDefinition 363 .getDeletedContainersBySite(siteId, 364 containerDefinitionName); 365 ArrayList stagingFields = getStagingFields(); 366 367 StringBuffer buff = new StringBuffer ("SELECT DISTINCT ctnid_jahia_fields_data,b.id_jahia_fields_data,b.value_jahia_fields_data,b.workflow_state,b.language_code FROM jahia_ctn_entries a, jahia_fields_data b, jahia_fields_def c, jahia_ctn_def d WHERE "); 368 369 if ( siteId != -1 ){ 370 buff.append(" jahiaid_jahia_ctn_entries="); 371 buff.append(siteId); 372 buff.append(" AND "); 373 } 374 375 if ( containerDefinitionName != null && 376 !"".equals(containerDefinitionName.trim()) ){ 377 buff.append(" ctndefid_jahia_ctn_entries = id_jahia_ctn_def "); 378 buff.append(" AND name_jahia_ctn_def='"); 379 buff.append(JahiaTools.quote(containerDefinitionName)); 380 buff.append("' AND "); 381 } 382 383 buff.append(" ( a.id_jahia_ctn_entries = b.ctnid_jahia_fields_data AND b.fielddefid_jahia_fields_data = c.id_jahia_fields_def AND c.name_jahia_fields_def='"); 384 buff.append(JahiaTools.quote(fieldName)); 385 buff.append("' ) AND ("); 386 buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(this.entryLoadRequest,true)); 387 buff.append(") ORDER BY "); 388 buff.append(ContainerFilterBean.FIELD_ID); 389 buff.append(","); 390 buff.append(ContainerFilterBean.FIELD_WORKFLOW_STATE); 391 392 String query = buff.toString(); 393 Connection dbConn = null; 394 Statement stmt = null; 395 ResultSet rs = null; 396 397 Locale locale = this.getEntryLoadRequest().getFirstLocale(true); 398 if ( locale == null ){ 399 locale = Locale.ENGLISH; 400 } 401 Collator collator = this.getCollator(); 402 403 Vector datas = new Vector (); 404 HashMap maps = new HashMap (); 405 try 406 { 407 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 408 stmt = dbConn.createStatement(); 409 rs = stmt.executeQuery( query ); 410 while (rs.next()) 411 { 412 int ctnID = rs.getInt(1); 413 int fieldID = rs.getInt(2); 414 String fieldValue = rs.getString(3); 415 int workflowState = rs.getInt(4); 416 String languageCode = rs.getString(5); 417 418 if ( bits == null || bits.get(ctnID) ){ 419 if ( this.entryLoadRequest.isCurrent() 420 || !deletedCtns.contains(new Integer (ctnID))){ 421 if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 422 workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE; 423 } 424 425 if (Jahia.getThreadParamBean() != null) { 426 ExpressionMarker exprMarker = ExpressionMarker.parseMarkerValue(fieldValue, Jahia.getThreadParamBean()); 428 if ( exprMarker != null ){ 429 try { 430 String value = exprMarker.getValue(); 431 if ( value != null && !"".equals(value) ){ 432 fieldValue = value; 433 } 434 } 435 catch (Throwable t) { 436 logger.debug("Problem while evaluating expression " + exprMarker.getExpr(), t); 437 } 438 } 439 } 440 441 ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(fieldValue); 443 if ( resMarker != null ){ 444 try { 445 String value = resMarker.getValue(locale); 446 if ( value != null && !"".equals(value) ){ 447 fieldValue = value; 448 } 449 } 450 catch (Throwable t) { 451 } 452 } 453 454 455 TempField aField = new TempField(fieldID,ctnID,0,workflowState,languageCode,fieldValue); 456 String key = fieldID + "_" + workflowState + "_" + languageCode; 457 maps.put(key,aField); 458 } 459 } 460 } 461 } 462 catch (SQLException se) 463 { 464 String errorMsg = "Error in getFieldValues : " + se.getMessage(); 465 JahiaConsole.println ("ContainerSorterByContainerDefinition.getFieldValues", errorMsg); 466 } finally { 467 468 closeStatement (stmt); 469 } 470 471 Vector addedIds = new Vector (); 472 int size = maps.size(); 473 Iterator iterator = maps.values().iterator(); 474 while ( iterator.hasNext() ){ 475 TempField aField = (TempField)iterator.next(); 476 if ( !addedIds.contains(new Integer (aField.id)) ){ 477 String key = aField.id + "_" + aField.workflowState + "_" + 478 locale.toString(); 479 if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE) 480 && maps.containsKey(key) && 481 !aField.languageCode.equals(locale.toString())) { 482 continue; 483 } 484 else if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE) 485 && !maps.containsKey(key)) { 486 aField.value = ""; 488 } 489 Object obj = null; 490 if (this.numberSort) { 491 try { 492 obj = new DataBean(aField.ctnID, 493 aField.value); 494 } 495 catch (Throwable t) { 496 obj = new DataBean(aField.ctnID, null); 498 } 499 } 500 else { 501 obj = new StrDataBean(aField.ctnID, aField.value, collator); 502 } 503 if (this.entryLoadRequest.isCurrent()) { 504 datas.add(obj); 505 addedIds.add(new Integer (aField.id)); 506 } 507 else if (this.entryLoadRequest.isStaging() 508 && 509 aField.workflowState > 510 EntryLoadRequest.ACTIVE_WORKFLOW_STATE) { 511 datas.add(obj); 512 addedIds.add(new Integer (aField.id)); 513 } 514 else if (aField.workflowState == 515 EntryLoadRequest.ACTIVE_WORKFLOW_STATE 516 && !stagingFields.contains(new Integer (aField.id))) { 517 datas.add(obj); 518 addedIds.add(new Integer (aField.id)); 519 } 520 } 521 } 522 523 return datas; 524 } 525 526 535 protected Vector doStringSort(BitSet bits) throws JahiaException 536 { 537 Vector results = new Vector (); 538 539 Vector datas = this.getFieldValues(bits); 540 541 Collator collator = this.getCollator(); 542 543 if ( datas.size()>1 ){ 545 StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering, collator); 547 Collections.sort(datas,dummyDataBean); 548 } 549 int size = datas.size(); 551 StrDataBean dataBean = null; 552 for ( int i=0; i<size ; i++ ){ 553 dataBean = (StrDataBean)datas.get(i); 554 results.add(new Integer (dataBean.ctnID)); 555 } 556 return results; 557 } 558 559 568 protected Vector doNumberSort(BitSet bits) throws JahiaException 569 { 570 Vector results = new Vector (); 571 572 Vector datas = this.getFieldValues(bits); 573 if ( datas.size()>1 ){ 575 DataBean dummyDataBean = new DataBean(ASC_Ordering); 577 Collections.sort(datas,dummyDataBean); 578 } 579 int size = datas.size(); 581 DataBean dataBean = null; 582 for ( int i=0; i<size ; i++ ){ 583 dataBean = (DataBean)datas.get(i); 584 results.add(new Integer (dataBean.ctnID)); 585 } 586 return results; 587 } 588 589 protected void closeStatement (Statement statement) 591 { 592 try { 594 if (statement!=null) { 595 statement.close(); 596 } 597 } 598 catch (SQLException sqlEx) { 599 JahiaException je = new JahiaException ("Cannot close a statement", 602 "Cannot close a statement", JahiaException.DATABASE_ERROR, 603 JahiaException.WARNING_SEVERITY, sqlEx); 604 logger.error("Error:", je); 605 } 606 } 607 608 609 protected class DataBean implements Comparator 610 { 611 int ctnID = 0; 612 String value = null; 613 boolean ASC_Ordering = true; 614 615 public DataBean (int ctnID, String value) 616 { 617 this.ctnID = ctnID; 618 this.value = value; 619 } 620 621 public DataBean (boolean ASC_Ordering) 622 { 623 this.ASC_Ordering = ASC_Ordering; 624 } 625 626 public int compare(Object obj1, Object obj2) throws ClassCastException { 627 628 DataBean dataBean1 = (DataBean)obj1; 629 DataBean dataBean2 = (DataBean)obj2; 630 if ( ASC_Ordering ){ 631 return NumberFormats.compareNumber(dataBean1.value,dataBean2.value, numberFormat); 632 } else { 633 return NumberFormats.compareNumber(dataBean2.value,dataBean1.value, numberFormat); 634 } 635 } 636 } 637 638 protected class StrDataBean implements Comparator 639 { 640 int ctnID = 0; 641 String value = ""; 642 boolean ASC_Ordering = true; 643 Collator collator = null; 644 645 public StrDataBean (int ctnID, String value, Collator collator) 646 { 647 this.ctnID = ctnID; 648 this.value = value; 649 this.collator = collator; 650 651 if ( this.collator == null ){ 652 this.collator = Collator.getInstance(); 653 } 654 } 655 656 public StrDataBean (boolean ASC_Ordering, Collator collator) 657 { 658 this.ASC_Ordering = ASC_Ordering; 659 this.collator = collator; 660 if ( this.collator == null ){ 661 this.collator = Collator.getInstance(); 662 } 663 } 664 665 public int compare(Object obj1, Object obj2) throws ClassCastException { 666 667 StrDataBean dataBean1 = (StrDataBean)obj1; 670 StrDataBean dataBean2 = (StrDataBean)obj2; 671 if ( this.ASC_Ordering ){ 672 return collator.compare(dataBean1.value,dataBean2.value); 673 } else { 674 return collator.compare(dataBean2.value,dataBean1.value); 675 } 676 } 677 } 678 679 protected class TempField{ 680 public int id; 681 public int ctnID; 682 public int versionID; 683 public int workflowState; 684 public String languageCode; 685 public String value; 686 687 public TempField(int id, int ctnID, int versionID, int workflowState, 688 String languageCode, String value){ 689 this.id = id; 690 this.ctnID = ctnID; 691 this.versionID = versionID; 692 this.workflowState = workflowState; 693 this.languageCode = languageCode; 694 this.value = value; 695 } 696 } 697 698 705 protected ArrayList getStagingFields() 706 throws JahiaException 707 { 708 709 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b, jahia_ctn_def c WHERE "); 710 711 if ( siteId != -1 ){ 712 buff.append(" jahiaid_jahia_ctn_entries="); 713 buff.append(siteId); 714 buff.append(" AND "); 715 } 716 717 if ( containerDefinitionName != null && 718 !"".equals(containerDefinitionName.trim()) ){ 719 buff.append(" ctndefid_jahia_ctn_entries = id_jahia_ctn_def "); 720 buff.append(" AND name_jahia_ctn_def='"); 721 buff.append(JahiaTools.quote(containerDefinitionName)); 722 buff.append("' AND"); 723 } 724 725 buff.append(" a.id_jahia_ctn_entries=b.ctnid_jahia_fields_data AND b.workflow_state>1 "); 726 727 Connection dbConn = null; 728 Statement stmt = null; 729 ResultSet rs = null; 730 731 ArrayList datas = new ArrayList (); 732 733 try 734 { 735 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 736 stmt = dbConn.createStatement(); 737 rs = stmt.executeQuery( buff.toString() ); 738 739 while (rs.next()) { 740 datas.add(new Integer (rs.getInt(1))); 741 } 742 } 743 catch (SQLException se) 744 { 745 String errorMsg = "Error in getStagingFields() : " + se.getMessage(); 746 JahiaConsole.println ("ContainerSorterByContainerDefinition.getStagingFields()", errorMsg); 747 } finally { 748 749 closeStatement (stmt); 750 } 751 return datas; 752 } 753 754 759 private Collator getCollator(){ 760 Collator collator = Collator.getInstance(); 761 if ( this.getEntryLoadRequest() != null ){ 762 Locale locale = null; 763 locale = this.getEntryLoadRequest().getFirstLocale(true); 764 if ( locale != null ){ 765 collator = Collator.getInstance(locale); 766 } 767 } 768 return collator; 769 } 770 771 } 772 | Popular Tags |