/**
 * Copyright © 2008-2009 DRIVER PROJECT (ICM UW)
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.resultset.impl.utils;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

/**
 * ResultSet Service utilities class.
 * 
 * @author Marek Horst
 * @version 0.01
 * 
 */
public class Utils {

	protected static final Logger log = Logger.getLogger(Utils.class);

	public static final String RESULT_SET_NAME = "theResultSet";

	/**
	 * Builds ResultSet end point reference for given serviceAddress and
	 * resultSetId.
	 * 
	 * @param serviceAddress
	 * @param resultSetId
	 * @param wsdlLocation
	 * @return resultSet EPR
	 */
	public static String buildResultSetEPR(String serviceAddress,
			String resultSetId, String wsdlLocation) {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		strBuff
				.append("<wsa:EndpointReference xmlns:wsa=\"http://www.driver.org/schema\" xmlns:driver=\"http://www.driver.org\" xmlns:wsaw=\"http://www.w3.org/2006/02/addressing/wsdl\" xmlns:wsdl=\"http://www.w3.org/2005/08/wsdl-instance\">");
		strBuff.append("<wsa:Address>");
		strBuff.append(serviceAddress);
		strBuff.append("</wsa:Address>");
		strBuff.append("<wsa:ReferenceParameters>");
		strBuff.append("<driver:ResourceIdentifier>");
		strBuff.append(resultSetId);
		strBuff.append("</driver:ResourceIdentifier>");
		strBuff.append("</wsa:ReferenceParameters>");
		strBuff.append("<wsa:Metadata wsdl:wsdlLocation=\"" + wsdlLocation
				+ "\">");
		strBuff.append("<wsaw:ServiceName>");
		strBuff.append(RESULT_SET_NAME);
		strBuff.append("</wsaw:ServiceName>");
		strBuff.append("</wsa:Metadata>");
		strBuff.append("</wsa:EndpointReference>");
		return strBuff.toString();
	}

	/**
	 * Builds W3C ResultSet end point reference for given serviceAddress and
	 * resultSetId.
	 * 
	 * @param serviceAddress
	 * @param resultSetId
	 * @param wsdlLocation
	 * @return W3C resultSet EPR
	 * @throws ParserConfigurationException 
	 */
	public static W3CEndpointReference buildW3CResultSetEPR(
			String serviceAddress, String resultSetId, String wsdlLocation) throws ParserConfigurationException {

		final W3CEndpointReferenceBuilder W3CResultSetEPR = new W3CEndpointReferenceBuilder();

		W3CResultSetEPR.address(serviceAddress);
		W3CResultSetEPR.serviceName(new QName("http://www.w3.org/2006/02/addressing/wsdl",RESULT_SET_NAME));
		W3CResultSetEPR.endpointName(new QName("http://www.driver.org/schema",RESULT_SET_NAME));
		W3CResultSetEPR.wsdlDocumentLocation(wsdlLocation);

		final Document doc = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder().newDocument();
		
		final Element referenceElement = doc.createElementNS(
				"http://www.driver.org", "ResourceIdentifier:ResourceIdentifier");
		referenceElement.setTextContent(resultSetId);
		W3CResultSetEPR.referenceParameter(referenceElement);
		
		return W3CResultSetEPR.build();
	}

}
