001package csheets.ext.database.controller; 002 003import csheets.ext.database.core.DBCsvReader; 004import csheets.ext.database.core.Database; 005import csheets.ext.database.core.DatabaseFacade; 006import csheets.ext.database.ui.UIImport; 007import java.io.FileNotFoundException; 008import java.sql.SQLException; 009import java.util.ArrayList; 010import java.util.Observer; 011 012import csheets.ui.ctrl.BaseAction; 013import java.awt.event.ActionEvent; 014 015 016/** 017 * The controller for UIImport 018 * @author João Carreira 019 */ 020public class ControllerImport implements Subject 021{ 022 private ArrayList<Observer> observers; 023 private DatabaseFacade facade; 024 private ArrayList<Database> dbList; 025 026 public ControllerImport(Observer o) 027 { 028 observers = new ArrayList<Observer>(); 029 addObserver(o); 030 /* instantiating new facade */ 031 facade = new DatabaseFacade(); 032 } 033 034 /** 035 * Connects with a database 036 * @param url path to database 037 * @param user username 038 * @param pass password 039 * @param dbName database name 040 * @throws Exception 041 */ 042 public void connect(String url, String user, String pass, String dbName) throws Exception 043 { 044 try 045 { 046 facade.createConnection(url, user, pass, dbName); 047 } 048 /* replace below with proper exceptions */ 049 catch(SQLException e) 050 { 051 this.notifyObserver("Error connecting to database!"); 052 } 053 catch(ClassNotFoundException e) 054 { 055 this.notifyObserver("Error: database driver not found!"); 056 } 057 } 058 059 /** 060 * returns the list of supported databases name 061 * @return 2D array with database name and url 062 */ 063 public String[][] getDBlist() throws FileNotFoundException 064 { 065 /* new DBCsvReader */ 066 DBCsvReader reader = new DBCsvReader(); 067 /* instantiating a new arraylist and getting all databases */ 068 dbList = new ArrayList<Database>(); 069 dbList = reader.getDBList(); 070 071 /* String array to store only the name of the databases */ 072 String [][]driversName = new String[dbList.size()][2]; 073 int i = 0; 074 for(; i < dbList.size(); i++) 075 { 076 driversName[i][0] = dbList.get(i).getName(); 077 driversName[i][1] = dbList.get(i).getUrl(); 078 } 079 /* returns all names of supported databases */ 080 return driversName; 081 } 082 083 @Override 084 public void addObserver(Observer o) 085 { 086 observers.add(o); 087 } 088 089 @Override 090 public void removerObserver(Observer o) 091 { 092 observers.remove(o); 093 } 094 095 @Override 096 public void notifyObserver(String str) 097 { 098 int i = 0; 099 for(; i < observers.size(); i++) 100 { 101 observers.get(i).update(null, str); 102 } 103 } 104 105 /** 106 * gets the table list of the selected database 107 * @param dbName name of the database 108 */ 109 public String[] getTableList() 110 { 111 return facade.getTableList(); 112 } 113 114 115 /** 116 * imports a table from the database 117 * @param tableName name of the table 118 */ 119 public String[][] loadTable(String tableName) 120 { 121 return facade.loadTable(tableName); 122 } 123}