package ch.elexis.core.data.extension;

import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.RegistryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.data.constants.ExtensionPointConstantsData;
import ch.elexis.core.data.interfaces.AbstractReferenceDataImporter;
import ch.elexis.core.jdt.NonNull;
import ch.elexis.core.jdt.Nullable;

/**
 * This class offers a centralized contact point for reference data imports. It forwards the update
 * to the appropriate importer. This update forwarding effects all importers included as
 * ExtensionPoint {@code ch.elexis.core.data.referenceDataImporter} and extending the
 * {@link AbstractReferenceDataImporter} class
 *
 */
public class ReferenceDataImporterExtensionPoint {
	private static Logger log = LoggerFactory.getLogger(ReferenceDataImporterExtensionPoint.class
		.getName());
	
	private static final String CLASS_PROPERTY = "class";
	private static final String ATTRIBUTE_REFDATA_ID = "referenceDataId";
	private static HashMap<String, AbstractReferenceDataImporter> importers;
	
	/**
	 * loads all ExtensionPoints of type {@code ExtensionPointConstantsData.REFERENCE_DATA_IMPORTER}
	 * and adds them to a list of available importers
	 */
	private ReferenceDataImporterExtensionPoint(){
		try {
			importers = new HashMap<String,AbstractReferenceDataImporter>();
			
			// load reference-data-extensionpoint
			IExtensionPoint refDataExtensionPoint =
				RegistryFactory.getRegistry().getExtensionPoint(
					ExtensionPointConstantsData.REFERENCE_DATA_IMPORTER);
			IConfigurationElement[] extensionPoints =
				refDataExtensionPoint.getConfigurationElements();
			
			// add all found extensionPoints to the importer list
			for (IConfigurationElement ePoint : extensionPoints) {
				Object o = ePoint.createExecutableExtension(CLASS_PROPERTY);
				String refDataId = ePoint.getAttribute(ATTRIBUTE_REFDATA_ID);
				
				if (o instanceof AbstractReferenceDataImporter) {
					AbstractReferenceDataImporter importer = (AbstractReferenceDataImporter) o;
					importers.put(refDataId, importer);
					
					log.debug("Added ReferenceDataImporter for... "+refDataId);
				}
			}
		} catch (CoreException e) {
			log.error("Exception occured trying to load ReferenceDataImporter ExtensionPoints", e);
		}
	}
	
	/**
	 * 
	 * @param refDataId the id of the requested reference data type
	 * @return the {@link AbstractReferenceDataImporter} if found, else null
	 */
	public static @Nullable AbstractReferenceDataImporter getReferenceDataImporterByReferenceDataId(@NonNull String refDataId) {
		return importers.get(refDataId);
	}
}

@@ -853,16 +853,17 @@
 class%0A *
+ 
 %0A */%0Apub
@@ -1225,24 +1225,408 @@
 ers;%0A%09%0A%09/**%0A
+%09 * %0A%09 * @param refDataId%0A%09 *            the id of the requested reference data type%0A%09 * @return the %7B@link AbstractReferenceDataImporter%7D if found, else null%0A%09 */%0A%09public static @Nullable%0A%09AbstractReferenceDataImporter getReferenceDataImporterByReferenceDataId(@NonNull%0A%09String refDataId)%7B%0A%09%09if (importers == null) %7B%0A%09%09%09initialize();%0A%09%09%7D%0A%09%09return importers.get(refDataId);%0A%09%7D%0A%09%0A%09/**%0A
 %09 * loads al
@@ -1780,43 +1780,30 @@
 ate 
-ReferenceDataImporterExtensionPoint
+static void initialize
 ()%7B%0A
@@ -1844,16 +1844,17 @@
 %3CString,
+ 
 Abstract
@@ -2671,9 +2671,11 @@
 .. %22
-+
+ + 
 refD
@@ -2825,326 +2825,5 @@
 %0A%09%7D%0A
-%09%0A%09/**%0A%09 * %0A%09 * @param refDataId the id of the requested reference data type%0A%09 * @return the %7B@link AbstractReferenceDataImporter%7D if found, else null%0A%09 */%0A%09public static @Nullable AbstractReferenceDataImporter getReferenceDataImporterByReferenceDataId(@NonNull String refDataId) %7B%0A%09%09return importers.get(refDataId);%0A%09%7D%0A%7D%0A
+%7D

Fix NPE in ReferenceDataImporterExtensionPoint