package cz.hsrs.rest.util;

import cz.hsrs.db.model.NoItemFoundException;
import cz.hsrs.db.model.vgi.VgiDataset;
import cz.hsrs.db.util.UserUtil;
import cz.hsrs.db.vgi.util.VgiDatasetsUtil;
import net.sf.json.JSONObject;

import java.sql.SQLException;
import java.util.List;

/**
 * Utility class for VgiDatasetRest services
 * @author mkepka
 *
 */
public class VgiDatasetRestUtil {

    private UserUtil userUt;
    private VgiDatasetsUtil dUtil;
    
    public VgiDatasetRestUtil(){
        this.userUt = new UserUtil();
        this.dUtil = new VgiDatasetsUtil();
    }
    
    /**
     * Method processes creating of new VgiDataset object
     * and inserts it to the DB
     * @param dataset object as JSON
     * @return new ID of the VgiDataset in DB
     */
    public int processInsertVgiDataset(JSONObject dataset, String userName) throws SQLException {
        try{
            int userId = userUt.getUserId(userName);
            VgiDataset newDataset = new VgiDataset(
                    dataset.getString("dataset_name"),
                    dataset.getString("description"), 
                    userId);
            return newDataset.insertToDB();
        } catch(SQLException | NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
    
    /**
     * Method processes GetVgiDataset by given datasetId
     * @param datasetId - ID of dataset
     * @param userName - name of user
     * @return VgiDataset object if there is one in the DB
     */
    public VgiDataset processGetVgiDataset(int datasetId, String userName) throws SQLException{
        try{
            int userId = userUt.getUserId(userName);
            return dUtil.getVgiDataset(userId, datasetId);
        } catch(NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
    
    /**
     * Method processes SelectVgiDatasets by given user
     * @param userName - name of user
     * @return LinkedList of VGIDataset objects associated to given user
     */
    public List<VgiDataset> processGetVgiDatasets(String userName) throws SQLException{
        try{
            int userId = userUt.getUserId(userName);
            return dUtil.getDatasetsList(userId);
        } catch(NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
    
    /**
     * Method processes DeleteVgiDataset by given datasetID
     * @param datasetId - ID of VgiDataset to be deleted
     * @param userName - name of user
     * @return true if VgiDataset was deleted, false if not
     */
    public boolean processDeleteVgiDataset(int datasetId, String userName) throws SQLException{
        try{
            int userId = userUt.getUserId(userName);
            int row = dUtil.deleteVgiDataset(userId, datasetId);
            return row == 1;
        } catch(SQLException | NoItemFoundException e){
            throw new SQLException(e.getMessage());
        }
    }
}