/*
 * Copyright (c) 2014, Wieland Hoffmann
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.musicbrainz.search.solrwriter;

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
	protected Marshaller createMarshaller() throws JAXBException {
		if (jsonContext == null) {
			return null;
		} else {
			return jsonContext.createMarshaller();
		}
	}

	@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
	public String getContentType(SolrQueryRequest arg0, SolrQueryResponse arg1) {
		return "application/json";
	}
}

@@ -2844,157 +2844,8 @@
 ) %7B%0A
-%09%09%09// Unable to initilize%0A%09%09%09// jaxb/Users/paul/code/MusicBrainz/SearchServer/servlet/src/main/resources/oxml.xml%0A%09%09%09// context, should never happen%0A
 %09%09%09t

remove a useless comment