/******************************************************************************* * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/


package com.n2bb.plugins;


import com.n2bb.security.SecurityManager;
import com.n2bb.util.N2bbSettings;
import com.n2bb.util.PropertyManager;
import com.n2bb.web.util.AppPopup;
import com.n2bb.web.util.Dashboard;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * Kind of a catch-all init class for the UI framework.
 */

public final class N2BBPlugIn implements PlugIn {   

    private Log n2bbLog = LogFactory.getLog(N2BBPlugIn.class);
    private ActionServlet servlet = null;
    private String appPopupPath = "/WEB-INF/appPopups";
    private String securityPath = "/WEB-INF/security";
    public void init(ActionServlet servlet, ModuleConfig config) throws ServletException {
        this.servlet = servlet;
 
        // page size for lists
        com.n2bb.util.PropertyManager propertyManager = com.n2bb.util.PropertyManager.getInstance();
        int ps = propertyManager.getInteger(
                PropertyManager.PAGE_SIZE_PROPERTY, 
                PropertyManager.DEFAULT_PAGE_SIZE
        );
        N2bbSettings.PAGE_SIZE = ps < 1 ? 10 : ps;
        n2bbLog.info("page size... " + N2bbSettings.PAGE_SIZE); 

        // site location
        String siteLocation = propertyManager.getValue(
                PropertyManager.SITE_LOCATION_PROPERTY,
                PropertyManager.DEFAULT_SITE_LOCATION
        );
        if (siteLocation.length() > 30) {
            siteLocation = siteLocation.substring(0, 30);
        }
        ServletContext servletContext = servlet.getServletContext();
        servletContext.setAttribute("siteLocation", siteLocation);

        // user and date in header
        servletContext.setAttribute("showUser",
                propertyManager.getValue(
                        PropertyManager.SHOW_USER_PROPERTY,
                        PropertyManager.DEFAULT_SHOW_USER
                )
        );
        servletContext.setAttribute("showDate",
                propertyManager.getValue(
                        PropertyManager.SHOW_DATE_PROPERTY,
                        PropertyManager.DEFAULT_SHOW_DATE
                )
        );

        // security init
        SecurityManager.init(servletContext, securityPath);


    }

    public void destroy() {
        servlet.getServletContext().removeAttribute("siteLocation");
        servlet.getServletContext().removeAttribute("appPopup");
        servlet.getServletContext().removeAttribute("dashboard");
        // destroy security manager
        SecurityManager.destroy();
    }

}

