/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The OWL2Prova library is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla.owl.converter.dlp;

/**
 * This Class extends the MapperList. There are implemented new methods to handle the 
 * update process. Especially to handle UpdateInfoElements.
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 */
public class UpdateMapperList extends MapperList {
	
	/**
	 * Constructor
	 */
	public UpdateMapperList () {
		super();
	}
	
	/**
	 * Transfers a normal MapList to an UpdateMapperList which contains UpdateInfoElements 
	 * 
	 * @param inputList MapList to transfer
	 */
	public void transferToUpdateList (MapperList inputList){
		for (int i =0; i < inputList.size(); i++){
			IndividualList tmpIndiList = inputList.getAtIndex(i).getIndiList();
			int tmpHash = inputList.getAtIndex(i).getHash();
			
			UpdateIndividualList UpdateList = new UpdateIndividualList();
			for (int j = 0; j < tmpIndiList.sizeOfList(); j++){
				String tmpElement = tmpIndiList.getIndi(j);
				UpdateList.addUpdateElement(new UpdateInfoElement(tmpElement, true));
			}
			this.add(new MapperElement(UpdateList, tmpHash));
		}
	}
	
	/**
	 * Transfers the intern UpdateMapperList to an MapperList without update informations
	 * 
	 * @return MapperList
	 */
	public MapperList transferToMapperList (){
		MapperList outputList = new MapperList();
		for (int i = 0; i < this.size(); i++){
			UpdateIndividualList tmpUpdateList = (UpdateIndividualList) this.getAtIndex(i).getIndiList();
			int tmpHash = this.getAtIndex(i).getHash();
			
			IndividualList tmpIndiList = new IndividualList();
			for (int j = 0; j < tmpUpdateList.sizeOfList(); j++){
				String tmpElement = tmpUpdateList.getUpdateInfoElement(j).getElement();
				tmpIndiList.addIndi(tmpElement);
			}
			outputList.add(new MapperElement(tmpIndiList, tmpHash));
		}
		return outputList;
	}
	
	public void printList(){
		for (int i = 0; i < this.size(); i++){
			System.out.print(this.getAtIndex(i).getHash() + " := ");
			this.getAtIndex(i).getIndiList().listToString();
			System.out.println("----------------------------");
		}
		
	}
	
	public void addElement(String internElement, String toAdd){
		for (int i = 0; i < this.size(); i++){
			UpdateIndividualList tmpUpdateList = (UpdateIndividualList) this.getAtIndex(i).getIndiList();
			if (tmpUpdateList.isElementOfList(internElement)){
				tmpUpdateList.addUpdateElement(new UpdateInfoElement(toAdd, false));
				this.getAtIndex(i).setIndiList(tmpUpdateList);
			}
		}
	}
	
	public void addElements(String internElement, IndividualList toAdd){
		for (int i = 0; i < this.size(); i++){
			UpdateIndividualList tmpUpdateList = (UpdateIndividualList) this.getAtIndex(i).getIndiList();
			if (tmpUpdateList.isElementOfList(internElement)){
				for (int j = 0; j < toAdd.sizeOfList(); j++){
					tmpUpdateList.addUpdateElement(new UpdateInfoElement(
							toAdd.getIndi(j),false));
				}
				
				this.getAtIndex(i).setIndiList(tmpUpdateList);
			}
		}
	}
	
	public void mergeIntern(String internElement1, String internElement2){
		for (int i = 0; i < this.size(); i++){
			UpdateIndividualList tmpUpdateList = (UpdateIndividualList) this.getAtIndex(i).getIndiList();
			if (tmpUpdateList.isElementOfList(internElement1)){
				for (int j = 0; j < this.size(); j++){
				UpdateIndividualList tmpUpdateList2 = (UpdateIndividualList) this.getAtIndex(j).getIndiList();
					if (tmpUpdateList2.isElementOfList(internElement2)){
						if (!(i==j)){
							merge(i,j);
						}
					}
				}
			}
		}
	}
	
	public void merge(int index1, int index2){
		UpdateIndividualList tmpList1 = (UpdateIndividualList) this.getAtIndex(index1).getIndiList();
		UpdateIndividualList tmpList2 = (UpdateIndividualList) this.getAtIndex(index2).getIndiList();
		int HashCode = this.getAtIndex(index1).getHash();
		for (int i = 0; i < tmpList2.sizeOfList(); i++){
			tmpList1.addUpdateElement(tmpList2.getUpdateInfoElement(i));
		}
		if (index1 < index2){
			this.removeAtIndex(index2);
			this.removeAtIndex(index1);
		}else{
			this.removeAtIndex(index1);
			this.removeAtIndex(index2);
		}
		this.add(new MapperElement(tmpList1,HashCode));
	}
	
}
