/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.dcm30_04.services.wadotest;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import cdac.medinfo.sdk.dcm30_04.services.wado.WADOServer;
import cdac.medinfo.sdk.dcm30_04.services.wado.WadoConfig;


/**
 * This class implements ServletContextListener which receive notifications about
 * changes to the servlet context of the web application
 *
 * On initialization of web context this class creates instance of WadoConfig with 
 * following parameters specified with Web.xml
 * 1) aeTitle - Specifies the AE Title of the this Application Entity
 * 2) cstorescpPort - Specifies the C-Store SCP port number for receiving incoming C-Store Requests 
 * 3) cmovescpIP -  Specifies the C-Move SCP IP Address
 * 4) cmovescpPort - Specifies the C-Move SCP Port Number
 * 
 * After that it create instance of WADOServer using WADOConfig and Starts CStore SCP Server
 * that will be used serve the  web requests.
 */
public class WadoAppListener implements ServletContextListener
{
	private static WADOServer wadoServer;
	
	@Override
	public void contextDestroyed(ServletContextEvent objServletContextEvent)
	{
		if(null != wadoServer)
			wadoServer.stopServer();
	}

	@Override
	public void contextInitialized(ServletContextEvent  objServletContextEvent) 
	{
        // initialize library
      //  

        // Read WADO Configuration from web.xml
        WadoConfig wadoConfig = new WadoConfig();        
        wadoConfig.setAETitle(objServletContextEvent.getServletContext().getInitParameter("aeTitle"));
        wadoConfig.setCStoreSCPPort(Integer.parseInt(objServletContextEvent.getServletContext().getInitParameter("cstorescpPort")));
        wadoConfig.setCMoveSCPIP(objServletContextEvent.getServletContext().getInitParameter("cmovescpIP"));
        wadoConfig.setCStoreSCPIP(objServletContextEvent.getServletContext().getInitParameter("cstorescpIP"));
        wadoConfig.setCMoveSCPPort(Integer.parseInt(objServletContextEvent.getServletContext().getInitParameter("cmovescpPort")));
        wadoConfig.setCMoveAETitle(objServletContextEvent.getServletContext().getInitParameter("cmoveAETitle"));
        wadoConfig.setCStoreAETitle(objServletContextEvent.getServletContext().getInitParameter("cstoreAETitle"));

        // Code that runs on application startup
        wadoServer = new WADOServer(wadoConfig);
        wadoServer.startServer();  
		
        objServletContextEvent.getServletContext().setAttribute("wadoserver",wadoServer);
	}

}
