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 createSSDFormatY1(String ssdfname, Object[] o)
	throws SQLException, IOException, AutoMedException
    {
	//Retrieve schema and schemaobjects
	Schema y1=Schema.getSchema("y1");
	y1.listSchema();
	SSDFormat ssdf=SSDFormat.createSSDFormat(ssdfname, y1);

	//ssdf.setSchema(y1);

	//Get schema objects and ...
	SchemaObject root=y1.getSchemaObject("<<root,list>>");
	SchemaObject student=y1.getSchemaObject("<<student,list,*,1,<<root,list>>>>");
	SchemaObject name=y1.getSchemaObject("<<name,string,-,1,<<root,list>>,<<student,list,*,1,<<root,list>>>>>>");
	SchemaObject course=y1.getSchemaObject("<<course,list,+,2,<<root,list>>,<<student,list,*,1,<<root,list>>>>>>");
	SchemaObject code=y1.getSchemaObject("<<code,string,-,1,<<root,list>>,<<student,list,*,1,<<root,list>>>>,<<course,list,+,2,<<root,list>>,<<student,list,*,1,<<root,list>>>>>>>>");
	SchemaObject year=y1.getSchemaObject("<<year,integer,_,2,<<root,list>>,<<student,list,*,1,<<root,list>>>>,<<course,list,+,2,<<root,list>>,<<student,list,*,1,<<root,list>>>>>>>>");
	SchemaObject grade=y1.getSchemaObject("<<grade,string,_,3,<<root,list>>,<<student,list,*,1,<<root,list>>>>,<<course,list,+,2,<<root,list>>,<<student,list,*,1,<<root,list>>>>>>>>");


	//... 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]);

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

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


	//Get schema objects of y2 and ...
	SchemaObject root=y2.getSchemaObject("<<root,list>>");
	SchemaObject course=y2.getSchemaObject("<<course,list,*,1,<<root,list>>>>");
	SchemaObject code=y2.getSchemaObject("<<code,string,-,1,<<root,list>>,<<course,list,*,1,<<root,list>>>>>>");
	SchemaObject student=y2.getSchemaObject("<<student,list,+,2,<<root,list>>,<<course,list,*,1,<<root,list>>>>>>");
	SchemaObject name=y2.getSchemaObject("<<name,string,-,1,<<root,list>>,<<course,list,*,1,<<root,list>>>>,<<student,list,+,2,<<root,list>>,<<course,list,*,1,<<root,list>>>>>>>>");
	SchemaObject year=y2.getSchemaObject("<<year,integer,_,2,<<root,list>>,<<course,list,*,1,<<root,list>>>>,<<student,list,+,2,<<root,list>>,<<course,list,*,1,<<root,list>>>>>>>>");
	SchemaObject grade=y2.getSchemaObject("<<grade,string,_,3,<<root,list>>,<<course,list,*,1,<<root,list>>>>,<<student,list,+,2,<<root,list>>,<<course,list,*,1,<<root,list>>>>>>>>");

	//... 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]);

	//Set links between ssd patterns and schemaobjects for y2
	ssdf.setPatternToSchemaObject(sp2, code, 1);
	ssdf.setPatternToSchemaObject(sp1, name, 1);
	ssdf.setPatternToSchemaObject(sp3, year, 1);
	ssdf.setPatternToSchemaObject(sp4, grade, 1);

	return ssdf;
    }   


    public static void main(String[] args) 
    {
	try 
	{
	    System.out.println("Creating SSD formats for schemas and datasource");
	    //Get patterns
	    Pattern p1=Pattern.getPatternByName("p1");
	    Pattern p2=Pattern.getPatternByName("p2");
	    Pattern p3=Pattern.getPatternByName("p3");
	    Pattern p4=Pattern.getPatternByName("p4");

	    SSDFormat.retract("format_y1");
	    SSDFormat format_y1=createSSDFormatY1("format_y1", new Object[]{p1, p2, p3, p4});

	    SSDFormat.retract("format_y2");
	    SSDFormat format_y2=createSSDFormatY2("format_y2", new Object[]{p1, p2, p3, p4});
	}
	catch (AutoMedException ae) {
	    ae.printStackTrace();
	}
	catch (SQLException se) { 
	    se.printStackTrace();
	}
	catch (IOException ie) {
	    ie.printStackTrace();
	}
    }

}




