package uwl.rss.portal;
 
import uwl.rss.provider.RssItemProvider;

import com.sap.netweaver.bc.uwl.IUWLService;
import com.sap.netweaver.bc.uwl.UWLException;
import com.sap.tc.logging.Category;
import com.sap.tc.logging.Location;
import com.sap.tc.logging.Severity;
import com.sapportals.portal.prt.runtime.PortalRuntime;
import com.sapportals.portal.prt.service.IServiceContext;

/**
 * @author Sumit Gogia,SAP
 *
 */
public class RssRegistrationService implements IRssRegistrationService{

  private IServiceContext mm_serviceContext;
  private static final Location loc = Location.getLocation(RssRegistrationService.class);

  private UwlRssConnector connector = null;
  
  /**
  * Generic init method of the service. Will be called by the portal runtime.
  * @param serviceContext
  */
  public void init(IServiceContext serviceContext)
  {
    mm_serviceContext = serviceContext;
    
	/*We use an xml file to store the rss urls for each user. This file is packaged as part of the
	 * portal archive. Here we get the absolute path to this file's location and give it to the Item Provider.
	 * Conceptually, our portal service is only a registrar of the connector. So it should have nothing to do
	 * with the Item Provider.
	 * This is just a work around to find the file location and give it to the Item Provider.
	 */ 
	String xmlLocation = serviceContext.getPrivateServicePath()+"/private/uwlrss/rssUrls.xml";
	RssItemProvider.pathXmlStore = xmlLocation;

  }

  /**
  * This method is called after all services in the portal runtime
  * have already been initialized.
  */
  public void afterInit()
  {
	try{
		//look up the UWLService and register the rss provider with it.
		IUWLService uwlService = (IUWLService)PortalRuntime.getRuntimeResources().getService(IUWLService.ALIAS_KEY);	 
		
		//create the connector instance
		connector = new UwlRssConnector(uwlService);
		
		//register with UWL
		uwlService.registerProviderConnector(connector);
	}
	catch(UWLException ue) {
		Category.APPLICATIONS.logThrowable(Severity.FATAL,
											loc,
											(Object)"init()",
											new Object[]{this},
											"Error Registering Connector with UWL - UWLException Thrown",
											ue);
	}
  }

  /**
  * configure the service
  * @param configuration
  * @deprecated
  */
  public void configure(com.sapportals.portal.prt.service.IServiceConfiguration configuration)
  {
  }

  /**
  * This method is called by the portal runtime
  * when the service is destroyed.
  */
  public void destroy()
  {
  }

  /**
  * This method is called by the portal runtime
  * when the service is released.
  * @deprecated
  */
  public void release()
  {
  }

  /**
  * @return the context of the service, which was previously set
  * by the portal runtime
  */
  public IServiceContext getContext()
  {
    return mm_serviceContext;
  }

  /**
  * This method should return a string that is unique to this service amongst all
  * other services deployed in the portal runtime.
  * @return a unique key of the service
  */
  public String getKey()
  {
    return KEY;
  }

}
