/* 
 * QoS Discovery Component
 * Copyright (C) 2006 Sebastian Gerlach and Le-Hung Vu
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package ch.epfl.qosdisc.wsmx;

import ch.epfl.qosdisc.database.*;
import ch.epfl.qosdisc.operators.*;
import ch.epfl.qosdisc.repmgnt.ReputationDataPreparation;

import java.io.*;


import lhvu.qos.utils.Constants;

/**
 * Loads the initial state of the database from the provided example ontologies.
 * Additional elements can be added below in the main function.
 * 
 * @author Sebastian Gerlach & Le-Hung Vu
 */
public class LoadDatabaseIntegratedDemo {
	
	/**
	 * Reloads all databases from scratch.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
        
		// Load the properties.
		PropertySet.setup(".");
		
		// Set to true to delete old database prior to starting.
		boolean fullReset = true;
		
		// Destroy exisiting database.
		if(fullReset) {
			String dbName = PropertySet.getPath()+Connection.name;
			LoadDatabase.deleteDir(new File(dbName));
		}
        
		// Now build the new database.
		try {
			
			// Open connection.
			if(fullReset) {
				PropertySet.props.remove("db.driver");
				PropertySet.props.remove("db.protocol");
			}
			Connection.open(PropertySet.props);
			
			// Execute create table script.
			if(fullReset)
				Connection.executeFile(PropertySet.getPath()+"dbinit/tables.txt");
			else
				Connection.executeFile(PropertySet.getPath()+"dbinit/clear.txt");
			
			// Store base QoS & ranking ontologies 
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/BankInter-Lite/Location.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/QoSBase.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Common/Ranking.wsml");

			//Load domain upper ontologies for QoS
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/FileQoSBase.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/HotelQoSBase.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSExecuteStockMarketQoSBase.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSGetNewsXigniteStockMarketQoSBase.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSGetQuoteBankinterStockMarketQoSBase.wsml");
			
			//Load hotel reservation services 
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/Hotel0.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/Hotel1.wsml");
			
			//Load some file hosting service descriptions 
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/Service0.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/Service1.wsml");
			//Uncomment the following line to generate some artificial service descriptions (for the file hosting domain)
//			for(int i=2;i<20;++i)
//				LiteServiceCreator.createService(i);

			
			//Load Stock market information services
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSExecuteIfQuotationMoreThan.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSGetNewsXignite.wsml");
			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/Lite/WSGetQuoteBankinter.wsml");
			
			//Uncomment the following line to load some more example services (provided in the package)
//			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/BankInter-Lite/WSGetNewsXignite.wsml");
//			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/BankInter-Lite/WSGetQuoteBankinter.wsml");
//			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/BankInter-Lite/WSGetNewsBankinter.wsml");
//			//One more:
//			WSMLStore.importWSMLFromURL("file:///$(local)ontologies/BankInter-Lite/WSExecuteIfQuotationMoreThan.wsml");
			
			
			//Uncomment the following lines to generate many reports for testing the reputation-based QoS estimation
			//ReputationDataPreparation.generateSimulationDataWithin(fullReset,false);

			// Add some more users. Name origin: most popular names for births in 1880 in 
			// the USA, the first year for which statistics are available on the SSA web site.
			// The first user, Anna, is a trusted agent, while the others are of unknown behaviors.
			int[] uid = new int[6];
			uid[0] = WSMLStore.addUser("Anna",Constants.USER_BEHAVIOR_TRUSTED);
			uid[1] = WSMLStore.addUser("Emma");
			uid[2] = WSMLStore.addUser("James");
			uid[3] = WSMLStore.addUser("John");
			uid[4] = WSMLStore.addUser("Mary");
			uid[5] = WSMLStore.addUser("William");

			
			//Uncomment the following line to create Catalog for CoDIMSD (to enable parallelized discovery)
			//CatalogManager.setup();
			
			// Close database connection.
			Connection.close();
			
		} catch(Exception ex) {
			
			ex.printStackTrace();
		}
	}

}

