Index: main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java	(revision 36155)
+++ main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java	(revision 36156)
@@ -279,95 +279,195 @@
 	}
 
+    static private void doBaseIndexInternal(Document to, Node searchNode, String new_indexes_str, String new_index_str, String existing_index_str) {
+
+	Element toElement = to.getDocumentElement();
+	Element indexes_element = to.createElement(new_indexes_str);//<Indexes> // INDEXES_ELEMENT
+	indexes_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+	//indexes_element.setAttribute(StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
+
+	NodeList index_children = ((Element) searchNode).getElementsByTagName(existing_index_str);//index INDEx_LOW_STR
+	int num_nodes = index_children.getLength();
+	
+	for (int i = 0; i < num_nodes; i++)
+	    {
+
+		Element index_element = to.createElement(new_index_str);//<Index> INDEX_ELEMENT
+		Element e = (Element) index_children.item(i);
+		String index_str = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+		String index_str_display = index_str;//for creating collectionmetadata for this index 
+		
+		// look for options inside the index, eg
+		// <option name="solrfieldtype" value="text_ja" />
+		String options_str = "";
+		NodeList option_children = e.getElementsByTagName(StaticStrings.OPTION_STR);
+		if(option_children != null) {
+		    for (int j = 0; j < option_children.getLength(); j++) {
+			Element el = (Element) option_children.item(j);
+			
+			String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+			options_str = options_str + ((name_str.equals("")) ? "" : (" " + name_str));
+			
+			String value_str = el.getAttribute(StaticStrings.VALUE_ATTRIBUTE);
+			options_str = options_str + ((name_str.equals("")) ? "" : (" " + value_str));
+			
+			if (name_str.equals("") && !value_str.equals(""))
+			    {
+				continue; //invalid condition
+			    }
+			
+			Element option_element = null;
+			option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
+			option_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+			
+			if (!name_str.equals(""))
+			    {
+				option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+			    }
+			if (value_str != null && !value_str.equals(""))
+			    {
+				XMLTools.setNodeText(option_element, value_str);
+			    }
+			index_element.appendChild(option_element);
+		    }
+		}
+
+		// Handling 'index' element
+		// Double check to make sure it's not colon separated style index.
+		if (index_str.indexOf(StaticStrings.COLON_CHARACTER) != -1)
+		    {
+			System.err.println("Something is wrong! the "+existing_index_str+" should NOT be level:source tuplets style.");
+			index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
+			
+		    }
+		//Each index may have a list of comma-separated strings.
+		//split them into 'content' elements in the internal structure
+		StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
+		while (content_tokenizer.hasMoreTokens())
+		    {
+			// Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.             
+			
+			Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
+			String content_str = content_tokenizer.nextToken();
+			// Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
+			if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR) || content_str.equals(StaticStrings.ALLFIELDS_STR)))
+			    {
+				content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
+			    }
+			content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
+			index_element.appendChild(content_element);
+			content_element = null;
+		    } //while ends
+
+		indexes_element.appendChild(index_element);
+		
+		index_element = null;
+
+		// Handling 'displayItem' element of this 'index' element
+		// 'e' is the parent element 'index' of 'displayItem' element
+		ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
+		appendArrayList(toElement, collectionmetadata_list);
+		
+	    } // for loop ends
+	toElement.appendChild(indexes_element);
+	//return toElement;
+
+    }
 	//This is actually doing indexes for both mgpp and lucene
 	static private void doMGPPIndexes(Document to, Node searchNode)
 	{
-		Element toElement = to.getDocumentElement();
-		Element indexes_element = to.createElement(StaticStrings.INDEXES_ELEMENT);//<Indexes>
-		indexes_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
-		indexes_element.setAttribute(StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
-
-		NodeList index_children = ((Element) searchNode).getElementsByTagName(StaticStrings.INDEX_LOW_STR);//index
-		int num_nodes = index_children.getLength();
-
-		for (int i = 0; i < num_nodes; i++)
-		{
-
-			Element index_element = to.createElement(StaticStrings.INDEX_ELEMENT);//<Index>
-			Element e = (Element) index_children.item(i);
-			String index_str = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
-			String index_str_display = index_str;//for creating collectionmetadata for this index 
-
-
-			String options_str = "";
-			NodeList option_children = e.getElementsByTagName(StaticStrings.OPTION_STR);
-			if(option_children != null) {
-			   for (int j = 0; j < option_children.getLength(); j++) {
-				Element el = (Element) option_children.item(j);
-
-				String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
-				options_str = options_str + ((name_str.equals("")) ? "" : (" " + name_str));
-
-				String value_str = el.getAttribute(StaticStrings.VALUE_ATTRIBUTE);
-				options_str = options_str + ((name_str.equals("")) ? "" : (" " + value_str));
-
-				if (name_str.equals("") && !value_str.equals(""))
-				{
-					continue; //invalid condition
-				}
-
-				Element option_element = null;
-				option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
-				option_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
-
-				if (!name_str.equals(""))
-				{
-					option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
-				}
-				if (value_str != null && !value_str.equals(""))
-				{
-					XMLTools.setNodeText(option_element, value_str);
-				}
-				index_element.appendChild(option_element);
-			   }
-			}
-
-			// Handling 'index' element
-			// Double check to make sure it's not colon separated style index.
-			if (index_str.indexOf(StaticStrings.COLON_CHARACTER) != -1)
-			{
-				System.err.println("Something is wrong! the index should NOT be level:source tuplets style.");
-				index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
-
-			}
-			//Each index may have a list of comma-separated strings.
-			//split them into 'content' elements in the internal structure
-			StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
-			while (content_tokenizer.hasMoreTokens())
-			{
-				// Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.             
-
-				Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
-				String content_str = content_tokenizer.nextToken();
-				// Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
-				if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR) || content_str.equals(StaticStrings.ALLFIELDS_STR)))
-				{
-					content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
-				}
-				content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
-				index_element.appendChild(content_element);
-				content_element = null;
-			} //while ends
-
-			indexes_element.appendChild(index_element);
-
-			index_element = null;
-
-			// Handling 'displayItem' element of this 'index' element
-			// 'e' is the parent element 'index' of 'displayItem' element
-			ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
-			appendArrayList(toElement, collectionmetadata_list);
-
-		} // for loop ends
-		toElement.appendChild(indexes_element);
+	     doBaseIndexInternal(to, searchNode, StaticStrings.INDEXES_ELEMENT, StaticStrings.INDEX_ELEMENT, StaticStrings.INDEX_LOW_STR);
+	     Element toElement = to.getDocumentElement();
+	     Element indexes_element = (Element) XMLTools.getChildByTagName(toElement, StaticStrings.INDEXES_ELEMENT);
+	    indexes_element.setAttribute(StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
+
+		// Element toElement = to.getDocumentElement();
+		// Element indexes_element = to.createElement(StaticStrings.INDEXES_ELEMENT);//<Indexes>
+		// indexes_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+		// indexes_element.setAttribute(StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
+
+		// NodeList index_children = ((Element) searchNode).getElementsByTagName(StaticStrings.INDEX_LOW_STR);//index
+		// int num_nodes = index_children.getLength();
+
+		// for (int i = 0; i < num_nodes; i++)
+		// {
+
+		// 	Element index_element = to.createElement(StaticStrings.INDEX_ELEMENT);//<Index>
+		// 	Element e = (Element) index_children.item(i);
+		// 	String index_str = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+		// 	String index_str_display = index_str;//for creating collectionmetadata for this index 
+
+		// 	// look for options inside the index, eg
+		// 	// <option name="solrfieldtype" value="text_ja" />
+		// 	String options_str = "";
+		// 	NodeList option_children = e.getElementsByTagName(StaticStrings.OPTION_STR);
+		// 	if(option_children != null) {
+		// 	   for (int j = 0; j < option_children.getLength(); j++) {
+		// 		Element el = (Element) option_children.item(j);
+
+		// 		String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+		// 		options_str = options_str + ((name_str.equals("")) ? "" : (" " + name_str));
+
+		// 		String value_str = el.getAttribute(StaticStrings.VALUE_ATTRIBUTE);
+		// 		options_str = options_str + ((name_str.equals("")) ? "" : (" " + value_str));
+
+		// 		if (name_str.equals("") && !value_str.equals(""))
+		// 		{
+		// 			continue; //invalid condition
+		// 		}
+
+		// 		Element option_element = null;
+		// 		option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
+		// 		option_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+
+		// 		if (!name_str.equals(""))
+		// 		{
+		// 			option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+		// 		}
+		// 		if (value_str != null && !value_str.equals(""))
+		// 		{
+		// 			XMLTools.setNodeText(option_element, value_str);
+		// 		}
+		// 		index_element.appendChild(option_element);
+		// 	   }
+		// 	}
+
+		// 	// Handling 'index' element
+		// 	// Double check to make sure it's not colon separated style index.
+		// 	if (index_str.indexOf(StaticStrings.COLON_CHARACTER) != -1)
+		// 	{
+		// 		System.err.println("Something is wrong! the index should NOT be level:source tuplets style.");
+		// 		index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
+
+		// 	}
+		// 	//Each index may have a list of comma-separated strings.
+		// 	//split them into 'content' elements in the internal structure
+		// 	StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
+		// 	while (content_tokenizer.hasMoreTokens())
+		// 	{
+		// 		// Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.             
+
+		// 		Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
+		// 		String content_str = content_tokenizer.nextToken();
+		// 		// Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
+		// 		if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR) || content_str.equals(StaticStrings.ALLFIELDS_STR)))
+		// 		{
+		// 			content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
+		// 		}
+		// 		content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
+		// 		index_element.appendChild(content_element);
+		// 		content_element = null;
+		// 	} //while ends
+
+		// 	indexes_element.appendChild(index_element);
+
+		// 	index_element = null;
+
+		// 	// Handling 'displayItem' element of this 'index' element
+		// 	// 'e' is the parent element 'index' of 'displayItem' element
+		// 	ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
+		// 	appendArrayList(toElement, collectionmetadata_list);
+
+		// } // for loop ends
+		// toElement.appendChild(indexes_element);
 
 		// create another set of <indexes> which will be used when user switches to MG
@@ -408,9 +508,174 @@
 	}
 
+    static private void doSorts(Document to, Node searchNode) {
+	doBaseIndexInternal(to, searchNode, StaticStrings.SORTS_ELEMENT, StaticStrings.SORT_ELEMENT, StaticStrings.SORT_LOW_STR);
+    }
+    static private void doSortsOld(Document to, Node searchNode)
+    {
+	Element toElement = to.getDocumentElement();
+	NodeList sort_children = ((Element) searchNode).getElementsByTagName(StaticStrings.SORT_LOW_STR);
+	int num_nodes = sort_children.getLength();
+	
+	// there are no sort fields
+	if (num_nodes < 1)
+	    {
+		return;
+	    }
+	
+	Element sorts = to.createElement(StaticStrings.SORTS_ELEMENT);
+
+	for (int i = 0; i < num_nodes; i++)
+	    {
+		Element sort_element = to.createElement(StaticStrings.SORT_ELEMENT);
+		Element sort_child = (Element) sort_children.item(i);
+		String name_str = sort_child.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+		sort_element.setAttribute(StaticStrings.NAME_STR, name_str);
+		sorts.appendChild(sort_element);
+
+		// Contructing 'collectionmetadata' elements from the 'displayItem' of this 'sort' element
+		ArrayList displayItem_list = XMLTools.getNamedElementList(sort_child, StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
+		if (displayItem_list == null)
+		    {
+			// there is no display item for this element
+			continue;
+		    }
+		for (int j = 0; j < displayItem_list.size(); j++)
+		    {
+			Element item = (Element) displayItem_list.get(j);
+			String text = XMLTools.getNodeText(item);
+			//If there is nothing to display, don't bother creating the element
+			// Either lang or key should be set. If key set, no textnode.
+			// if lang set, should have textnode. Check if it's meaningful
+			if (item.hasAttribute(StaticStrings.LANG_STR) && text.equals(""))
+			    {
+				continue;
+			    }
+			
+			// get the value in 'lang=langcode' or 'key=keyname'
+			// We can have either a key attribute (with optional dictionary attribute, else dictionary is implicit)
+			// OR we can have a lang attr, with the nodetext containing the actual display strin.g
+			String lang = item.getAttribute(StaticStrings.LANG_ATTRIBUTE);
+			String key = item.getAttribute(StaticStrings.KEY_ATTRIBUTE);
+			
+			
+			Element collectionmetadata = to.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
+			collectionmetadata.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+			collectionmetadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+			
+			if(!lang.equals("")) {
+			    collectionmetadata.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, lang);
+			    XMLTools.setNodeText(collectionmetadata, text);
+			}
+			if(!key.equals("")) {
+			    collectionmetadata.setAttribute(StaticStrings.KEY_ATTRIBUTE, key);
+			    String dictionary = item.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
+			    if(!dictionary.equals("")) {
+				collectionmetadata.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary);
+			    }
+			}
+			
+			appendProperly(toElement, collectionmetadata);
+		    }
+	    }
+	toElement.appendChild(sorts);
+	}
+
+    // TODO
+    static private void doDefaultSort(Document to, Node searchNode)
+    {
+	Element toElement = to.getDocumentElement();
+	
+	Element from_e = (Element) XMLTools.getChildByTagName(searchNode, StaticStrings.SORT_DEFAULT_ELEMENT);
+	if (from_e == null) {
+	    return; // there is no default
+	}
+	Element default_sort = to.createElement(StaticStrings.SORT_DEFAULT_ELEMENT);
+	
+	default_sort.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+	String name = from_e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+	default_sort.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
+	appendProperly(toElement, default_sort);
+    }
+    static private void doFacets(Document to, Node searchNode) {
+	doBaseIndexInternal(to, searchNode, StaticStrings.FACETS_ELEMENT, StaticStrings.FACET_ELEMENT, StaticStrings.FACET_LOW_STR);
+    }
+
+    
+    static private void doFacetsOld(Document to, Node searchNode)
+    {
+	Element toElement = to.getDocumentElement();
+	NodeList facet_children = ((Element) searchNode).getElementsByTagName(StaticStrings.FACET_LOW_STR);
+	int num_nodes = facet_children.getLength();
+	
+	// there are no facet fields
+	if (num_nodes < 1)
+	    {
+		return;
+	    }
+	
+	Element facets = to.createElement(StaticStrings.FACETS_ELEMENT);
+
+	for (int i = 0; i < num_nodes; i++)
+	    {
+		Element facet_element = to.createElement(StaticStrings.FACET_ELEMENT);
+		Element facet_child = (Element) facet_children.item(i);
+		String name_str = facet_child.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+		facet_element.setAttribute(StaticStrings.NAME_STR, name_str);
+		facets.appendChild(facet_element);
+
+		// Contructing 'collectionmetadata' elements from the 'displayItem' of this 'facet' element
+		ArrayList displayItem_list = XMLTools.getNamedElementList(facet_child, StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
+		if (displayItem_list == null)
+		    {
+			// there is no display item for this element
+			continue;
+		    }
+		for (int j = 0; j < displayItem_list.size(); j++)
+		    {
+			Element item = (Element) displayItem_list.get(j);
+			String text = XMLTools.getNodeText(item);
+			//If there is nothing to display, don't bother creating the element
+			// Either lang or key should be set. If key set, no textnode.
+			// if lang set, should have textnode. Check if it's meaningful
+			if (item.hasAttribute(StaticStrings.LANG_STR) && text.equals(""))
+			    {
+				continue;
+			    }
+			
+			// get the value in 'lang=langcode' or 'key=keyname'
+			// We can have either a key attribute (with optional dictionary attribute, else dictionary is implicit)
+			// OR we can have a lang attr, with the nodetext containing the actual display strin.g
+			String lang = item.getAttribute(StaticStrings.LANG_ATTRIBUTE);
+			String key = item.getAttribute(StaticStrings.KEY_ATTRIBUTE);
+			
+			
+			Element collectionmetadata = to.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
+			collectionmetadata.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+			collectionmetadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+			
+			if(!lang.equals("")) {
+			    collectionmetadata.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, lang);
+			    XMLTools.setNodeText(collectionmetadata, text);
+			}
+			if(!key.equals("")) {
+			    collectionmetadata.setAttribute(StaticStrings.KEY_ATTRIBUTE, key);
+			    String dictionary = item.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
+			    if(!dictionary.equals("")) {
+				collectionmetadata.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary);
+			    }
+			}
+			
+			appendProperly(toElement, collectionmetadata);
+		    }
+	    }
+	toElement.appendChild(facets);
+	}
+
+
   //This processes <sort> (lucene), or <sort> and <facet> (solr) subelements of <search> also <defaultSort>
   // Note that GLI at present does not allow the user to set or modify these.
   // This function, and its inverse convertSolrFacetsAndSorts, are only here to preserve such elements if they already exist in the collectionConfig.xml. At present they need to be manually added there.
   // it just copies them into storage.
-	static private void doSolrFacetsAndSorts(Document to, Node searchNode)
+	static private void doSolrFacetsAndSortsdeletE(Document to, Node searchNode)
 	{
 		Element toElement = to.getDocumentElement();
@@ -2104,4 +2369,10 @@
 		//Get the 'search' element from 'to' which has already been created in 'convertBuildType'
 		Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
+		String buildtype = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
+		if (buildtype.equals(StaticStrings.MG_STR)){
+		    convertMGIndex(from, to);
+		    return;
+		}
+
 
 		//THere are two sets of indexes elements, find the one which is assigned 'true'
@@ -2119,17 +2390,39 @@
 		}
 
-		//find out it's mg or mgpp/lucene
-		String mg = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
-		boolean mg_indexer = false;
-		if (mg.equals(StaticStrings.MG_STR))
-		{
-			mg_indexer = true;//it's mg, then the level is set as attribute of
-		}
-		if (mg_indexer == false)
-		{
-			// It's mgpp. Construct 'level' and 'defaultLevel' elements separately.
-			convertLevels(from, to, search);
-		}
-
+		// do the levels
+		convertLevels(from, to, search);
+
+		// indexes
+		convertBaseIndex(from, to, search, StaticStrings.INDEXES_ELEMENT, StaticStrings.INDEX_ELEMENT, StaticStrings.INDEX_LOW_STR);
+		//Convert default index
+		convertDefaultIndex(from, to, search);
+		
+		// sortfields
+		if (buildtype.equals(StaticStrings.LUCENE_STR) || buildtype.equals(StaticStrings.SOLR_STR)) {
+		    convertBaseIndex(from, to, search, StaticStrings.SORTS_ELEMENT, StaticStrings.SORT_ELEMENT, StaticStrings.SORT_LOW_STR);
+		}
+		// facet fields
+		if (buildtype.equals(StaticStrings.SOLR_STR)) {
+		    convertBaseIndex(from, to, search, StaticStrings.FACETS_ELEMENT, StaticStrings.FACET_ELEMENT, StaticStrings.FACET_LOW_STR);
+		}
+		convertIndexOptions(from, to, search);
+
+	}
+
+    private static void convertBaseIndex(Document from, Document to, Element search, String internal_indexes_str, String internal_index_str, String output_index_str) {
+	
+	Element source = from.getDocumentElement();
+	Element indexes = XMLTools.getNamedElement(source, internal_indexes_str, StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+		if (indexes == null)
+		{
+			return;
+		}
+		NodeList index_elements = indexes.getElementsByTagName(internal_index_str);
+		int index_elements_length = index_elements.getLength();
+
+		if (index_elements_length == 0)
+		{ // no indexes
+			return;
+		}
 		for (int j = 0; j < index_elements_length; j++)
 		{
@@ -2140,5 +2433,5 @@
 			}
 
-			Element index_ele = to.createElement(StaticStrings.INDEX_LOW_STR);//index
+			Element index_ele = to.createElement(output_index_str);//index
 
 			// Used for creating displayItem for this element 'index_ele' further below
@@ -2148,15 +2441,4 @@
 
 			StringBuffer index_value = new StringBuffer();
-			if (mg_indexer == true)
-			{
-				// For mg indexer, there is a 'level' attribute in the index element of the internal structure
-				// But mgpp/lucene don't
-				level_str = index_element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
-				if (level_str.length() > 0)
-				{
-					index_value.append(level_str).append(StaticStrings.COLON_CHARACTER);
-					//index_value = index_value.StaticStrings.COLON_CHARACTER;
-				}
-			}
 
 			NodeList content_elements = index_element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
@@ -2205,9 +2487,4 @@
 			{
 				//try the full name, i.e. with 'ex.'
-				if (mg_indexer == true)
-				{
-					// but first append level info if we are mg
-					full_index_name = level_str + StaticStrings.COLON_CHARACTER + full_index_name;
-				}
 				collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, full_index_name);
 			}
@@ -2260,4 +2537,165 @@
 		} //for loop ends
 
+
+	}
+	static private void convertMGIndex(Document from, Document to)
+	{
+		Element source = from.getDocumentElement();
+		//Get the 'search' element from 'to' which has already been created in 'convertBuildType'
+		Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
+
+		//THere are two sets of indexes elements, find the one which is assigned 'true'
+		Element indexes = XMLTools.getNamedElement(source, StaticStrings.INDEXES_ELEMENT, StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+		if (indexes == null)
+		{
+			return;
+		}
+		NodeList index_elements = indexes.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
+		int index_elements_length = index_elements.getLength();
+
+		if (index_elements_length == 0)
+		{ // no indexes
+			return;
+		}
+
+		// //find out it's mg or mgpp/lucene
+		// String mg = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
+		// boolean mg_indexer = false;
+		// if (mg.equals(StaticStrings.MG_STR))
+		// {
+		// 	mg_indexer = true;//it's mg, then the level is set as attribute of
+		// }
+		// if (mg_indexer == false)
+		// {
+		// 	// It's mgpp. Construct 'level' and 'defaultLevel' elements separately.
+		// 	convertLevels(from, to, search);
+		// }
+
+		for (int j = 0; j < index_elements_length; j++)
+		{
+			Element index_element = (Element) index_elements.item(j);
+			if (index_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+			{
+				continue;
+			}
+
+			Element index_ele = to.createElement(StaticStrings.INDEX_LOW_STR);//index
+
+			// Used for creating displayItem for this element 'index_ele' further below
+			// full_index_names contain 'ex.'
+			String full_index_name = "";
+			String level_str = "";
+
+			StringBuffer index_value = new StringBuffer();
+			//	if (mg_indexer == true)
+			//{
+				// For mg indexer, there is a 'level' attribute in the index element of the internal structure
+				// But mgpp/lucene don't
+				level_str = index_element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
+				if (level_str.length() > 0)
+				{
+					index_value.append(level_str).append(StaticStrings.COLON_CHARACTER);
+					//index_value = index_value.StaticStrings.COLON_CHARACTER;
+				}
+				//}
+
+			NodeList content_elements = index_element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
+			int content_elements_length = content_elements.getLength();
+
+			for (int k = 0; k < content_elements_length; k++)
+			{
+				Element content_element = (Element) content_elements.item(k);
+				if (content_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+				{
+					continue;
+				}
+				String name_str = content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+
+				full_index_name = full_index_name + name_str;
+				if (k < content_elements_length - 1)
+				{
+					full_index_name = full_index_name + StaticStrings.COMMA_CHARACTER;
+				}
+
+				if (name_str.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name_str.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
+				{
+					name_str = name_str.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
+				}
+
+				index_value.append(name_str);
+				name_str = null;
+				// Make it comma separated string
+				if (k < content_elements_length - 1)
+				{
+					index_value.append(StaticStrings.COMMA_CHARACTER);
+				}
+				content_element = null;
+			}//for loop ends
+
+			String temp_str = index_value.toString();
+			index_ele.setAttribute(StaticStrings.NAME_ATTRIBUTE, temp_str);
+
+			// Now constructing 'displayItem' element for this 'index_ele' element
+			// The index names in the collectionmetadata elements in the internal structure are not the names that
+			// are used in the content elements (i.e. ex.Source or dc.Subject and keywords), but the names that are
+			// in the configuration files (i.e. Source or dc.Subject)
+			ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, temp_str);
+
+			if (collectionmetadata_list == null)
+			{
+			    //try the full name, i.e. with 'ex.', first appending level info as we are mg
+			    full_index_name = level_str + StaticStrings.COLON_CHARACTER + full_index_name;
+			
+				collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, full_index_name);
+			}
+
+			if (collectionmetadata_list != null)
+			{
+
+				for (int k = 0; k < collectionmetadata_list.size(); k++)
+				{
+					Element collectionmetadata = (Element) collectionmetadata_list.get(k);
+					if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+					{
+						continue;
+					}
+					Element displayItem = constructDisplayItem(collectionmetadata, to);
+
+					index_ele.appendChild(displayItem);
+				}
+			}
+
+			//mg has no options
+			// deal with any <option name='' value=''> children of this index
+			// e.g. <option name='solrfieldtype' value='text_es'>
+			// NodeList option_children = index_element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
+			// for (int k = 0; k < option_children.getLength(); k++)
+			// {
+			// 	Element el = (Element) option_children.item(k);
+			// 	if (el.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+			// 	{
+			// 		continue;
+			// 	}
+			// 	String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+			// 	String value_str = XMLTools.getNodeText(el);
+
+			// 	if (name_str == null && value_str == null)
+			// 	{
+			// 		continue;
+			// 	}
+			// 	Element option_element = to.createElement(StaticStrings.OPTION_STR);
+			// 	if (!name_str.equals("")) {
+			// 	    option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+			// 	}
+			// 	if (!value_str.equals("")) { // or no value attribute without name attribute?
+			// 	    option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, value_str);	    
+			// 	}
+			// 	index_ele.appendChild(option_element);
+			// }
+
+			search.appendChild(index_ele);
+
+		} //for loop ends
+
 		//Convert default index
 		convertDefaultIndex(from, to, search);
@@ -2265,4 +2703,134 @@
 	}
 
+
+	static private void convertSort(Document from, Document to)
+	{
+		Element source = from.getDocumentElement();
+		//Get the 'search' element from 'to' which has already been created in 'convertBuildType'
+		Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
+
+		// Get the Sorts element from the internal structure
+		Element sorts = (Element) XMLTools.getChildByTagName(source, StaticStrings.SORTS_ELEMENT);
+		if (sorts == null)
+		{
+			return;
+		}
+		NodeList sort_elements = sorts.getElementsByTagName(StaticStrings.SORT_ELEMENT);
+		int sort_elements_length = sort_elements.getLength();
+
+		if (sort_elements_length == 0)
+		{
+			return;
+		}
+
+		for (int j = 0; j < sort_elements_length; j++)
+		{
+			Element element = (Element) sort_elements.item(j);
+			if (element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+			{
+				continue;
+			}
+
+			// Create sort element
+			Element new_sort = to.createElement(StaticStrings.SORT_LOW_STR);
+
+			String name_str = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+			new_sort.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+
+			// Now constructing 'displayItem' element for this 'indexLanguage' element
+			// from the collectionmetadata element
+			// TODO this needs work to differentiate it from index
+			ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, name_str);
+
+			if (collectionmetadata_list != null)
+			{
+
+				for (int k = 0; k < collectionmetadata_list.size(); k++)
+				{
+					Element collectionmetadata = (Element) collectionmetadata_list.get(k);
+					if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+					{
+						continue;
+					}
+					Element displayItem = constructDisplayItem(collectionmetadata, to);
+					new_sort.appendChild(displayItem);
+				}
+			}
+
+			search.appendChild(new_sort);
+
+		} //for loop ends
+
+		// Convert DefaultSort
+		// Get the DefaultSort element from the internal structure
+		Element default_sort = (Element) XMLTools.getChildByTagName(source, StaticStrings.SORT_DEFAULT_ELEMENT);
+		if (default_sort != null)
+		{
+			String sort_name = default_sort.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+			Element new_default_sort = to.createElement(StaticStrings.SORT_DEFAULT_ELEMENT);
+			new_default_sort.setAttribute(StaticStrings.NAME_ATTRIBUTE, sort_name);
+			search.appendChild(new_default_sort);
+		}
+	}
+
+	static private void convertFacet(Document from, Document to)
+	{
+		Element source = from.getDocumentElement();
+		//Get the 'search' element from 'to' which has already been created in 'convertBuildType'
+		Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
+
+		// Get the Facets element from the internal structure
+		Element facets = (Element) XMLTools.getChildByTagName(source, StaticStrings.FACETS_ELEMENT);
+		if (facets == null)
+		{
+			return;
+		}
+		NodeList facet_elements = facets.getElementsByTagName(StaticStrings.FACET_ELEMENT);
+		int facet_elements_length = facet_elements.getLength();
+
+		if (facet_elements_length == 0)
+		{
+			return;
+		}
+
+		for (int j = 0; j < facet_elements_length; j++)
+		{
+			Element element = (Element) facet_elements.item(j);
+			if (element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+			{
+				continue;
+			}
+
+			// Create facet element
+			Element new_facet = to.createElement(StaticStrings.FACET_LOW_STR);
+
+			String name_str = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
+			new_facet.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
+
+			// Now constructing 'displayItem' element for this 'indexLanguage' element
+			// from the collectionmetadata element
+			// TODO this needs work to differentiate it from index
+			ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, name_str);
+
+			if (collectionmetadata_list != null)
+			{
+
+				for (int k = 0; k < collectionmetadata_list.size(); k++)
+				{
+					Element collectionmetadata = (Element) collectionmetadata_list.get(k);
+					if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
+					{
+						continue;
+					}
+					Element displayItem = constructDisplayItem(collectionmetadata, to);
+					new_facet.appendChild(displayItem);
+				}
+			}
+
+			search.appendChild(new_facet);
+
+		} //for loop ends
+	
+	}
     	//convert <facet> and <sort> and <defaultSort> elements 
         //which go in 'search' element in collectionConfig.xml
@@ -2274,5 +2842,5 @@
     // <sort> and <facet>. This function, and its inverse doSolrFacetsAndSorts, is only here to preserve such
     // elements if they already exist in the collectionConfig.xml. At present they need to be manually added there.
-	static private void convertSolrFacetsAndSorts(Document from, Document to)
+	static private void convertSolrFacetsAndSortsDELETE(Document from, Document to)
 	{
 		Element source = from.getDocumentElement();
@@ -2750,5 +3318,10 @@
 
 		if(buildtype_value.equalsIgnoreCase("solr") || buildtype_value.equalsIgnoreCase("lucene")) {
-		    doSolrFacetsAndSorts(dOc, searchNode); // <facet><displayItem /></facet> and <sort><displayItem /></sort>
+		    //doSolrFacetsAndSorts(dOc, searchNode); // <facet><displayItem /></facet> and <sort><displayItem /></sort>
+		    doSorts(dOc, searchNode);
+		    doDefaultSort(dOc, searchNode);
+		    if (buildtype_value.equalsIgnoreCase("solr")) {
+			doFacets(dOc, searchNode);
+		    }
 		    // lucene will only have sort elements
 		}
@@ -2886,4 +3459,7 @@
 	static private Document convertInternalToCollectionConfig(Document dOc)
 	{
+	    ///ystem.err.println("internal documnet:");
+	    ///ystem.err.println(XMLTools.xmlNodeToString(XMLTools.getChildByTagName(dOc.getDocumentElement(), "Facets")));
+	    ///ystem.err.println(XMLTools.xmlNodeToString(XMLTools.getChildByTagName(dOc.getDocumentElement(), "Sorts")));
 		//first parse an empty skeleton of xml config file
 		//The aim is to convert the internal structure into this skeleton
@@ -2897,6 +3473,8 @@
 		convertBuildType(dOc, skeleton);
 		convertDatabaseType(dOc, skeleton);
-		convertIndex(dOc, skeleton);
-		convertSolrFacetsAndSorts(dOc, skeleton);
+		convertIndex(dOc, skeleton); // this will do index, defaultIndex, indexOptions, facets and sorting if needed
+		//convertSolrFacetsAndSorts(dOc, skeleton);
+		//convertSort(dOc, skeleton);
+		//convertFacet(dOc, skeleton);
 		convertPlugins(dOc, skeleton);//also do the plugout element
 		convertClassifier(dOc, skeleton);
