/*******************************************************************************
 * 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 <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.features.vaadin.dashboard.config.ui;

import com.vaadin.data.util.BeanItemContainer;
import org.opennms.features.vaadin.dashboard.model.Wallboard;
import org.opennms.features.vaadin.dashboard.model.Wallboards;

import javax.xml.bind.JAXB;
import java.io.File;

/**
 * This class is used for loading, holding and saving of {@link Wallboard} definitions.
 */
public class WallboardProvider {
    /**
     * Instance variable for this singleton object
     */
    private static WallboardProvider m_wallboardProvider = new WallboardProvider();
    /**
     * A variable fro holding a {@link Wallboards} instance
     */
    private Wallboards m_wallboards = null;
    /**
     * The configuration {@link File} to be used.
     */
    private File m_cfgFile = new File("etc/dashboard-config.xml");
    /**
     * The beancontainer this class uses.
     */
    private BeanItemContainer<Wallboard> m_beanItemContainer = new BeanItemContainer<Wallboard>(Wallboard.class);

    /**
     * Private default constructor used to instantiate this class.
     */
    private WallboardProvider() {
        load();
    }

    /**
     * Method to return the {@link BeanItemContainer} associated with this object.
     *
     * @return the {@link BeanItemContainer}
     */
    public BeanItemContainer<Wallboard> getBeanContainer() {
        return m_beanItemContainer;
    }

    /**
     * Method to return the instance of this singleton.
     *
     * @return the singleton instance
     */
    public static WallboardProvider getInstance() {
        return m_wallboardProvider;
    }

    /**
     * This method saves the data represented by this object to disk.
     */
    public synchronized void save() {
        if (m_wallboards == null) {
            load();
        }

        JAXB.marshal(m_wallboards, m_cfgFile);
    }

    /**
     * This method loads the configuration data from disk.
     */
    public synchronized void load() {
        if (!m_cfgFile.exists()) {
            m_wallboards = new Wallboards();
        } else {
            m_wallboards = JAXB.unmarshal(m_cfgFile, Wallboards.class);
        }

        updateBeanItemContainer();
    }

    /**
     * This method updates the {@link BeanItemContainer} of this object.
     */
    private void updateBeanItemContainer() {
        m_beanItemContainer.removeAllItems();
        for (Wallboard wallboard : m_wallboards.getWallboards()) {
            m_beanItemContainer.addItem(wallboard);
        }
    }

    /**
     * Checks whether this object contains {@link Wallboard} data concerning the given title.
     *
     * @param title the title to search for
     * @return true, if a {@link Wallboard} with the given title exists, false otherwise
     */
    public synchronized boolean containsWallboard(String title) {
        for (Wallboard wallboard : m_wallboards.getWallboards()) {
            if (wallboard.getTitle().equals(title)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Returns the {@link Wallboard} for the given title.
     *
     * @param title the title to search for
     * @return the {@link Wallboard} instance if found, null otherwise
     */
    public synchronized Wallboard getWallboard(String title) {
        for (Wallboard wallboard : m_wallboards.getWallboards()) {
            if (wallboard.getTitle().equals(title)) {
                return wallboard;
            }
        }

        return null;
    }

    /**
     * Checks whether this object contains a given {@link Wallboard} instance.
     *
     * @param wallboard the {@link Wallboard} instance to search for
     * @return true, if the {@link Wallboard} exists, false otherwise
     */
    public synchronized boolean containsWallboard(Wallboard wallboard) {
        return m_wallboards.getWallboards().contains(wallboard);
    }

    /**
     * This method adds a {@link Wallboard} to this provider.
     *
     * @param wallboard the {@link Wallboard} instance to be added
     */
    public synchronized void addWallboard(Wallboard wallboard) {
        if (m_wallboards == null) {
            load();
        }
        m_wallboards.getWallboards().add(wallboard);
        save();
        updateBeanItemContainer();
    }

    /**
     * This method removes a {@link Wallboard} from this provider.
     *
     * @param wallboard the {@link Wallboard} instance to be removed
     */
    public synchronized void removeWallboard(Wallboard wallboard) {
        if (m_wallboards == null) {
            load();
        }
        m_wallboards.getWallboards().remove(wallboard);
        save();
        updateBeanItemContainer();
    }
}

@@ -1305,16 +1305,17 @@
 tainer;%0A
+%0A
 import o
@@ -1460,16 +1460,17 @@
 d.JAXB;%0A
+%0A
 import j
@@ -2019,180 +2019,8 @@
 l%22);
-%0A    /**%0A     * The beancontainer this class uses.%0A     */%0A    private BeanItemContainer%3CWallboard%3E m_beanItemContainer = new BeanItemContainer%3CWallboard%3E(Wallboard.class);
 %0A%0A  
@@ -2384,19 +2384,21 @@
  return 
-m_b
+new B
 eanItemC
@@ -2405,16 +2405,74 @@
 ontainer
+%3CWallboard%3E(Wallboard.class, m_wallboards.getWallboards())
 ;%0A    %7D%0A
@@ -3224,360 +3224,8 @@
    %7D
-%0A%0A        updateBeanItemContainer();%0A    %7D%0A%0A    /**%0A     * This method updates the %7B@link BeanItemContainer%7D of this object.%0A     */%0A    private void updateBeanItemContainer() %7B%0A        m_beanItemContainer.removeAllItems();%0A        for (Wallboard wallboard : m_wallboards.getWallboards()) %7B%0A            m_beanItemContainer.addItem(wallboard);%0A        %7D
 %0A   
@@ -4957,43 +4957,8 @@
 ();%0A
-        updateBeanItemContainer();%0A
     
@@ -5301,32 +5301,32 @@
 ove(wallboard);%0A
+
         save();%0A
@@ -5329,43 +5329,8 @@
 ();%0A
-        updateBeanItemContainer();%0A
     

Fix for NMS-7230 Part II