/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.InstallPak.packageMgr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author LH
 */
public class InstallationRegistry
{
    
    private String dataBasePath;
    boolean disconnected=false;
    private Connection dbc;
     Statement st ;
    public InstallationRegistry(String databasePath)
    {
        try
        {
            dbc = DriverManager.getConnection( "jdbc:sqlite:"+databasePath);
            st = dbc.createStatement();
            
            
            //initialisieren und tabelle erstellen falls nicht vorhanden...
            st.executeUpdate("CREATE  TABLE  IF NOT EXISTS \"main\".\"Registry\" (\"ProgramID\" VARCHAR PRIMARY KEY  NOT NULL , \"ProgramName\" VARCHAR, \"UpdateURL\" TEXT, \"UninstallScript\" TEXT)");
            
            
            
            //tableDef = 
            //rs = st.executeQuery(Query);
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
            System.out.println("Database not found");
        }
    }
    
    
    public void disconnectRegistry()
    {
        disconnected=true;
        try
        {
            dbc.close();
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        
    }
    
    
    
    
    
    public void registerProgram(Program P)
    {        
        if(disconnected)
            throw new BullshitRequestedException("Database already unloaded!");
        try
        {
            ResultSet rs = st.executeQuery("Select count * from Registry where ProgramID="+P.getProgramID());
            
            if(rs.getInt(1)==1);
            throw new BullshitRequestedException("Program already registered");
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        
        
    }
    
    public void unregisterProgram(String programID)
    {
        if(disconnected)
            throw new BullshitRequestedException("Database already unloaded!");
        
        
        
    }
    public String[] getInstalledProgramIDs()
    {
        if(disconnected)
            throw new BullshitRequestedException("Database already unloaded!");
        
        
        
        return null;
    }
    
    public String getProgramName(String pId)
    {
        if(disconnected)
            throw new BullshitRequestedException("Database already unloaded!");
        
        
        return null;
    }
    
    public Program getProgram()
    {
        if(disconnected)
            throw new BullshitRequestedException("Database already unloaded!");
        
        
        
        return null;
    }
    
    
}
