001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.tools.workplace.logging;
029
030import org.opencms.workplace.list.CmsListItem;
031import org.opencms.workplace.list.I_CmsListItemComparator;
032
033import java.util.Comparator;
034import java.util.Locale;
035
036import org.apache.logging.log4j.Level;
037
038/**
039 * Help function to select the comparator. <p>
040 * Returns the comparator for the requested Column.<p>
041 *
042 * */
043
044public class CmsLogLevelListItemComparator implements I_CmsListItemComparator {
045
046    /** Constant for the DEBUG level. */
047    public static final String DEBUG = "DEBUG";
048
049    /** Constant for the INFO level. */
050    public static final String INFO = "INFO";
051
052    /** Constant for the WARN level. */
053    public static final String WARN = "WARN";
054
055    /** Constant for the ERROR level. */
056    public static final String ERROR = "ERROR";
057
058    /** Constant for the FATAL level. */
059    public static final String FATAL = "FATAL";
060
061    /** Constant for the OFF level. */
062    public static final String OFF = "OFF";
063
064    /**
065     * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale)
066     */
067    public Comparator<CmsListItem> getComparator(String columnId, Locale locale) {
068
069        Comparator<CmsListItem> compa = null;
070        // returns the Comparator for the "DEBUG" column
071        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_DEBUG)) {
072            compa = new Comparator<CmsListItem>() {
073
074                public int compare(CmsListItem o1, CmsListItem o2) {
075
076                    int test = 0;
077                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
078                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
079                    // returns 0 if both rows have the loglevel "Debug"
080                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(DEBUG)
081                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(DEBUG)) {
082                        test = 0;
083                    } else {
084                        // returns < 0 if the first rows have the loglevel "Debug"
085                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(DEBUG)) {
086                            test = -1;
087                        } else
088                            // returns > 0 if the second rows have the loglevel "Debug"
089                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(DEBUG)) {
090                            test = 1;
091                        } else {
092                            // sort other values by level value
093                            if (o1temp.intLevel() == o2temp.intLevel()) {
094                                test = 0;
095                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
096                                test = -1;
097                            } else {
098                                test = 1;
099                            }
100                        }
101                    }
102                    return test;
103                }
104
105            };
106        }
107        // returns the Comparator for the "INFO" column
108        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_INFO)) {
109            compa = new Comparator<CmsListItem>() {
110
111                public int compare(CmsListItem o1, CmsListItem o2) {
112
113                    int test = 0;
114                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
115                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
116                    // returns 0 if both rows have the loglevel "INFO"
117                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(INFO)
118                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(INFO)) {
119                        test = 0;
120                    } else {
121                        // returns < 0 if the first rows have the loglevel "INFO"
122                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(INFO)) {
123                            test = -1;
124                        } else
125                            // returns > 0 if the second rows have the loglevel "INFO"
126                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(INFO)) {
127                            test = 1;
128                        } else {
129                            // sort other values by level value
130                            if (o1temp.intLevel() == o2temp.intLevel()) {
131                                test = 0;
132                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
133                                test = -1;
134                            } else {
135                                test = 1;
136                            }
137                        }
138                    }
139                    return test;
140                }
141
142            };
143        }
144        // returns the Comparator for the "WARN" column
145        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_WARN)) {
146            compa = new Comparator<CmsListItem>() {
147
148                public int compare(CmsListItem o1, CmsListItem o2) {
149
150                    int test = 0;
151                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
152                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
153                    // returns 0 if both rows have the loglevel "WARN"
154                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(WARN)
155                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(WARN)) {
156                        test = 0;
157                    } else {
158                        // returns < 0 if the first rows have the loglevel "WARN"
159                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(WARN)) {
160                            test = -1;
161                        } else
162                            // returns > 0 if the second rows have the loglevel "WARN"
163                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(WARN)) {
164                            test = 1;
165                        } else {
166                            // sort other values by level value
167                            if (o1temp.intLevel() == o2temp.intLevel()) {
168                                test = 0;
169                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
170                                test = -1;
171                            } else {
172                                test = 1;
173                            }
174                        }
175                    }
176                    return test;
177                }
178
179            };
180        }
181        // returns the Comparator for the "ERROR" column
182        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_ERROR)) {
183            compa = new Comparator<CmsListItem>() {
184
185                public int compare(CmsListItem o1, CmsListItem o2) {
186
187                    int test = 0;
188                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
189                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
190                    // returns 0 if both rows have the loglevel "ERROR"
191                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(ERROR)
192                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(ERROR)) {
193                        test = 0;
194                    } else {
195                        // returns < 0 if the first rows have the loglevel "ERROR"
196                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(ERROR)) {
197                            test = -1;
198                        } else
199                            // returns > 0 if the second rows have the loglevel "ERROR"
200                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(ERROR)) {
201                            test = 1;
202                        } else {
203                            // sort other values by level value
204                            if (o1temp.intLevel() == o2temp.intLevel()) {
205                                test = 0;
206                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
207                                test = -1;
208                            } else {
209                                test = 1;
210                            }
211                        }
212                    }
213                    return test;
214                }
215
216            };
217        }
218        // returns the Comparator for the "FATAL" column
219        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_FATAL)) {
220            compa = new Comparator<CmsListItem>() {
221
222                public int compare(CmsListItem o1, CmsListItem o2) {
223
224                    int test = 0;
225                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
226                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
227                    // returns 0 if both rows have the loglevel "FATAL"
228                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(FATAL)
229                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(FATAL)) {
230                        test = 0;
231                    } else {
232                        // returns < 0 if the first rows have the loglevel "FATAL"
233                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(FATAL)) {
234                            test = -1;
235                        } else
236                            // returns > 0 if the second rows have the loglevel "FATAL"
237                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(FATAL)) {
238                            test = 1;
239                        } else {
240                            // sort other values by level value
241                            if (o1temp.intLevel() == o2temp.intLevel()) {
242                                test = 0;
243                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
244                                test = -1;
245                            } else {
246                                test = 1;
247                            }
248                        }
249                    }
250                    return test;
251                }
252
253            };
254        }
255        // returns the Comparator for the "OFF" column
256        if (columnId.equals(CmsLog4JAdminDialog.COLUMN_OFF)) {
257            compa = new Comparator<CmsListItem>() {
258
259                public int compare(CmsListItem o1, CmsListItem o2) {
260
261                    int test = 0;
262                    Level o1temp = Level.toLevel((String)o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
263                    Level o2temp = Level.toLevel((String)o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL));
264                    // returns 0 if both rows have the loglevel "Off"
265                    if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(OFF)
266                        && o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(OFF)) {
267                        test = 0;
268                    } else {
269                        // returns < 0 if the first rows have the loglevel "Off"
270                        if (o1.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(OFF)) {
271                            test = -1;
272                        } else
273                            // returns > 0 if the second rows have the loglevel "Off"
274                            if (o2.get(CmsLog4JAdminDialog.COLUMN_LOG_LEVEL).equals(OFF)) {
275                            test = 1;
276                        } else {
277                            // sort other values by level value
278                            if (o1temp.intLevel() == o2temp.intLevel()) {
279                                test = 0;
280                            } else if (o1temp.intLevel() < o2temp.intLevel()) {
281                                test = -1;
282                            } else {
283                                test = 1;
284                            }
285                        }
286                    }
287                    return test;
288                }
289
290            };
291        }
292
293        return compa;
294    }
295
296}