controller.RetrieveSchema.java Source code

Java tutorial

Introduction

Here is the source code for controller.RetrieveSchema.java

Source

/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package controller;

import java.io.IOException;
import java.sql.SQLException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import dao.GetSchemaDao;
import exceptions.ConnectionClosedException;
import exceptions.DatabaseException;
import exceptions.DatabaseValueNotFoundException;
import exceptions.MissingPropertiesFile;
import model.Column;
import model.Schema;
import model.State;
import model.Table;

public class RetrieveSchema {
    private GetSchemaDao dao = null;
    private Json json = new Json();

    @SuppressWarnings("unchecked")
    public String retrieveSchema(String _projectId) throws IOException, DatabaseException, MissingPropertiesFile,
            SQLException, ConnectionClosedException, DatabaseValueNotFoundException {

        JSONObject obj = new JSONObject();
        dao = new GetSchemaDao();
        Schema schema = dao.getSchema(Integer.parseInt(_projectId));

        JSONArray tables = new JSONArray();
        JSONArray columns = new JSONArray();
        for (Table table : schema.getTables()) {

            tables.add(table.getName());
        }

        for (Column column : schema.getColumns()) {
            columns.add(column.getName());
        }

        obj.put("tables", tables);
        obj.put("columns", columns);

        return json.nestedJson(State.PASSED, obj);

    }

}