<commit_before>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2013-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.features.vaadin.dashboard.dashlets;

import com.vaadin.server.Page;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.core.criteria.Fetch;
import org.opennms.core.criteria.restrictions.InRestriction;
import org.opennms.core.criteria.restrictions.Restriction;
import org.opennms.features.topology.plugins.browsers.*;
import org.opennms.features.vaadin.dashboard.config.ui.editors.CriteriaBuilderHelper;
import org.opennms.features.vaadin.dashboard.model.AbstractDashlet;
import org.opennms.features.vaadin.dashboard.model.AbstractDashletComponent;
import org.opennms.features.vaadin.dashboard.model.DashletComponent;
import org.opennms.features.vaadin.dashboard.model.DashletSpec;
import org.opennms.netmgt.dao.api.AlarmDao;
import org.opennms.netmgt.dao.api.AlarmRepository;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.model.*;
import org.opennms.osgi.EventProxy;
import org.opennms.osgi.VaadinApplicationContextImpl;

import java.util.LinkedList;
import java.util.List;

/**
 * This class represents a Alert Dashlet with some details.
 *
 * @author <NAME>
 */
public class AlarmDetailsDashlet extends AbstractDashlet {
    /**
     * The {@link AlarmDao} used
     */
    private AlarmDao m_alarmDao;
    /**
     * The {@link NodeDao} used
     */
    private NodeDao m_nodeDao;
    /**
     * Helper for handling criterias
     */
    private CriteriaBuilderHelper m_criteriaBuilderHelper = new CriteriaBuilderHelper(OnmsAlarm.class, OnmsNode.class, OnmsCategory.class, OnmsEvent.class);
    /**
     * wallboard layout
     */
    DashletComponent m_wallboardComponent;
    /**
     * dashboard layout
     */
    DashletComponent m_dashboardComponent;
    /**
     * alarm table
     */
    AlarmRepository m_alarmRepository;

    /**
     * Constructor for instantiating new objects.
     *
     * @param dashletSpec the {@link DashletSpec} to be used
     * @param alarmDao    the {@link AlarmDao} to be used
     * @param nodeDao     the {@link NodeDao} to be used
     */
    public AlarmDetailsDashlet(String name, DashletSpec dashletSpec, AlarmDao alarmDao, NodeDao nodeDao, AlarmRepository alarmRepository) {
        super(name, dashletSpec);

        /**
         * Setting the member fields
         */
        m_alarmDao = alarmDao;
        m_nodeDao = nodeDao;
        m_alarmRepository = alarmRepository;
    }

    @Override
    public DashletComponent getWallboardComponent() {

        if (m_wallboardComponent == null) {
            m_wallboardComponent = new AbstractDashletComponent() {

                private VerticalLayout m_verticalLayout;

                {
                    m_verticalLayout = new VerticalLayout();
                    m_verticalLayout.setCaption(getName());
                    m_verticalLayout.setWidth("100%");

                    refresh();
                }

                /**
                 * Injects CSS styles on current page for this dashlet
                 */
                private void injectWallboardStyles() {
                    Page.getCurrent().getStyles().add(".alert-details.cleared { background: #AAAAAA; border-left: 14px solid #858585; }");
                    Page.getCurrent().getStyles().add(".alert-details.normal { background: #AAAAAA; border-left: 14px solid #336600; }");
                    Page.getCurrent().getStyles().add(".alert-details.indeterminate { background: #AAAAAA; border-left: 14px solid #999; }");
                    Page.getCurrent().getStyles().add(".alert-details.warning { background: #AAAAAA; border-left: 14px solid #FFCC00; }");
                    Page.getCurrent().getStyles().add(".alert-details.minor { background: #AAAAAA; border-left: 14px solid #FF9900; }");
                    Page.getCurrent().getStyles().add(".alert-details.major { background: #AAAAAA; border-left: 14px solid #FF3300; }");
                    Page.getCurrent().getStyles().add(".alert-details.critical { background: #AAAAAA; border-left: 14px solid #CC0000; }");
                    Page.getCurrent().getStyles().add(".alert-details-font {color: #000000; font-size: 17px; line-height: normal; }");
                    Page.getCurrent().getStyles().add(".alert-details-noalarms-font { font-size: 17px; line-height: normal; }");
                    Page.getCurrent().getStyles().add(".alert-details { padding: 5px 5px; margin: 1px; }");
                }

                @Override
                public void refresh() {
                    List<OnmsAlarm> alarms = getAlarms();
                    m_verticalLayout.removeAllComponents();
                    injectWallboardStyles();

                    setBoosted(false);

                    addComponents(m_verticalLayout, alarms);

                    setBoosted(checkBoosted(alarms));
                }

                @Override
                public Component getComponent() {
                    return m_verticalLayout;
                }
            };
        }
        return m_wallboardComponent;
    }

    @Override
    public DashletComponent getDashboardComponent() {
        if (m_dashboardComponent == null) {
            m_dashboardComponent = new AbstractDashletComponent() {

                private AlarmTable m_alarmTable;

                {
                    m_alarmTable = new AlarmTable("Alarms", new AlarmDaoContainer(m_alarmDao), m_alarmRepository);

                    m_alarmTable.setSizeFull();

                    m_alarmTable.setSortEnabled(false);

                    m_alarmTable.addHeaderClickListener(new Table.HeaderClickListener() {
                        @Override
                        public void headerClick(Table.HeaderClickEvent headerClickEvent) {
                            m_alarmTable.setSortContainerPropertyId(headerClickEvent.getPropertyId());
                            m_alarmTable.setSortEnabled(true);
                        }
                    });

                    final VaadinApplicationContextImpl context = new VaadinApplicationContextImpl();
                    final UI currentUI = UI.getCurrent();

                    context.setSessionId(currentUI.getSession().getSession().getId());
                    context.setUiId(currentUI.getUIId());

                    m_alarmTable.setVaadinApplicationContext(context);

                    final EventProxy eventProxy = new EventProxy() {
                        @Override
                        public <T> void fireEvent(final T eventObject) {
                            System.out.println("got event: {}" + eventObject);
                        }

                        @Override
                        public <T> void addPossibleEventConsumer(final T possibleEventConsumer) {
                            System.out.println("(ignoring) add consumer: {}" + possibleEventConsumer);
                        }
                    };

                    m_alarmTable.setEventProxy(eventProxy);

                    m_alarmTable.setColumnReorderingAllowed(true);
                    m_alarmTable.setColumnCollapsingAllowed(true);
                    m_alarmTable.setSortContainerPropertyId("id");
                    m_alarmTable.setSortAscending(false);
                    m_alarmTable.setCellStyleGenerator(new AlarmTableCellStyleGenerator());

                    m_alarmTable.addGeneratedColumn("severity", new SeverityGenerator());
                    m_alarmTable.addGeneratedColumn("id", new AlarmIdColumnLinkGenerator(m_alarmDao, "id"));
                    m_alarmTable.setVisibleColumns("id", "severity", "nodeLabel", "counter", "lastEventTime", "logMsg");
                    m_alarmTable.setColumnHeaders("ID", "Severity", "Node", "Count", "Last Event Time", "Log Message");

                    refresh();
                }


                @Override
                public void refresh() {
                    List<OnmsAlarm> alarms = getAlarms();

                    if (alarms.size()>0) {
                        List<Integer> alarmIds = new LinkedList<Integer>();

                        for (OnmsAlarm onmsAlarm : alarms) {
                            alarmIds.add(onmsAlarm.getId());
                        }

                        List<Restriction> restrictions = new LinkedList<Restriction>();
                        restrictions.add(new InRestriction("id", alarmIds));

                        ((OnmsDaoContainer) m_alarmTable.getContainerDataSource()).setRestrictions(restrictions);
                    }

                    setBoosted(checkBoosted(alarms));

                    m_alarmTable.markAsDirtyRecursive();
                }

                @Override
                public Component getComponent() {
                    return m_alarmTable;
                }
            };
        }
        return m_dashboardComponent;
    }

    private boolean checkBoosted(List<OnmsAlarm> alarms) {
        for (OnmsAlarm onmsAlarm : alarms) {
            OnmsSeverity boostSeverity = OnmsSeverity.valueOf(getDashletSpec().getParameters().get("boostSeverity"));
<commit_msg>Fix for issue NMS-7917: In the case an Alarm Dashlet's filter criteria returned an empty set of alarmIds the restriction was not correctly applied to the AlarmRepository instance. Now the empty set will be replaced by a set containing only the integer zero (0), so no alarms will match which is the correct expected behaviour.<commit_after>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2013-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.features.vaadin.dashboard.dashlets;

import com.vaadin.server.Page;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.core.criteria.Fetch;
import org.opennms.core.criteria.restrictions.InRestriction;
import org.opennms.core.criteria.restrictions.Restriction;
import org.opennms.features.topology.plugins.browsers.*;
import org.opennms.features.vaadin.dashboard.config.ui.editors.CriteriaBuilderHelper;
import org.opennms.features.vaadin.dashboard.model.AbstractDashlet;
import org.opennms.features.vaadin.dashboard.model.AbstractDashletComponent;
import org.opennms.features.vaadin.dashboard.model.DashletComponent;
import org.opennms.features.vaadin.dashboard.model.DashletSpec;
import org.opennms.netmgt.dao.api.AlarmDao;
import org.opennms.netmgt.dao.api.AlarmRepository;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.model.*;
import org.opennms.osgi.EventProxy;
import org.opennms.osgi.VaadinApplicationContextImpl;

import java.util.LinkedList;
import java.util.List;

/**
 * This class represents a Alert Dashlet with some details.
 *
 * @author <NAME>
 */
public class AlarmDetailsDashlet extends AbstractDashlet {
    /**
     * The {@link AlarmDao} used
     */
    private AlarmDao m_alarmDao;
    /**
     * The {@link NodeDao} used
     */
    private NodeDao m_nodeDao;
    /**
     * Helper for handling criterias
     */
    private CriteriaBuilderHelper m_criteriaBuilderHelper = new CriteriaBuilderHelper(OnmsAlarm.class, OnmsNode.class, OnmsCategory.class, OnmsEvent.class);
    /**
     * wallboard layout
     */
    DashletComponent m_wallboardComponent;
    /**
     * dashboard layout
     */
    DashletComponent m_dashboardComponent;
    /**
     * alarm table
     */
    AlarmRepository m_alarmRepository;

    /**
     * Constructor for instantiating new objects.
     *
     * @param dashletSpec the {@link DashletSpec} to be used
     * @param alarmDao    the {@link AlarmDao} to be used
     * @param nodeDao     the {@link NodeDao} to be used
     */
    public AlarmDetailsDashlet(String name, DashletSpec dashletSpec, AlarmDao alarmDao, NodeDao nodeDao, AlarmRepository alarmRepository) {
        super(name, dashletSpec);

        /**
         * Setting the member fields
         */
        m_alarmDao = alarmDao;
        m_nodeDao = nodeDao;
        m_alarmRepository = alarmRepository;
    }

    @Override
    public DashletComponent getWallboardComponent() {

        if (m_wallboardComponent == null) {
            m_wallboardComponent = new AbstractDashletComponent() {

                private VerticalLayout m_verticalLayout;

                {
                    m_verticalLayout = new VerticalLayout();
                    m_verticalLayout.setCaption(getName());
                    m_verticalLayout.setWidth("100%");

                    refresh();
                }

                /**
                 * Injects CSS styles on current page for this dashlet
                 */
                private void injectWallboardStyles() {
                    Page.getCurrent().getStyles().add(".alert-details.cleared { background: #AAAAAA; border-left: 14px solid #858585; }");
                    Page.getCurrent().getStyles().add(".alert-details.normal { background: #AAAAAA; border-left: 14px solid #336600; }");
                    Page.getCurrent().getStyles().add(".alert-details.indeterminate { background: #AAAAAA; border-left: 14px solid #999; }");
                    Page.getCurrent().getStyles().add(".alert-details.warning { background: #AAAAAA; border-left: 14px solid #FFCC00; }");
                    Page.getCurrent().getStyles().add(".alert-details.minor { background: #AAAAAA; border-left: 14px solid #FF9900; }");
                    Page.getCurrent().getStyles().add(".alert-details.major { background: #AAAAAA; border-left: 14px solid #FF3300; }");
                    Page.getCurrent().getStyles().add(".alert-details.critical { background: #AAAAAA; border-left: 14px solid #CC0000; }");
                    Page.getCurrent().getStyles().add(".alert-details-font {color: #000000; font-size: 17px; line-height: normal; }");
                    Page.getCurrent().getStyles().add(".alert-details-noalarms-font { font-size: 17px; line-height: normal; }");
                    Page.getCurrent().getStyles().add(".alert-details { padding: 5px 5px; margin: 1px; }");
                }

                @Override
                public void refresh() {
                    List<OnmsAlarm> alarms = getAlarms();
                    m_verticalLayout.removeAllComponents();
                    injectWallboardStyles();

                    setBoosted(false);

                    addComponents(m_verticalLayout, alarms);

                    setBoosted(checkBoosted(alarms));
                }

                @Override
                public Component getComponent() {
                    return m_verticalLayout;
                }
            };
        }
        return m_wallboardComponent;
    }

    @Override
    public DashletComponent getDashboardComponent() {
        if (m_dashboardComponent == null) {
            m_dashboardComponent = new AbstractDashletComponent() {

                private AlarmTable m_alarmTable;

                {
                    m_alarmTable = new AlarmTable("Alarms", new AlarmDaoContainer(m_alarmDao), m_alarmRepository);

                    m_alarmTable.setSizeFull();

                    m_alarmTable.setSortEnabled(false);

                    m_alarmTable.addHeaderClickListener(new Table.HeaderClickListener() {
                        @Override
                        public void headerClick(Table.HeaderClickEvent headerClickEvent) {
                            m_alarmTable.setSortContainerPropertyId(headerClickEvent.getPropertyId());
                            m_alarmTable.setSortEnabled(true);
                        }
                    });

                    final VaadinApplicationContextImpl context = new VaadinApplicationContextImpl();
                    final UI currentUI = UI.getCurrent();

                    context.setSessionId(currentUI.getSession().getSession().getId());
                    context.setUiId(currentUI.getUIId());

                    m_alarmTable.setVaadinApplicationContext(context);

                    final EventProxy eventProxy = new EventProxy() {
                        @Override
                        public <T> void fireEvent(final T eventObject) {
                            System.out.println("got event: {}" + eventObject);
                        }

                        @Override
                        public <T> void addPossibleEventConsumer(final T possibleEventConsumer) {
                            System.out.println("(ignoring) add consumer: {}" + possibleEventConsumer);
                        }
                    };

                    m_alarmTable.setEventProxy(eventProxy);

                    m_alarmTable.setColumnReorderingAllowed(true);
                    m_alarmTable.setColumnCollapsingAllowed(true);
                    m_alarmTable.setSortContainerPropertyId("id");
                    m_alarmTable.setSortAscending(false);
                    m_alarmTable.setCellStyleGenerator(new AlarmTableCellStyleGenerator());

                    m_alarmTable.addGeneratedColumn("severity", new SeverityGenerator());
                    m_alarmTable.addGeneratedColumn("id", new AlarmIdColumnLinkGenerator(m_alarmDao, "id"));
                    m_alarmTable.setVisibleColumns("id", "severity", "nodeLabel", "counter", "lastEventTime", "logMsg");
                    m_alarmTable.setColumnHeaders("ID", "Severity", "Node", "Count", "Last Event Time", "Log Message");

                    refresh();
                }

                @Override
                public void refresh() {
                    List<OnmsAlarm> alarms = getAlarms();

                    List<Integer> alarmIds = new LinkedList<Integer>();

                    if (alarms.size() > 0) {
                        for (OnmsAlarm onmsAlarm : alarms) {
                            alarmIds.add(onmsAlarm.getId());
                        }
                    } else {
                        alarmIds.add(0);
                    }

                    List<Restriction> restrictions = new LinkedList<Restriction>();
                    restrictions.add(new InRestriction("id", alarmIds));

                    ((OnmsDaoContainer<?, ?>) m_alarmTable.getContainerDataSource()).setRestrictions(restrictions);

                    setBoosted(checkBoosted(alarms));

                    m_alarmTable.markAsDirtyRecursive();
                }

                @Override
                public Component getComponent() {
                    return m_alarmTable;
                }
            };
        }
        return m_dashboardComponent;
    }

    private boolean checkBoosted(List<OnmsAlarm> alarms) {
        for (OnmsAlarm onmsAlarm : alarms) {
            OnmsSeverity boostSeverity = OnmsSeverity.valueOf(getDashletSpec().getParameters().get("boostSeverity"));

{'fraction_non_alphanumeric': 0.11168744646426192, 'fraction_numerical': 0.007613971637955649, 'mean_word_length': 2.143605086013463, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6352262', 'n_tokens_mistral': 5807, 'n_tokens_neox': 5466, 'n_words': 1169}