/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.radiologyfhirsupport;


import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.module.ModuleActivator;
import org.openmrs.module.fhir.api.DiagnosticReportService;
import org.openmrs.module.radiologyfhirsupport.api.handler.MRRTTemplateHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * This class contains the logic that is run every time this module is either started or stopped.
 */
@Component
public class RadiologyFHIRSupportActivator implements ModuleActivator {
	
	protected Log log = LogFactory.getLog(getClass());
	@Value("${project.parent.artifactId}.handlerName}")
	String mrrtTemplateHandlerName;
	/**
	 * @see ModuleActivator#willRefreshContext()
	 */
	public void willRefreshContext() {
		log.info("Refreshing Radiology FHIR Support Module");
	}
	
	/**
	 * @see ModuleActivator#contextRefreshed()
	 */
	public void contextRefreshed() {
		log.info("Radiology FHIR Support Module refreshed");
	}
	
	/**
	 * @see ModuleActivator#willStart()
	 */
	public void willStart() {
		log.info("Starting Radiology FHIR Support Module");
		DiagnosticReportService diagnosticReportService = Context.getService(DiagnosticReportService.class);
		log.info("Registering FHIR Diagnostic Report Handler for MRRT templates. Handler name is : " + mrrtTemplateHandlerName);
		diagnosticReportService.registerHandler(mrrtTemplateHandlerName,new MRRTTemplateHandler());
	}
	
	/**
	 * @see ModuleActivator#started()
	 */
	public void started() {
		log.info("Radiology FHIR Support Module started");
	}
	
	/**
	 * @see ModuleActivator#willStop()
	 */
	public void willStop() {
		log.info("Stopping Radiology FHIR Support Module");
	}
	
	/**
	 * @see ModuleActivator#stopped()
	 */
	public void stopped() {
		log.info("Radiology FHIR Support Module stopped");
	}
		
}

@@ -676,16 +676,50 @@
 actory;%0A
+import org.openmrs.EncounterType;%0A
 import o
@@ -750,16 +750,16 @@
 ontext;%0A
-
 import o
@@ -1042,16 +1042,40 @@
 onent;%0A%0A
+import java.util.Date;%0A%0A
 /**%0A * T
@@ -1395,16 +1395,115 @@
 erName;%0A
+%09@Value(%22$%7Bproject.parent.artifactId%7D.handlerDescription%22)%0A%09String mrrtTemplateHandlerDescription;%0A
 %09/**%0A%09 *
@@ -1927,328 +1927,8 @@
 %22);%0A
-%09%09DiagnosticReportService diagnosticReportService = Context.getService(DiagnosticReportService.class);%0A%09%09log.info(%22Registering FHIR Diagnostic Report Handler for MRRT templates. Handler name is : %22 + mrrtTemplateHandlerName);%0A%09%09diagnosticReportService.registerHandler(mrrtTemplateHandlerName,new MRRTTemplateHandler());%0A
 %09%7D%0A%09
@@ -2047,24 +2047,44 @@
  started%22);%0A
+%09%09activateModule();%0A
 %09%7D%0A%09%0A%09/**%0A%09 
@@ -2273,24 +2273,24 @@
 stopped() %7B%0A
-
 %09%09log.info(%22
@@ -2332,14 +2332,922 @@
 ed%22);%0A%09%7D
+%0A%0A%09/**%0A%09 * Initialize connection with FHIR module%0A%09 */%0A%09public void activateModule()%7B%0A%0A%09%09DiagnosticReportService diagnosticReportService = Context.getService(DiagnosticReportService.class);%0A%09%09log.info(%22Registering FHIR Diagnostic Report Handler for MRRT templates. Handler name is : %22 + mrrtTemplateHandlerName);%0A%09%09diagnosticReportService.registerHandler(mrrtTemplateHandlerName,new MRRTTemplateHandler());%0A%09%09log.info(%22Registering a new EncounterType for MRRTTemplates. This will be used for lookup by FHIR module : %22 + mrrtTemplateHandlerDescription);%0A%09%09EncounterType mrrtFhirEncounterType = new EncounterType(mrrtTemplateHandlerName, mrrtTemplateHandlerDescription);%0A%09%09mrrtFhirEncounterType.setCreator(Context.getAuthenticatedUser());%0A%09%09mrrtFhirEncounterType.setDateCreated(new Date());%0A%09%09mrrtFhirEncounterType.setRetired(false);%0A%09%09Context.getEncounterService().saveEncounterType(mrrtFhirEncounterType);%0A%09%7D
 %0A%09%09%0A%7D%0A

update ModuleActivator to install module properly and solve callback timing issue