/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.installation;

import org.bodington.database.*;
import org.bodington.server.*;
import org.bodington.server.realm.*;
import org.bodington.server.resources.*;
import org.bodington.text.*;

import java.io.*;
import java.util.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;


/**
 *
 * @author  bmb6jrm
 */
public class DemoSiteBuilder implements org.bodington.server.JobSession
{
    
    /** Creates new DemoSiteBuilder */
    public DemoSiteBuilder()
    {
    }
    
    
    
    static BuildingServer server;
    
    static String system_zone_prefix = "sys";
    static String system_zone_name = "System Administration";
    
    static String user_zone_prefix = "campus";
    static String user_zone_name = "Our On-Line Campus";
    
    //PrimaryKey system_zone_id, user_zone_id;
    
    static Integer admin_zone_user_group_id;
    
    static Integer sys_zone_user_group_id;
    static Integer sys_zone_staff_group_id;
    static Integer sys_zone_student_group_id;
    static Integer sys_zone_other_group_id;
    static Integer zone_user_group_id;
    static Integer zone_staff_group_id;
    static Integer zone_student_group_id;
    static Integer zone_other_group_id;
    static Integer all_user_group_id		;
    static Integer all_staff_group_id		;
    static Integer all_student_group_id	;
    static Integer all_other_group_id		;
    
    
    static String building_name_recycler = "recycler";
    static String building_title_recycler = "Recycling Building";
    static String building_description_recycler = "This building is where deleted resources go.";
    
    static String building_name_admin = "admin";
    static String building_title_admin = "Site Administration Building";
    static String building_description_admin = "This building is for site administation.";
    
    static String building_name_demo = "faculties";
    static String building_title_demo = "OnLine Learning Building";
    static String building_description_demo = "Learning resources provided by each Faculty";
    
    static String u_name_sysadmin			= "System Administrator";
    static String u_surname_sysadmin		= "Administrator";
    static String u_initials_sysadmin 	= "SA";
    static String u_user_name_sysadmin	= "sysadmin";
    
    static String u_name_anon1			= "Anonymous Internet User";
    static String u_surname_anon1		= "Anonymous";
    static String u_initials_anon1 	= "AIU1";
    
    static String u_name_anon2			= "Anonymous Internet User 2";
    static String u_surname_anon2		= "Anonymous";
    static String u_initials_anon2 	= "AIU2";
    
    
    
    public void createSite( String props_file )
    throws BuildingServerException, IOException
    {
	BuildingContext context = BuildingContext.getContext();
	
	JobResult job_result = context.getJobResult();
	BigString output_message=null;
	if ( job_result != null )
	    output_message = job_result.getBigString();
	
	output_message.setString( "Initializing process..." );
	output_message.save();
	
	StringBuffer progress = new StringBuffer();
	
	
	int n=1;
	
	// not really ids - these are special group flag numbers
	all_user_group_id		= new Integer(n++);
	all_staff_group_id		= new Integer(n++);
	all_student_group_id	= new Integer(n++);
	all_other_group_id		= new Integer(n++);
	zone_user_group_id= new Integer(n++);
	zone_staff_group_id= new Integer(n++);
	zone_student_group_id= new Integer(n++);
	zone_other_group_id= new Integer(n++);
	sys_zone_user_group_id= new Integer(n++);
	sys_zone_staff_group_id= new Integer(n++);
	sys_zone_student_group_id= new Integer(n++);
	sys_zone_other_group_id= new Integer(n++);
	
	
	
	output_message.setString(progress.toString()); output_message.save();
	Object obj=null;
	Zone user_zone;
	PassPhrase sysadminpass;
	
	Group newgroup, allusers, allstaff, allstudents, allothers, sysadmins, anonymi;
	Group zone_users, zone_staff, zone_students, zone_others;
	Group bio_users, bio_staff, bio_students;
	Group med_users, med_staff, med_students;
	Group bio_class[];
	Group med_class[];
	Member newmember;
	Acl siteacl, recycleracl, thisacl;
	AclEntry newaclentry;
	AliasEditor alias_editor;
	Resource res0, res1, res2, res3, res4, res5;
	Resource r_anonymi, r_allusers, r_allstaff, r_allstudents, r_allothers;
	Resource biology, medicine;
	ResourceTree tree;
	
	
	
	try
	{
	    
	    bio_class = new Group[10];
	    med_class = new Group[10];
	    
	    allusers = Group.findGroupByName("allusers" );
	    allstaff = Group.findGroupByName( "allstaff" );
	    allstudents = Group.findGroupByName( "allstudents" );
	    
	    user_zone = new Zone();
	    user_zone.setPrefix( user_zone_prefix );
	    user_zone.setName( user_zone_name );
	    user_zone.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();

	    
	    
	    tree = ResourceTreeManager.getInstance();
	    
	    res0=tree.findRootResource();
	    
	    res1=new Resource();
	    res1.setZone( user_zone );
	    res1.setName( building_name_demo );
	    res1.setTitle( building_title_demo );
	    res1.setDescription( building_description_demo );
	    res1.setIntroduction( building_description_demo );
	    res1.setHttpFacilityNo( 23 );
	    res1.setUseParentAcl( true );
	    tree.addResource( res0, res1 );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    biology=new Resource();
	    biology.setName( "biology" );
	    biology.setTitle( "Faculty of Biological Sciences" );
	    biology.setDescription( "Learning resources and online learning support." );
	    biology.setIntroduction( "Learning resources and online learning support." );
	    biology.setHttpFacilityNo( 3 );
	    biology.setUseParentAcl( true );
	    tree.addResource( res1, biology );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    medicine=new Resource();
	    medicine.setName( "medicine" );
	    medicine.setTitle( "Faculty of Medicine" );
	    medicine.setDescription( "Learning resources and online learning support." );
	    medicine.setIntroduction( "Learning resources and online learning support." );
	    medicine.setHttpFacilityNo( 3 );
	    medicine.setUseParentAcl( true );
	    tree.addResource( res1, medicine );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
/*		res3=new Resource();
		res3.setName( "room" );
		res3.setTitle( "A Room" );
		res3.setDescription( "Just an empty room." );
		res3.setIntroduction( "Just an empty room." );
		res3.setHttpFacilityNo( 5 );
		res3.setUseParentAcl( true );
		tree.addResource( res2, res3 );
 */

	    res2=tree.findResource( "/admin/ground/" );
	    
	    res3=new Resource();
	    res3.setZone( user_zone );
	    res3.setName( user_zone_prefix );
	    res3.setTitle( "User Administration for " + user_zone_name + " zone." );
	    res3.setDescription( "Contains tools for administration of users." );
	    res3.setIntroduction( "This room contains tools for administration of users." );
	    res3.setHttpFacilityNo( 5 );
	    tree.addResource( res2, res3 );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res4=new AliasEditor();
	    res4.setName( "userid" );
	    res4.setTitle( "Unique Identifier Tool" );
	    res4.setDescription( "Use this tool to manage identifiers for users." );
	    res4.setIntroduction( "Use this tool to manage identifiers for users." );
	    res4.setHttpFacilityNo( 25 );
	    tree.addResource( res3, res4 );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res4=new AliasEditor();
	    res4.setName( "usercreate" );
	    res4.setTitle( "User Creation Tool" );
	    res4.setDescription( "Use this tool to create users." );
	    res4.setIntroduction( "Use this tool to create users." );
	    res4.setHttpFacilityNo( 26 );
	    tree.addResource( res3, res4 );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res4=new AliasEditor();
	    res4.setName( user_zone_prefix );
	    res4.setTitle( "Groups of Users" );
	    res4.setDescription( "Use this tool to put users into named groups." );
	    res4.setIntroduction( "Use this tool to put users into named groups." );
	    res4.setHttpFacilityNo( 27 );
	    tree.addResource( res3, res4 );
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    
	    res5=new Resource();
	    res5.setName( "users" );
	    res5.setTitle( user_zone_prefix + "." + "users" );
	    res5.setDescription( "Users in the " + user_zone_name + " zone."  );
	    res5.setIntroduction( res5.getDescription() );
	    res5.setHttpFacilityNo( 6 );
	    tree.addResource( res4, res5 );
	    zone_users = new Group();
	    zone_users.setResourceId( res5.getResourceId() );
	    zone_users.setName( res5.getTitle() );
	    zone_users.setDescription( res5.getDescription() );
	    zone_users.setSpecialGroup( zone_user_group_id );
	    zone_users.save();
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allusers );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res5=new Resource();
	    res5.setName( "students" );
	    res5.setTitle( user_zone_prefix + "." + "students" );
	    res5.setDescription( "Students of the " + user_zone_name + " zone."  );
	    res5.setIntroduction( res5.getDescription() );
	    res5.setHttpFacilityNo( 6 );
	    tree.addResource( res4, res5 );
	    zone_students = new Group();
	    zone_students.setResourceId( res5.getResourceId() );
	    zone_students.setName( res5.getTitle() );
	    zone_students.setDescription( res5.getDescription() );
	    zone_students.setSpecialGroup( zone_student_group_id );
	    zone_students.save();
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allusers );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res5=new Resource();
	    res5.setName( "staff" );
	    res5.setTitle( user_zone_prefix + "." + "staff" );
	    res5.setDescription( "Staff of " + user_zone_name + "."  );
	    res5.setIntroduction( res5.getDescription() );
	    res5.setHttpFacilityNo( 6 );
	    tree.addResource( res4, res5 );
	    zone_staff = new Group();
	    zone_staff.setResourceId( res5.getResourceId() );
	    zone_staff.setName( res5.getTitle() );
	    zone_staff.setDescription( res5.getDescription() );
	    zone_staff.setSpecialGroup( zone_staff_group_id );
	    zone_staff.save();
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allusers );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res5=new Resource();
	    res5.setName( "other" );
	    res5.setTitle( user_zone_prefix + "." + "other" );
	    res5.setDescription( "Other users of " + user_zone_name + "."  );
	    res5.setIntroduction( res5.getDescription() );
	    res5.setHttpFacilityNo( 6 );
	    tree.addResource( res4, res5 );
	    zone_others = new Group();
	    zone_others.setResourceId( res5.getResourceId() );
	    zone_others.setName( res5.getTitle() );
	    zone_others.setDescription( res5.getDescription() );
	    zone_others.setSpecialGroup( zone_other_group_id );
	    zone_others.save();
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allusers );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res5=new Resource();
	    res5.setName( "admin" );
	    res5.setTitle( user_zone_prefix + "." + "admin" );
	    res5.setDescription( "Administrators for " + user_zone_name + "."  );
	    res5.setIntroduction( res5.getDescription() );
	    res5.setHttpFacilityNo( 6 );
	    tree.addResource( res4, res5 );
	    
	    newgroup = new Group();
	    newgroup.setResourceId( res5.getResourceId() );
	    newgroup.setName( res5.getTitle() );
	    newgroup.setDescription( res5.getDescription() );
	    newgroup.save();
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allusers );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res5=new AliasEditor();
	    res5.setName( "biology" );
	    res5.setTitle( user_zone_prefix + "." + "biology" );
	    res5.setDescription( "Users in the Faculty of Biological Sciences." );
	    res5.setIntroduction( "Users in the Faculty of Biological Sciences." );
	    res5.setUseParentAcl( true );
	    res5.setHttpFacilityNo( 27 );
	    tree.addResource( res4, res5 );
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allstaff );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res3=new Resource();
	    res3.setName( "users" );
	    res3.setTitle( user_zone_prefix + "." + "biology.users" );
	    res3.setDescription( "All users in the Faculty of Biological Sciences"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    bio_users = new Group();
	    bio_users.setResourceId( res3.getResourceId() );
	    bio_users.setName( res3.getTitle() );
	    bio_users.setDescription( res3.getDescription() );
	    bio_users.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res3=new Resource();
	    res3.setName( "staff" );
	    res3.setTitle( user_zone_prefix + "." + "biology.staff" );
	    res3.setDescription( "All staff in the Faculty of Biological Sciences"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    bio_staff = new Group();
	    bio_staff.setResourceId( res3.getResourceId() );
	    bio_staff.setName( res3.getTitle() );
	    bio_staff.setDescription( res3.getDescription() );
	    bio_staff.save();
	    
	    // give this group rights over the Faculty of Biological Sciences areea
	    thisacl = biology.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( bio_staff );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    newaclentry.addPermission( Permission.EDIT );
	    newaclentry.addPermission( Permission.CREATE );
	    newaclentry.addPermission( Permission.POST );
	    newaclentry.addPermission( Permission.UPLOAD );
	    newaclentry.addPermission( Permission.MANAGE );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res3=new Resource();
	    res3.setName( "students" );
	    res3.setTitle( user_zone_prefix + "." + "biology.students" );
	    res3.setDescription( "All students in the Faculty of Biological Sciences"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    bio_students = new Group();
	    bio_students.setResourceId( res3.getResourceId() );
	    bio_students.setName( res3.getTitle() );
	    bio_students.setDescription( res3.getDescription() );
	    bio_students.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    for (int i=0; i<10; i++)
	    {
		
		res3=new Resource();
		res3.setName( "class"+(i+1) );
		res3.setTitle( user_zone_prefix + "." + "biology.class"+(i+1) );
		res3.setDescription( "Students in the Faculty of Biological Sciences"  );
		res3.setIntroduction( res3.getDescription() );
		res3.setUseParentAcl( true );
		res3.setHttpFacilityNo( 6 );
		tree.addResource( res5, res3 );
		
		bio_class[i] = new Group();
		bio_class[i].setResourceId( res3.getResourceId() );
		bio_class[i].setName( res3.getTitle() );
		bio_class[i].setDescription( res3.getDescription() );
		bio_class[i].save();
		progress.append(".");
		output_message.setString(progress.toString()); output_message.save();
	    }
	    
	    res5=new AliasEditor();
	    res5.setName( "medicine" );
	    res5.setTitle( user_zone_prefix + "." + "medicine" );
	    res5.setDescription( "Users in the Faculty of Medicine." );
	    res5.setIntroduction( "Users in the Faculty of Medicine." );
	    res5.setUseParentAcl( true );
	    res5.setHttpFacilityNo( 27 );
	    tree.addResource( res4, res5 );
	    thisacl = res5.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( allstaff );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    
	    res3=new Resource();
	    res3.setName( "users" );
	    res3.setTitle( user_zone_prefix + "." + "medicine.users" );
	    res3.setDescription( "All users in the Faculty of Medicine"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    med_users = new Group();
	    med_users.setResourceId( res3.getResourceId() );
	    med_users.setName( res3.getTitle() );
	    med_users.setDescription( res3.getDescription() );
	    med_users.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res3=new Resource();
	    res3.setName( "staff" );
	    res3.setTitle( user_zone_prefix + "." + "medicine.staff" );
	    res3.setDescription( "All staff in the Faculty of Medicine"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    med_staff = new Group();
	    med_staff.setResourceId( res3.getResourceId() );
	    med_staff.setName( res3.getTitle() );
	    med_staff.setDescription( res3.getDescription() );
	    med_staff.save();
	    
	    // give this group rights over the Faculty of Medicine areea
	    thisacl = medicine.getAcl();
	    newaclentry = new AclEntry();
	    newaclentry.setPrincipal( med_staff );
	    newaclentry.addPermission( Permission.SEE );
	    newaclentry.addPermission( Permission.VIEW );
	    newaclentry.addPermission( Permission.EDIT );
	    newaclentry.addPermission( Permission.POST );
	    newaclentry.addPermission( Permission.CREATE );
	    newaclentry.addPermission( Permission.UPLOAD );
	    newaclentry.addPermission( Permission.MANAGE );
	    thisacl.addEntry( newaclentry );
	    thisacl.save();
	    
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    res3=new Resource();
	    res3.setName( "students" );
	    res3.setTitle( user_zone_prefix + "." + "medicine.students" );
	    res3.setDescription( "All students in the Faculty of Medicine"  );
	    res3.setIntroduction( res3.getDescription() );
	    res3.setUseParentAcl( true );
	    res3.setHttpFacilityNo( 6 );
	    tree.addResource( res5, res3 );
	    
	    med_students = new Group();
	    med_students.setResourceId( res3.getResourceId() );
	    med_students.setName( res3.getTitle() );
	    med_students.setDescription( res3.getDescription() );
	    med_students.save();
	    progress.append(".");
	    output_message.setString(progress.toString()); output_message.save();
	    for (int i=0; i<10; i++)
	    {
		
		res3=new Resource();
		res3.setName( "class"+(i+1) );
		res3.setTitle( user_zone_prefix + "." + "medicine.class"+(i+1) );
		res3.setDescription( "Students in the Faculty of Medicine" );
		res3.setIntroduction( res3.getDescription() );
		res3.setUseParentAcl( true );
		res3.setHttpFacilityNo( 6 );
		tree.addResource( res5, res3 );
		
		med_class[i] = new Group();
		med_class[i].setResourceId( res3.getResourceId() );
		med_class[i].setName( res3.getTitle() );
		med_class[i].setDescription( res3.getDescription() );
		med_class[i].save();
		progress.append(".");
		output_message.setString(progress.toString()); output_message.save();
	    }
	    
	    progress.append("\ndone.\n");
	    
	    progress.append("Creating students...\n");
	    output_message.setString(progress.toString()); output_message.save();
	    //read in the students
	    
	    InputStream in = getClass().getResourceAsStream("/org/bodington/installation/demostudents.txt");
	    if (in==null) throw new BuildingServerException("Can't find student data");
	    BufferedReader buf = new BufferedReader(new InputStreamReader(in));
	    if (buf==null) throw new BuildingServerException("Can't create BufferedReader");
	    
	    String line="";
	    String name_string="";
	    String surname_string="";
	    String initials_string="";
	    String username_string="";
	    
	    
	    int index1=-1;
	    int index2=-1;
	    int med_count=0;
	    int bio_count=0;
	    int loop_count=0;
	    while (line!=null)
	    {
		
		line = buf.readLine();
		if (line != null)
		{
		    progress.append(".");
		    output_message.setString(progress.toString()); output_message.save();
		    loop_count++;
		    if (loop_count==100)
		    {
			loop_count=0;
			progress.append("\n");
			output_message.setString(progress.toString()); output_message.save();
		    }
		    index1 = line.indexOf(',');
		    name_string = line.substring(0,index1);
		    index1++; index2 = line.indexOf(',',index1);
		    surname_string = line.substring(index1,index2);
		    index1=index2+1;index2 = line.indexOf(',',index1);
		    initials_string = line.substring(index1,index2);
		    index1=index2+1;index2 = line.length();
		    username_string = line.substring(index1,index2);
		    
		    User new_user = new User();
		    new_user.setZone(user_zone);
		    new_user.setName(name_string);
		    new_user.setSurname(surname_string);
		    new_user.setInitials(initials_string);
		    new_user.save();
		    
		    //set the username and pass phrase
		    PassPhrase pass = new PassPhrase(new_user);
		    pass.setUserName(username_string);
            pass.changePassPhrase("student");
		    pass.save();
		    
		    //add to all users group
		    allusers.addMember(new_user);
		    allusers.save();
		    //add to all students group
		    allstudents.addMember(new_user);
		    allstudents.save();
		    //add to zone users groups
		    zone_users.addMember(new_user);
		    zone_users.save();
		    //add to zone students group
		    zone_students.addMember(new_user);
		    zone_students.save();
		    
		    //ALLOCATE to the campus.medicine.* and campus.biology.* groups
		    if (username_string.startsWith("bio"))
		    {
			bio_users.addMember(new_user);
			bio_users.save();
			
			bio_students.addMember(new_user);
			bio_students.save();
			
			bio_class[bio_count].addMember(new_user);
			bio_class[bio_count].save();
			
			bio_count++;
			if (bio_count==10) bio_count=0;
		    }
		    else if (username_string.startsWith("med"))
		    {
			med_users.addMember(new_user);
			med_users.save();
			
			med_students.addMember(new_user);
			med_students.save();
			
			med_class[med_count].addMember(new_user);
			med_class[med_count].save();
			
			med_count++;
			if (med_count==10)
			{
			    med_count=0;
			}
			
		    }
		    
		}
	    }
	    buf.close();
	    progress.append("\ndone.\n");
	    output_message.setString(progress.toString()); output_message.save();
	    
	    progress.append("\nCreating tutors.\n");
	    output_message.setString(progress.toString()); output_message.save();
	    for (int i=0; i<50; i++)
	    {
		User new_user = new User();
		new_user.setZone(user_zone);
		new_user.setName("Tutor "+(i+1));
		new_user.setSurname("Tutor");
		new_user.setInitials("A");
		new_user.save();
		
		//set the username and pass phrase
		PassPhrase pass = new PassPhrase(new_user);
		pass.setUserName("tutor"+(i+1));
        pass.changePassPhrase("faculty");
		pass.save();
		
		//add to all users group
		allusers.addMember(new_user);
		allusers.save();
		//add to all staff group
		allstaff.addMember(new_user);
		allstaff.save();
		//add to zone users groups
		zone_users.addMember(new_user);
		zone_users.save();
		//add to zone staff group
		zone_staff.addMember(new_user);
		zone_staff.save();
		
		if (i<25)
		{
		    bio_users.addMember(new_user);
		    bio_users.save();
		    
		    bio_staff.addMember(new_user);
		    bio_staff.save();
		}
		else
		{
		    med_users.addMember(new_user);
		    med_users.save();
		    
		    med_staff.addMember(new_user);
		    med_staff.save();
		}
		progress.append(".");
		output_message.setString(progress.toString()); output_message.save();
	    }
	    progress.append("\ndone.\n");
	    output_message.setString(progress.toString()); output_message.save();
	    BuildingServer.setupComplete();
	    
	}
	catch ( java.security.acl.NotOwnerException e )
	{
	    throw new BuildingServerException( "NotOwnerException trying to create demo data." );
	}
    }
    
    
}
