KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > validation > ValidationErrorSorter


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  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 28-Feb-2005, Commaro, Benjamin Papez
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.engines.validation;
42
43 import java.util.Comparator JavaDoc;
44
45 import org.jahia.data.containers.ContainerEditViewFieldGroup;
46 import org.jahia.data.fields.JahiaField;
47 import org.jahia.exceptions.JahiaException;
48
49 /**
50 *
51 * <p>Title: ValidationErrorSorter</p>
52 * <p>Description: This class sorts the validation messages. The messages, which are on the currently
53 * processed page are the first items. Then the messages are sorted according to the
54 * appearance on the screen (first tab, first field to last tab, last field).</p>
55 * <p>Copyright: Copyright (c) 2004</p>
56 * <p>Company: Jahia Ltd</p>
57 * @author not attributable
58 * @version 1.0
59 */

60
61 public class ValidationErrorSorter implements Comparator JavaDoc {
62   ContainerEditViewFieldGroup fieldGroup = null;
63   public ValidationErrorSorter(ContainerEditViewFieldGroup currentFieldGroup) {
64     super();
65     fieldGroup = currentFieldGroup;
66   }
67
68   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
69     Object JavaDoc leftObj = ((ValidationError)o1).getSource();
70     Object JavaDoc rightObj = ((ValidationError)o2).getSource();
71
72     if (leftObj != null
73       && rightObj != null
74       && leftObj instanceof JahiaField
75       && rightObj instanceof JahiaField
76       && fieldGroup != null) {
77
78       JahiaField leftField = (JahiaField)leftObj;
79       JahiaField rightField = (JahiaField)rightObj;
80
81       try {
82         boolean leftExists =
83           fieldGroup.fieldExists(leftField.getDefinition().getName());
84         boolean rightExists =
85           fieldGroup.fieldExists(
86             rightField.getDefinition().getName());
87
88         if (leftExists != rightExists)
89           return leftExists == true ? -1 : 1;
90         else {
91           return Math.abs(leftField.getID())
92             < Math.abs(rightField.getID())
93             ? -1
94             : 1;
95         }
96       } catch (JahiaException e) {}
97     }
98     if (leftObj != null
99       && leftObj instanceof JahiaField
100       && (rightObj == null || !(rightObj instanceof JahiaField)))
101       return -1;
102
103     if (rightObj != null
104       && rightObj instanceof JahiaField
105       && (leftObj == null || !(leftObj instanceof JahiaField)))
106       return 1;
107
108     return leftObj.hashCode() < rightObj.hashCode()
109       ? -1
110       : (leftObj.equals(rightObj) ? 0 : 1);
111   }
112
113   public boolean equals(Object JavaDoc obj) {
114         if (this == obj) return true;
115         
116         if (obj != null && this.getClass() == obj.getClass()) {
117             return true;
118         }
119         return false;
120   }
121 }
122
Popular Tags