1 14 package org.jahia.services.version; 15 16 import java.util.Comparator ; 17 import java.util.Locale ; 18 19 import org.jahia.utils.LanguageCodeConverters; 20 21 30 public class RevisionsComparator implements Comparator { 31 32 private static org.apache.log4j.Logger logger = 33 org.apache.log4j.Logger.getLogger(RevisionsComparator.class); 34 35 public static final int ASC_ORDER = 1; 36 public static final int DESC_ORDER = 2; 37 38 public static final int SORT_BY_DATE = 1; 39 public static final int SORT_BY_STATE = 2; 40 public static final int SORT_BY_LANG = 3; 41 public static final int SORT_BY_TITLE = 4; 42 public static final int SORT_BY_OBJECT = 5; 43 public static final int SORT_BY_REVISION_TYPE = 6; 44 public static final int SORT_BY_DESCR = 7; 45 46 private String languageCode; 47 private Locale displayLocale; 48 private int sortOrder; 49 private int sortAttribute; 50 51 public RevisionsComparator(String languageCode, 52 int sortAttribute, 53 int sortOrder) { 54 this.languageCode = languageCode; 55 this.displayLocale = 56 LanguageCodeConverters.languageCodeToLocale(languageCode); 57 this.sortOrder = sortOrder; 58 this.sortAttribute = sortAttribute; 59 } 60 61 68 public int compare(Object object1, Object object2) { 69 70 RevisionEntry rev1 = (RevisionEntry)object1; 71 RevisionEntry rev2 = (RevisionEntry)object2; 72 73 if (this.sortAttribute == RevisionsComparator.SORT_BY_DATE) { 74 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 75 return (new Integer (rev1.getVersionID()) 76 .compareTo(new Integer (rev2.getVersionID()))); 77 } else { 78 return (new Integer (rev2.getVersionID()) 79 .compareTo(new Integer (rev1.getVersionID()))); 80 } 81 } else if (this.sortAttribute == RevisionsComparator.SORT_BY_STATE) { 82 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 83 return (new Integer (rev1.getWorkflowState()) 84 .compareTo(new Integer (rev2.getWorkflowState()))); 85 } else { 86 return (new Integer (rev2.getWorkflowState()) 87 .compareTo(new Integer (rev1.getWorkflowState()))); 88 } 89 } else if (this.sortAttribute == RevisionsComparator.SORT_BY_LANG) { 90 Locale lang1 = LanguageCodeConverters 91 .languageCodeToLocale(rev1.getLanguageCode()); 92 Locale lang2 = LanguageCodeConverters 93 .languageCodeToLocale(rev2.getLanguageCode()); 94 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 95 return (lang1.getDisplayName(this.displayLocale) 96 .compareTo(lang2.getDisplayName(this.displayLocale))); 97 } else { 98 return (lang2.getDisplayName(this.displayLocale) 99 .compareTo(lang1.getDisplayName(this.displayLocale))); 100 } 101 } else if (this.sortAttribute == RevisionsComparator.SORT_BY_TITLE ) { 102 String title1 = (String )rev1.getProperty(RevisionEntry.REVISION_TITLE); 103 String title2 = (String )rev2.getProperty(RevisionEntry.REVISION_TITLE); 104 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 105 return title1.compareTo(title2); 106 } else { 107 return title2.compareTo(title1); 108 } 109 } else if (this.sortAttribute == RevisionsComparator.SORT_BY_OBJECT ){ 110 String obj1 = rev1.getObjectKey().getType(); 111 String obj2 = rev2.getObjectKey().getType(); 112 int res = 0; 113 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 114 res = (obj1.compareTo(obj2)); 115 } else { 116 res = (obj2.compareTo(obj1)); 117 } 118 if ( res == 0 ){ 119 int i1 = rev1.getObjectKey().getIdInType(); 120 int i2 = rev2.getObjectKey().getIdInType(); 121 if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){ 122 res = i1<i2 ? -1 : (i1==i2 ? 0 : 1); 123 } else { 124 res = i2<i1 ? -1 : (i1==i2 ? 0 : 1); 125 } 126 } 127 return res; 128 } else if (this.sortAttribute == RevisionsComparator.SORT_BY_REVISION_TYPE ) { 129 } 131 return 0; 132 } 133 134 } 135 | Popular Tags |