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.i18n.CmsMessageContainer;
031import org.opencms.workplace.list.CmsListDirectAction;
032
033import java.util.Locale;
034
035import org.apache.logging.log4j.Level;
036import org.apache.logging.log4j.LogManager;
037import org.apache.logging.log4j.Logger;
038
039/**
040 * Action handler for single actions from logging view.<p>
041 *
042 * */
043public class CmsChangeLogLevelAction extends CmsListDirectAction {
044
045    /** The actual log level of this row. */
046    protected Level m_logLevel;
047
048    /** Constructor so generate an instance.<p>
049     *
050     * @param id The id of the Log-channel
051     * @param level The level of the Log-channel
052     */
053    public CmsChangeLogLevelAction(String id, Level level) {
054
055        super(id);
056        m_logLevel = level;
057    }
058
059    /** Constructor so generate an instance.<p>
060     *
061     * @param id Id of the Log-channel
062     * @param level Level of the Log-channel
063     * @param helpText Helptext of the Log-Channel
064     */
065    public CmsChangeLogLevelAction(String id, Level level, CmsMessageContainer helpText) {
066
067        super(id);
068        m_logLevel = level;
069        setName(null);
070        setHelpText(helpText);
071    }
072
073    /** Constructor so generate an instance.<p>
074     *
075     * @param id Id of the Log-channel
076     * @param level Level of the Log-channel
077     * @param name Name of the Log-channel
078     * @param helpText Helptext of the Log-Channel
079     */
080    public CmsChangeLogLevelAction(String id, Level level, CmsMessageContainer name, CmsMessageContainer helpText) {
081
082        super(id);
083        m_logLevel = level;
084        setName(name);
085        setHelpText(helpText);
086    }
087
088    /**
089     * Help method to resolve the name to use.<p>
090     *
091     * @param locale the used locale
092     *
093     * @return the name
094     */
095    @Override
096    protected String resolveName(Locale locale) {
097
098        return getName().key(locale);
099    }
100
101    /**
102     * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
103     */
104    @Override
105    public boolean isEnabled() {
106
107        boolean isVisible = false;
108        Logger logger = null;
109        if (getItem() != null) {
110            String loggerName = getItem().getId();
111            if (loggerName != null) {
112                logger = LogManager.getLogger(loggerName);
113            }
114        }
115        if (logger != null) {
116            isVisible = !logger.getLevel().equals(m_logLevel);
117        }
118        return isVisible;
119    }
120}