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 UIUpdate
014 * @author João Carreira
015 */
016public class ControllerUpdate implements Subject
017{
018    private ArrayList<Observer> observers;
019    private DatabaseFacade facade;
020    private ArrayList<Database> dbList;
021    
022    public ControllerUpdate() 
023    {
024       
025    }
026    
027    /**
028     * Constructor with observer
029     * @param o Observer object
030     */
031    public ControllerUpdate(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    
128    
129    /**
130     * imports a table from the database
131     * @param tableName name of the table
132     */
133    public String[][] loadTable(String tableName) 
134    {
135        return facade.loadTable(tableName);
136    }
137
138    
139    /**
140     * gets the table list of the selected database
141     * @param dbName name of the database
142     */
143    public String[] getTableList() 
144    {
145        return facade.getTableList();
146    } 
147
148    /**
149     * converts selected spreadsheet content in a 2D String array
150     * @param cells selected cells in spreadsheet
151     * @return 2D array with content in selected cells
152     */
153    public String[][] cellsTo2dArray(Cell[][] cells) 
154    {
155       return facade.cellsTo2dArray(cells);
156    }
157
158    /**
159     * compares if there's any difference in content between the selected cells
160     * in the spreadsheet and the ones imported from the DB
161     * @param tableData content imported from BD
162     * @param selectedCells cells selected in the spreadsheet
163     * @return true if there's any difference, false if they're equal
164     */
165    public boolean compareCellsWithDB(String[][] tableData, String[][] selectedCells) 
166    {
167        return facade.compareCellsWithDB(tableData, selectedCells);
168    }
169
170    /**
171     * updates a database table based on the selected cells
172     * @param tableName target table to be updated 
173     * @param tableData 2D array with current table data
174     * @param selectedCells 2D array with selected cells in spreadsheet
175     */
176    public void updateTable(String tableName, String[][] tableData, String[][] selectedCells, Cell [][]cells) 
177    {
178        facade.updateTable(tableName, tableData, selectedCells, cells);
179    }
180
181    
182    /**
183     * updates a spreadsheets table by deleting unselected cells and editing modified cells
184     * @param tableName table name in database
185     * @param tableData table content from database
186     * @param selectedCells selected content from cleansheets
187     * @param cells selected cells
188     */
189    public void updateTableWithDeletion(String tableName, String[][] tableData, String[][] selectedCells, Cell[][] cells) 
190    {
191        facade.updateTableWithDeletion(tableName, tableData, selectedCells, cells);
192    }
193}