package de.sekmi.li2b2.services;

import java.net.MalformedURLException;
import java.net.URL;

import org.glassfish.hk2.utilities.binding.AbstractBinder;

import de.sekmi.li2b2.api.crc.QueryManager;
import de.sekmi.li2b2.api.ont.Ontology;
import de.sekmi.li2b2.api.pm.ProjectManager;
import de.sekmi.li2b2.api.pm.User;
import de.sekmi.li2b2.services.impl.OntologyImpl;
import de.sekmi.li2b2.services.impl.ProjectManagerImpl;
import de.sekmi.li2b2.services.impl.crc.QueryManagerImpl;
import de.sekmi.li2b2.services.token.TokenManager;

public class MyBinder extends AbstractBinder{

	@Override
	protected void configure() {
//		bind(Impl.class).to(Inter.class);
		// singleton
		
		// project manager
		ProjectManagerImpl pm = new ProjectManagerImpl();
		User user = pm.addUser("demo");//, "i2b2demo");
		user.setPassword("demouser".toCharArray());
		pm.addProject("Demo", "li2b2 Demo").addUserRoles(user, "USER","EDITOR","DATA_OBFSC");
		pm.addProject("Demo2", "li2b2 Demo2").addUserRoles(user, "USER","DATA_OBFSC");
		bind(pm).to(ProjectManager.class);
		
		
		// ontology
		Ontology ont = null;
		String onturl = System.getenv("i2b2ONTSERV_ONTURL");
		if (onturl != null && onturl.matches("(?!file\\b)\\w+?:\\/\\/.*")) {
			try {
				URL url = new URL(onturl);
				ont = OntologyImpl.parse(url);
			} catch (MalformedURLException e) {
				System.err.println("URL cannot be parsed! Maybe the wrong format?");
			}
		}
		else
			ont = OntologyImpl.parse(getClass().getResource("/ontology.xml"));
		bind(ont).to(Ontology.class);
		
		// crc
		QueryManagerImpl crc = new QueryManagerImpl();
		crc.addResultType("PATIENT_COUNT_XML", "CATNUM", "Number of patients");//"Patient count (simple)");
		crc.addResultType("MULT_SITE_COUNT", "CATNUM", "Number of patients per site");//"Patient count (simple)");
		bind(crc).to(QueryManager.class);

		bind(new TokenManagerImpl()).to(TokenManager.class);
		//bind(PMService.class).to(AbstractCell.class);
		//bind(WorkplaceService.class).to(AbstractCell.class);
	}

}

@@ -580,103 +580,918 @@
 %7B%0A%0A%09
-@Override%0A%09protected void configure() %7B%0A//%09%09bind(Impl.class).to(Inter.class);%0A%09%09// singleton%0A%09%09
+/**%0A%09 * Get the URL for the ontology XML data.%0A%09 * Searches for system property ontology.url or %0A%09 * environment variable i2b2ONTSERV_ONTURL.%0A%09 * If neither is specified, the included test ontology %0A%09 * is used.%0A%09 *%0A%09 * @return ontology URL%0A%09 */%0A%09private static URL getOntologyURL()%7B%0A%09%09URL url = null;%0A%09%09// search system property first%0A%09%09String other = System.getProperty(%22ontology.url%22);%0A%09%09// search environment variable second%0A%09%09if( other == null )%7B%0A%09%09%09other = System.getenv(%22i2b2ONTSERV_ONTURL%22);%0A%09%09%7D%0A%09%09if( other != null )%7B%0A%09%09%09// allow any type of local/remote URL%0A%09%09%09try %7B%0A%09%09%09%09url = new URL(other);%0A%09%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09%09System.err.println(%22URL cannot be parsed: %22+other);%0A%09%09%09%09other = null;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09if( other == null )%7B%0A%09%09%09// no other URL specified or parse error%0A%09%09%09url = MyBinder.class.getResource(%22/ontology.xml%22);%0A%09%09%7D%0A%09%09return url;%0A%09%7D%0A%09@Override%0A%09protected void configure() %7B
 %0A%09%09/
@@ -1903,409 +1903,42 @@
 t = 
-null;%0A%09%09String onturl = System.getenv(%22i2b2ONTSERV_ONTURL%22);%0A%09%09if (onturl != null && onturl.matches(%22(?!file%5C%5Cb)%5C%5Cw+?:%5C%5C/%5C%5C/.*%22)) %7B%0A%09%09%09try %7B%0A%09%09%09%09URL url = new URL(onturl);%0A%09%09%09%09ont = OntologyImpl.parse(url);%0A%09%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09%09System.err.println(%22URL cannot be parsed! Maybe the wrong format?%22);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else%0A%09%09%09ont = OntologyImpl.parse(getClass().getResource(%22/ontology.xml%22
+OntologyImpl.parse(getOntologyURL(
 ));%0A

allow other ontology specified for tests via system property ontology.url