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