All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openehr.terminology.SimpleTerminologyService Maven / Gradle / Ivy

The newest version!
/*
 * component:   "openEHR Reference Implementation"
 * description: "Class SimpleTerminologyService"
 * keywords:    "terminology"
 *
 * author:      "Rong Chen "
 * copyright:   "Copyright (c) 2007 Rong Chen"
 * license:     "See notice at bottom of class"
 *
 * file:        "$URL$"
 * revision:    "$LastChangedRevision$"
 * last_change: "$LastChangedDate$"
 */
package org.openehr.terminology;

import java.util.*;
import org.apache.log4j.Logger;
import org.openehr.rm.support.terminology.*;

/**
 * A simple implementation of terminology service that provides
 * complete openEHR terminology and necessary code sets for the 
 * kernel to work properly
 * 
 * TODO: load complete external codesets 
 * 
 * @author rong.chen
 */
public class SimpleTerminologyService implements TerminologyService {

	/**
	 * Gets an instance of terminology service
	 * 
	 * @return terminology service
	 * @throws Exception
	 */
	public static TerminologyService getInstance() throws Exception {
		return soleInstance;
	}
	
	public TerminologyAccess terminology(String name) {
		return terminologies.get(name);
	}

	public CodeSetAccess codeSet(String name) {
		return codeSets.get(name);
	}

	public CodeSetAccess codeSetForId(OpenEHRCodeSetIdentifiers id) {
		String name = codeSetInternalIdToExternalName.get(id.toString());
		if(name == null) {
			return null;
		}
		return codeSets.get(name);
	}

	public boolean hasTerminology(String name) {
		return terminologies.containsKey(name);
	}

	public boolean hasCodeSet(String name) {
		return codeSetInternalIdToExternalName.containsKey(name);
	}

	public List terminologyIdentifiers() {
		return new ArrayList(terminologies.keySet());
	}

	public List codeSetIdentifiers() {
		return new ArrayList(codeSets.keySet());
	}

	public Map openehrCodeSets() {
		return Collections.unmodifiableMap(codeSetInternalIdToExternalName);
	}
	
	/*
	 * Creates a simpleTerminologyService
	 */
	private SimpleTerminologyService() {
		
		terminologies = new HashMap();
		codeSets = new HashMap();
		codeSetInternalIdToExternalName = new HashMap();
		
		try {			
			TerminologySource terminologySource = 
				TerminologySourceFactory.getOpenEHRTerminology();
			
			loadTerminologies(terminologySource);
			loadCodeSets(terminologySource);
			
			terminologySource = 
			TerminologySourceFactory.getExternalTerminologies();
				
			loadTerminologies(terminologySource);
			loadCodeSets(terminologySource);			
			
		} catch(Exception e) {
			log.error("failed to initialize terminology service..", e);
			throw new RuntimeException(e);
		}
	}
	
	private void loadTerminologies(TerminologySource source) {	
		
		SimpleTerminologyAccess terminology = (SimpleTerminologyAccess)
				terminologies.get(TerminologyService.OPENEHR);
		if(terminology == null) {
			terminology = new SimpleTerminologyAccess(TerminologyService.OPENEHR);
		}
		
		List groups = source.getConceptGroups();
		for(Group group : groups) {
			Set codes = new HashSet();
			Map names = new HashMap();
			names.put("en", group.name);
			for(Concept concept : group.concepts) {
				codes.add(concept.id);
				terminology.addRubric("en", concept.id, concept.rubric);
			}
			// English name as group id
			terminology.addGroup(group.name, codes, names);
		}		
		terminologies.put(TerminologyService.OPENEHR, terminology);
	}
	
	private void loadCodeSets(TerminologySource source) {		
		
		for(CodeSet codeset : source.getCodeSets()) {
			SimpleCodeSetAccess codeSetAccess = new SimpleCodeSetAccess(
					codeset.externalId, new HashSet(codeset.codes));
			codeSets.put(codeset.externalId, codeSetAccess);
			codeSetInternalIdToExternalName.put(codeset.openehrId, 
					codeset.externalId);
		}
	}
	
	/* static final field */
	private static final Logger log = 
		Logger.getLogger(SimpleTerminologyService.class);
	
	/* code sets indexed by external codeset name */
	private Map codeSets;
	
	/* terminology indexed by name */
	private Map terminologies;
	
	/* mapping between external name and openEHR codeset id */ 
	private Map codeSetInternalIdToExternalName;
	
	/* static instance */
	private static TerminologyService soleInstance = new SimpleTerminologyService();
}
/*
 *  ***** BEGIN LICENSE BLOCK *****
 *  Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 *  The contents of this file are subject to the Mozilla Public License Version
 *  1.1 (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.mozilla.org/MPL/
 *
 *  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.
 *
 *  The Original Code is SimpleTerminologyService.java
 *
 *  The Initial Developer of the Original Code is Rong Chen.
 *  Portions created by the Initial Developer are Copyright (C) 2007
 *  the Initial Developer. All Rights Reserved.
 *
 *  Contributor(s):
 *
 * 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.
 *
 *  ***** END LICENSE BLOCK *****
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy