/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * 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.data.index.utils;

import java.util.Collection;

/**
 * Driver xquery provider.
 * @author Marek Horst
 * @version 0.7.6
 *
 */
public class QueryProvider {

	/**
	 * Returns query content for finding MDFormatResource for given formatName.
	 * @param formatName
	 * @return query content
	 */
	public static String findMDFormatResource(String formatName) {
		return "for $el in " +
				"fn:collection(\"/DRIVER/MDFormatDSResources/MDFormatDSResourceType\")" +
				"/RESOURCE_PROFILE[BODY/CONFIGURATION/NAME/string() = '"+formatName+"'] " +
				"return $el";
	}
	
	/**
	 * Returns query content for finding IndexDS profile for given indexId.
	 * @param indexId
	 * @return query content
	 */
	public static String findIndexDSProfile(String indexId) {
		return "for $el in " +
				"fn:collection(\"/DRIVER/IndexDSResources/IndexDSResourceType\")" +
				"/RESOURCE_PROFILE[HEADER/RESOURCE_IDENTIFIER/@value/string() = '"+indexId+"'] " +
				"return $el";
	}

	/**
	 * Returns query content for finding IndexDS profiles for given indexIds collection.
	 * @param indexIds
	 * @return query content
	 */
	public static String findIndexDSProfiles(Collection<String> indexIds) {
		if (indexIds==null || indexIds.size()==0)
			return null;
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("for $el in ");
		strBuff.append("fn:collection(\"/DRIVER/IndexDSResources/IndexDSResourceType\")/RESOURCE_PROFILE[");
		int index = 0;
		for (String currentId : indexIds) {
			strBuff.append("HEADER/RESOURCE_IDENTIFIER/@value/string() = '"+currentId+"'");
			if (++index<indexIds.size()) {
				strBuff.append(" or ");
			}
		}
		strBuff.append("] return $el");
		return strBuff.toString();
	}
	
	/**
	 * Returns all collection profiles.
	 * @return all collection profiles
	 */
	public static String getAllCollectionProfiles() {
		return "for $el in " +
				"fn:collection(\"/DRIVER/CollectionDSResources/CollectionDSResourceType\") " +
				"return $el";
	}

}
