package caise05;

import java.sql.*;
import java.io.*;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.ssd.*;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * An application to demonstrate how to create format and patterns for 
 * semistructured data
 */

public class DefineSSDFormats {

    public static SSDFormat createSSDFormatS1(String ssdfname, Object[] o)
	throws SQLException, IOException, AutoMedException
    {
	//Retrieve schema and schemaobjects
	Schema s1=Schema.getSchema("ug");
	s1.listSchema();
	SSDFormat ssdf=SSDFormat.createSSDFormat(ssdfname, s1);

	//Get schema objects and ...
	SchemaObject name=s1.getSchemaObject("<<<<root,ug>>,name>>");
	SchemaObject tutor=s1.getSchemaObject("<<<<root,ug>>,tutor>>");
	SchemaObject code=s1.getSchemaObject("<<<<<<root,ug>>,course>>,code>>");
	SchemaObject dept=s1.getSchemaObject("<<<<<<root,ug>>,course>>,dept>>");
	SchemaObject lecturer=s1.getSchemaObject("<<<<<<root,ug>>,course>>,lecturer>>");
	SchemaObject year=s1.getSchemaObject("<<<<<<root,ug>>,course>>,year>>");
	SchemaObject grade=s1.getSchemaObject("<<<<<<root,ug>>,course>>,grade>>");

	//... define ssd patterns for each schema object
	SSDPattern sp1=ssdf.createSSDPattern((Pattern)o[0]);
	SSDPattern sp2=ssdf.createSSDPattern((Pattern)o[1]);
	SSDPattern sp3=ssdf.createSSDPattern((Pattern)o[2]);
	SSDPattern sp4=ssdf.createSSDPattern((Pattern)o[3]);
	SSDPattern sp5=ssdf.createSSDPattern((Pattern)o[4]);
	SSDPattern sp6=ssdf.createSSDPattern((Pattern)o[5]);
	SSDPattern sp7=ssdf.createSSDPattern((Pattern)o[6]);

	//Set links between ssd patterns and schemaobjects for y1
	ssdf.setPatternToSchemaObject(sp1, name, 1);
	ssdf.setPatternToSchemaObject(sp2, tutor, 1);
	ssdf.setPatternToSchemaObject(sp3, code, 1);
	ssdf.setPatternToSchemaObject(sp4, dept, 1);
	ssdf.setPatternToSchemaObject(sp5, lecturer, 1);
	ssdf.setPatternToSchemaObject(sp6, year, 1);
	ssdf.setPatternToSchemaObject(sp7, grade, 1);

	return ssdf;
    }

    public static SSDFormat createSSDFormatS2(String ssdfname, Object[] o)
	throws SQLException, IOException, AutoMedException
    {
	//Retrieve schema and schemaobjects
	Schema s2=Schema.getSchema("student");
	s2.listSchema();
	SSDFormat ssdf=SSDFormat.createSSDFormat(ssdfname, s2);

	//Get schema objects and ...
	SchemaObject code=s2.getSchemaObject("<<<<root,course>>,code>>");
	SchemaObject dept=s2.getSchemaObject("<<<<root,course>>,dept>>");
	SchemaObject building=s2.getSchemaObject("<<<<root,course>>,building>>");
	SchemaObject name=s2.getSchemaObject("<<<<<<root,course>>,ug_pg>>,name>>");
	SchemaObject level=s2.getSchemaObject("<<<<<<root,course>>,ug_pg>>,level>>");
	SchemaObject year=s2.getSchemaObject("<<<<<<root,course>>,ug_pg>>,year>>");
	SchemaObject grade=s2.getSchemaObject("<<<<<<root,course>>,ug_pg>>,grade>>");

	//... define ssd patterns for each schema object
	SSDPattern sp1=ssdf.createSSDPattern((Pattern)o[0]);
	SSDPattern sp2=ssdf.createSSDPattern((Pattern)o[1]);
	SSDPattern sp3=ssdf.createSSDPattern((Pattern)o[2]);
	SSDPattern sp4=ssdf.createSSDPattern((Pattern)o[3]);
	SSDPattern sp5=ssdf.createSSDPattern((Pattern)o[4]);

	//Set links between ssd patterns and schemaobjects for y1
	ssdf.setPatternToSchemaObject(sp1, code, 1);
	ssdf.setPatternToSchemaObject(sp1, dept, 2);
	ssdf.setPatternToSchemaObject(sp1, building, 3);
	ssdf.setPatternToSchemaObject(sp2, name, 1);
	ssdf.setPatternToSchemaObject(sp3, level, 1);
	ssdf.setPatternToSchemaObject(sp4, year, 1);
	ssdf.setPatternToSchemaObject(sp5, grade, 1);

	return ssdf;
    }
 

    public static void main(String[] args) 
    {
	try 
	{
	    System.out.println("Creating SSD formats for schemas and datasource");
	    //Get patterns
	    Pattern xmlname=Pattern.getPatternByName("xmlname");
	    Pattern xmltutor=Pattern.getPatternByName("xmltutor");
	    Pattern xmlcode=Pattern.getPatternByName("xmlcode");
	    Pattern xmldept=Pattern.getPatternByName("xmldept");
	    Pattern xmllecturer=Pattern.getPatternByName("xmllecturer");
	    Pattern xmlyear=Pattern.getPatternByName("xmlyear");
	    Pattern xmlgrade=Pattern.getPatternByName("xmlgrade");

	    Pattern csb=Pattern.getPatternByName("csb");
	    Pattern student=Pattern.getPatternByName("student");
	    Pattern level=Pattern.getPatternByName("level");
	    Pattern year=Pattern.getPatternByName("year");
	    Pattern grade=Pattern.getPatternByName("grade");

	    SSDFormat.retract("format_ug");
	    SSDFormat.retract("format_student");
	    SSDFormat formatS1=createSSDFormatS1("format_ug", new Object[]{xmlname, xmltutor, xmlcode, xmldept, xmllecturer, xmlyear, xmlgrade});
	    SSDFormat formatS2=createSSDFormatS2("format_student", new Object[]{csb,student,level,year,grade});
	}
	catch (AutoMedException ae) {
	    ae.printStackTrace();
	}
	catch (SQLException se) { 
	    se.printStackTrace();
	}
	catch (IOException ie) {
	    ie.printStackTrace();
	}
    }

}




