KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > version > RevisionsComparator


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13

14 package org.jahia.services.version;
15
16 import java.util.Comparator JavaDoc;
17 import java.util.Locale JavaDoc;
18
19 import org.jahia.utils.LanguageCodeConverters;
20
21 /**
22  *
23  * <p>Title: Comparator for Revision Entry</p>
24  * <p>Description: </p>
25  * <p>Copyright: Copyright (c) 2002</p>
26  * <p>Company: </p>
27  * @author Khue Nguyen
28  * @version 1.0
29  */

30 public class RevisionsComparator implements Comparator JavaDoc {
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 JavaDoc languageCode;
47     private Locale JavaDoc displayLocale;
48     private int sortOrder;
49     private int sortAttribute;
50
51     public RevisionsComparator(String JavaDoc 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     /**
62      * Compare two Revision Entry objects
63      *
64      * @param object1
65      * @param object2
66      * @return
67      */

68     public int compare(Object JavaDoc object1, Object JavaDoc 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 JavaDoc(rev1.getVersionID())
76                         .compareTo(new Integer JavaDoc(rev2.getVersionID())));
77             } else {
78                 return (new Integer JavaDoc(rev2.getVersionID())
79                         .compareTo(new Integer JavaDoc(rev1.getVersionID())));
80             }
81         } else if (this.sortAttribute == RevisionsComparator.SORT_BY_STATE) {
82             if ( this.sortOrder == RevisionsComparator.ASC_ORDER ){
83                 return (new Integer JavaDoc(rev1.getWorkflowState())
84                         .compareTo(new Integer JavaDoc(rev2.getWorkflowState())));
85             } else {
86                 return (new Integer JavaDoc(rev2.getWorkflowState())
87                         .compareTo(new Integer JavaDoc(rev1.getWorkflowState())));
88             }
89         } else if (this.sortAttribute == RevisionsComparator.SORT_BY_LANG) {
90             Locale JavaDoc lang1 = LanguageCodeConverters
91                          .languageCodeToLocale(rev1.getLanguageCode());
92             Locale JavaDoc 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 JavaDoc title1 = (String JavaDoc)rev1.getProperty(RevisionEntry.REVISION_TITLE);
103             String JavaDoc title2 = (String JavaDoc)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 JavaDoc obj1 = rev1.getObjectKey().getType();
111             String JavaDoc 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             // @todo
130
}
131         return 0;
132     }
133
134 }
135
Popular Tags