/*
 * 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;

import java.util.LinkedList;

/**
 * Container for individual mapping.
 * The class extends the IndiviualList. For the update process it is necessary to keep 
 * the information if an Element is one of the previous mapping description or 
 * of the current owl description. This information is kept by the UpdateInfoElement. 
 * This class enables the IndividualList to store such elements.
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 */
public class UpdateIndividualList extends IndividualList{

	/**
	 * Constuctor
	 */
	
	public UpdateIndividualList () {
		super();
	}
	
	/**
	 * Add, remove, select and get methods for the UpdateInfoElement. For further
	 * Information see org.mandarax.owl.IndividualList.
	 */
	
	  public boolean addUpdateElement (UpdateInfoElement updateElement) {
	    return IndiList.add( updateElement );
	  }


	  public void addUpdateAt (int index, UpdateInfoElement updateElement){
	    IndiList.add(index, updateElement);
	  }
	  
	  public void addUpdateFirst (UpdateInfoElement updateElement){
	    IndiList.addFirst( updateElement );
	  }

	  public void addUpdateLast (UpdateInfoElement updateElement){
	    IndiList.addLast(updateElement );
	  }

	  public boolean isFirstUpdateInfoElement(){
	  	if (IndiList.getFirst() instanceof UpdateInfoElement){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }
	  public UpdateInfoElement getFirstUpdateInfoElement(){
	    return (UpdateInfoElement) IndiList.getFirst();
	  }

	  public boolean isLastUpdateInfoElement(){
	  	if (IndiList.getLast() instanceof UpdateInfoElement){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }	 
	  
	  public UpdateInfoElement getLastUpdateInfoElement(){
	    return (UpdateInfoElement) IndiList.getLast();
	  }	  

	  public boolean isUpdateInfoElement(int index){
	  	if (IndiList.get(index) instanceof UpdateInfoElement){
	  		return true;
	  	}else{
	  		return false;
	  	}
	  }	 
	  
	  public UpdateInfoElement getUpdateInfoElement(int index){
	    return (UpdateInfoElement) IndiList.get(index);
	  }

	  public UpdateInfoElement removeFirstUpdateInfoElment(){
	    return (UpdateInfoElement) IndiList.removeFirst();
	  }

	  public UpdateInfoElement removeLastUpdateInfoElement(){
	    return (UpdateInfoElement) IndiList.removeLast();
	  }

	  public boolean removeUpdateInfoElement (UpdateInfoElement updateElement) {
	    return IndiList.remove(updateElement);
	  }

	  public boolean containsUpdateInfoElement (UpdateInfoElement updateElement){
	    return IndiList.contains(updateElement);
	  }

	  /* (non-Javadoc)
	 * @see org.mandarax.owl.IndividualList#listToString()
	 */
	public void listToString (){
	    for (int i=0; i < this.sizeOfList(); i++){
	    	if (this.isString(i)){
	    		System.out.println(this.getIndi(i));
	    	}else if(this.isUpdateInfoElement(i)){
	    		System.out.print("{");
	    		System.out.print(this.getUpdateInfoElement(i).getElement() + ", ");
	    		System.out.print(this.getUpdateInfoElement(i).isOld());
	    		System.out.print("}");
	    	}
	    	else{
	    		System.out.print("[");
	    		this.getIndiList(i).listToString();
	    		System.out.println("]");
	    	}
	    }
	  }
	  
	  /* 
	 * @see org.mandarax.owl.IndividualList#isElementOfList(java.lang.String)
	 */
	public boolean isElementOfList (String element){
	  	boolean result = false;
	  	for (int i=0; i < this.sizeOfList(); i++){
	  		if (this.isString(i)){
	  			if (this.getIndi(i).equals(element)){
	  				return true;
	  			}
	  		}else if(this.isUpdateInfoElement(i)){
	  			if (this.getUpdateInfoElement(i).getElement().equals(element)){
	  				return true;
	  			}
	  		}
	  		else{
	  			IndividualList tmp = this.getIndiList(i);
	  			result = tmp.isElementOfList(element);
	  			if (result){
	  				return true;
	  			}
	  		}
	  	}
	  	return result;
	  }
	  
	  /**
	   * special Update method which is used in phase 3 of the UdpateIdentifyMapper
	   * 
	 * @param ToAdd element to add
	 * @param old is the element of previous description
	 * @param Selector 
	 * @return
	 */
	public boolean phase3update(String ToAdd, boolean old, String Selector){
	  	for (int i=0; i < this.sizeOfList(); i++){
	  		if (!this.isString(i) && !this.isUpdateInfoElement(i)){
	  			if (this.getIndiList(i).isElementOfList(Selector)){
	  				UpdateIndividualList tmp = (UpdateIndividualList) this.getIndiList(i);
	  				tmp.addUpdateElement(new UpdateInfoElement(ToAdd, old));
	  				return true;
	  			}
	  		}
	  	}
	  	return false;
	  }
	
	
}
