001package csheets.ext.database.controller;
002
003import java.io.FileNotFoundException;
004import java.sql.SQLException;
005import java.util.*;
006
007import csheets.core.Cell;
008import csheets.ext.database.core.*;
009
010/**
011 * Creates a new controller for the sync function
012 * 
013 * @author Andre
014 * 
015 */
016public class ControllerSync {
017
018        /** list of databases */
019        List<Database> dbList;
020        /** facade patern for the database connection */
021        DatabaseFacade facade;
022
023        /**
024         * Creates a new controller
025         */
026        public ControllerSync() {
027                facade = new DatabaseFacade();
028        }
029
030        /**
031         * Search for avaliables databases
032         * 
033         * @return a list with databases
034         * @throws FileNotFoundException
035         */
036        public String[][] getDBlist() throws FileNotFoundException {
037                /* new DBCsvReader */
038                DBCsvReader reader = new DBCsvReader();
039                /* instantiating a new arraylist and getting all databases */
040                dbList = new ArrayList<Database>();
041                dbList = reader.getDBList();
042
043                /* String array to store only the name of the databases */
044                String[][] driversName = new String[dbList.size()][2];
045                int i = 0;
046                for (; i < dbList.size(); i++) {
047                        driversName[i][0] = dbList.get(i).getName();
048                        driversName[i][1] = dbList.get(i).getUrl();
049                }
050                /* returns all names of supported databases */
051                return driversName;
052        }
053
054        /**
055         * Start the sync between application and database
056         * 
057         * @param user
058         *            username
059         * @param pass
060         *            username's password
061         * @param cells
062         *            cells to be sync
063         * @param tableName
064         *            name of the table
065         * @param url
066         *            database url
067         * @param observer
068         *            the observer object
069         * 
070         */
071        public void startSync(String user, String pass, Cell[][] cells,
072                        String tableName, String url, Observer observer) {
073                facade.startSync(user, pass, cells, tableName, url, observer);
074        }
075
076        /**
077         * Creates a new connection to the database
078         * 
079         * @param url
080         *            link of the database
081         * @param user
082         *            username
083         * @param pass
084         *            username's password
085         * @param dbName
086         *            database name
087         */
088        public void connect(String url, String user, String pass, String dbName) {
089                try {
090                        facade.createConnection(url, user, pass, dbName);
091                }
092                /* replace below with proper exceptions */
093                catch (SQLException e) {
094                } catch (ClassNotFoundException e) {
095                } catch (Exception e) {
096                }
097        }
098
099}