/*
 *  ruleCondition.java
 * 
 *  RELOAD Editor - An IMS Metadata, Content Packaging and Learning Design tool 
 *  with ADAPTATION AT ACTIVITY LEVEL
 *
 *  Copyright (c) 2006 Miguel Fern�ndez Lores
 *
 *  Technical Contact:
 *  Miguel Fernandez Lores
 *  e-mail: tucias@hotmail.com
 *
 */

package uk.ac.reload.editor.scormadaptive.rules.jdom.rule;

import org.jdom.Attribute;
import org.jdom.Element;

/**
 * Class that contains the information corresponding to a 
 * ruleCondition element of a PREEXISTING RULE
 *
 * @author  lagar
 * @version 0.2 
 */
public class RuleCondition {
    
    private String sComparativeOperator;
    
    private String sCO;
    
    private String sOperator;
    
    private String sComparativeValue;
    
    private adaptationCriterion oaC;
    
    /** Creates a new instance of ruleCondition */
    public RuleCondition(adaptationCriterion aC, String operator, String value, String op) {
        oaC = aC;
        sComparativeOperator = operator;
        sCO = operator;
        if (operator.equals("==")) sComparativeOperator = "equals";
        else if (operator.equals("<")) sComparativeOperator = "lessThan";
        else if (operator.equals("<=")) sComparativeOperator = "lessEqualThan";
        else if (operator.equals(">=")) sComparativeOperator = "moreEqualThan";
        else if (operator.equals(">")) sComparativeOperator = "moreThan";
        else {
            sComparativeOperator = operator;
            if (sComparativeOperator.equals("equals")) sCO = "==";
            else if (sComparativeOperator.equals("lessThan")) sCO = "<";
            else if (sComparativeOperator.equals("lessEqualThan")) sCO = "<=";
            else if (sComparativeOperator.equals("moreEqualThan")) sCO = ">=";
            else if (sComparativeOperator.equals("moreThan")) sCO = ">";
        }
        sComparativeValue = value;
        sOperator = op;
    }
    
    public String getText(){
        String sAux = "";
        if (sOperator.equals("noOp")) sAux = "(" + oaC.getValue() + " ";   
        else sAux = "(not(" + oaC.getValue() + " ";
        
        sAux += sCO + " ";
        
        if (sOperator.equals("noOp")) sAux += sComparativeValue + ")";
        else sAux += sComparativeValue + "))";
        
        return sAux;
    }
    
    public void setOperator(String sOp){
        sOperator = sOp;
    }
    
    public String getOperator(){
        return sOperator;
    }
    
    public adaptationCriterion getAdapatationCriterion(){
        return oaC;
    }
    
    public Element getElement(){
        Attribute oAtt;
        
        Element oEl =  new Element("ruleCondition");
        if (sOperator=="not") {
            oAtt = new Attribute("operator","not");
            oEl.setAttribute(oAtt);
        }
        oEl.addContent(oaC.getElement());
        Element oElComparator = new Element("comparative");
        oElComparator.setText(sComparativeValue);
        oAtt =  new Attribute("comparator",sComparativeOperator);
        oElComparator.setAttribute(oAtt);
        oEl.addContent(oElComparator);
        
        return oEl;
    }
}
