package uk.ac.ebi.quickgo.output.xml;

import javax.xml.bind.JAXBException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import uk.ac.ebi.quickgo.cache.query.service.CacheRetrieval;
import uk.ac.ebi.quickgo.ontology.go.GOTerm;
import uk.ac.ebi.quickgo.output.EntityToStream;
import uk.ac.ebi.quickgo.render.Format;
import uk.ac.ebi.quickgo.solr.query.service.ontology.TermRetrieval;

/**
 * Integration test for XML Generation
 * @author cbonill
 *
 */
public class XMLGenerationIntegrationTest {


	static ApplicationContext appContext;
	static CacheRetrieval<GOTerm> cacheRetrieval;
	static TermRetrieval termRetrieval;
	
	public static void main(String[] args) throws JAXBException{
		
		appContext = new ClassPathXmlApplicationContext("service-beans.xml","common-beans.xml","query-beans.xml");
		
		EntityToStream<GOTerm> termEntityToStream = (EntityToStream<GOTerm>) appContext.getBean("goTermEntityToStream");
		cacheRetrieval = (CacheRetrieval<GOTerm>) appContext.getBean("goTermCacheRetrieval");
		termRetrieval = (TermRetrieval) appContext.getBean("termRetrieval");
		// Get basic information for a few entries and convert them to XML
		GOTerm goTerm = getEntry("GO:0005458");
		termEntityToStream.convertToXMLStream(goTerm, Format.XML, System.out);
		
		goTerm = getEntry("GO:0043066");
		termEntityToStream.convertToXMLStream(goTerm, Format.XML, System.out);
	}
	
	/**
	 * Get an entry from Solr and once it's cached, get it from the in-memory cache
	 * @param entry Entry to retrieve
	 */
	private static GOTerm getEntry(String entry) {		
		
		try {
			// First time the entry is gotten from Solr
			long startTime = System.currentTimeMillis();
			GOTerm term = cacheRetrieval.retrieveEntry(entry, GOTerm.class);
			long estimatedTime = System.currentTimeMillis() - startTime;
			System.out.println("Term ID: " + term.getId());
			System.out.println("Term NAME: " + term.getName());
			System.out.println("Term ONTOLOGY: " + term.getAspect().description);
			System.out.println("Time (ms) getting information from Solr: "
					+ estimatedTime);

			// Second time it's already cached
			startTime = System.currentTimeMillis();
			term = cacheRetrieval.retrieveEntry(entry, GOTerm.class);
			estimatedTime = System.currentTimeMillis() - startTime;
			System.out.println("Time (ms) getting information from in-memory cache: "
					+ estimatedTime);
			System.out.println("====================");
			return term;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}

@@ -70,16 +70,48 @@
 ption;%0A%0A
+import org.slf4j.LoggerFactory;%0A
 import o
@@ -735,16 +735,105 @@
 ieval;%0A%09
+static final Logger logger = LoggerFactory.getLogger(XMLGenerationIntegrationTest.class)%0A
 %0A%09public
@@ -887,18 +887,16 @@
 eption%7B%0A
-%09%09
 %0A%09%09appCo
@@ -997,18 +997,16 @@
 .xml%22);%0A
-%09%09
 %0A%09%09Entit
@@ -1456,18 +1456,16 @@
 m.out);%0A
-%09%09
 %0A%09%09goTer
@@ -1568,17 +1568,16 @@
 ut);%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 
@@ -1742,21 +1742,17 @@
 entry) %7B
-%09%09%0A%09%09
+%0A
 %0A%09%09try %7B
@@ -1982,34 +1982,27 @@
 ime;%0A%09%09%09
-System.out.println
+logger.info
 (%22Term I
@@ -2026,34 +2026,27 @@
 ());%0A%09%09%09
-System.out.println
+logger.info
 (%22Term N
@@ -2074,34 +2074,27 @@
 ());%0A%09%09%09
-System.out.println
+logger.info
 (%22Term O
@@ -2140,34 +2140,27 @@
 on);%0A%09%09%09
-System.out.println
+logger.info
 (%22Time (
@@ -2424,34 +2424,27 @@
 ime;%0A%09%09%09
-System.out.println
+logger.info
 (%22Time (
@@ -2521,26 +2521,19 @@
 %0A%09%09%09
-System.out.println
+logger.info
 (%22==
@@ -2603,26 +2603,22 @@
 %0A%09%09%09
-e.printStackTrace(
+logger.error(e
 );%0A%09
@@ -2638,12 +2638,11 @@
 ull;%0A%09%7D%0A
-%09
 %0A%7D%0A

Move from using Log4j to Simple Logging Facade For Java (SLF4J)