/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The Regression Test 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.rmltoprova;

import java.util.List;

import org.jdom.*;

import rbsla.rmltoprova.*;


/**
 * SLADefinitions
 * 
 * Translates RBSLA 0.01 general SLA definitions/information into Prova facts
 * 
 * Note: Since RBSLA 0.1 the general SLA information is move to the RBSLA 0.1 Semantic Web ontology
 * and can be used as types and individuals for Prova facts which represent the general info.
 * 
 * @author <A HREF="mailto:adrian.paschke@gmx.de">Adrian Paschke</A>
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 * @deprecated Since RuleML 0.87 and RBSLA 0.1 XSLT Stylesheets are provided for translation
 */
public class SLADefinition
{
	   private Element sla;
	    private String slaType;
	    private String slaName;
	    
	    private Element providerElement;
	    private Party provider;
	    
	    private Element customerElement;
	    private Party customer;
	    
	    private Element startElement;
	    private Runtime start;
	    
	    private Element endElement;
	    private Runtime end;
	    
	    private List serviceDefinitionElements;
	    private ServiceDefinitionBase sdb;
	    
	    private Namespace ns;
	    

	    

    /**
     * Constructor.
     * @param _info the root element of the info section    
     * @param _ns the namespace of the info section
     */    
	public SLADefinition(Element _sla)
    {
        sla = _sla;
        ns=sla.getNamespace();
        slaType = sla.getAttributeValue("type",ns);
        slaName = sla.getAttributeValue("name",ns);
        
        providerElement = sla.getChild("Parties",ns).getChild("ServiceProvider",ns);
        provider = new Party(providerElement,slaName);
        
        customerElement = sla.getChild("Parties",ns).getChild("ServiceCustomer",ns);
        customer = new Party(customerElement,slaName);
        
        startElement = sla.getChild("runtime",ns).getChild("start",ns);
        start = new Runtime(startElement,slaName);
        
        endElement = sla.getChild("runtime",ns).getChild("end",ns);
        end = new Runtime(endElement,slaName);
        
        serviceDefinitionElements = sla.getChildren("ServiceDefinition",ns);
        sdb = new ServiceDefinitionBase(serviceDefinitionElements,slaName);
        

    }
    
    public String toString()
    {
        StringBuffer sb = new StringBuffer("\n% SLA Definition \n");
		sb.append("slaName("+slaName+").\r\n");
		sb.append("slaType("+slaType+").\r\n");
        sb.append(provider.toString());
        sb.append(customer.toString());
        sb.append(start.toString());
        sb.append(end.toString());
        sb.append(sdb.toString());

        return sb.toString();
    }


}

class Party
{
    Element party;
    String slaName;
    String partyName;
    
    Party(Element _party,String _slaName)
    {
        party = _party;
        slaName = _slaName;
        partyName = party.getValue();
    }
    
    public String toString()
    {
        return party.getName().toLowerCase()  + "(" + partyName + ", "+slaName+").\n";
    }
    
}

class Runtime
{
    Element timeElement;
    String slaName;
    String date;
    String time;
    private Namespace ns;
    
    Runtime(Element _timeElement,String _slaName)
    {
        timeElement = _timeElement;
        ns = timeElement.getNamespace();
        slaName = _slaName;
        date = timeElement.getChildText("date",ns);
        time = timeElement.getChildText("time",ns);
    }
    
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append(timeElement.getName() + "Date" +"(\"" + date + "\", "+slaName+").\n");
        sb.append(timeElement.getName() +"Time"+ "(\"" + time + "\", "+slaName+").\n");
        return sb.toString();
    }
}  
