package UGPGStudentsExample;

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("sc_s1");
	s1.listSchema();
	SSDFormat ssdf=SSDFormat.createSSDFormat(ssdfname, s1);

	//ssdf.setSchema(s1);

	//Get schema objects and ...
	//SchemaObject root=s1.getSchemaObject("<<root>>");
	//SchemaObject student=s1.getSchemaObject("<<root,student>>");
	SchemaObject name=s1.getSchemaObject("<<<<root,student>>,name>>");
	SchemaObject tutor=s1.getSchemaObject("<<<<root,student>>,tutor>>");
	SchemaObject ppt=s1.getSchemaObject("<<<<root,student>>,ppt>>");
	//SchemaObject course=s1.getSchemaObject("<<<<root,student>>,course>>");
	SchemaObject code=s1.getSchemaObject("<<<<<<root,student>>,course>>,code>>");
	SchemaObject dept=s1.getSchemaObject("<<<<<<root,student>>,course>>,dept>>");
	SchemaObject year=s1.getSchemaObject("<<<<<<root,student>>,course>>,year>>");
	SchemaObject grade=s1.getSchemaObject("<<<<<<root,student>>,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, ppt, 1);
	ssdf.setPatternToSchemaObject(sp4, code, 1);
	ssdf.setPatternToSchemaObject(sp5, dept, 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("sc_s2");
	s2.listSchema();
	SSDFormat ssdf=SSDFormat.createSSDFormat(ssdfname, s2);

	//ssdf.setSchema(s2);

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

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

	//Set links between ssd patterns and schemaobjects for y1
	ssdf.setPatternToSchemaObject(sp1, code, 1);
	ssdf.setPatternToSchemaObject(sp1, dept, 2);
	ssdf.setPatternToSchemaObject(sp2, name, 1);
	ssdf.setPatternToSchemaObject(sp2, tutor, 2);
	ssdf.setPatternToSchemaObject(sp2, year, 3);
	ssdf.setPatternToSchemaObject(sp2, grade, 4);

	return ssdf;
    }
 

    public static void main(String[] args) 
    {
	try 
	{
	    System.out.println("Creating SSD formats for schemas and datasource");
	    //Get patterns
	    Pattern pname=Pattern.getPatternByName("pname");
	    Pattern ptutor=Pattern.getPatternByName("ptutor");
	    Pattern pppt=Pattern.getPatternByName("pppt");
	    Pattern pcode=Pattern.getPatternByName("pcode");
	    Pattern pdept=Pattern.getPatternByName("pdept");
	    Pattern pyear=Pattern.getPatternByName("pyear");
	    Pattern pgrade=Pattern.getPatternByName("pgrade");

	    Pattern s2_course=Pattern.getPatternByName("s2_course");
	    Pattern s2_student=Pattern.getPatternByName("s2_student");

	    SSDFormat.retract("format_s1");
	    SSDFormat.retract("format_s2");
	    SSDFormat formatS1=createSSDFormatS1("format_s1", new Object[]{pname, ptutor, pppt, pcode, pdept, pyear, pgrade});
	    SSDFormat formatS2=createSSDFormatS2("format_s2", new Object[]{s2_course,s2_student});
	}
	catch (AutoMedException ae) {
	    ae.printStackTrace();
	}
	catch (SQLException se) { 
	    se.printStackTrace();
	}
	catch (IOException ie) {
	    ie.printStackTrace();
	}
    }

}




