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 RevisionEntrySetComparator implements Comparator { 31 32 private static org.apache.log4j.Logger logger = 33 org.apache.log4j.Logger.getLogger(RevisionEntrySetComparator.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 RevisionEntrySetComparator(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 RevisionEntrySet rev1 = (RevisionEntrySet)object1; 71 RevisionEntrySet rev2 = (RevisionEntrySet)object2; 72 73 if (this.sortAttribute == RevisionEntrySetComparator.SORT_BY_DATE) { 74 if ( this.sortOrder == RevisionEntrySetComparator.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 == RevisionEntrySetComparator.SORT_BY_STATE) { 82 if ( this.sortOrder == RevisionEntrySetComparator.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 == RevisionEntrySetComparator.SORT_BY_TITLE ) { 90 String title1 = (String )rev1.getProperty(RevisionEntry.REVISION_TITLE); 91 String title2 = (String )rev2.getProperty(RevisionEntry.REVISION_TITLE); 92 if ( this.sortOrder == RevisionEntrySetComparator.ASC_ORDER ){ 93 return title1.compareTo(title2); 94 } else { 95 return title2.compareTo(title1); 96 } 97 } else if (this.sortAttribute == RevisionEntrySetComparator.SORT_BY_DESCR ) { 98 String descr1 = rev1.getDescription(this.languageCode); 99 String descr2 = rev2.getDescription(this.languageCode); 100 if ( this.sortOrder == RevisionEntrySetComparator.ASC_ORDER ){ 101 return descr1.compareTo(descr2); 102 } else { 103 return descr2.compareTo(descr1); 104 } 105 } else if (this.sortAttribute == RevisionEntrySetComparator.SORT_BY_OBJECT ){ 106 String obj1 = rev1.getObjectKey().getType(); 107 String obj2 = rev2.getObjectKey().getType(); 108 int res = 0; 109 if ( this.sortOrder == RevisionEntrySetComparator.ASC_ORDER ){ 110 res = (obj1.compareTo(obj2)); 111 } else { 112 res = (obj2.compareTo(obj1)); 113 } 114 if ( res == 0 ){ 115 int i1 = rev1.getObjectKey().getIdInType(); 116 int i2 = rev2.getObjectKey().getIdInType(); 117 if ( this.sortOrder == RevisionEntrySetComparator.ASC_ORDER ){ 118 res = i1<i2 ? -1 : (i1==i2 ? 0 : 1); 119 } else { 120 res = i2<i1 ? -1 : (i1==i2 ? 0 : 1); 121 } 122 } 123 return res; 124 } else if (this.sortAttribute == RevisionEntrySetComparator.SORT_BY_REVISION_TYPE ) { 125 } 127 return 0; 128 } 129 130 } 131 | Popular Tags |