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.params.ParamBean; 41 import org.jahia.registries.ServicesRegistry; 42 import org.jahia.resourcebundle.ResourceBundleMarker; 43 import org.jahia.services.fields.ContentField; 44 import org.jahia.services.version.EntryLoadRequest; 45 import org.jahia.utils.JahiaConsole; 46 import org.jahia.utils.JahiaTools; 47 48 55 56 public class ContainerSorterBean implements Serializable , ContainerSorterInterface { 57 58 private static org.apache.log4j.Logger logger = 59 org.apache.log4j.Logger.getLogger(ContainerSorterBean.class); 60 61 private static final String CLASS_NAME = ContainerSorterBean.class.getName(); 62 63 protected int ctnListID = -1; 64 65 protected String fieldName; 66 67 protected boolean updated = false; 68 69 protected long lastSortingTime = -1; 70 71 protected boolean numberSort = false; 72 73 protected String numberFormat = NumberFormats.LONG_FORMAT; 74 75 protected boolean isValid = false; 76 77 protected boolean ASC_Ordering = true; 79 protected Vector result; 81 82 protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT; 83 84 92 public ContainerSorterBean(int ctnListID, String fieldName) 93 throws JahiaException 94 { 95 this(ctnListID,fieldName,null); 96 } 97 98 107 public ContainerSorterBean(int ctnListID, String fieldName, 108 EntryLoadRequest entryLoadRequest) 109 throws JahiaException 110 { 111 this(ctnListID,fieldName,false,entryLoadRequest); 112 } 113 114 124 public ContainerSorterBean(int ctnListID, String fieldName, boolean numberSort, 125 EntryLoadRequest entryLoadRequest) 126 throws JahiaException 127 { 128 this(ctnListID, fieldName, numberSort, null, entryLoadRequest); 129 } 130 131 142 public ContainerSorterBean(int ctnListID, String fieldName, boolean numberSort, 143 String numberFormat, EntryLoadRequest entryLoadRequest) 144 throws JahiaException 145 { 146 if ( ctnListID >0 && fieldName != null && !fieldName.trim().equals("") ) 147 { 148 this.ctnListID = ctnListID; 149 this.fieldName = fieldName; 150 this.numberSort = numberSort; 151 if ( NumberFormats.isValidFormat(numberFormat) ){ 152 this.numberFormat = numberFormat; 153 } 154 this.isValid = true; 155 } 156 if (entryLoadRequest != null){ 157 this.entryLoadRequest = entryLoadRequest; 158 } 159 } 160 161 170 public ContainerSorterBean(String containerListName, ParamBean params, String fieldName) 171 throws JahiaException 172 { 173 this(containerListName,params,fieldName,params.getEntryLoadRequest()); 174 } 175 176 186 public ContainerSorterBean(String containerListName, ParamBean params, String fieldName, 187 EntryLoadRequest entryLoadRequest) 188 throws JahiaException 189 { 190 this(containerListName,params,fieldName,false,entryLoadRequest); 191 } 192 193 203 public ContainerSorterBean(String containerListName, ParamBean params, String fieldName, boolean numberSort) 204 throws JahiaException 205 { 206 this(containerListName,params,fieldName,numberSort,params.getEntryLoadRequest()); 207 } 208 209 220 public ContainerSorterBean(String containerListName, ParamBean params, String fieldName, 221 boolean numberSort, EntryLoadRequest entryLoadRequest) 222 throws JahiaException 223 { 224 this(containerListName,params,fieldName,numberSort,null,entryLoadRequest); 225 } 226 227 239 public ContainerSorterBean(String containerListName, ParamBean params, String fieldName, 240 boolean numberSort, String numberFormat, 241 EntryLoadRequest entryLoadRequest) 242 throws JahiaException 243 { 244 JahiaConsole.println("ContainerSorterBean","Created container sort for clist: " + containerListName + " on the field " + fieldName); 245 246 if ( containerListName != null ){ 247 int clistID = ServicesRegistry.getInstance().getJahiaContainersService(). 248 getContainerListID( containerListName, params.getPage().getID() ); 249 if ( clistID >0 && fieldName != null && !fieldName.trim().equals("") ) 250 { 251 this.ctnListID = clistID; 252 this.fieldName = fieldName; 253 this.numberSort = numberSort; 254 this.isValid = true; 255 if ( NumberFormats.isValidFormat(numberFormat) ){ 256 this.numberFormat = numberFormat; 257 } 258 } 260 } 261 if (entryLoadRequest != null){ 262 this.entryLoadRequest = entryLoadRequest; 263 } else if ( params.getEntryLoadRequest() != null ){ 264 this.entryLoadRequest = params.getEntryLoadRequest(); 265 } 266 } 267 268 275 public Vector doSort(BitSet bits) 276 { 277 JahiaConsole.println("ContainerSorterBean.doSort", 278 "Started"); 279 280 this.result = null; 281 282 try { 283 Vector ctnIds = null; 284 285 if ( this.isValid ){ 286 JahiaConsole.println("ContainerSorterBean.doSort", 288 "Sorting : On field : " + getSortingFieldName()); 289 if ( this.numberSort ) 290 { 291 this.result = doNumberSort(bits); 292 } else { 293 this.result = doStringSort(bits); 294 } 295 } 296 } catch ( Throwable t ){ 297 JahiaConsole.println("ContainerSorterBean.doSort", 298 "Exception occured :" + t.getMessage()); 299 t.printStackTrace(); 300 } 301 302 this.lastSortingTime = System.currentTimeMillis(); 304 305 return this.result; 306 } 307 308 309 314 public Vector result() 315 { 316 return this.result; 317 } 318 319 324 public boolean isAscOrdering() 325 { 326 return this.ASC_Ordering; 327 } 328 329 334 public boolean isNumberOrdering() 335 { 336 return this.numberSort; 337 } 338 339 344 public boolean setNumberOrdering(boolean val) 345 { 346 return this.numberSort = val; 347 } 348 349 354 public void setDescOrdering() 355 { 356 this.ASC_Ordering = false; 357 } 358 359 364 public boolean setAscOrdering() 365 { 366 return this.ASC_Ordering = true; 367 } 368 369 374 public boolean setAscOrdering(boolean val) 375 { 376 return this.ASC_Ordering = val; 377 } 378 379 385 public int getCtnListID() 386 { 387 return this.ctnListID; 388 } 389 390 public void setCtnListID(int listId){ 391 this.ctnListID = listId; 392 } 393 394 400 public String getSortingFieldName() 401 { 402 return this.fieldName; 403 } 404 405 411 public long getLastSortingTime() 412 { 413 return this.lastSortingTime; 414 } 415 416 422 public boolean isValid() 423 { 424 return this.isValid; 425 } 426 427 433 public boolean getUpdateStatus() 434 { 435 return this.updated; 436 } 437 438 443 public void setUpdateStatus() 444 { 445 this.updated = true; 446 } 447 448 public EntryLoadRequest getEntryLoadRequest(){ 450 return this.entryLoadRequest; 451 } 452 453 458 public void resetUpdateStatus() 459 { 460 this.updated = false; 461 } 462 463 472 protected Vector getFieldValues(int ctnListID, String fieldName, boolean convertValueAsLong, BitSet bits) 473 throws JahiaException 474 { 475 476 ArrayList deletedCtns = ContainerFilterBean.getDeletedContainers(ctnListID); 477 ArrayList stagingFields = getStagingFields(ctnListID); 478 479 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 WHERE listid_jahia_ctn_entries="); 480 buff.append(ctnListID); 481 buff.append(" AND ( 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='"); 482 buff.append(JahiaTools.quote(fieldName)); 483 buff.append("' ) AND ("); 484 buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(this.entryLoadRequest,true)); 485 buff.append(") ORDER BY "); 486 buff.append(ContainerFilterBean.FIELD_ID); 487 buff.append(","); 488 buff.append(ContainerFilterBean.FIELD_WORKFLOW_STATE); 489 490 String query = buff.toString(); 491 Connection dbConn = null; 492 Statement stmt = null; 493 ResultSet rs = null; 494 495 Locale locale = this.getEntryLoadRequest().getFirstLocale(true); 496 if ( locale == null ){ 497 locale = Locale.ENGLISH; 498 } 499 500 Collator collator = this.getCollator(); 501 502 Vector datas = new Vector (); 503 HashMap maps = new HashMap (); 504 try 505 { 506 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 507 stmt = dbConn.createStatement(); 508 rs = stmt.executeQuery( buff.toString() ); 509 while (rs.next()) 510 { 511 int ctnID = rs.getInt(1); 512 int fieldID = rs.getInt(2); 513 String fieldValue = rs.getString(3); 514 int workflowState = rs.getInt(4); 515 String languageCode = rs.getString(5); 516 517 if ( bits == null || bits.get(ctnID) ){ 518 if ( this.entryLoadRequest.isCurrent() 519 || !deletedCtns.contains(new Integer (ctnID))){ 520 if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 521 workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE; 522 } 523 524 if (Jahia.getThreadParamBean() != null) { 525 ExpressionMarker exprMarker = ExpressionMarker.parseMarkerValue(fieldValue, Jahia.getThreadParamBean()); 527 if ( exprMarker != null ){ 528 try { 529 String value = exprMarker.getValue(); 530 if ( value != null && !"".equals(value) ){ 531 fieldValue = value; 532 } 533 } 534 catch (Throwable t) { 535 logger.debug("Problem while evaluating expression " + exprMarker.getExpr(), t); 536 } 537 } 538 } 539 540 ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(fieldValue); 542 if ( resMarker != null ){ 543 try { 544 String value = resMarker.getValue(locale); 545 if ( value != null && !"".equals(value) ){ 546 fieldValue = value; 547 } 548 } 549 catch (Throwable t) { 550 } 551 } 552 553 TempField aField = new TempField(fieldID,ctnID,0,workflowState,languageCode,fieldValue); 554 String key = fieldID + "_" + workflowState + "_" + languageCode; 555 maps.put(key,aField); 556 } 557 } 558 } 559 } 560 catch (SQLException se) 561 { 562 String errorMsg = "Error in getFieldValues : " + se.getMessage(); 563 JahiaConsole.println ("ContainerFilterBean.getFieldValues", errorMsg); 564 } finally { 565 566 closeStatement (stmt); 567 } 568 569 Vector addedIds = new Vector (); 570 int size = maps.size(); 571 Iterator iterator = maps.values().iterator(); 572 while ( iterator.hasNext() ){ 573 TempField aField = (TempField)iterator.next(); 574 if ( !addedIds.contains(new Integer (aField.id)) ){ 575 String key = aField.id + "_" + aField.workflowState + "_" + 576 locale.toString(); 577 if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE) 578 && maps.containsKey(key) && 579 !aField.languageCode.equals(locale.toString())) { 580 continue; 581 } 582 else if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE) 583 && !maps.containsKey(key)) { 584 aField.value = ""; 586 } 587 588 Object obj = null; 589 if (convertValueAsLong) { 590 try { 591 obj = new DataBean(aField.ctnID, 592 aField.value); 593 } 594 catch (Throwable t) { 595 obj = new DataBean(aField.ctnID, null); 597 } 598 } 599 else { 600 obj = new StrDataBean(aField.ctnID, aField.value, collator); 601 } 602 if (this.entryLoadRequest.isCurrent()) { 603 datas.add(obj); 604 addedIds.add(new Integer (aField.id)); 605 } 606 else if (this.entryLoadRequest.isStaging() 607 && 608 aField.workflowState > 609 EntryLoadRequest.ACTIVE_WORKFLOW_STATE) { 610 datas.add(obj); 611 addedIds.add(new Integer (aField.id)); 612 } 613 else if (aField.workflowState == 614 EntryLoadRequest.ACTIVE_WORKFLOW_STATE 615 && !stagingFields.contains(new Integer (aField.id))) { 616 datas.add(obj); 617 addedIds.add(new Integer (aField.id)); 618 } 619 } 620 } 621 622 return datas; 623 } 624 625 634 protected Vector doStringSort(BitSet bits) throws JahiaException 635 { 636 Vector results = new Vector (); 637 638 Vector datas = this.getFieldValues(this.ctnListID,this.fieldName,this.isNumberOrdering(),bits); 639 640 Collator collator = this.getCollator(); 641 642 if ( datas.size()>1 ){ 644 StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering,collator); 646 Collections.sort(datas,dummyDataBean); 647 } 648 int size = datas.size(); 650 StrDataBean dataBean = null; 651 for ( int i=0; i<size ; i++ ){ 652 dataBean = (StrDataBean)datas.get(i); 653 results.add(new Integer (dataBean.ctnID)); 654 } 655 return results; 656 } 657 658 667 protected Vector doNumberSort(BitSet bits) throws JahiaException 668 { 669 Vector results = new Vector (); 670 671 Vector datas = this.getFieldValues(this.ctnListID,this.fieldName,this.isNumberOrdering(),bits); 672 if ( datas.size()>1 ){ 674 DataBean dummyDataBean = new DataBean(ASC_Ordering); 676 Collections.sort(datas,dummyDataBean); 677 } 678 int size = datas.size(); 680 DataBean dataBean = null; 681 for ( int i=0; i<size ; i++ ){ 682 dataBean = (DataBean)datas.get(i); 683 results.add(new Integer (dataBean.ctnID)); 684 } 685 return results; 686 } 687 688 protected void closeStatement (Statement statement) 690 { 691 try { 693 if (statement!=null) { 694 statement.close(); 695 } 696 } 697 catch (SQLException sqlEx) { 698 JahiaException je = new JahiaException ("Cannot close a statement", 701 "Cannot close a statement", JahiaException.DATABASE_ERROR, 702 JahiaException.WARNING_SEVERITY, sqlEx); 703 logger.error("Error:", je); 704 } 705 } 706 707 708 protected class DataBean implements Comparator 710 { 711 int ctnID = 0; 712 String value = null; 713 boolean ASC_Ordering = true; 714 715 public DataBean (int ctnID, String value) 716 { 717 this.ctnID = ctnID; 718 this.value = value; 719 } 720 721 public DataBean (boolean ASC_Ordering) 722 { 723 this.ASC_Ordering = ASC_Ordering; 724 } 725 726 public int compare(Object obj1, Object obj2) throws ClassCastException { 727 728 DataBean dataBean1 = (DataBean)obj1; 729 DataBean dataBean2 = (DataBean)obj2; 730 if ( ASC_Ordering ){ 731 return NumberFormats.compareNumber(dataBean1.value,dataBean2.value, numberFormat); 732 } else { 733 return NumberFormats.compareNumber(dataBean2.value,dataBean1.value, numberFormat); 734 } 735 } 736 } 737 738 protected class StrDataBean implements Comparator 740 { 741 int ctnID = 0; 742 String value = ""; 743 boolean ASC_Ordering = true; 744 Collator collator = null; 745 746 public StrDataBean (int ctnID, String value, Collator collator) 747 { 748 this.ctnID = ctnID; 749 this.value = value; 750 this.collator = collator; 751 752 if ( this.collator == null ){ 753 this.collator = Collator.getInstance(); 754 } 755 } 756 757 public StrDataBean (boolean ASC_Ordering, Collator collator) 758 { 759 this.ASC_Ordering = ASC_Ordering; 760 this.collator = collator; 761 if ( this.collator == null ){ 762 this.collator = Collator.getInstance(); 763 } 764 } 765 766 public int compare(Object obj1, Object obj2) throws ClassCastException { 767 768 StrDataBean dataBean1 = (StrDataBean)obj1; 769 StrDataBean dataBean2 = (StrDataBean)obj2; 770 if ( this.ASC_Ordering ){ 771 return collator.compare(dataBean1.value,dataBean2.value); 772 } else { 773 return collator.compare(dataBean2.value,dataBean1.value); 774 } 775 } 776 } 777 778 protected class TempField{ 779 public int id; 780 public int ctnID; 781 public int versionID; 782 public int workflowState; 783 public String languageCode; 784 public String value; 785 786 public TempField(int id, int ctnID, int versionID, int workflowState, 787 String languageCode, String value){ 788 this.id = id; 789 this.ctnID = ctnID; 790 this.versionID = versionID; 791 this.workflowState = workflowState; 792 this.languageCode = languageCode; 793 this.value = value; 794 } 795 } 796 797 805 protected ArrayList getStagingFields(int ctnListID) 806 throws JahiaException 807 { 808 809 StringBuffer buff = new StringBuffer ("SELECT DISTINCT id_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b WHERE a.listid_jahia_ctn_entries="); 810 buff.append(ctnListID); 811 buff.append(" AND a.id_jahia_ctn_entries=b.ctnid_jahia_fields_data AND b.workflow_state>1 "); 812 buff.append(" AND ( "); 813 buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(this.getEntryLoadRequest())); 814 buff.append(" )"); 815 816 Connection dbConn = null; 817 Statement stmt = null; 818 ResultSet rs = null; 819 820 ArrayList datas = new ArrayList (); 821 822 try 823 { 824 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 825 stmt = dbConn.createStatement(); 826 rs = stmt.executeQuery( buff.toString() ); 827 828 while (rs.next()) { 829 datas.add(new Integer (rs.getInt(1))); 830 } 831 } 832 catch (SQLException se) 833 { 834 String errorMsg = "Error in getStagingFields() : " + se.getMessage(); 835 JahiaConsole.println ("ContainerFilterBean.getStagingFields()", errorMsg); 836 } finally { 837 838 closeStatement (stmt); 839 } 840 return datas; 841 } 842 843 848 private Collator getCollator(){ 849 Collator collator = Collator.getInstance(); 850 if ( this.getEntryLoadRequest() != null ){ 851 Locale locale = null; 852 locale = this.getEntryLoadRequest().getFirstLocale(true); 853 if ( locale != null ){ 854 collator = Collator.getInstance(locale); 855 } 856 } 857 return collator; 858 } 859 860 public String toString() { 861 StringBuffer buf = new StringBuffer (); 862 if (result != null) { 863 buf.append("Result:"); 864 buf.append(result); 865 } 866 return buf.toString(); 867 } 868 869 } 870 | Popular Tags |