package org.musicbrainz.search.solrwriter;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.musicbrainz.mmd2.Metadata;

public class MBJSONWriter extends MBXMLWriter {
	/**
	 * A separate context for json. The context for XML is still necessary to
	 * *un*marshal the _store field
	 */
	private JAXBContext jsonContext = null;

	private JAXBContext createJAXBJSONContext() throws JAXBException {
		try {
			Map<String, Object> properties = new HashMap<String, Object>(3);
			properties.put(JAXBContextProperties.OXM_METADATA_SOURCE,
					"oxml.xml");
			properties
					.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
			properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
			properties.put(JAXBContextProperties.JSON_VALUE_WRAPPER, "name");
			return JAXBContextFactory.createContext(
					new Class[] { Metadata.class }, properties);
		} catch (JAXBException ex) {
			// Unable to initilize
			// jaxb/Users/paul/code/MusicBrainz/SearchServer/servlet/src/main/resources/oxml.xml
			// context, should never happen
			throw new RuntimeException(ex);
		}
	}

	@Override
	public void init(NamedList initArgs) {
		super.init(initArgs);
		try {
			jsonContext = createJAXBJSONContext();
			marshaller = createMarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
	}

	@Override
	protected Marshaller createMarshaller() throws JAXBException {
		if (jsonContext == null) {
			return null;
		} else {
			return jsonContext.createMarshaller();
		}
	}

	@Override
	public String getContentType(SolrQueryRequest arg0, SolrQueryResponse arg1) {
		return "application/json";
	}

	@Override
	protected void doWrite(Writer writer) throws IOException {
		StringWriter sw = new StringWriter();
		try {
			marshaller.marshal(this.metadatalistwrapper.getCompletedMetadata(),
					sw);
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		writer.write(sw.toString());
	}
}

@@ -1528,32 +1528,213 @@
 on(ex);%0A%09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09protected Marshaller createMarshaller() throws JAXBException %7B%0A%09%09if (jsonContext == null) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09%09%09return jsonContext.createMarshaller();%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Override%0A%09publ
@@ -1993,189 +1993,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09protected Marshaller createMarshaller() throws JAXBException %7B%0A%09%09if (jsonContext == null) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09%09%09return jsonContext.createMarshaller();%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Ov

Move createMarshaller